#include #include #include #include // Define a constant for the value of PI #define GL_PI 3.1415f float M[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; static float angleRad; static float orbitDegreeZ = 0.0f; static float orbitDegreeX = 0.0f; static float zoom = 1.0f; static float heliX = 0.0f; static float heliY = 0.0f; static float heliZ = 40.0f; static float heliAngle = 0.0f; static float alfa = 0.0f; static int showName = 1; GLint i = 0; static float red=1.0f, green=0.0f, blue=0.0f; static int MenuID, SubMenuID; void drawPine(int x, int y, int z) { glPushMatrix(); glTranslatef((float)x,(float)y,(float)z); glColor3f(0.6f,0.4f,0.2f); //glutSolidCone(5.0,5.0,60,10); gluCylinder(gluNewQuadric(), 2.0,2.0,10,60,10); glColor3f(0.0f,0.6f,0.0f); glTranslatef(0.0,0.0,10.0); glutSolidCone(10.0,20.0,10,10); glTranslatef(0.0,0.0,10.0); glutSolidCone(7.0,15.0,10,10); glTranslatef(0.0,0.0,10.0); glutSolidCone(4.0,15.0,10,10); glPopMatrix(); } void drawNormalTree(int x, int y) { glPushMatrix(); glTranslatef((float)x,(float)y,0.0); glColor3f(0.6f,0.4f,0.2f); //glutSolidCone(5.0,5.0,60,10); gluCylinder(gluNewQuadric(), 2.0,2.0,30,60,10); glTranslatef(0.0,0.0,30.0); glColor3f(0.0f,0.8f,0.0f); glutSolidSphere(10.0,40,40); //glutSolidCone(10.0,20.0,10,10); glPopMatrix(); } 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 rotor() { glPushMatrix(); glTranslatef(0.0,0.0,25.0); gluCylinder(gluNewQuadric(), 2.0,2.0,5,60,10); glTranslatef(0.0,0.0,5); glRotatef(alfa, 0.0,0.0,1.0); glColor3f(0.5f,0.5f,0.5f); glBegin(GL_POLYGON); glVertex3i(0,0,0); glVertex3i(-50,0,0); glVertex3i(-50,5,0); glVertex3i(0,5,0); glEnd(); glBegin(GL_POLYGON); glVertex3i(0,0,0); glVertex3i(50,0,0); glVertex3i(50,-5,0); glVertex3i(0,-5,0); glEnd(); glPopMatrix(); } void kormany() { glPushMatrix(); glTranslatef(70.0,0.0,30); glRotatef(90,-1.0,0.0,0.0); glScalef(0.2,0.2,0.2); rotor(); glPopMatrix(); } void helicopter() { // orr glColor3f(0.0f,0.3f,1.0f); glBegin(GL_POLYGON); glVertex3i(-35,-5,15); glVertex3i(-35,5,15); glVertex3i(-15,10,25); glVertex3i(-15,-10,25); glEnd(); /*glColor3f(1.0f,0.0f,0.0f);*/ glColor3f(red,green,blue); glBegin(GL_POLYGON); // also glVertex3i(-35,-5,15); glVertex3i(-35,5,15); glVertex3i(-25,10,5); glVertex3i(-25,-10,5); glEnd(); glBegin(GL_POLYGON); // jobb also glVertex3i(-35,5,15); glVertex3i(-25,10,5); glVertex3i(-25,15,15); glEnd(); glBegin(GL_POLYGON); // jobb felso glVertex3i(-35,5,15); glVertex3i(-25,15,15); glVertex3i(-15,10,25); glEnd(); glBegin(GL_POLYGON); // bal also glVertex3i(-35,-5,15); glVertex3i(-25,-10,5); glVertex3i(-25,-15,15); glEnd(); glBegin(GL_POLYGON); // bal felso glVertex3i(-35,-5,15); glVertex3i(-15,-10,25); glVertex3i(-25,-15,15); glEnd(); //torzs glBegin(GL_POLYGON); // teteje glVertex3i(-15,10,25); glVertex3i(15,10,25); glVertex3i(15,-10,25); glVertex3i(-15,-10,25); glEnd(); glBegin(GL_POLYGON); // alja glVertex3i(-25,10,5); glVertex3i(15,10,5); glVertex3i(15,-10,5); glVertex3i(-25,-10,5); glEnd(); glColor3f(1.0f,1.0f,1.0f); glBegin(GL_POLYGON); // jobb felso glVertex3i(-25,15,15); glVertex3i(15,15,15); glVertex3i(15,10,25); glVertex3i(-15,10,25); glEnd(); glBegin(GL_POLYGON); // bal felso glVertex3i(-25,-15,15); glVertex3i(15,-15,15); glVertex3i(15,-10,25); glVertex3i(-15,-10,25); glEnd(); glPushMatrix(); glColor3f(0.0f,0.0f,0.8f); glTranslatef(-15.5,-15.5,15); glRotatef(63,1.0,0.0,0.0); glScalef(0.12f,0.08f,0.02f); glLineWidth(4); StrokeText("Heli"); glPopMatrix(); /*glColor3f(1.0f,0.0f,0.0f);*/ glColor3f(red,green,blue); glBegin(GL_POLYGON); // jobb also glVertex3i(-25,15,15); glVertex3i(15,15,15); glVertex3i(15,10,5); glVertex3i(-25,10,5); glEnd(); glBegin(GL_POLYGON); // bal also glVertex3i(-25,-15,15); glVertex3i(15,-15,15); glVertex3i(15,-10,5); glVertex3i(-25,-10,5); glEnd(); //farokto glBegin(GL_POLYGON); // teteje glVertex3i(15,10,25); glVertex3i(30,7,25); glVertex3i(30,-7,25); glVertex3i(15,-10,25); glEnd(); glBegin(GL_POLYGON); // alja glVertex3i(15,10,5); glVertex3i(30,7,20); glVertex3i(30,-7,20); glVertex3i(15,-10,5); glEnd(); glBegin(GL_POLYGON); // jobb felso glVertex3i(15,15,15); glVertex3i(30,7,20); glVertex3i(30,7,25); glVertex3i(15,10,25); glEnd(); glBegin(GL_POLYGON); // bal felso glVertex3i(15,-15,15); glVertex3i(30,-7,20); glVertex3i(30,-7,25); glVertex3i(15,-10,25); glEnd(); glBegin(GL_POLYGON); // jobb also glVertex3i(15,15,15); glVertex3i(30,7,20); glVertex3i(15,10,5); glEnd(); glBegin(GL_POLYGON); // bal also glVertex3i(15,-15,15); glVertex3i(30,-7,20); glVertex3i(15,-10,5); glEnd(); // farok glBegin(GL_POLYGON); // teteje glVertex3i(30,7,25); glVertex3i(70,2,25); glVertex3i(70,-2,25); glVertex3i(30,-7,25); glEnd(); glBegin(GL_POLYGON); // alja glVertex3i(30,7,20); glVertex3i(70,2,20); glVertex3i(70,-2,20); glVertex3i(30,-7,20); glEnd(); glColor3f(1.0f,1.0f,1.0f); glBegin(GL_POLYGON); // jobb felso glVertex3i(30,7,20); glVertex3i(70,2,20); glVertex3i(70,2,25); glVertex3i(30,7,25); glEnd(); glBegin(GL_POLYGON); // jobb felso glVertex3i(30,-7,20); glVertex3i(70,-2,20); glVertex3i(70,-2,25); glVertex3i(30,-7,25); glEnd(); glColor3f(0.0f,1.0f,0.0f); glBegin(GL_POLYGON); // vege glVertex3i(70,-2,20); glVertex3i(70,2,20); glVertex3i(70,2,25); glVertex3i(70,-2,25); glEnd(); glBegin(GL_POLYGON); glVertex3i(70,2,25); glVertex3i(55,2,25); glVertex3i(70,0,35); glVertex3i(80,0,35); glEnd(); glBegin(GL_POLYGON); glVertex3i(70,-2,25); glVertex3i(55,-2,25); glVertex3i(70,0,35); glVertex3i(80,0,35); glEnd(); glBegin(GL_POLYGON); glVertex3i(55,2,25); glVertex3i(70,0,35); glVertex3i(55,-2,25); glEnd(); glBegin(GL_POLYGON); glVertex3i(70,2,25); glVertex3i(80,0,35); glVertex3i(70,-2,25); glEnd(); rotor(); kormany(); } // Called to draw scene void RenderScene(void) { int j = 0; int x,y; // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (showName == 1) { BitmapText(0,-60,"Nemeth Gabor"); } /*glPushMatrix();*/ /* glRotatef(-50.0 + orbitDegreeX, 1.0f, 0.0f, 0.0f); glRotatef(orbitDegreeZ, 0.0f,0.0f,1.0f); */ glPushMatrix(); // glRotatef(heliAngle, 0.0f, 0.0f, 1.0f); glTranslatef( -heliX, -heliY, -heliZ ); for( y = -400; y < 400; y+=10 ) { for ( x = -400; x < 400; x+=10 ) { glBegin(GL_TRIANGLE_STRIP); glColor3f( 0.0f,0.7f,0.0f ); glVertex3f((float) x, (float) y, (float) 5*(sin(10.0f*x)+sin(10.0f*y))); glColor3f( 0.3f,0.7f,0.0f ); glVertex3f((float) x+10.0, (float) y, (float) 5*(sin((float) 10.f*(x+10.0))+sin(10.0f*y))); glColor3f( 0.2f,0.8f,0.2f ); glVertex3f((float) x, (float) y+10.0, (float) 5*(sin((float) 10.0f * x)+sin((float) 10.0f * (y+10.0)))); glColor3f( 0.1f,0.8f,0.0f ); glVertex3f((float) x+10.0, (float) y+10.0, (float) 5*(sin((float) 10.0f * (x+10.0f))+sin((float) 10.0f * (y+10.0)))); glEnd(); if ( ( x %100 == 0 ) && ( y %100 == 0 ) ) { glPushMatrix(); glScalef(0.75f,0.75f,0.75f); //drawPine(x,y, (float) 5*(sin(10.0f * x)+sin(10.0f * y)) ); drawPine(x,y, (float) 5*(sin((float) 10.0f * x)+sin((float) 10.0f * (y+10.0f)))); glPopMatrix(); } } } glPopMatrix(); glPushMatrix(); /*glTranslatef( heliX, heliY, heliZ );*/ glScalef(0.25f,0.25f,0.25f); helicopter(); glPopMatrix(); /*glPopMatrix();*/ /*glPopMatrix();*/ // Flush drawing commands glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); } void Timer(int value) { //printf("Timer esemeny (%d)\n", value); alfa = alfa+5.0; if( alfa==360 ) alfa= 0; glutPostRedisplay(); glutTimerFunc(2, Timer, value + 1); } void Idle() { glutPostRedisplay(); } void ChangeSizeOrtho(int w, int h) { //GLfloat nRange = 25.0f; //GLfloat nRange = 30.0f; 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 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); */ if (w <= h) glOrtho (-300, 300, -300*h/w, 300*h/w, -300, 300); else glOrtho (-300*w/h, 300*w/h, -300, 300, -300, 300); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /*glMultMatrixf(M);*/ } void ChangeSizePerspective(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 1.0, // zNear 800.0 // zFar ); gluLookAt(heliX+100, heliY, heliZ+30, // eye heliX-50, heliY, heliZ, // center 0.0, 0.0, (float) heliZ / (float) heliZ // up ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /*glMultMatrixf(M);*/ } void Keyboard(unsigned char key, int x, int y) { if ( key == 'w' ) { heliZ+=1.0f; /*orbitDegreeX += 1.0f;*/ } if (key == 's' ) { /*orbitDegreeX -= 1.0f;*/ heliZ-=1.0f; } /* if (key == 'a' ) { orbitDegreeZ -= 1.0f; } if (key == 'd') { orbitDegreeZ += 1.0f; } */ if (heliZ > 100.0f) heliZ = 100.0f; if (heliZ < (float) 5*(sin(10.f*heliX)+sin(10.0f*heliY))) heliZ = (float) 5*(sin(10.f*heliX)+sin(10.0f*heliY)); /* 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 SpecialKeys(int key, int x, int y) { angleRad = 3.14 * heliAngle / 180.0; if (key == GLUT_KEY_UP ) { //heliX -= 1.0f; heliX -= 1.0f * cos(angleRad); heliY -= 1.0f * sin(-angleRad); } if (key == GLUT_KEY_DOWN ) { //heliX += 1.0f; heliX += 1.0f * cos(angleRad); heliY += 1.0f * sin(-angleRad); } if (key == GLUT_KEY_LEFT ) { heliAngle -= 1.0f; } if (key == GLUT_KEY_RIGHT) { heliAngle += 1.0f; } if (heliAngle <= 360.0f) heliAngle -= 360.0f; if (heliAngle < 0.0f) heliAngle = 360.0f - heliAngle; /* angleRad = 3.14 * heliAngle / 180.0; M[0] = cos(angleRad); M[5] = -sin(angleRad); M[8] = 0; M[12] = heliX; M[2] = sin(angleRad); M[5] = cos(angleRad); M[9] = 0; M[13] = heliY; M[2] = 0; M[6] = 0; M[10] = 1; M[14] = 0; M[3] = 0; M[7] = 0; M[11] = 0; M[15] = 1; */ /* 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 ProcessMenu(int value) { if (value == 1) { /* latszik-e a nevem? */ showName = 1- showName; } if (value == 2) { /* a helikopter szine zold lesz */ red = 0.0f; green = 0.7f; blue = 0.0f; } if (value == 3 ) { /* a helikopter szine piros lesz */ red = 1.0f; green = 0.0f; blue = 0.0f; } if (value == 4) { /* a helikopter szine sarga lesz */ red = 1.0f; green = 1.0f; blue = 0.0f; } if (value == 5 ) { /* kilepes */ exit(0); } } int main(int argc, char* argv[]) { // >> Inicializalas glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(300, 300); glutCreateWindow("GLUT Alap"); /* Almenü készítése */ SubMenuID = glutCreateMenu( ProcessMenu ); glutAddMenuEntry( "Zold", 2 ); glutAddMenuEntry( "Piros", 3 ); glutAddMenuEntry( "Sarga", 4 ); /* Főmenü készítése */ MenuID = glutCreateMenu( ProcessMenu ); glutAddMenuEntry( "Nev be/ki", 1 ); glutAddSubMenu( "Helicopter color", SubMenuID ); /* Almenü hozzáadása */ glutAddMenuEntry( "Kilepes", 5 ); glutAttachMenu( GLUT_RIGHT_BUTTON ); /* Menü a jobb egérgomb lenyomására jelenik majd meg. */ // << Inicializalas // >> Callback fuggvenyek //glutReshapeFunc(ChangeSizeOrtho); // Parhuzamos vetites glutReshapeFunc(ChangeSizePerspective); // Perspektiv vetites glutSpecialFunc(SpecialKeys); glutKeyboardFunc(Keyboard); //glutKeyboardFunc(Keyboard); glutDisplayFunc(RenderScene); glutTimerFunc(2, Timer, 1); // 1 mp mulva meghivodik a Timer() fuggveny glutIdleFunc(Idle); // Idle(), ha nem tortenik semmi // << Callback fuggvenyek SetupRC(); glutMainLoop(); return 0; }