newbie OpenGL question. Please help!

Started by
2 comments, last by xargon123 13 years, 11 months ago
Hello everyone, I have been trying all sorts of things for hours to get this working but no luck! I have a simple code that creates a 2D grid. My problem is that I am unable to apply any transformation to the grid. Even if I try to load an identity matrix, everything goes crazy! Ok, here is how I set my OpenGL views. I have transformation that comes in screen space, so I do the following:

glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
Now, when I have no transformation, I can draw the grid just fine. The code is as follows:

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);        
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
int i, end;
for (i = 0; i < ih; i += 2) {
   glVertex2i(0, i);
   glVertex2i(iw, i);
}
end = i - 2;
for (int i = 0; i < iw; i += 2) {
  glVertex2i(i, 0);
  glVertex2i(i, end);
}
glEnd();
This works just fine and shows me a grid. Now as soon as I try to load a transformation (even an identity transformation!), the grid disappears and I think it gets drawn somewhere far away. So, the code for transformation is as follows:

// identity transformation!
float transformation[16] = {0.0f};        
transformation[0] = 1.0f;
transformation[5] = 1.0f;
transformation[10] = 1.0f;        
transformation[15] = 1.0f;
glLoadMatrixf(transformation);

// grid drawing code
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);        
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
int i, end;

// ih = 512
// iw = 512

for (i = 0; i < ih; i += 2) {
   glVertex2i(0, i);
   glVertex2i(iw, i);
}
end = i - 2;

for (int i = 0; i < iw; i += 2) {
  glVertex2i(i, 0);
  glVertex2i(i, end);
}
glEnd();
glPopMatrix();
So, here I have tried to use the glLoadMatrixf and loaded my identity transformation through that. What am I doing wrong? I would really appreciate some help as this is quite urgent for me and I have spend about 5 hours trying all sorts of thing now... Please help! Many thanks, xarg
Advertisement
glOrtho should be in your GL_PROJECTION matrix, not your GL_MODELVIEW matrix. When you call glLoadMatrix you're overwriting your glOrtho call, which is why everything looks screwed up.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
The only call you make to glMatrixMode is glMatrixMode(GL_MODELVIEW);

After that you call glOrtho(0, w, 0, h, -1, 1); which makes your model-view matrix a orthographic projection matrix. The model-view matrix and the projection matrix gets multiplied in the end, so there is nothing wrong with putting a projection matrix in the model-view matrix (but it makes more sense to have it in the projection matrix).

However when you later create your identity matrix you overwrite the projection already in the model-view matrix (since the active matrix is the model-view matrix), and you end up having no projection.

You could try something like this:
// Set up projection matrixglMatrixMode( GL_PROJECTION );glLoadIdentity();glOrtho(0, w, 0, h, -1, 1);// Set up model-view matrixglMatrixMode( GL_MODELVIEW );glLoadIdentity();glRotate3f( 45.0, 0.0, 0.0, 1.0 );
Thank you guys! Sorry, very new to this and making these silly mistakes.

Many thanks!

xarg

This topic is closed to new replies.

Advertisement