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