Simple application - only produces a black screen?

What am I missing here? All I’m attempting to do at this point is create a blank floor, but all I’m getting is a black screen. I’ll admit that despite reading tutorial after tutorial OpenGL still confuses me senseless but… I’ve matched a lot of this up against what I’ve seen elsewhere… What am I doing wrong here?

#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

using namespace std;

void init(void){
    glEnable(GL_DEPTH_TEST);
}

void resize(int w, int h){
    // prevent divide by zero
    if(h == 0) { h = 1; }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,w,h);
    gluPerspective(45, 1, 0, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
	gluLookAt(0, 3, 5, 0, 0, 0,	0.0f, 1.0f, 0.0f);
}

void renderScene(void){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw ground
    glColor3f(1, 0, .5);
    glBegin(GL_QUADS);
        glVertex3f(200, 0, 200);
        glVertex3f(-200, 0, 200);
        glVertex3f(-200, 0, -200);
        glVertex3f(200, 0, -200);
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    // initialize glut
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,00);
    glutInitWindowSize(800,800);

    init();

    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutReshapeFunc(resize);

    glutCreateWindow("3d test");
    glutMainLoop();

    return 0;

}

Any advice here would be great… I’m evidently a horrible graphics programmer because I can’t understand this stuff for the life of me and they’ve literally just thrown me head first into it at work. Wonderful, right?

Try two things: 1) comment out the gluLookAt for now, and 2) add a glTranslatef (0, -100, -700) just before you define your quad. That should make the pink quad visible. I think the problem was that most of your quad was outside the volume of space visible to the camera (set up by gluPerspective). Try playing around with the values in glTranslate to get a feel for this. Also, when you uncomment the gluLookAt command, put it just after gluPerspective. Good luck.

Thanks for your reply! Still don’t see anything though… I’m looking at other code online and I don’t see much different between it and mine… even copy pasted some from online and ran it successfully. blah.

you’ve generated an invalid projection matrix
having the near clipping plane at 0 with perspective projection is like trying to create a black hole :wink:

simply change

gluPerspective(45, 1, 0, 1000);

to

gluPerspective(45, 1, 1, 1000);

and with glu perspective you want the aspect ratio to be width/height of your screen, otherwise stuff will look distorted :wink:


gluPerspective(45, 1, 0, 1000);

Very bad. :slight_smile:

znear should never be set to zero or you will get a very poor depth buffer precision and the perspective projection won’t be valid.

You should do something like this:


float aspect = (float)w/h;
gluPerspective(45, aspect, 1, 1000);

EDIT:
OK, I am to slow…

My version (below) of your code works on my system. As you can see, I made only minimal changes to your program. Try this. If it works, look for differences between our codes other than the glTranslate. BTW, I also noticed that zero is not supposed to be used for the near clipping plane value in gluPerspective. But I tried it with 10 and with zero. It made no difference in this case. So I don’t think that’s your problem.


#include <stdio.h>
#include <stdlib.h>
#include <glut.h>

void resize (int w, int h)
{
    if (h == 0)  h = 1;      // prevent divide by zero

    glMatrixMode   (GL_PROJECTION);
    glLoadIdentity ();
    glViewport     (0, 0, w, h);
    gluPerspective (45, 1, 0, 1000);
//  gluLookAt      (0, 3, 5, 0, 0, 0,	0.0f, 1.0f, 0.0f);

    glMatrixMode   (GL_MODELVIEW);
    glLoadIdentity ();
//  gluLookAt      (0, 3, 5, 0, 0, 0,	0.0f, 1.0f, 0.0f);
}

void renderScene (void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode   (GL_MODELVIEW);
    glLoadIdentity ();

    glColor3f (1, 0, .5);

	glTranslatef (0, -100, -700);

    glBegin (GL_QUADS);
        glVertex3f ( 200, 0,  200);
        glVertex3f (-200, 0,  200);
        glVertex3f (-200, 0, -200);
        glVertex3f ( 200, 0, -200);
    glEnd ();

    glutSwapBuffers();
}

int main (int argc, char **argv)
{
    glutInit (&argc, argv);

    glutInitDisplayMode    (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition (100, 100);
    glutInitWindowSize     (800, 800);

    glutCreateWindow ("3d test");
    glutDisplayFunc  (renderScene);

//  glutIdleFunc     (renderScene);
    glutReshapeFunc  (resize);

    glutMainLoop ();
    return 1;
}

Ah ha! I think I’ve got it figured out. I had the Display and Reshape funcs listed after the Create window func. I didn’t realize this made a difference but after looking at the only differences between my code and MaxH’s code… I changed it the whole thing cleared up instantly. feels sheepish

New question… if I supposedly start off centered at (0,0,0), facing down the -z axis, why do I see the entire quad in front of me? Shouldn’t it extend on all sides around me?

EDIT: Dur. Nevermind. The -700 transformation. Sorry. I told you I was horrible at OpenGL -_-;;

You have stumbled onto one of the more frustrating and confusing aspects of OpenGL/Glut programming (to me). It is the order in which certain commands have to be called. Sometimes it simply makes no sense to me - and the manuals don’t help at all. They don’t even address the issue. The only way (for me) to proceed is to find snippets of code that work and experiment around with them. This is not just a problem for you or novice OpenGL programmers. Notice that no one who responded to your query (including myself) seemed to know that CreateWindow has to be called before DisplayFunc and ReshapeFunc.

Notice that no one who responded to your query (including myself) seemed to know that CreateWindow has to be called before DisplayFunc and ReshapeFunc.

Registering a display a callback for a window that is not created itself would certainly lead to some problems!
See glutDisplayFunc man page.