Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // OpenGL-03
- //
- // Created by Alexander Kevin on 3/16/16.
- // Copyright © 2016 Alexander Kevin. All rights reserved.
- //
- // CASE : https://www.dropbox.com/s/doa7j9llrzleqet/T00740020220154042Tugas%20dan%20Latihan%203.doc?dl=0
- // .EXE : https://www.dropbox.com/s/k26cfr2iyet6qck/OpenGL-03?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
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glFlush();
- }
- void display_soal_satu()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix();
- glColor3f(1.0, 1.0, 1.0);
- glPushMatrix();
- glRotated(45, 0, 0, 1);
- glScalef(1, 4, 1);
- glutWireCube(2);
- glPopMatrix();
- glRotated(-45, 0, 0, 1);
- glScalef(1, 4, 1);
- glutWireCube(2);
- glPopMatrix();
- glFlush();
- }
- void display_soal_dua()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- // belakang
- glColor3f(1,0.6,0.8);
- glBegin(GL_POLYGON);
- glVertex3f(-1.25, 0, 0);
- //kiri atas
- glVertex3f(-2.8125, 1.5625, 0);
- glVertex3f(-1.5625, 2.8125, 0);
- glVertex3f(0, 1.25, 0);
- //kanan atas
- glVertex3f(1.5625, 2.8125, 0);
- glVertex3f(2.8125, 1.5625, 0);
- glVertex3f(1.25, 0, 0);
- //kanan bawah
- glVertex3f(2.8125, -1.5625, 0);
- glVertex3f(1.5625, -2.8125, 0);
- glVertex3f(0, -1.25, 0);
- //kanan bawah
- glVertex3f(-1.5625, -2.8125, 0);
- glVertex3f(-2.8125, -1.5625, 0);
- glEnd();
- //sisi sisi
- //kiri samping kiri (atas)
- glColor3f(0.9,0.5,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-2.8125, 1.5625, 1.0);
- glVertex3f(-1.25, 0, 1.0);
- glVertex3f(-1.25, 0, 0);
- glVertex3f(-2.8125, 1.5625, 0);
- glEnd();
- //kiri atas
- glColor3f(0.9,0.0,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-2.8125, 1.5625, 1.0);
- glVertex3f(-1.5625, 2.8125, 1.0);
- glVertex3f(-1.5625, 2.8125, 0);
- glVertex3f(-2.8125, 1.5625, 0);
- glEnd();
- //kiri samping kanan
- glColor3f(0.9,0.3,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-1.5625, 2.8125, 1.0);
- glVertex3f(0, 1.25, 1.0);
- glVertex3f(0, 1.25, 0);
- glVertex3f(-1.5625, 2.8125, 0);
- glEnd();
- //kanan samping kiri(atas)
- glColor3f(0.9,0.5,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(0, 1.25, 1.0);
- glVertex3f(0, 1.25, 0);
- glVertex3f(1.5625, 2.8125, 0);
- glVertex3f(1.5625, 2.8125, 1.0);
- glEnd();
- //kanan atas
- glColor3f(0.9,0.0,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(2.8125, 1.5625, 1.0);
- glVertex3f(1.5625, 2.8125, 1.0);
- glVertex3f(1.5625, 2.8125, 0);
- glVertex3f(2.8125, 1.5625, 0);
- glEnd();
- //kanan samping kanan
- glColor3f(0.9,0.3,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(2.8125, 1.5625, 1.0);
- glVertex3f(2.8125, 1.5625, 0);
- glVertex3f(1.25, 0, 0);
- glVertex3f(1.25, 0, 1.0);
- glEnd();
- //kanan samping kiri(bawah)
- glColor3f(0.9,0.3,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(0, -1.25, 1.0);
- glVertex3f(0, -1.25, 0);
- glVertex3f(1.5625, -2.8125, 0);
- glVertex3f(1.5625, -2.8125, 1.0);
- glEnd();
- //kanan atas
- glColor3f(0.9,0.0,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(2.8125, -1.5625, 1.0);
- glVertex3f(1.5625, -2.8125, 1.0);
- glVertex3f(1.5625, -2.8125, 0);
- glVertex3f(2.8125, -1.5625, 0);
- glEnd();
- //kanan samping kanan
- glColor3f(0.9,0.5,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(2.8125, -1.5625, 1.0);
- glVertex3f(2.8125, -1.5625, 0);
- glVertex3f(1.25, 0, 0);
- glVertex3f(1.25, 0, 1.0);
- glEnd();
- //kiri samping kiri (bawah)
- glColor3f(0.9,0.3,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-2.8125, -1.5625, 1.0);
- glVertex3f(-1.25, 0, 1.0);
- glVertex3f(-1.25, 0, 0);
- glVertex3f(-2.8125, -1.5625, 0);
- glEnd();
- //kiri atas
- glColor3f(0.9,0.0,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-2.8125, -1.5625, 1.0);
- glVertex3f(-1.5625, -2.8125, 1.0);
- glVertex3f(-1.5625, -2.8125, 0);
- glVertex3f(-2.8125, -1.5625, 0);
- glEnd();
- //kiri samping kanan
- glColor3f(0.9,0.5,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-1.5625, -2.8125, 1.0);
- glVertex3f(0, -1.25, 1.0);
- glVertex3f(0, -1.25, 0);
- glVertex3f(-1.5625, -2.8125, 0);
- glEnd();
- //depan
- glColor3f(1,0.6,1.0);
- glBegin(GL_POLYGON);
- glVertex3f(-1.25, 0, 1.0);
- //kiri atas
- glVertex3f(-2.8125, 1.5625, 1.0);
- glVertex3f(-1.5625, 2.8125, 1.0);
- glVertex3f(0, 1.25, 1.0);
- //kanan atas
- glVertex3f(1.5625, 2.8125, 1.0);
- glVertex3f(2.8125, 1.5625, 1.0);
- glVertex3f(1.25, 0, 1.0);
- //kanan bawah
- glVertex3f(2.8125, -1.5625, 1.0);
- glVertex3f(1.5625, -2.8125, 1.0);
- glVertex3f(0, -1.25, 1.0);
- //kanan bawah
- glVertex3f(-1.5625, -2.8125, 1.0);
- glVertex3f(-2.8125, -1.5625, 1.0);
- glEnd();
- glFlush();
- }
- void myinit()
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.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 kunci(unsigned char key, int x, int y)
- {
- static int rot=0;
- switch (key)
- {
- /* aplikasi berhenti ketika tombol q ditekan */
- case 27 :
- case 'z':
- exit(0);
- break;
- case 'q':
- glRotated(5, 0,0,1);
- break;
- case 'e':
- glRotated(-5, 0,0,1);
- break;
- case 's':
- glRotated(5, 1,0,0);
- break;
- case 'a':
- glRotated(-5, 0,1,0);
- break;
- case 'w':
- glRotated(-5, 1,0,0);
- break;
- case 'd':
- glRotated(5, 0,1,0);
- break;
- case '1':
- glLoadIdentity();
- glutDisplayFunc(display_soal_satu);
- break;
- case '2':
- glLoadIdentity();
- glutDisplayFunc(display_soal_dua);
- break;
- }
- glutPostRedisplay();
- }
- int main(int argc, char* argv[])
- {
- printf("Welcome,\n");
- printf("Press Keyboard\n");
- printf("(1) for WireCube X(Case 1)\n");
- printf("(2) for Polygon X(Case 2)\n");
- printf("for x-y-rotation use standart WASD (up,left,down,right)\n");
- printf("and q for left rotate and e for right z-rotation\n");
- printf("<press z to exit>\n");
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(400,400);
- glutInitWindowPosition(100,100);
- glutCreateWindow("Kotak");
- glutDisplayFunc(display);
- myinit();
- glutKeyboardFunc(kunci);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement