PDA

View Full Version : Simple application - only produces a black screen?



omdown
06-11-2009, 02:25 PM
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(&amp;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?

MaxH
06-11-2009, 04:03 PM
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.

omdown
06-11-2009, 04:14 PM
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.

dukey
06-11-2009, 04:31 PM
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 ;)

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 ;)

dletozeun
06-11-2009, 04:37 PM
gluPerspective(45, 1, 0, 1000);


Very bad. :)

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...

MaxH
06-11-2009, 06:23 PM
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 (&amp;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;
}

omdown
06-12-2009, 07:01 AM
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 -_-;;

MaxH
06-12-2009, 11:21 AM
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. 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.

dletozeun
06-13-2009, 07:12 AM
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 (http://www.opengl.org/resources/libraries/glut/spec3/node46.html#SECTION00081000000000000000).