Results 1 to 9 of 9

Thread: Simple application - only produces a black screen?

  1. #1
    Member Contributor
    Join Date
    Jun 2009
    Posts
    68

    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?

    Code :
    #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?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Simple application - only produces a black screen?

    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.
    Am I doing your homework for you?

  3. #3
    Member Contributor
    Join Date
    Jun 2009
    Posts
    68

    Re: Simple application - only produces a black screen?

    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.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    258

    Re: Simple application - only produces a black screen?

    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

  5. #5
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Simple application - only produces a black screen?

    Code :
    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:

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

    EDIT:
    OK, I am to slow...

  6. #6
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Simple application - only produces a black screen?

    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.

    Code :
    #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;
    }
    Am I doing your homework for you?

  7. #7
    Member Contributor
    Join Date
    Jun 2009
    Posts
    68

    Re: Simple application - only produces a black screen?

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

  8. #8
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Simple application - only produces a black screen?

    Quote Originally Posted by omdown
    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.
    Am I doing your homework for you?

  9. #9
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Simple application - only produces a black screen?

    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.

Similar Threads

  1. mat4 and vec4 multiplication produces empty screen
    By Bilgin in forum OpenGL: GLSL
    Replies: 2
    Last Post: 08-06-2018, 02:22 AM
  2. Replies: 9
    Last Post: 07-27-2012, 06:31 AM
  3. Open GLES application displays black screen
    By sowmyaa in forum OpenGL ES
    Replies: 8
    Last Post: 05-08-2012, 12:44 AM
  4. simple texturing shader not working...Black Screen
    By abhishek bansal in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-18-2011, 10:57 AM
  5. Replies: 0
    Last Post: 09-30-2003, 08:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean