Created
January 12, 2020 11:47
-
-
Save dwichan0905/d364e96e6fc75c64b4b950c460dc25b5 to your computer and use it in GitHub Desktop.
Pembentukan Object 3D dan Keyboard Input di OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdarg.h> | |
#include <GL\glut.h> | |
#include <math.h> | |
// Inisialisasi variabel untuk pembentukan objek | |
float w = 480, h = 480; | |
float sudut = 0, sx = 0, sy = 0, sz = 0, z = -320; | |
// Membentuk object sphere | |
void sphere() { | |
glColor3d(1, 1, 0); // warna kuning | |
glutWireSphere(100, 150, 100); // membentuk Wireframe Sphere | |
} | |
// fungsi keyboard spesial | |
void mySpecialKeyboard(int key, int x, int y) { | |
switch (key) { | |
case GLUT_KEY_LEFT: // ketika user menekan tombol arah kiri | |
sy += 10; | |
//sx = 0; | |
sudut -= 10; // sudut yang ada dikurangi 10 | |
break; | |
case GLUT_KEY_RIGHT: // ketika user menekan tombol arah kanan | |
sy -= 10; | |
//sx = 0; | |
sudut += 10; // sudut yg ada ditambah 10 | |
break; | |
case GLUT_KEY_UP: // ketika user menekan tombol arah atas | |
//sy = 1; | |
sx -= 10; | |
sudut -= 10; // sudut yg ada ditambah 10 | |
break; | |
case GLUT_KEY_DOWN: // ketika user menekan tombol arah bawah | |
//sy = 1; | |
sx += 10; | |
sudut += 10; // sudut yg ada ditambah 10 | |
break; | |
} | |
} | |
// fungsi untuk update display | |
void update(int value) { | |
glutPostRedisplay(); | |
glutTimerFunc(50, update, 0); | |
} | |
// fungsi yang akan dijalankan pertama kali | |
void init() { | |
glClearColor(0.0, 0.0, 0.0, 1.0); // membersihkan semua warna menjadi warna hitam | |
glEnable(GL_DEPTH_TEST); // mengaktifkan fungsi kedalaman 3D | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45., (GLdouble)w / (GLdouble)h, 1., 300.); | |
glMatrixMode(GL_MODELVIEW); | |
} | |
// fungsi sebelum membuat wireframe sphere dan penggambaran posisi sphere baru | |
void renderScene(void) { | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // fungsi untuk membersihkan layar dan mengambil fungsi pewarnaan | |
glLoadIdentity(); | |
glTranslatef(0, 0, z); | |
glRotatef(sudut, sx, sy, sz); // putar object | |
sphere(); // buat object sphere | |
glutSwapBuffers(); | |
} | |
// fungsi pertama yang akan dijalankan pertama kali | |
void main(int argc, char **argv) { | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); // mode penggambaran opengl | |
glutInitWindowPosition(100, 100); // posisi jendela di layar | |
glutInitWindowSize(w, h); // ukuran jendela | |
glutCreateWindow("Input dan Objek 3D"); // judul jendela | |
glutDisplayFunc(renderScene); // fungsi untuk memulai render objek | |
init(); | |
glutTimerFunc(50, update, 0); | |
glutSpecialFunc(mySpecialKeyboard); // fungsi wajib untuk membaca input keyboard | |
glutMainLoop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment