Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // OpenGL-05
- //
- // Created by Alexander Kevin on 4/3/16.
- // Copyright © 2016 Alexander Kevin. All right reserved.
- //
- //CASE LINK : https://www.dropbox.com/s/2y8siyes8tfqmfr/T00740020220154042Tugas%20dan%20Latihan%20GSLC-2.doc?dl=0
- //.exe LINK : https://www.dropbox.com/s/giwgt1llyr406p8/OpenGL-05?dl=0
- #ifdef WIN32
- #include <windows.h>
- #endif
- #include <stdlib.h>
- #include <iostream>
- #include <fstream>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #else
- #include <GL/glut.h>
- #include <GL/glu.h>
- #include <GL/gl.h>
- #endif
- float r=0,g=1,b=1;
- int flag=0;
- bool isdownKeyPressed,isupKeyPressed;
- static GLfloat spin = 0.0;
- int sumbu_x=0,sumbu_y=0,sumbu_z=1;
- void init()
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-7.0,7.0,-7.0,7.0,-7.0,7.0);
- glMatrixMode(GL_MODELVIEW);
- glClearColor(0.0,0.0,0.0,1.0);
- glColor3f(0.0,0.0,0.0);
- glShadeModel(GL_FLAT);
- glEnable(GL_DEPTH_TEST);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- //bintang itu panah keatas sama panah ke kiri/kanan
- //panah keatas
- glFlush();
- }
- void display_soal_satu(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- //bintang itu panah keatas sama panah ke kiri/kanan
- //panah keatas
- glColor3f(r,g,b);
- glBegin(GL_POLYGON);
- glVertex3f(0, -2.7, 0);
- glVertex3f(-3.1, -5.0, 0);
- glVertex3f(0, 5.0, 0);
- glVertex3f(3.1, -5.0, 0);
- glEnd();
- //panah kekiri
- glBegin(GL_POLYGON);
- glVertex3f(1.9, -1.3, 0);
- glVertex3f(3.1, -5, 0);
- glVertex3f(-5, 1.2, 0);
- glVertex3f(5, 1.2, 0);
- glEnd();
- glFlush();
- }
- void spinDisplay(void)
- {
- spin = spin + 1;
- if (spin > 360.0)
- spin = spin - 360.0;
- glutPostRedisplay();
- }
- void display_soal_dua(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix();
- glRotatef(spin, sumbu_x,sumbu_y,sumbu_z);
- glColor3f(1.0, 0, 1.0);
- glRectf(-5, -5, 5, 5);
- glPopMatrix();
- glutSwapBuffers();
- }
- void handleSpecialKeypress(int key, int x, int y) {
- switch (key) {
- case GLUT_KEY_DOWN:
- isupKeyPressed = true;
- if (!isdownKeyPressed) {
- srand(time(NULL));
- r=(float)rand()/(float)(RAND_MAX/1.0);
- g=(float)rand()/(float)(RAND_MAX/0.8);
- b=(float)rand()/(float)(RAND_MAX/0.6);
- }
- glutPostRedisplay();
- break;
- case GLUT_KEY_UP:
- isdownKeyPressed = true;
- if (!isupKeyPressed) {
- srand(time(NULL));
- r=(float)rand()/(float)(RAND_MAX/1);
- g=(float)rand()/(float)(RAND_MAX/0.8);
- b=(float)rand()/(float)(RAND_MAX/0.6);
- }
- glutPostRedisplay();
- break;
- case 's':
- case 'S':
- if(flag==0){
- glutIdleFunc(spinDisplay);
- flag=1;
- }
- else{
- glutIdleFunc(NULL);
- flag=0;
- }
- break;
- case 'z':
- case 'Z':
- sumbu_z=1;
- sumbu_x=0;
- sumbu_y=0;
- break;
- case 'x':
- case 'X':
- sumbu_z=0;
- sumbu_x=1;
- sumbu_y=0;
- break;
- case 'y':
- case 'Y':
- sumbu_z=0;
- sumbu_x=0;
- sumbu_y=1;
- break;
- case 27:
- case 'q':
- case 'Q':
- exit(0);
- break;
- case '1':
- glLoadIdentity();
- glutDisplayFunc(display_soal_satu);
- glutPostRedisplay();
- break;
- case '2':
- glLoadIdentity();
- glutDisplayFunc(display_soal_dua);
- glutPostRedisplay();
- break;
- }
- }
- void handleSpecialKeyReleased(int key, int x, int y) {
- switch (key) {
- case GLUT_KEY_UP:
- isupKeyPressed = false;
- break;
- case GLUT_KEY_DOWN:
- isdownKeyPressed = false;
- break;
- }
- }
- int main(int argc, char** argv)
- {
- printf("Welcome,\n");
- printf("Press Keyboard\n");
- printf("(1) for Star (Case 1)\n");
- printf("(2) for Square Animation(Case 2)\n");
- printf("for case 1, press up and down arrow to change color\n");
- printf("and for case 2, press s/S to start rotation\n");
- printf("then x/X to rotate on X\n");
- printf("then y/Y to rotate on Y\n");
- printf("then z/Z to rotate on Z\n");
- printf("<press esc/q to exit>\n");
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize (400, 400);
- glutCreateWindow (argv[0]);
- init();
- glutSpecialFunc(handleSpecialKeypress);
- glutSpecialUpFunc(handleSpecialKeyReleased);
- glutDisplayFunc (display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement