1
\$\begingroup\$

I'm learning OpenGL to build small games.

However, my program seems not plotting points and lines using GL_LINE and GL_POINT, but polygons are ok.

I found that there seems to be several ways to initialize opengl "context" but I don't really understand that. I was wondering whether it's the way I setup opengl context wrong?

#include <GL/glut.h>  // GLUT, include glu.h and gl.h

/* Global variables */
char title[] = "3D Shapes";

/* Initialize OpenGL Graphics */
void initGL() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
    glClearDepth(1.0f);                   // Set background depth to farthest
    glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
    glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
}

/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
    glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix

                                    // Render a color-cube consisting of 6 quads with different colors
    glLoadIdentity();                 // Reset the model-view matrix
    glTranslatef(1.5f, 0.0f, -7.0f);  // Move right and into the screen
    gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, -10.0, 0.0, 1.0, 0.0);

    glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
                                      // Top face (y = 1.0f)
                                      // Define vertices in counter-clockwise (CCW) order with normal pointing out
    glColor3f(0.0f, 1.0f, 0.0f);     // Green
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    // Bottom face (y = -1.0f)
    glColor3f(1.0f, 0.5f, 0.0f);     // Orange
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    // Front face  (z = 1.0f)
    glColor3f(1.0f, 0.0f, 0.0f);     // Red
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    // Back face (z = -1.0f)
    glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
    glVertex3f(1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    // Left face (x = -1.0f)
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    // Right face (x = 1.0f)
    glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glEnd();  // End of drawing color-cube


    glLoadIdentity();
    glLineWidth(2.5);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(15, 0, 0);
    glEnd();

    glLoadIdentity();
    glPointSize(3.0);
    glTranslatef(-5.0, 0.0, 0.0);
    glBegin(GL_POINTS);
    glVertex3f(0.0, 0.0, 0.0);
    glEnd();

    glFlush();
}

/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) {  // GLsizei for non-negative integer
                                               // Compute aspect ratio of the new window
    if (height == 0) height = 1;                // To prevent divide by 0
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    // Set the viewport to cover the new window
    glViewport(0, 0, width, height);

    // Set the aspect ratio of the clipping volume to match the viewport
    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
    glLoadIdentity();             // Reset
                                  // Enable perspective projection with fovy, aspect, zNear and zFar
    gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}

/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
    glutInit(&argc, argv);            // Initialize GLUT
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); // Enable double buffered mode
    glutInitWindowSize(640, 480);   // Set the window's initial width & height
    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
    glutCreateWindow(title);          // Create window with the given title
    glutDisplayFunc(display);       // Register callback handler for window re-paint event
    glutReshapeFunc(reshape);       // Register callback handler for window re-size event
    initGL();                       // Our own OpenGL initialization
    glutMainLoop();                 // Enter the infinite event-processing loop
    return 0;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ You should not use OpenGL 1, a lot of GPUs don't support it. There' a high chance, that this is the case here too \$\endgroup\$ – Bálint Oct 18 '17 at 8:59
  • \$\begingroup\$ @Bálint - near clipping plane. \$\endgroup\$ – Maximus Minimus Oct 18 '17 at 9:05
5
\$\begingroup\$

You're drawing your points and lines with Z = 0, but your view frustum range (set by gluPerspective) sets zNear to 0.1 and zFar to 100. So therefore your points and lines are discarded by the near clipping plane. OpenGL is just doing what you've told it to do.

To resolve: don't set your near clipping plane to 0: This will cause a division by 0 in your vertex transformations and mess everything up. Instead, push your points and lines in a little.

Incidentally: your context creation is mostly fine, but I would typically advise using GLUT_DOUBLE instead of GLUT_SINGLE and glutSwapBuffers instead of glFlush - single buffered contexts made sense in 1993 when not all hardware supported double-buffering, but nowadays the opposite is true and it's single-buffer that is going to be poorly supported.

\$\endgroup\$
  • \$\begingroup\$ Good catch. I didn't notice the clipping plane issue \$\endgroup\$ – Bálint Oct 18 '17 at 9:48
  • \$\begingroup\$ If we're making suggestions on not using outdated stuff in OpenGL, we should probably also mention that GLUT is deprecated (no longer being updated) and that immediate mode has been removed from modern OpenGL. You should be using Vertex Arrays or something more modern instead of calling glBegin/glEnd. \$\endgroup\$ – user1118321 Oct 18 '17 at 22:09
  • 1
    \$\begingroup\$ @user1118321 - I differentiate between (1) outdated stuff that is still required by the spec to work in compatibility contexts and (2) outdated stuff that is only going to cause tears and suffering. Since OpenGL doesn't specify context creation and management, and therefore vendors and/or platforms are free to change and/or break it as they see fit, use of a single-buffered context falls squarely into (2). \$\endgroup\$ – Maximus Minimus Oct 19 '17 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.