Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: glTranslate Problem

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6

    Exclamation 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
    Last edited by Ahmad Shadeed; 06-10-2012 at 01:07 PM.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    892
    First, why don't you use the depth buffer? Second, have you setup a viewport and projection?

  3. #3
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Location
    Poland
    Posts
    109
    call glPointSize() before glBegin();,

    Quote 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.
    (...)

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    892
    Quote Originally Posted by kowal
    call glPointSize() before glBegin();
    Originally Posted by http://www.opengl.org/sdk/docs/man/x...lPointSize.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.

  5. #5
    Intern Newbie
    Join Date
    Jun 2009
    Posts
    47
    Are you using single buffer or double buffer? Without translate do you get correct output? Have you over-ridden background erase event?

  6. #6
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    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

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    892
    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

    Code :
     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:

    Code :
    void resize(int width, int height)
    {  
     
     
     }
     
    // in main add
    glutReshapeFunc(&resize);

  8. #8
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    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();
    }

  9. #9
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    892
    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

    Code :
     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:

    Code :
    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);

  10. #10
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    6
    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 ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •