PDA

View Full Version : colors acting weird



Raposo
06-19-2011, 12:51 PM
hey guys, i was programming a simple application that has a rotating box. I only drew 2 of the 6 sides of that box and put them rotating. The thing is that i chose different colors for each of the side and the colors are blinking as those sides rotate. Worse than that sometimes you can see the color of side that is supposedly omited by the one in the front...
I'd appreciate some help.
Here is the code:
main.cpp

#include <GL/glut.h>
#include "CardDraw.h"

static GLfloat spin = 0.0;

void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CardDraw cD;
cD.draw((double) spin, 0);
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
}

void spinDisplay(void){
spin += 1.0;
if(spin > 360.0){
spin -= 360.0;
}
glutPostRedisplay();
}

void reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, 16.0 / 9.0, 0, 200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 100, 0, 0, 0, 0, 1, 0);
}

void mouse(int button, int state, int x, int y){
switch(button){
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN){
glutIdleFunc(spinDisplay);
}
break;
case GLUT_RIGHT_BUTTON:
if(state == GLUT_DOWN){
glutIdleFunc(NULL);
}
break;
default:
break;
}
}

void init(void){
glClearColor(0, 0, 0, 0);
glShadeModel(GL_FLAT);
}

int main(int argc, char** argv){

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(480, 270);
glutCreateWindow("Test by Raposo");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();

return 0;

}

and CardDraw.h

#include <GL/glut.h>

class CardDraw{

private:
int _size[3];

public:
CardDraw(){
_size[0] = 72;
_size[1] = 96;
_size[2] = 10;
}
virtual ~CardDraw(){}
void draw(double angle, int mode){
glPushMatrix();
switch(mode){
case 0:
glRotatef(angle, 0, 1, 0);
break;
case 1:
glRotatef(angle, 1, 0, 0);
break;
default:
break;
}
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3d(-_size[0] / 2.0, -_size[1] / 2.0, _size[2] / 2.0);
glVertex3d(_size[0] / 2.0, -_size[1] / 2.0, _size[2] / 2.0);
glVertex3d(_size[0] / 2.0, _size[1] / 2.0, _size[2] / 2.0);
glVertex3d(-_size[0] / 2.0, _size[1] / 2.0, _size[2] / 2.0);
glEnd();
glColor3f(0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3d(_size[0] / 2.0, -_size[1] / 2.0, -_size[2] / 2.0);
glVertex3d(-_size[0] / 2.0, -_size[1] / 2.0, -_size[2] / 2.0);
glVertex3d(-_size[0] / 2.0, _size[1] / 2.0, -_size[2] / 2.0);
glVertex3d(_size[0] / 2.0, _size[1] / 2.0, -_size[2] / 2.0);
glEnd();
glPopMatrix();
}

};

thanks in advance
ps: try run the program to see the awful effect i'm talking about.

vivek vyas
06-19-2011, 02:03 PM
gluPerspective(90.0, 16.0 / 9.0, 0, 200);

gluPerspective(90.0, 16.0 / 9.0, 1, 200);

Raposo
06-19-2011, 04:06 PM
gluPerspective(90.0, 16.0 / 9.0, 0, 200);

gluPerspective(90.0, 16.0 / 9.0, 1, 200);

that worked! can you tell me why? i mean what's the problem having the zNear plane at 0 distance from the viewer?

Alfonse Reinheart
06-19-2011, 04:23 PM
Short answer: because it causes the projection math to not work out.

Long answer: Read this (http://www.arcsynthesis.org/gltut/Positioning/Tut04%20Perspective%20Projection.html#d0e3831).

Look at the equation for computing the clip-space Z. If you put a zNear of 0 into that equation, you come out with -Zcamera. If you then perform the perspective divide, with a W of -Zcamera, you get a Zndc value of 1. For everything, regardless of its camera-space Z position.