glTranslate Problem

Hi All,

I’am a begginner in OpenGL, and I tried to use the glTranslate function but the result was wrong

in the following code I’am trying to move a single point 2-points right

void point()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(2,0,-4.0);
glBegin(GL_POINTS);
glPointSize(5.0);
glVertex3f(0,0,0);
glEnd();
glFlush();
}

and the output is a black window

anyone can help ?

Thanks :slight_smile:

First, why don’t you use the depth buffer? Second, have you setup a viewport and projection?

call glPointSize() before glBegin();,

That wouldn’t alter the point size but should not prevent anything from being rendered. If the projection and viewport are correctly initialized it should at least render a single white pixel. What’s important, if you got a double-buffered context, you have to swap the buffers after rendering, or in your case flushing. Otherwise you’ll never see the contents of the back-buffer. If swapping is handled automatically by whatever widget toolkit you’re using then that’s not necessary. But I like to turn off automatic swapping and add it myself for clarity.

Are you using single buffer or double buffer? Without translate do you get correct output? Have you over-ridden background erase event?

thank you all,

this is the code

#include<GLUT/glut.h>
void point()
{
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(2,0,-4.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(0,0,0);
glEnd();
glutSwapBuffers();
}

int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow(“test”);
glutDisplayFunc(point);
glutMainLoop();
}

can anyone help me how to make the projection and viewport work correctly

can anyone help me how to make the projection and viewport work correctly

The viewport is simple. I think you’ll want to render to the whole buffer so you setup the viewport with

 glViewport(0, 0, 600, 600); 

Since you already use legacy stuff you can check out gluPerspective(). Note that the viewport should be resized to the new window size after a resize and the aspect ratio for gluPerspective is the quotient width / height.

So in conclusion something like this:


void resize(int width, int height)
{  


 }

// in main add
glutReshapeFunc(&resize);




plz write the right lines here in my code
i.e give me a running program … thanks again

#include<GLUT/glut.h>
void point()
{
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(2,0,-4.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(0,0,0);
glEnd();
glutSwapBuffers();
}

int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow(“test”);
glutDisplayFunc(point);
glutMainLoop();
}

can anyone help me how to make the projection and viewport work correctly

The viewport is simple. I think you’ll want to render to the whole buffer so you setup the viewport with

 glViewport(0, 0, 600, 600); 

Since you already use legacy stuff you can check out gluPerspective(). Note that the viewport should be resized to the new window size after a resize and the aspect ratio for gluPerspective is the quotient width / height.

So in conclusion something like this:


void resize(int width, int height)
{  
  glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));

  glMatrixMode(GL_PROJECTION); 
  glLoadIdentity();

  // choose your FOV accordingly, 75.0 is simply an example, so are near and far
  gluPerspective(75.0, static_cast<GLdouble>(width) / static_cast<GLdouble>(height), 1.0, 100);  

  // don't forget to go back to the model-view matrix-stack
  glMatrixMode(GL_MODELVIEW);
}

// in main add
glutReshapeFunc(&resize);

I have applied what you see in your comment , but when hiding // the glTranslate , the point is not appear !!

#include<GLUT/glut.h>
void point()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//glTranslatef(2,0,-4.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex3f(0,0,0);
glEnd();
glutSwapBuffers();
}

void resize(int width, int height)
{
glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));

glMatrixMode(GL_PROJECTION); 
glLoadIdentity();

// choose your FOV accordingly, 75.0 is simply an example, so are near and far
gluPerspective(75.0, static_cast&lt;GLdouble&gt;(width) / static_cast&lt;GLdouble&gt;(height), 1.0, 100);  

// don't forget to go back to the model-view matrix-stack
glMatrixMode(GL_MODELVIEW);

}

int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(600,600);
glutCreateWindow(“test”);
glutDisplayFunc(point);
glutReshapeFunc(&resize);
//gluOrtho2D(-150,150,-150,150);
glutMainLoop();
}

and another question , what the benifit we get from the resize() function ?

but when hiding // the glTranslate , the point is not appear !!

Well, if that’s surprising to you, you should think about what glTranslate does in general and in your context with your values.