PDA

View Full Version : problem of glColor4f



Sumiko
15th March 2013, 00:49
Hi, I am writing a test program of the Qt and openGL project for drawing a red square.

The code is:


void GLImageWidget::initializeGL()
{
static const GLfloat lightPos[4] = { 5.0f, 5.0f, 10.0f, 1.0f };

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, image_nx, image_ny);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glShadeModel( GL_SMOOTH );
glClearDepth( 1.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glEnable(GL_NORMALIZE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
slicelocation = 0;

/* checked OK!
printf("nv = %d", nv);
getchar();
int i=0;
for(i=0; i<nv; i++)
{
printf("%.4f,", fImage[i]);
}*/
}


void GLImageWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
glPushMatrix();

int i, j, k;
int half_nx = image_nx/2;
int half_ny = image_ny/2;
printf("now paintGL width = %d, height = %d\n", image_nx, image_ny);

glPushMatrix();
glRotatef(-90.0, 0, 0, 1);
for ( j = 0; j < image_ny; j++)
for ( k = 0; k < image_nx; k++)
{
glBegin(GL_QUADS);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(k-half_nx, j-half_ny, 0);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(k+1-half_nx, j-half_ny, 0);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(k+1-half_nx, j+1-half_ny, 0);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertex3f(k-half_nx, j+1-half_ny, 0);

glEnd();
}
glPopMatrix();
}

The square is not drawn... anything wrong here?
Thank you in advance!

wysota
15th March 2013, 06:45
Why do you think the problem is with glColor4f? I'd rather guess you're simply drawing outside your viewport.

Sumiko
15th March 2013, 15:55
Hi Wysota,

thank you so much for your answer, :-)

I changed the third argument of glVertex3f(k-half_nx, j-half_ny, 0), such as 100, 200... it still doesn't work... Need me initial the coordinate? how? Sorry, I am a starter, :-)

wysota
15th March 2013, 17:05
What is the value of image_nx?

d_stranz
15th March 2013, 19:08
For one thing, you are calling glPopMatrix() only once (line 61), but you are calling glPushMatrix() twice. Push and pop should be matched - you will eventually run out of stack otherwise.

But I'm not really sure what you are trying to do with this code. You aren't drawing a red square, you are drawing (image_nx * image_ny) red squares. You are also applying a translation and a rotation to each vertex in the quads. I have not checked your code, but also make sure you are specifying the vertexes of the quads in the correct order. If you do it wrong, the faces of your quads either will not be created correctly or they will not be facing in the right direction to be illuminated by your light.

You also do not need to call glColor4f() for every vertex. If all the vertexes are the same color, then you can move that call to outside the glBegin() (or even outside the loops completely).

You may also need to set material properties using glMaterialfv() before your objects are visible using lighting.

I suggest that if you are trying to teach yourself Qt and OpenGL at the same time, you start with a simple OpenGL example (like draw 1 red square using GL defaults for everything), port it to Qt, and get that to work first. Once that is working, then you can try to do something harder.