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