PDA

View Full Version : glTranslate Problem



Ahmad Shadeed
06-10-2012, 10:18 AM
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 :)

thokra
06-11-2012, 12:20 AM
First, why don't you use the depth buffer? Second, have you setup a viewport and projection?

kowal
06-11-2012, 01:48 AM
call glPointSize() before glBegin();,



(...)
GL_INVALID_OPERATION is generated if glPointSize
is executed between the execution of glBegin
and the corresponding execution of glEnd.
(...)

thokra
06-11-2012, 02:03 AM
call glPointSize() before glBegin();

Originally Posted by http://www.opengl.org/sdk/docs/man/xhtml/glPointSize.xml
(...)
GL_INVALID_OPERATION is generated if glPointSize
is executed between the execution of glBegin
and the corresponding execution of glEnd.
(...)



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.

rakeshthp
06-11-2012, 04:32 AM
Are you using single buffer or double buffer? Without translate do you get correct output? Have you over-ridden background erase event?

Ahmad Shadeed
06-11-2012, 04:46 AM
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

thokra
06-11-2012, 05:03 AM
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 (http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml)(). 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);

Ahmad Shadeed
06-11-2012, 05:07 AM
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();
}

thokra
06-11-2012, 05:07 AM
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 (http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml)(). 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);

Ahmad Shadeed
06-11-2012, 05:13 AM
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<GLdouble>(width) / static_cast<GLdouble>(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 ?

thokra
06-11-2012, 05:28 AM
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.