Question

I'm a complete beginner with OpenGL, just trying to learn (starting with freeglut for the moment). So far I have the following code that should draw some basic 3D objects. The problem is that whatever I put in the render function (although it does execute), it only displays a blank window.

#include "stdafx.h"
#include <iostream>
#include "dependente\glew\glew.h"
#include "dependente\freeglut\glut.h"

void render()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_DEPTH_TEST);

    glTranslatef(-1.5f, 1.0f, -6.0f);                                     // Translate back and to the left

    glPushMatrix();                                                         // Push the current modelview matrix on the matrix                         // Rotate on all 3 axis
    glBegin(GL_TRIANGLES);                                                // Draw a pyramid
    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of front face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Left of front face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);                                    // Right of front face

    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of right face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);                                    // Left of right face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Right of right face

    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of back face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Left of back face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Right of back face

    glColor3f(1.0f, 0.0f, 0.0f);                                      // Red
    glVertex3f(0.0f, 1.0f, 0.0f);                                     // Top of left face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Left of left face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Right of left face
    glEnd();

    // Render a quad for the bottom of our pyramid
    glBegin(GL_QUADS);
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Left/right of front/left face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(1.0f, -1.0f, 1.0f);                                    // Right/left of front/right face
    glColor3f(0.0f, 1.0f, 0.0f);                                      // Green
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Right/left of right/back face
    glColor3f(0.0f, 0.0f, 1.0f);                                      // Blue
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Left/right of right/back face
    glEnd();
    glPopMatrix();

    glTranslatef(3.0f, 0.0f, 0.0f);                                        // Translate right
    glPushMatrix();                                                         // Push the current modelview matrix on the matrix stack                         // Rotate the primitive on all 3 axis
    glBegin(GL_QUADS);
    // Top face
    glColor3f(0.0f, 1.0f, 0.0f);                                   // Green
    glVertex3f(1.0f, 1.0f, -1.0f);                                   // Top-right of top face
    glVertex3f(-1.0f, 1.0f, -1.0f);                                   // Top-left of top face
    glVertex3f(-1.0f, 1.0f, 1.0f);                                   // Bottom-left of top face
    glVertex3f(1.0f, 1.0f, 1.0f);                                   // Bottom-right of top face

    // Bottom face
    glColor3f(1.0f, 0.5f, 0.0f);                                  // Orange
    glVertex3f(1.0f, -1.0f, -1.0f);                                  // Top-right of bottom face
    glVertex3f(-1.0f, -1.0f, -1.0f);                                  // Top-left of bottom face
    glVertex3f(-1.0f, -1.0f, 1.0f);                                  // Bottom-left of bottom face
    glVertex3f(1.0f, -1.0f, 1.0f);                                  // Bottom-right of bottom face

    // Front face
    glColor3f(1.0f, 0.0f, 0.0f);                                  // Red
    glVertex3f(1.0f, 1.0f, 1.0f);                                  // Top-Right of front face
    glVertex3f(-1.0f, 1.0f, 1.0f);                                  // Top-left of front face
    glVertex3f(-1.0f, -1.0f, 1.0f);                                  // Bottom-left of front face
    glVertex3f(1.0f, -1.0f, 1.0f);                                  // Bottom-right of front face

    // Back face
    glColor3f(1.0f, 1.0f, 0.0f);                                 // Yellow
    glVertex3f(1.0f, -1.0f, -1.0f);                                 // Bottom-Left of back face
    glVertex3f(-1.0f, -1.0f, -1.0f);                                 // Bottom-Right of back face
    glVertex3f(-1.0f, 1.0f, -1.0f);                                 // Top-Right of back face
    glVertex3f(1.0f, 1.0f, -1.0f);                                 // Top-Left of back face

    // Left face
    glColor3f(0.0f, 0.0f, 1.0f);                                   // Blue
    glVertex3f(-1.0f, 1.0f, 1.0f);                                   // Top-Right of left face
    glVertex3f(-1.0f, 1.0f, -1.0f);                                   // Top-Left of left face
    glVertex3f(-1.0f, -1.0f, -1.0f);                                   // Bottom-Left of left face
    glVertex3f(-1.0f, -1.0f, 1.0f);                                   // Bottom-Right of left face

    // Right face
    glColor3f(1.0f, 0.0f, 1.0f);                                   // Violet
    glVertex3f(1.0f, 1.0f, 1.0f);                                   // Top-Right of left face
    glVertex3f(1.0f, 1.0f, -1.0f);                                   // Top-Left of left face
    glVertex3f(1.0f, -1.0f, -1.0f);                                   // Bottom-Left of left face
    glVertex3f(1.0f, -1.0f, 1.0f);                                   // Bottom-Right of left face
    glEnd();
    glPopMatrix();

    glTranslatef(-1.5f, -3.0f, 0.0f);                                     // Back to center and lower screen
    glPushMatrix();
    glColor3f(1.0f, 1.0f, 0.0f);                                          // Yellow
    glutSolidSphere(1.0f, 16, 16);                                        // Use GLUT to draw a solid sphere
    glScalef(1.01f, 1.01f, 1.01f);
    glColor3f(1.0f, 0.0f, 0.0f);                                          // Red
    glutWireSphere(1.0f, 16, 16);                                         // Use GLUT to draw a wireframe sphere
    glPopMatrix();
}

void initGlut(int argc, char* argv[]) {
    std::cout << "Initialise OpenGL..." << std::endl;

    glutInit(&argc, argv);
    int iScreenWidth = glutGet(GLUT_SCREEN_WIDTH);
    int iScreenHeight = glutGet(GLUT_SCREEN_HEIGHT);

    glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowPosition(120, 120);
    glutInitWindowSize(600, 600);

    glutCreateWindow("OpenGL");

    // Register GLUT callbacks
    glutDisplayFunc(render);

    // Setup initial GL State
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClearDepth(1.0f);

    glShadeModel(GL_SMOOTH);
    glutMainLoop();
    std::cout << "Initialise OpenGL: Success!" << std::endl;
}

int _tmain(int argc, char* argv[])
{
    initGlut(argc, argv);
    return 0;
}

Hopefully someone with more experience will let me know what obvious thing I'm missing.

Was it helpful?

Solution

Here's how I go about debugging the problem "OpenGL isn't drawing anything":

  1. Add this code to the start of my render() function: glClearColor (1, 1, 0, 1); glClear (GL_COLOR_BUFFER_BIT); If the output turns yellow, it's calling your render() function and clearing the output properly. You can then remove that code or comment it out. If the output doesn't turn yellow, then either your render() function isn't getting called or it is, but your OpenGL state is set up not to draw to the screen. (Perhaps the wrong context is current at the time, or the color attachment for the current FBO isn't what you think it is.)

  2. Attempt to draw a single white triangle, with no textures or shaders, centered at the origin. If it shows up, then the other geometry you're trying to draw could be wrong. If it doesn't show up, the problem could be your matrix calculations (projection or modelview matrix). (Are you pointing the "camera" where you think you are? Are your objects being drawn where you think?) It could also be lighting, blending, or depth testing. I turn all of those off for this sort of test just to be sure. (See glEnable()/glDisable() for how to turn them on and off.)

  3. If that stuff works, I start turning on the things that I turned off above: texturing, shaders, lighting, blending, depth testing. I turn them on one at a time until something goes wrong.

If nothing goes wrong, then probably the geometry for my objects is wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top