#include #include #include #include // Define a constant for the value of PI #define GL_PI 3.1415f // Rotation amounts static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; static int wireframe = 1; static int showNormals = 1; // Light values and coordinates static GLfloat lightPos[] = { 0.0f, 0.0f, 75.0f, 1.0f }; static GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; static GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; static GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f}; static GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f }; static int piecesX = 20; static int piecesY = 20; static int shine = 128; void BitmapText(GLfloat x, GLfloat y, char *string) { int len, i; glRasterPos2f(x, y); len = (int) strlen (string); for (i = 0; i < len; i++){ glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, string[i]); } } void SolidQuad(float width, int piecesX, int piecesY) { float h, wx, wy, x, y; int i, j; //int pieces = 10; h = width / 2.0; wx = width / piecesX; wy = width / piecesY; glBegin(GL_QUADS); glNormal3f(0.0, 0.0, 1.0); for(y = h, j = 0; j < piecesY; y -= wy, j++) for(x = -h, i = 0; i < piecesX; x += wx, i++) { glVertex3f(x, y, 0); glVertex3f(x, y - wy, 0); glVertex3f(x + wx, y - wy, 0); glVertex3f(x + wx, y, 0); } glEnd(); } void SolidQuadNormals(float width, int piecesX, int piecesY) { float h, wx, wy, x, y; int i, j; //int pieces = 10; h = width / 2.0; wx = width / piecesX; wy = width / piecesY; glBegin(GL_QUADS); glNormal3f(0.0, 0.0, 1.0); for(y = h, j = 0; j < piecesY; y -= wy, j++) for(x = -h, i = 0; i < piecesX; x += wx, i++) { glVertex3f(x, y, 0); glVertex3f(x, y - wy, 0); glVertex3f(x + wx, y - wy, 0); glVertex3f(x + wx, y, 0); } glEnd(); glColor3f(0.0, 0.8, 0.0); glBegin(GL_LINES); for(y = h, j = 0; j < piecesY; y -= wy, j++) for(x = -h, i = 0; i < piecesX; x += wx, i++) { glVertex3f(x, y, 0); glVertex3f(x, y, 10); glVertex3f(x, y - wy, 0); glVertex3f(x, y - wy, 10); glVertex3f(x + wx, y - wy, 0); glVertex3f(x + wx, y - wy, 10); glVertex3f(x + wx, y, 0); glVertex3f(x + wx, y, 10); } glEnd(); } void SolidCube(float width, int piecesX, int piecesY, int piecesZ) { glPushMatrix(); glTranslatef(0.0, 0.0, width / 2.0); SolidQuad(width, piecesX, piecesY); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 0.0, -width / 2.0); glRotatef(180.0, 0.0, 1.0, 0.0); SolidQuad(width, piecesX, piecesY); glPopMatrix(); glPushMatrix(); glTranslatef(width / 2.0, 0.0, 0.0); glRotatef(90.0, 0.0, 1.0, 0.0); SolidQuad(width, piecesZ, piecesY); glPopMatrix(); glPushMatrix(); glTranslatef(-width / 2.0, 0.0, 0.0); glRotatef(-90.0, 0.0, 1.0, 0.0); SolidQuad(width, piecesZ, piecesY); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, width / 2.0, 0.0); glRotatef(-90.0, 1.0, 0.0, 0.0); SolidQuad(width, piecesX, piecesZ); glPopMatrix(); glPushMatrix(); glTranslatef(0.0, -width / 2.0, 0.0); glRotatef(90.0, 1.0, 0.0, 0.0); SolidQuad(width, piecesX, piecesZ); glPopMatrix(); } // Called to draw scene void RenderScene(void) { char buff[255]; // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(wireframe) glPolygonMode(GL_FRONT, GL_LINE); else glPolygonMode(GL_FRONT, GL_FILL); glPushMatrix(); // Specify new position and direction in rotated coords. ////glRotatef(90.0, 0.0, 1.0, 0.0); //glRotatef(yRot, 0.0f, 1.0f, 0.0f); //glRotatef(xRot, 1.0f, 0.0f, 0.0f); glMateriali( GL_FRONT, GL_SHININESS, shine ); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir); // Draw a red cone to enclose the light source glColor3ub(255,0,0); // Translate origin to move the cone out to where the light // is positioned. glTranslatef(lightPos[0],lightPos[1],lightPos[2]); glutSolidCone(4.0f,6.0f,15,15); // Draw a smaller displaced sphere to denote the light bulb // Save the lighting state variables glPushAttrib(GL_LIGHTING_BIT); // Turn off lighting and specify a bright yellow sphere glDisable(GL_LIGHTING); glColor3ub(255,255,0); glutSolidSphere(3.0f, 15, 15); glColor3f(1.0, 1.0, 1.0); BitmapText(-60, 60, "Nyilak, W, N, F1, F2, F3, F4"); sprintf( buff, "Shininess: %d", shine ); BitmapText(-60, -60, buff); // Restore lighting state variables glPopAttrib(); glPopMatrix(); glColor3f(1.0, 0.0, 0.0); // Save the matrix state and do the rotations glPushMatrix(); glRotatef(yRot, 0.0f, 1.0f, 0.0f); glRotatef(xRot, 1.0f, 0.0f, 0.0f); //glRotatef(10, 0.5f, 1.0f, 0.5f); if(showNormals) SolidQuadNormals(80.0, piecesX, piecesY); else SolidQuad(80.0, piecesX, piecesY); // Restore the matrix state glPopMatrix(); // Display the results glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { glEnable(GL_DEPTH_TEST); // Hidden surface removal glFrontFace(GL_CCW); // Counter clock-wise polygons face out //glEnable(GL_CULL_FACE); // Do not calculate inside of jet glShadeModel(GL_SMOOTH); // Light blue background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_LIGHTING); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); // Setup and enable light 0 //glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); //glLightfv(GL_LIGHT0, GL_POSITION, lightPos); // Specific spot effects // Cut off angle glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f); // Fairly shiny spot glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 80.0f); glEnable(GL_LIGHT0); // Enable Material color tracking glEnable(GL_COLOR_MATERIAL); // Front material ambient and diffuse colors track glColor glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); // All materials hereafter have full specular reflectivity // with a high shine glMaterialfv(GL_FRONT, GL_SPECULAR, specref); glEnable(GL_NORMALIZE); } void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) xRot-= 1.0f; if(key == GLUT_KEY_DOWN) xRot += 1.0f; if(key == GLUT_KEY_LEFT) yRot -= 1.0f; if(key == GLUT_KEY_RIGHT) yRot += 1.0f; if(xRot > 359.0f) xRot = 0.0f; if(xRot < 0.0f) xRot = 359.0f; if(yRot > 359.0f) yRot = 0.0f; if(yRot < 0.0f) yRot = 359.0f; if(key == GLUT_KEY_F1) { piecesX ++; piecesY ++; } if((key == GLUT_KEY_F2) && (piecesX > 1)) { piecesX --; piecesY --; } if( ( key == GLUT_KEY_F3 ) && ( shine > 0 ) ) shine --; if( ( key == GLUT_KEY_F4 ) && ( shine < 128 ) ) shine ++; // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { GLfloat nRange = 80.0f; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void Keyboard(unsigned char key, int x, int y) { // ... int state; printf("Billentyu lenyomva, kodja %c, pozicio (%d,%d). ", key, x, y); state = glutGetModifiers(); if(state & GLUT_ACTIVE_SHIFT) printf("SHIFT lenyomva. "); if(state & GLUT_ACTIVE_CTRL) printf("CTRL lenyomva. "); if(state & GLUT_ACTIVE_ALT) printf("ALT lenyomva. "); printf("\n"); fflush(stdout); if(key == 'w' || key == 'W') { wireframe = !wireframe; } if(key == 'n' || key == 'N') { showNormals = !showNormals; } glutPostRedisplay(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Spotlight - Tank"); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutKeyboardFunc(Keyboard); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }