#include #include #include #include // Define a constant for the value of PI #define GL_PI 3.1415f static float camX, camY, camZ; static float viewX, viewY, viewZ; static float step = 0.01f; static float orbitDegreeZ = 0.0f; static float orbitDegreeX = 0.0f; static float zoom = 1.0f; static GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f}; static GLfloat spotDir[] = { -1.0f, 0.0f, -1.0f }; static GLfloat lightPos[] = { 15.0f, 15.0f, 20.0f, 1.0f }; static GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; static GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; static float orbitDegree = 0.0f; GLint i = 0; void BitmapText(int x, int 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 StrokeText(char *string) { int len, i; len = (int) strlen(string); for (i=0; i < len; i++) { glutStrokeCharacter(GLUT_STROKE_ROMAN, string[i]); } } void drawBoard() { float x0, x1, y0, y1; glPushMatrix(); drawTorch(); glColor3f(0.7f,1.0f, 0.7f); glBegin(GL_QUADS); glNormal3f(0.0, 0.0, 1.0); for ( x0= 0.0f, x1 = 0.2f; x1<60.0f; x0+=0.2f, x1+=0.2f ) { for (y0= 0.0f, y1 = 0.2f; y1<60.0f; y0+=0.2f, y1+=0.2f ) { glVertex3f(x1,y1,3.0f); glVertex3f( x0,y1,3.0f); glVertex3f( x0,y0,3.0f); glVertex3f(x1,y0,3.0f); } } glEnd(); /* glBegin(GL_QUADS); glVertex3f(20.0f,30.0f,3.0f); glVertex3f( 10.0f,30.0f,3.0f); glVertex3f( 10.0f,0.0f,3.0f); glVertex3f(20.0f, 0.0f,3.0f); glEnd(); glBegin(GL_QUADS); glVertex3f(30.0f,30.0f,3.0f); glVertex3f( 20.0f,30.0f,3.0f); glVertex3f( 20.0f, 0.0f,3.0f); glVertex3f(30.0f, 0.0f,3.0f); glEnd(); */ /* glBegin(GL_QUADS); glVertex3f(30.0f,30.0f,3.0f); glVertex3f( 0.0f,30.0f,3.0f); glVertex3f( 0.0f, 0.0f,3.0f); glVertex3f(30.0f, 0.0f,3.0f); glEnd(); */ glColor3f(0.0f,0.0f,0.8f); glTranslatef(2.0f,20.0f,5.0f); glScalef(0.05f,0.05f,0.02f); glLineWidth(2); StrokeText("The light"); glPopMatrix(); } void drawTorch() { glPushMatrix(); //glTranslatef(0.0f,0.0f,0.0f); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir); /* glPushMatrix(); glTranslatef(spotDir[0], spotDir[1], spotDir[2]); glColor3f( 1.0f, 0.0f, 0.0f ); glutSolidSphere(2,10,10); glPopMatrix(); */ glColor3f( 1.0f, 1.0f, 0.0f ); glPushAttrib(GL_LIGHTING_BIT); // Turn off lighting and specify a bright yellow sphere glTranslatef(lightPos[0], lightPos[1], lightPos[2]); glDisable(GL_LIGHTING); glutSolidSphere(4.0f,10,10); //glutSolidCone(0.5f,5.0f,10,10); //glEnable(GL_LIGHTING); glPopAttrib(); //glutSolidCone(4.0f,6.0f,15,15); //glPushAttrib(GL_LIGHTING_BIT); glPopMatrix(); } // Called to draw scene void RenderScene(void) { int j = 0; // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); //glRotatef(-90, 1.0f, 0.0f, 0.0f); glRotatef(-50.0 + orbitDegreeX, 1.0f, 0.0f, 0.0f); //glRotatef(orbitDegreeZ, 0.0f,0.0f,1.0f); glPushMatrix(); glRotatef(90.0f, 1.0f,0.0f, 0.0f); drawBoard(); glPopMatrix(); glPopMatrix(); // Flush drawing commands 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, 100.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); glMateriali(GL_FRONT, GL_SHININESS, 128); glEnable(GL_NORMALIZE); } void Timer(int value) { printf("Timer esemeny (%d)\n", value); i++; if( i==10 ) i= 0; glutPostRedisplay(); glutTimerFunc(1000, Timer, value + 1); } void Idle() { glutPostRedisplay(); } void ChangeSizeOrtho(int w, int h) { //GLfloat nRange = 25.0f; //GLfloat nRange = 30.0f; GLfloat nRange = 50.0f ; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset projection matrix stack 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); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void SpecialKeys(int key, int x, int y) { if (key == GLUT_KEY_UP ) { //orbitDegreeX += 1.0f; spotDir[1] += 0.2f; } if (key == GLUT_KEY_DOWN ) { //orbitDegreeX -= 1.0f; spotDir[1] -= 0.2f; } if (key == GLUT_KEY_LEFT ) { //orbitDegreeZ -= 1.0f; spotDir[0] -= 0.2f; } if (key == GLUT_KEY_RIGHT) { //orbitDegreeZ += 1.0f; spotDir[0] += 0.2f; } if ( orbitDegreeX > 80.0f ) orbitDegreeX = 80.0f; if ( orbitDegreeX < -50.0f ) orbitDegreeX = -50.0f; if (orbitDegreeZ <= 360.0f) orbitDegreeZ -= 360.0f; if (orbitDegreeZ < 0.0f) orbitDegreeZ = 360.0f - orbitDegreeZ; glutPostRedisplay(); } void Keyboard(unsigned char key, int x, int y) { printf("Billentyu lenyomva, kodja %c, pozicio (%d,%d). ", key, x, y); if (key == 'i') viewY += step; if (key == 'k') viewY -= step; if (key == 'j') viewX -= step; if (key == 'l') viewX += step; printf("\n"); fflush (stdout); glutPostRedisplay(); } int main(int argc, char* argv[]) { // >> Inicializalas camX = 0.0f; camY = -60.0f; camZ = 40.0f; viewX = 0.0f; viewY = 0.0f; viewZ = 0.0f; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(300, 300); glutCreateWindow("GLUT Alap"); // << Inicializalas // >> Callback fuggvenyek glutReshapeFunc(ChangeSizeOrtho); // Parhuzamos vetites //glutReshapeFunc(ChangeSizePerspective); // Perspektiv vetites glutSpecialFunc(SpecialKeys); //glutKeyboardFunc(Keyboard); //glutKeyboardFunc(Keyboard); glutDisplayFunc(RenderScene); //glutTimerFunc(1000, Timer, 1); // 1 mp mulva meghivodik a Timer() fuggveny //glutIdleFunc(Idle); // Idle(), ha nem tortenik semmi // << Callback fuggvenyek SetupRC(); glutMainLoop(); return 0; }