Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SURSA: lighthouse3D: http://www.lighthouse3d.com/tutorials/glut-tutorial/keyboard-example-moving-around-the-world/
- #include<gl/freeglut.h>
- #include<math.h>
- // angle of rotation for the camera direction
- float angle=0.0;
- // actual vector representing the camera's direction
- float lx=0.0f,lz=-1.0f;
- // XZ position of the camera
- float x=0.0f,z=5.0f;
- void changeSize(int w, int h)
- {
- // Prevent a divide by zero, when window is too short
- // (you cant make a window of zero width).
- if (h == 0)
- h = 1;
- float ratio = w * 1.0 / h;
- // Use the Projection Matrix
- glMatrixMode(GL_PROJECTION);
- // Reset Matrix
- glLoadIdentity();
- // Set the viewport to be the entire window
- glViewport(0, 0, w, h);
- // Set the correct perspective.
- gluPerspective(45.0f, ratio, 0.1f, 100.0f);
- // Get Back to the Modelview
- glMatrixMode(GL_MODELVIEW);
- }
- enum {
- IARNA, PRIMAVARA
- };
- int rendermode = IARNA;
- void menu(int selection)
- {
- rendermode = selection;
- glutPostRedisplay();
- }
- void drawTree() {
- glColor3d (0.4, 0.25, 0.12);
- GLUquadricObj *cylinder;
- glPushMatrix ( );
- glRotatef (-90.0, 1.0, 0, 0);
- cylinder = gluNewQuadric ( );
- gluCylinder (cylinder, 0.33, 0.33, 1.5, 40, 20);
- glPopMatrix ( );
- glColor3d(0.2,0.8,0.0);
- glTranslatef(0.0f,1.7f,0.0f);
- glutSolidSphere(0.70f,20,20);
- glutPostRedisplay();
- }
- void drawSnowMan() {
- glColor3f(1.0f, 1.0f, 1.0f);
- // Draw Body
- glTranslatef(0.0f ,0.75f, 0.0f);
- glutSolidSphere(0.75f,20,20);
- // Draw Head
- glTranslatef(0.0f, 1.0f, 0.0f);
- glutSolidSphere(0.25f,20,20);
- // Draw Eyes
- glPushMatrix();
- glColor3f(0.0f,0.0f,0.0f);
- glTranslatef(0.05f, 0.10f, 0.18f);
- glutSolidSphere(0.05f,10,10);
- glTranslatef(-0.1f, 0.0f, 0.0f);
- glutSolidSphere(0.05f,10,10);
- glPopMatrix();
- // Draw Nose
- glColor3f(1.0f, 0.5f , 0.5f);
- glutSolidCone(0.08f,0.5f,10,2);
- }
- void renderScene(void) {
- // Clear Color and Depth Buffers
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Reset transformations
- glLoadIdentity();
- // Set the camera
- gluLookAt( x, 1.0f, z,
- x+lx, 1.0f, z+lz,
- 0.0f, 1.0f, 0.0f);
- glClearColor(0.529, 0.808, 0.980,0.0);
- switch(rendermode)
- {
- case PRIMAVARA:
- // Draw ground
- glColor3f(0.0f, 0.6f, 0.0f);
- glBegin(GL_QUADS);
- glVertex3f(-100.0f, 0.0f, -100.0f);
- glVertex3f(-100.0f, 0.0f, 100.0f);
- glVertex3f( 100.0f, 0.0f, 100.0f);
- glVertex3f( 100.0f, 0.0f, -100.0f);
- glEnd();
- glColor3f(1.000, 0.843, 0.000);
- glPushMatrix();
- glTranslatef(10.0,12.0,-25.0);
- glutSolidSphere(2.0f,30,30);
- glPopMatrix();
- // Draw 36 trees
- for(int i = -3; i < 3; i++)
- for(int j=-3; j < 3; j++) {
- glPushMatrix();
- glTranslatef(i*10.0,0,j * 10.0);
- drawTree();
- glPopMatrix();
- }
- break;
- case IARNA:
- glColor3f(0.9f, 0.9f, 0.9f);
- glBegin(GL_QUADS);
- glVertex3f(-100.0f, 0.0f, -100.0f);
- glVertex3f(-100.0f, 0.0f, 100.0f);
- glVertex3f( 100.0f, 0.0f, 100.0f);
- glVertex3f( 100.0f, 0.0f, -100.0f);
- glEnd();
- for(int i = -3; i < 3; i++)
- for(int j=-3; j < 3; j++) {
- glPushMatrix();
- glTranslatef(i*10.0,0,j * 10.0);
- drawSnowMan();
- glPopMatrix();
- }
- break;
- }
- glutSwapBuffers();
- }
- void processNormalKeys(unsigned char key, int x, int y)
- {
- float fraction = 0.1f;
- switch (key) {
- case 'l' :
- angle -= 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case 'a' :
- angle -= 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case 'd' :
- angle += 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case 'w' :
- x += lx * fraction;
- z += lz * fraction;
- break;
- case 's' :
- x -= lx * fraction;
- z -= lz * fraction;
- break;
- }
- if (key == 27)
- exit(0);
- }
- void processSpecialKeys(int key, int xx, int yy) {
- float fraction = 0.1f;
- switch (key) {
- case GLUT_KEY_LEFT :
- angle -= 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case GLUT_KEY_RIGHT :
- angle += 0.01f;
- lx = sin(angle);
- lz = -cos(angle);
- break;
- case GLUT_KEY_UP :
- x += lx * fraction;
- z += lz * fraction;
- break;
- case GLUT_KEY_DOWN :
- x -= lx * fraction;
- z -= lz * fraction;
- break;
- }
- }
- int main(int argc, char **argv) {
- // init GLUT and create window
- glutInit(&argc, argv);
- glColor3f(0.3f, 0.7f, 0.9f);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(320,320);
- glutCreateWindow("Scena 3D cu oameni de zapada");
- // register callbacks
- glutDisplayFunc(renderScene);
- glutReshapeFunc(changeSize);
- glutIdleFunc(renderScene);
- glutKeyboardFunc(processNormalKeys);
- glutSpecialFunc(processSpecialKeys);
- // OpenGL init
- glEnable(GL_DEPTH_TEST);
- glutCreateMenu(menu);
- glutAddMenuEntry("Iarna", IARNA);
- glutAddMenuEntry("Primavara", PRIMAVARA);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- // enter GLUT event processing cycle
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment