0

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.

Chris Leyva
  • 3,478
  • 1
  • 26
  • 47
Eugen
  • 1,537
  • 7
  • 29
  • 57
  • 1
    It looks like you don't have a projection matrix, which is necessary for rendering 3D objects – chbaker0 Dec 29 '13 at 01:45
  • Something else I've just noticed is that the last line "Initialise OpenGL: Success!" of the initGlut function is never printed into the console. Stupid question, is that related to the projection matrix? – Eugen Dec 29 '13 at 01:51
  • 1
    No, `glutMainLoop()` should be an infinite loop that just gets events and responds to them. You probably want to move the print statement above the call to `glutMainLoop()`. – user1118321 Dec 29 '13 at 01:52
  • The projection matrix is set up by setting the matrix mode to `GL_PROJECTION` and calling either [glFrustum](http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) or [gluPerspective](http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml) to create the perspective matrix. This matrix is what turns the vertices into a "picture" we an see on screen, with further away objects appearing smaller. – chbaker0 Dec 29 '13 at 01:54
  • 1
    You really may want to check out a tutorial teaching modern OpenGL, such as [this](http://www.arcsynthesis.org/gltut/). What you are using is called "fixed function", which means OpenGL does most of the actual rendering work for you. However, this creates bad habits and results in people not actually knowing what goes on behind the scenes. Modern OpenGL "core profiles" actually force you to write your own shaders and create your own buffers, and result in a deeper understanding of how the stuff actually works. – chbaker0 Dec 29 '13 at 02:20
  • Thanks. I will. Unfortunately I am very time-restricted to get a working diamond-square terrain generation program that will be used in a larger "scientific" research application so I'm looking for the easiest solutions right now. Appreciate the advice and help though. – Eugen Dec 29 '13 at 02:40

1 Answers1

3

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.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • My code fails the first test apparently. Nothing rendered at all. I read above that the projection matrix might be missing. Sorry for the stupid question but can you show me what a really basic example of a projection matrix look for my case? – Eugen Dec 29 '13 at 01:57
  • 1
    There's a [good explanation of it here](http://www.songho.ca/opengl/gl_projectionmatrix.html). – user1118321 Dec 29 '13 at 02:05
  • Wow, ok, I'm sure that is a great explanation, although I was hoping for a simpler answer to be honest. Something like a magical, solve-everything "default-value" :)) – Eugen Dec 29 '13 at 02:08
  • 1
    @Eugen there is no "default value", it totally depends on your scene and what you want everything to look like. If you don't understand the theory, you won't get anywhere. – chbaker0 Dec 29 '13 at 02:21
  • Look up [`glFrustum()`](http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml). It's a simple call to set up some reasonable defaults for your scene. – user1118321 Dec 29 '13 at 03:39