user385261
user385261

Reputation: 4169

glOrtho is not working?

 /* OpenGL animation code goes here */
        glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
        glClear (GL_COLOR_BUFFER_BIT);
        glDisable(GL_DEPTH_TEST);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0f,512.0f,0.0f,512.0f,0.0f,1.0f);

        glMatrixMode(GL_MODELVIEW_MATRIX);
        glPushMatrix ();
        glLoadIdentity();
        glTranslatef(x,y,0.0f);
        //glRotatef (theta, 0.0f, 0.0f, 1.0f);

        glBegin (GL_TRIANGLES);
        glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
        glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, -0.5f);
        glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.8f, -0.5f);
        glEnd ();
        glPopMatrix ();

        SwapBuffers (hDC);
        //theta += 0.1f;
        Sleep(1);
        }

This is the main simple opengl loop.

Recently, I come back to study Opengl (after leaving it for like 2 yrs). The problem is, I tried to use glOrtho to create 2D environment, but It is not working. I mean, the program seems to ignore glOrtho call (no errors, no warnings, no projection, nothing). This causes the vertex (0,1) of GL_TRIANGLE to hit the top of windows, and anything more than 1.0f will fly off the viewport (it is still an identity matrix I think?). I tried changing the parameters but no changes occur in the vieewport. I don't know if I am missing something (maybe missing some initialization step or some deprecation that I am not aware of).

Upvotes: 1

Views: 3043

Answers (1)

datenwolf
datenwolf

Reputation: 162317

glOrtho(0.0f,512.0f,0.0f,512.0f,0.0f,1.0f);

This projection effectively selects the box of width 512, height 512 and depth 1 units to be the visible world space. However the vertices you specify will all be within a very small subset of the box, i.e. within one unit.

glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.8f, -0.5f);
glEnd();

I think you just confused the use of glOrtho with glViewport.

Upvotes: 1

Related Questions