PDA

View Full Version : just get a black screen



chewabob
05-11-2011, 12:44 PM
Hi,
I try now for hours to understand why my screen is still black. I use qt to draw a simple quad. At first I tried a polygon in 2d. That worked. Here is the code

void GLtest::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_MULTISAMPLE);
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}
//! [6]

//! [7]
void GLtest::paintGL()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLenum errorcode;
errorcode = glGetError();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
errorcode = glGetError();
glBegin(GL_QUADS);
glVertex3f(400,400,-8);
glVertex3f(400,-400,8);
glVertex3f(-400,-400,-8);
glVertex3f(-400,400,8);
glEnd();
errorcode = glGetError();

}
//! [7]

//! [8]
void GLtest::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 150.0);
#else
glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 150.0);
#endif
glMatrixMode(GL_MODELVIEW);

}

It is a Problem I get often playing with opengl.Is there a short shecklist, what to do if the screen keeps beeing black? What is the minimal code to get this quad drawn in my window?

Thanks for help

kowal
05-11-2011, 02:09 PM
I don't use Qt, but You should probably call some function to swap buffers in your paintGL, or at least glFlush if you are using single buffered context

chewabob
05-12-2011, 09:03 AM
Thanks for fast answer. However I think, this is done by qt.
I tried some other code and got it working with this:

void GLtest::initializeGL()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_SMOOTH);
}
//! [6]

//! [7]
void GLtest::paintGL()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

// We will draw things a little deeper into the screen
glTranslatef(0,0,-20);

glColor3f(1,0,0);
/* glBegin(GL_TRIANGLES);

glVertex3f(0,2,0);
glVertex3f(3,0,0);
glVertex3f(-3,0,0);

glEnd();*/
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(1,1,0);
glVertex3f(0,1,0);
glEnd();


}
//! [7]

//! [8]
void GLtest::resizeGL(int width, int height)
{
glViewport (0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) width/(GLfloat) height, 1.0, 21.0);
glMatrixMode (GL_MODELVIEW);

}

So I will compare to find my mistake.

dukey
05-12-2011, 10:15 AM
you are drawing outside the view frustum, so nothing will be visible

try glOrtho with near and far at -100 and 100
you'll see something then