rakkarage's picture

OpenGL programming guide example 1

#include <whateverYouNeed.h>
main() {
InitializeAWindowPlease();
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
UpdateTheWindowAndCheckForEvents();
}

i am trying to make this first example from the red book work in opentk, i edited QuickStart and put that code in OnRenderFrame but does not seem to work... can anyone tell me what i am doing wrong? thanks

protected override void OnRenderFrame(FrameEventArgs e)
{
	base.OnRenderFrame(e);
 
	GL.ClearColor(Color.Black);
	GL.Color3(Color.White);
 
	GL.Clear(ClearBufferMask.ColorBufferBit);
 
	GL.Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
 
	GL.Begin(BeginMode.Polygon);
	GL.Vertex3(0.25, 0.25, 0.0);
	GL.Vertex3(0.75, 0.25, 0.0);
	GL.Vertex3(0.75, 0.75, 0.0);
	GL.Vertex3(0.25, 0.75, 0.0);
	GL.End();
 
	SwapBuffers();
}

Comments

the Fiddler's picture

GL.Ortho multiplies the current matrix with a matrix representing an orthographic projection. In most cases, you need to reset your projection matrix before multiplying with GL.Ortho:

GL.LoadIdentity();
GL.Ortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
rakkarage's picture

thank you. that fixed it... but is this a difference between using OpenTK and not using it? the redbook omits that step because Projection matrix is identity by default? but in OpenTK its not?

thanks

the Fiddler's picture

No, this is the difference between:

GL.Ortho(...);

and

while (true)
{
    GL.Ortho(...);
}

OnRenderFrame in OpenTK is called in a loop. The main function in the redbook example is called only once. If you move to the later redbook examples, you'll see that they use LoadIdentity, too.

rakkarage's picture

thanks :)