Simple app not working
Posted Wednesday, 27 February, 2008 - 21:16 by rhay in
Hello!
This simple app is a rewritten NeHe's example. But it doesn't show anything. What am I doing wrong?
When the app starts-up, the window's title changes from "OpenTK Game Window" to the given name. Is that a bug or a feature?
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Platform;
namespace OpenGLProject
{
class MainWindow : GameWindow
{
public MainWindow()
: base("OpenGL Project", 800, 600)
{
Keyboard.KeyDown += KeyDownHandler;
Keyboard.KeyUp += KeyUpHandler;
}
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
// logic
}
public override void OnRenderFrame(RenderFrameEventArgs e)
{
// drawing
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.LoadIdentity();
GL.Color3(1, 1, 1);
GL.Translate(-1.5f, 0.0f, -6.0f);
GL.Begin(BeginMode.Triangles); // Drawing Using Triangles
GL.Vertex3(0.0f, 1.0f, 0.0f); // Top
GL.Vertex3(-1.0f, -1.0f, 0.0f); // Bottom Left
GL.Vertex3(1.0f, -1.0f, 0.0f); // Bottom Right
GL.End();
GL.Translate(3.0f, 0.0f, 0.0f); // Move Right 3 Units
GL.Begin(BeginMode.Quads); // Draw A Quad
GL.Vertex3(-1.0f, 1.0f, 0.0f); // Top Left
GL.Vertex3(1.0f, 1.0f, 0.0f); // Top Right
GL.Vertex3(1.0f, -1.0f, 0.0f); // Bottom Right
GL.Vertex3(-1.0f, -1.0f, 0.0f); // Bottom Left
GL.End();
this.SwapBuffers();
}
// key handlers
public void KeyDownHandler(KeyboardDevice sender, Key key)
{
}
public void KeyUpHandler(KeyboardDevice sender, Key key)
{
}
public new void Run()
{
this.Run(25, 0);
}
// load and resize handlers
public override void OnLoad(EventArgs e)
{
GL.ShadeModel(ShadingModel.Smooth);
GL.ClearColor(0, 0, 0, 0);
GL.ClearDepth(1);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
}
protected override void OnResize(ResizeEventArgs e)
{
int myWidth = Width;
if (myWidth == 0)
myWidth = 1;
GL.Viewport(0, 0, myWidth, Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Glu.Perspective(45.0, myWidth * 1.0 / Height, 0.1, 100.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
}
}
}
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Platform;
namespace OpenGLProject
{
class MainWindow : GameWindow
{
public MainWindow()
: base("OpenGL Project", 800, 600)
{
Keyboard.KeyDown += KeyDownHandler;
Keyboard.KeyUp += KeyUpHandler;
}
public override void OnUpdateFrame(UpdateFrameEventArgs e)
{
// logic
}
public override void OnRenderFrame(RenderFrameEventArgs e)
{
// drawing
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.LoadIdentity();
GL.Color3(1, 1, 1);
GL.Translate(-1.5f, 0.0f, -6.0f);
GL.Begin(BeginMode.Triangles); // Drawing Using Triangles
GL.Vertex3(0.0f, 1.0f, 0.0f); // Top
GL.Vertex3(-1.0f, -1.0f, 0.0f); // Bottom Left
GL.Vertex3(1.0f, -1.0f, 0.0f); // Bottom Right
GL.End();
GL.Translate(3.0f, 0.0f, 0.0f); // Move Right 3 Units
GL.Begin(BeginMode.Quads); // Draw A Quad
GL.Vertex3(-1.0f, 1.0f, 0.0f); // Top Left
GL.Vertex3(1.0f, 1.0f, 0.0f); // Top Right
GL.Vertex3(1.0f, -1.0f, 0.0f); // Bottom Right
GL.Vertex3(-1.0f, -1.0f, 0.0f); // Bottom Left
GL.End();
this.SwapBuffers();
}
// key handlers
public void KeyDownHandler(KeyboardDevice sender, Key key)
{
}
public void KeyUpHandler(KeyboardDevice sender, Key key)
{
}
public new void Run()
{
this.Run(25, 0);
}
// load and resize handlers
public override void OnLoad(EventArgs e)
{
GL.ShadeModel(ShadingModel.Smooth);
GL.ClearColor(0, 0, 0, 0);
GL.ClearDepth(1);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
}
protected override void OnResize(ResizeEventArgs e)
{
int myWidth = Width;
if (myWidth == 0)
myWidth = 1;
GL.Viewport(0, 0, myWidth, Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Glu.Perspective(45.0, myWidth * 1.0 / Height, 0.1, 100.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
}
}
}




Comments
Feb 27
21:51:16Re: Simple app not working
posted by InertiaGL.Color3(1, 1, 1);
change the '1' either into '1f', '1.0f' or '1.0', else C# will select the byte/int overload where '1' is almost pitch black.
In the OnResize override you might want to compare Height vs. 0 rather than width, to prevent a possible divide by 0.
Glu.Perspective(45.0, Width / (double) myHeight, 0.1, 100.0);
If that doesn't help, check your 2 GL.Clear* calls which overload is selected there, I've never tried to feed them with anything but floating point numbers so not sure if there actually exist integer overloads ;)
Feb 27
21:58:55Re: Simple app not working
posted by rhayThanks! Um, I was rewriting the code too carelessly, hence the width/height thing. But the byte overload catch was tricky.
Feb 27
22:47:59Re: Simple app not working
posted by InertiaYou're welcome, hope that fixed it :)