Hello. I just started using openGL and was attempting to make a simple box using GL_QUADS.
The problem that I am having is that when I rotate it by doing:
glRotatef(_angle, -1.0f, 0.0f, 0.0f);
One of the sides is missing. (Right side, or Green quad as commented in the last section of code)
However when I rotate it in a different direction:
glRotatef(_angle, 0.0f, -1.0f, 0.0f);
Then none of the sides are missing.
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <GL/glut.h>
using namespace std;
float _angle = 196.0f;
void handleKeypress(unsigned char key, int x, int y) {
switch(key) {
case 44:
_angle -= 5.0f;
if(_angle < 0)
_angle += 360;
glutPostRedisplay();
break;
case 46:
_angle += 5.0f;
if(_angle > 360)
_angle -= 360;
glutPostRedisplay();
break;
default:
cout << "Unknown key: " << static_cast<int>(key) << endl;
}
}
void init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
//glRotatef(_angle, 0.0f, -1.0f, 0.0f);
glRotatef(_angle, -1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(-1.0f, 0.0f, 0.0f); // Top Left
glVertex3f(0.0f, 0.0f, 0.0f); // Top Right
glVertex3f(0.0f, -1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
// Right
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(0.0f, 0.0f, 0.0f); // Top Left
glVertex3f(0.0f, 0.0f, -1.0f); // Top Right
glVertex3f(0.0f, -1.0f, -1.0f); // Bottom Right
glVertex3f(0.0f, -1.0f, 0.0f); // Bottom Left
// Left
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 0.0f, 0.0f); // Top Left
glVertex3f(-1.0f, 0.0f, -1.0f); // Top Right
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right
glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Back
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(-1.0f, 0.0f, -1.0f); // Top Left
glVertex3f(0.0f, 0.0f, -1.0f); // Top Right
glVertex3f(0.0f, -1.0f, -1.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Title");
init();
glutDisplayFunc(draw);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop();
return 0;
}
Why is this happening?