Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // GEOMPRA-CV2b
- //
- // Created by Jakub Mejtský on 27.02.12.
- // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <GLUT/GLUT.h>
- #include <math.h>
- GLfloat X = 100.0f; // Translate screen to x direction (left or right)
- GLfloat Y = 100.0f; // Translate screen to y direction (up or down)
- GLfloat Z = 0.0f; // Translate screen to z direction (zoom in or out)
- GLfloat rotX = 0.0f; // Rotate screen on x axis
- GLfloat rotY = 0.0f; // Rotate screen on y axis
- GLfloat rotZ = 0.0f; // Rotate screen on z axis
- void onResize(int, int);
- void onDisplay(void);
- void onKeyboard(unsigned char, int, int);
- void specialKey(int key, int x, int y);
- void onResize(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, w, 0, h, -1, 1);
- }
- void onDisplay(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glClearColor(0.8, 0.8, 0.8, 0.0);
- glClear(GL_DEPTH_BUFFER_BIT);
- GLfloat A[] = {0.0f, 0.0f, 0.0f};
- GLfloat B[] = {100.0f, 0.0f, 0.0f};
- GLfloat C[] = {100.0f, 100.0f, 0.0f};
- GLfloat D[] = {0.0f, 100.0f, 0.0f};
- GLfloat E[] = {0.0f, 0.0f, 100.0f};
- GLfloat F[] = {100.0f, 0.0f, 100.0f};
- GLfloat G[] = {100.0f, 100.0f, 100.0f};
- GLfloat H[] = {0.0f, 100.0f, 100.0f};
- glPolygonMode(GL_FRONT, GL_FILL);
- glPolygonMode(GL_BACK, GL_FILL);
- glMatrixMode(GL_MODELVIEW);
- glDisable(GL_CULL_FACE);
- glEnable(GL_DEPTH_TEST);
- glLoadIdentity();
- glTranslatef(X, Y, Z);
- glRotatef(rotX,1.0,0.0,0.0); // Rotate on x
- glRotatef(rotY,0.0,1.0,0.0); // Rotate on y
- glRotatef(rotZ,0.0,0.0,1.0); // Rotate on z
- glColor3f(1.0f, 0.0f, 0.0f);
- glBegin(GL_POLYGON);
- glVertex3fv(A);
- glVertex3fv(B);
- glVertex3fv(C);
- glVertex3fv(D);
- glEnd();
- glColor3f(0.0f, 1.0f, 0.0f);
- glBegin(GL_POLYGON);
- glVertex3fv(E);
- glVertex3fv(F);
- glVertex3fv(G);
- glVertex3fv(H);
- glEnd();
- glColor3f(0.0f, 0.0f, 1.0f);
- glBegin(GL_POLYGON);
- glVertex3fv(B);
- glVertex3fv(F);
- glVertex3fv(G);
- glVertex3fv(C);
- glEnd();
- glColor3f(0.0f, 1.0f, 1.0f);
- glBegin(GL_POLYGON);
- glVertex3fv(A);
- glVertex3fv(E);
- glVertex3fv(H);
- glVertex3fv(D);
- glEnd();
- glColor3f(1.0f, 0.0f, 1.0f);
- glBegin(GL_POLYGON);
- glVertex3fv(A);
- glVertex3fv(B);
- glVertex3fv(F);
- glVertex3fv(E);
- glEnd();
- glColor3f(1.0f, 1.0f, 0.0f);
- glBegin(GL_POLYGON);
- glVertex3fv(D);
- glVertex3fv(C);
- glVertex3fv(G);
- glVertex3fv(H);
- glEnd();
- glFlush();
- }
- // This function is used for the navigation keys
- void onKeyboard (unsigned char key, int x, int y){
- switch (key) {
- case 27: exit(0);
- case 'x': // Rotates screen on x axis
- rotX -= 1.0f;
- break;
- case 'X': // Opposite way
- rotX += 1.0f;
- break;
- case 'y': // Rotates screen on y axis
- rotY -= 1.0f;
- break;
- case 'Y': // Opposite way
- rotY += 1.0f;
- break;
- case 'z': // Rotates screen on z axis
- rotZ -= 1.0f;
- break;
- case 'Z': // Opposite way
- rotZ += 1.0f;
- break;
- }
- glutPostRedisplay();
- }
- void specialKey(int key, int x, int y) {
- switch(key) {
- case GLUT_KEY_LEFT : // Rotate on x axis
- X -= 2.0f;
- break;
- case GLUT_KEY_RIGHT : // Rotate on x axis (opposite)
- X += 2.0f;
- break;
- case GLUT_KEY_UP : // Rotate on y axis
- Y += 2.0f;
- break;
- case GLUT_KEY_DOWN : // Rotate on y axis (opposite)
- Y -= 2.0f;
- break;
- case GLUT_KEY_F1: // Rotate on z axis
- Z -= 2.0f;
- break;
- case GLUT_KEY_F2:// Rotate on z axis (opposite)
- Z += 2.0f;
- break;
- }
- glutPostRedisplay();
- }
- int main (int argc, char * argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB);
- glutCreateWindow("Úkol Geometrické praktikum - CV2b");
- glutReshapeWindow(350, 350);
- glutPositionWindow(50, 50);
- glutDisplayFunc(onDisplay);
- glutReshapeFunc(onResize);
- glutKeyboardFunc(onKeyboard);
- glutSpecialFunc(specialKey);
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment