glVertex3f

Hi

this is probably a silly question
But I wonder why my triangle is not drawn

Here’s a bit of the code

OG regards

void display()
{
    /* Draw a grid on the screen */
    int x, y, w, h;

    w = glutGet(GLUT_WINDOW_WIDTH);
    h = glutGet(GLUT_WINDOW_HEIGHT);
    
    /* Clear the screen */
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    /* Set color to light grey */
    glColor3f(0, 1.0, 0);
    /* Loop over all points and set those that are on a multiple of five in
       either coordinate to grey, this will give a 5x5 grid */
    glVertex3f(2.0f, 2.0f, 2.0f);    // lower left vertex
    glVertex3f( 1.0f, 1.0f, 1.0f);    // lower right vertex
    glVertex3f( 0.0f,  0.0f, 0.0f);  
    glEnd();

    /* Force the commands to complete */
    glFlush();
}

void idle()
{
    /* Update the GUI */
    if(!tickGUI())
    {
        /* The GUI window has been closed */
        exit(EXIT_SUCCESS);
    }
}

void reshape(int width, int height)
{
    /* Setup the viewport and transformations that must be used for
       the project */
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(44.0f, width/height, 0.2f, 255.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

It is not a triangle, because 3 vertices are on the same line.

Ok, ok

So I tried this
But the screen is totally white
…mysterious…

OG regards

 /* Clear the screen */
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_LINES);
    glColor3f(0, 1.0, 0);


    glVertex3f(-2.0f, 2.0f, 0.0f);
    glVertex3f(2.0f, -2.0f, 0.0f);

    glEnd();

You’re in perspective mode, the viewer is in the origin at (0.0, 0.0, 0.0) in OpenGL, you’re drawing on the z == 0.0 plane, but your frustum starts at zNear == 0.2 (see gluPerspective(44.0f, width/height, 0.2f, 255.0f)), means your line is not inside the view volume but in front of it.

You need to move the viewer backwards to get the line into view. This is done by moving all geometry into the -z direction.

Do this:

 
void reshape(int width, int height)
{
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(44.0f, width/height, 0.2f, 255.0f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0f, 0.0f, -128.0f); // move origin near the center of the viewing frustum
} 

This should result in small green diagonal line from top left to bottom right on black background.
If you got a white window did you choose a double buffered pixelformat and forget to swap?

Tip:

  • Default glClearColor is black. For debugging you should set it to a different color to see black rendering.
  • Don’t put constant vertex attributes into begin end:
   
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 1.0, 0); // Set constant vertex state outside of glBegin-glEnd.
glBegin(GL_LINES);
glVertex3f(-2.0f, 2.0f, 0.0f);
glVertex3f(2.0f, -2.0f, 0.0f);
glEnd();