glRotate

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?

Can you post a screen shot of what you are seeing without the rotation and then with it ? Did you try making it turn (sligthtly update angle in the update loop to see it turn on itself) to see what your problem may be ?

You’re sure the quad is always there? How do you know all the quads are visible at any given time? Is culling turned on?

Your left and right polygons are facing the same direction, and hence one of them becomes ‘back-facing’ and disappears depending on how you are rotating, and which way your culling is working.

First, disable back-face culling:

glDisable(GL_CULL_FACE);

This should recover your hidden face.

If you want to keep back-face culling in (recommended), then change the orientation of your face. I.e, whether you start defining the vertices clock-wise or anti-clockwise will determine which way they are looking (which way their normal is pointing), so to speak.

Thanks :slight_smile: