PDA

View Full Version : Cant glTranslatef



ethereal
09-23-2012, 06:59 AM
Morning. I'm trying to use glTranslatef still I can't move my object. I read something about the projection matrix, put some lines but it didnt work. What do I have to do?




#include "GL/freeglut.h"
#include "GL/gl.h"

void Draw(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(0.0f, -1.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glVertex2f(-0.25, 0.25);
glVertex2f(-0.75, 0.25);
glVertex2f(-0.75, 0.75);
glVertex2f(-0.25, 0.75);
glTranslatef(-1.0, 0.0, 0.0);
glEnd();
glFlush();
}
void Init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Translation");
glutDisplayFunc(Draw);
glutIdleFunc(Draw);
Init();
glutMainLoop();
}

mhagain
09-23-2012, 07:04 AM
First of all, go back to your original code and post that; reading "something about" things and just ramdomly slapping in extra lines of code is not going to fix the problem, and will only make diagnosis even more difficult.

Secondly, glTranslatef (and glMatrixMode, and glLoadIdentity) are illegal between glBegin/glEnd pairs. This is called out in the documentation, which I recommend that you read. OpenGL may have a reputation for being easy, but that doesn't mean that you get to skip reading documentation.

Thirdly, if you want to translate an object you make your glTranslatef call before drawing it, not after. glTranslatef then updates the current matrix and objects drawn after the call will use the newly updated matrix.