PDA

View Full Version : glTranslatef improperly used: nothing displayed



ramy1989
11-11-2012, 08:21 AM
I am trying to write a simple application with xcode that displaces a quad.For this I am using glTranslatef.The evidenced part of the code is where I try to translate the points.



#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>


int width=500, height=500;


void init()
{
glViewport(0, 0, width, height);
glLoadIdentity();
glOrtho(0, width, height, 0, 0, 1);
}


void display()
{
glClearColor(0.9, 0.9, 0.9, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW); /************************/
glLoadIdentity();[/B] /**************/
glTranslatef(100,0,0); /****** trying to translate the vertexes */
glBegin(GL_QUADS);
glVertex2i(100, 100);
glVertex2i(300, 100);
glVertex2i(300, 300);
glVertex2i(100, 300);
glEnd();
glFlush();
}


int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow("Test");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}


The quad should be displaced of 100 units in the widnow.Since in the glOrtho I specify the size of the view, I think that the quad shall be displayed inside the window.But instead I don't see anything: there isn't a quad in the window.So maybe I'm not using properly glTranslatef, but I can't find the error.

Rosario Leonardi
11-11-2012, 08:45 AM
In the init you were modifing the model matrix, and then overwriting it in the render.

void init()
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // Add this line
glLoadIdentity();
glOrtho(0, width, height, 0, 0, 1);
}

Tips: Don't use fixed pipeline anymore is deprecated. Don't use Glut is outdated.

ramy1989
11-12-2012, 10:54 AM
Ok but it seems like I'm missing something of how matrixes work, if I add a roratef before the glTranslatef:



glRotatef(0.01, 1.0, 0.0, 0.0);


Again nothing is displayed, what am I missing (I added the glMatrixMode(GL_PROJECTION); line).

Rosario Leonardi
11-12-2012, 04:56 PM
However this is a lot trickier.

Now you are doing
This is your rotation matrix A
1 0 0 0
0 c -s 0
0 s c 0
0 0 0 1
where c = cos(0.01) Degree
where s = sin(0.01) Degree
cause sin of 0.01 is really low this is almost an identity

This is your translation matrix B
1 0 0 100
0 1 0 0
0 0 1 0
0 0 0 1

Your transformation is vEye = A* (B * v)

1 0 0 100
0 c -s 0
0 s c 0
0 0 0 1

the first point should be in 200 100*c 100*-s 1

The Z in negative, but your glOrtho specify a near plane in 0. So all de vertexes are culled away.




glOrtho(0, width, height, 0, -1, 1);

should do the trick.

Mmm... btw, why are you doing random transformation? Is not better to read a book or a tutorial to learn?