All pastes #734361 Raw Edit

k4f example

public text v1 · immutable
·published 2007-10-12 17:11 UTC
rendered paste body
/* 3DTransformation.c
 *
 * This example shows how to draw a simple 3D object, in this case a cube.
 * To see that the object is 3D, rotation is needed, so in this example rotation
 * is allowed on the X, Y, and Z, axis.  This example deliberetly has visual
 * issues which are covered in the next example.
 */

#include <gl/glut.h>

void display(void);
void init_window(int argc, char** argv);
void other_init(void);
void keyboard(unsigned char key, int x, int y);

// Rotation Variables
GLfloat xRot,      // Angle of rotation on the x axis
		yRot,      // Angle of rotation on the y axis
		zRot;      // Angle of rotation on the z axis

// Points of the 8 corners of a cube 2 units in size centered on the origin
GLfloat points[8][3] = {{-1.0f, -1.0f, 1.0f}, 
                        {-1.0f, -1.0f, -1.0f}, 
                        {1.0f, -1.0f, -1.0f}, 
                        {1.0f, -1.0f, 1.0f},
                        {-1.0f, 1.0f, 1.0f}, 
                        {-1.0f, 1.0f, -1.0f}, 
                        {1.0f, 1.0f, -1.0f}, 
                        {1.0f, 1.0f, 1.0f}};

int main(int argc, char** argv) {
    init_window(argc,argv);
    other_init();

    glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);

	// We initialize the rotation variables to their initial states.
	xRot = yRot = zRot = 0.0f;

    glutMainLoop();
	return 0;
}

// Key event function
void keyboard(unsigned char key, int x, int y) {

	switch (key) {

		// Rotate the square counter-clockwise along the X axis
		case 'x': 
			xRot += 15.0f;   break;
		// Rotate the square clockwise along the X axis
		case 'X':
			xRot -= 15.0f;   break;

		// Rotate the square counter-clockwise along the Y axis
		case 'y': 
			yRot += 15.0f;   break;
		// Rotate the square clockwise along the Y axis
		case 'Y':
			yRot -= 15.0f;   break;

		// Rotate the square counter-clockwise along the Z axis
		case 'z': 
			zRot += 15.0f;   break;
		// Rotate the square clockwise along the Z axis
		case 'Z':
			zRot -= 15.0f;   break;
			
		// Resets all of the transformation variables to their initial states.
		case 'r': case 'R':
			xRot = yRot = zRot = 0.0f;
			break;

		// If the 'q' or 'Q' key is pressed, the program exits.
		case 'q': case 'Q':
			exit(0);
	}

	// Mark the current window as needing to be redisplayed.
	glutPostRedisplay();
}

// Refresh callback function
void display(void) {
	glClear(GL_COLOR_BUFFER_BIT);

	// Loads the identity matrix to clear any previous transformations
	glLoadIdentity();

	// Rotates xRot degrees about the X axis
	glRotatef(xRot, 1.0f, 0.0f, 0.0f);

	// Rotates yRot degrees about the Y axis
	glRotatef(yRot, 0.0f, 1.0f, 0.0f);

	// Rotates zRot degrees about the Z axis
	glRotatef(zRot, 0.0f, 0.0f, 1.0f);

	// Draws the cube using 6 polygons(four sided), each a different color.
	// Instead of glVertex3f, we will use glVertex3fv, which expects
	// a GLfloat array of 3 points instead of 3 GLfloats as the parameter.
	// The points given for each polygon are in counter clockwise order, 
	// while this is not necessary now, it will be needed later.

	// Face 1 - Red
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_POLYGON);
		glVertex3fv(points[0]);
		glVertex3fv(points[1]);
		glVertex3fv(points[2]);
		glVertex3fv(points[3]);
	glEnd();

	// Face 2 - Green
	glColor3f(0.0f, 1.0f, 0.0f);
	glBegin(GL_POLYGON);
		glVertex3fv(points[7]);
		glVertex3fv(points[6]);
		glVertex3fv(points[5]);
		glVertex3fv(points[4]);
	glEnd();

	// Face 3 - Blue
	glColor3f(0.0f, 0.0f, 1.0f);
	glBegin(GL_POLYGON);
		glVertex3fv(points[0]);
		glVertex3fv(points[3]);
		glVertex3fv(points[7]);
		glVertex3fv(points[4]);
	glEnd();

	// Face 4 - Yellow
	glColor3f(1.0f, 1.0f, 0.0f);
	glBegin(GL_POLYGON);
		glVertex3fv(points[0]);
		glVertex3fv(points[4]);
		glVertex3fv(points[5]);
		glVertex3fv(points[1]);
	glEnd();

	// Face 5 - Purple
	glColor3f(1.0f, 0.0f, 1.0f);
	glBegin(GL_POLYGON);
		glVertex3fv(points[1]);
		glVertex3fv(points[5]);
		glVertex3fv(points[6]);
		glVertex3fv(points[2]);
	glEnd();

	// Face 6 - Cyan
	glColor3f(0.0f, 1.0f, 1.0f);
	glBegin(GL_POLYGON);
		glVertex3fv(points[3]);
		glVertex3fv(points[2]);
		glVertex3fv(points[6]);
		glVertex3fv(points[7]);
	glEnd();

	glFlush();
}

// GLUT Window Initialization
void init_window(int argc, char** argv) {
    glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_RGB);
	glutInitWindowSize(600, 600);
	glutInitWindowPosition(0,0);
	glutCreateWindow("3DTransformation");
}

// This function handles some other initializations
void other_init() {
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-4, 4, -4, 4, -4, 4);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}