#include #include #include // Define a constant for the value of PI #define GL_PI 3.1415f // Put here your own global variables and procedures static int MenuID; static float xRot = 0.0; static float yRot = 0.0; void ReduceToUnit(float vector[3]) { float length; // Calculate the length of the vector length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2])); // Keep the program from blowing up by providing an exceptable // value for vectors that may calculated too close to zero. if(length == 0.0f) length = 1.0f; // Dividing each element by the length will result in a // unit normal vector. vector[0] /= length; vector[1] /= length; vector[2] /= length; } void calcNormal(float vertices[3][3], float out[3]) { float v1[3], v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; // Calculate two vectors from the three points v1[x] = vertices[0][x] - vertices[1][x]; v1[y] = vertices[0][y] - vertices[1][y]; v1[z] = vertices[0][z] - vertices[1][z]; v2[x] = vertices[1][x] - vertices[2][x]; v2[y] = vertices[1][y] - vertices[2][y]; v2[z] = vertices[1][z] - vertices[2][z]; // Take the cross product of the two vectors to get // the normal vector which will be stored in out out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Normalize the vector (shorten length to one) ReduceToUnit(out); } void drawNormalFront(float vertex[3], float normal[3]) { glColor3f(0.0, 0.0, 1.0); glBegin(GL_LINES); glVertex3fv(vertex); glVertex3f(vertex[0]+5.0*normal[0], vertex[1]+5.0*normal[1], vertex[2]+5.0*normal[2]); glEnd(); } void StrokeText(char *string) { int len, i; len = (int) strlen (string); for (i = 0; i < len; i++) { glutStrokeCharacter (GLUT_STROKE_ROMAN, string[i]); } } // Called to draw scene void RenderScene(void) { int i; float normal[3]; float vector[3][3]; float vertices[8][3]={{-15.0, -15.0, 15.0}, {-15.0, -15.0, -15.0}, {15.0, -15.0, -15.0}, {15.0, -15.0, 15.0}, {-15.0, 15.0, -15.0}, {-15.0, 15.0, 15.0}, {15.0, 15.0, 15.0}, {15.0, 15.0, -15.0}}; // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); // Bottom and top for (i=0; i<2; i++) { glColor3f(0.9, 0.9, 0.1); vector[0][0]=vertices[0+i*4][0]; vector[0][1]=vertices[0+i*4][1]; vector[0][2]=vertices[0+i*4][2]; vector[1][0]=vertices[1+i*4][0]; vector[1][1]=vertices[1+i*4][1]; vector[1][2]=vertices[1+i*4][2]; vector[2][0]=vertices[2+i*4][0]; vector[2][1]=vertices[2+i*4][1]; vector[2][2]=vertices[2+i*4][2]; calcNormal(vector, normal); glBegin(GL_QUADS); glNormal3fv(normal); glVertex3fv(vertices[0+i*4]); glVertex3fv(vertices[1+i*4]); glVertex3fv(vertices[2+i*4]); glVertex3fv(vertices[3+i*4]); glEnd(); drawNormalFront(vertices[0+i*4], normal); drawNormalFront(vertices[1+i*4], normal); drawNormalFront(vertices[2+i*4], normal); drawNormalFront(vertices[3+i*4], normal); } // Left and right for (i=0; i<2; i++) { glColor3f(0.9, 0.9, 0.1); vector[0][0]=vertices[1+i*2][0]; vector[0][1]=vertices[1+i*2][1]; vector[0][2]=vertices[1+i*2][2]; vector[1][0]=vertices[0+i*2][0]; vector[1][1]=vertices[0+i*2][1]; vector[1][2]=vertices[0+i*2][2]; vector[2][0]=vertices[4+i*2][0]; vector[2][1]=vertices[4+i*2][1]; vector[2][2]=vertices[4+i*2][2]; calcNormal(vector, normal); glBegin(GL_QUADS); glNormal3fv(normal); glVertex3fv(vertices[0+i*2]); glVertex3fv(vertices[1+i*2]); glVertex3fv(vertices[4+i*2]); glVertex3fv(vertices[5+i*2]); glEnd(); drawNormalFront(vertices[0+i*2], normal); drawNormalFront(vertices[1+i*2], normal); drawNormalFront(vertices[4+i*2], normal); drawNormalFront(vertices[5+i*2], normal); } // Front and back for (i=0; i<2; i++) { glColor3f(0.9, 0.9, 0.1); vector[0][0]=vertices[0+i*1][0]; vector[0][1]=vertices[0+i*1][1]; vector[0][2]=vertices[0+i*1][2]; vector[1][0]=vertices[3+i*1][0]; vector[1][1]=vertices[3+i*1][1]; vector[1][2]=vertices[3+i*1][2]; vector[2][0]=vertices[5-i*3][0]; vector[2][1]=vertices[5-i*3][1]; vector[2][2]=vertices[5-i*3][2]; calcNormal(vector, normal); glBegin(GL_QUADS); glNormal3fv(normal); glVertex3fv(vertices[0+i*1]); glVertex3fv(vertices[3+i*1]); glVertex3fv(vertices[6+i*1]); glVertex3fv(vertices[5-i*3]); glEnd(); drawNormalFront(vertices[0+i*1], normal); drawNormalFront(vertices[3+i*1], normal); drawNormalFront(vertices[6+i*1], normal); drawNormalFront(vertices[5-i*3], normal); } glPopMatrix(); // Flush drawing commands glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f }; GLfloat specular[] = { 0.6f, 0.6f, 0.6f, 1.0f}; // Enable lighting glEnable(GL_LIGHTING); // glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); // Setup and enable light 0 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); glLightfv(GL_LIGHT0,GL_SPECULAR,specular); glEnable(GL_LIGHT0); // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Set Material properties to follow glColor values glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); } void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; if(key > 356.0f) xRot = 0.0f; if(key < -1.0f) xRot = 355.0f; if(key > 356.0f) yRot = 0.0f; if(key < -1.0f) yRot = 355.0f; // Refresh the Window glutPostRedisplay(); } void Keyboard(unsigned char key, int x, int y) { // ... glutPostRedisplay(); } void Timer(int value) { // ... glutPostRedisplay(); glutTimerFunc(1000, Timer, value + 1); } void Idle() { glutPostRedisplay(); } void ChangeSize(GLsizei w, GLsizei h) { GLfloat fAspect; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); fAspect = (GLfloat)w/(GLfloat)h; // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Produce the perspective projection gluPerspective(60.0f, // fovy fAspect, // aspect 10.0, // zNear 100.0 // zFar ); // Positioning the camera gluLookAt(0.0, 0.0, 50.0, // eye 0.0, 0.0, 0.0, // center 0.0, 1.0, 0.0 // up ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void ProcessMenu(int value) { switch(value) { case 1: printf("1. menupont kivalasztva.\n"); break; case 2: printf("2. menupont kivalasztva.\n"); break; case 3: exit(0); break; default: break; } glutPostRedisplay(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Kocka megvilagitva"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutKeyboardFunc(Keyboard); glutDisplayFunc(RenderScene); glutTimerFunc(1000, Timer, 1); // 1 mp mulva meghivodik a Timer() fuggveny glutIdleFunc(Idle); // Idle(), ha nem tortenik semmi MenuID = glutCreateMenu(ProcessMenu); glutAddMenuEntry("1. menupont", 1); glutAddMenuEntry("2. menupont", 2); glutAddMenuEntry("Kilepes", 3); glutAttachMenu(GLUT_RIGHT_BUTTON); SetupRC(); glutMainLoop(); return 0; }