Contoh Membuat Objek 3D Dengan Blender dan Gambar Transformasi 3D Dengan OpenGL
Assalamualaikum Wr.Wb
Balik lagi nih guys sama kelompok 5, kalau sebelumnya kita ngasih tau cara membuat gambar 2D dengan OpenGL kali ini kita akan
menulis tentang membuat objek 3d pada Blender dan gambar transformasi 3D dengan OpenGL. Buat yang belum tau apa itu OpenGL dan Transformasi 2D,
bisa lihat di postingan kita sebelum nya disini ya.
BLENDER
Pertama tama saya akan menjelaskan cara membuat objek 3D pada aplikasi blender. Langkah pertama kalian harus sudah menginnstall aplikasi blender dilaptop / pc.
1. Buka aplikasi blender yang telah kalian install tadi, pada awal setiap kali buka aplikasi blender, kalian akan melihat tampilan seperti ini:
2. Lalu klik disembarang tempat dan aplikasi blender sudah menyiapkan satu objek kubus di halaman kerja.
3. Untuk menSelect objek di blender defaultnya menggunakan klik kanan. tetapi bisa di rubah menjadi klik kiri dengan cara klik menu File > User Preferences > Input dan pilih Left di Select with lalu save.
5. Lalu kalian bisa memilih objek 3D yang kalian inginkan, misalnya silinder atau Torus yang seperti donat, maka bentuknya akan seperti ini:
Jika kalian ingin membuat objek 3D lainnya yang lebih spesifik seperti Gelas, Meja, Rumah atau mobil dan lain halnya kalian bisa mencari tutorialnya di google ataupun youtube.
Contoh gambar 3D dari aplikasi Blender kita saya buat.
OpenGL
OpenGL menggunakan matrix sebagai komponen dasar
untuk menghasilkan tampilan pada layar. Semua matrix ini didefinisikan untuk
dapat memproses operasi-operasi dalam 3 dimensi.
Jika pada postingan sebelumnya obyek dibuat dalam 2
dimensi, sebenarnya obyek-obyek tersebut adalah obyek 3 dimensi. Hanya saja dimensi
ketiga diabaikan. Termasuk didalam konsep ini adalah transformasi. Transformasi
selalu dilakukan dalam format 3 dimensi.
Oke langsung aja langkah-langkah untuk membuatnya
adalah sebagai berikut.
1.
Buka DevC++ nya kemudian jalankan new project. Caranya bisa lihat disini.
2.
Masukan kode dibawah ini.
#include
<windows.h>
#include
<GL/glut.h>
/* Variable Global */
char title[] = "Miqdad 3D
Shapes";
/* Inisialisai OpenGL
Graphics */
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Mengatur warna latar menjadi hitam dan buram
glClearDepth(1.0f); // Mengaur kedalaman latar belakang ke terjauh
glEnable(GL_DEPTH_TEST); // Mengaktifkan pengujian kedalaman untuk z-culling
glDepthFunc(GL_LEQUAL); // Setel jenis uji kedalaman
glShadeModel(GL_SMOOTH); // Mengaktifkan bayangan halus
glHint(GL_PERSPECTIVE_CORRECTION_HINT,
GL_NICEST); // Koreksi perspektif yang bagus
}
/ * Handler untuk acara window-repaint. Dipanggil kembali ketika jendela pertama kali muncul dan kapan saja jendela perlu dicat ulang. * /
void display() {
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); // Untuk beroperasi pada matriks model-view
// Render sebuah kubus warna yang terdiri dari 6 quadsdengan warna yang berbeda
glLoadIdentity(); // Atur ulang matriks model-view
glTranslatef(1.5f, 0.0f, -7.0f); // Bergerak ke kanan dan ke layar
glBegin(GL_QUADS); // Memulai menggambar kubus warna dengan 6 quads
// Tampilan atas (y = 1.0f)
// Definisikan simpul dalam urutan berlawanan arah jarum jam (CCW) dengan normal
glColor3f(0.0f, 1.0f, 0.0f); // Hijau
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Tampilan bawah (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Oren
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Tampilan depan (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Merah
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Tampilan belakang (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Kuning
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Tampilan kiri (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Biru
glVertex3f(-1.0f, 1.0f,
1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Tampilan kanan (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f,
1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
// Akhir menggambar warna-kubus
// Membuat piramida terdiri dari 4 segitiga
glLoadIdentity(); // Atur ulang matriks model-view
glTranslatef(-1.5f, 0.0f, -6.0f); // Pindah ke kiri dan ke layar
glBegin(GL_TRIANGLES); // Mulai gambar piramid dengan 4 segitiga
// Depan
glColor3f(1.0f, 0.0f, 0.0f); // Merah
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Hijau
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Biru
glVertex3f(1.0f, -1.0f, 1.0f);
// Kanan
glColor3f(1.0f, 0.0f, 0.0f); // Merah
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Biru
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Hijau
glVertex3f(1.0f, -1.0f, -1.0f);
// Belakang
glColor3f(1.0f, 0.0f, 0.0f); // Merah
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Hijau
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Biru
glVertex3f(-1.0f, -1.0f, -1.0f);
// Kiri
glColor3f(1.0f,0.0f,0.0f); // Merah
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f); // Biru
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f); // Hijau
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();
// Selesai menggambar piramid
glutSwapBuffers(); // Tukar frame depan dan belakang buffer (buffering ganda)
}
/ * Handler untuk acara ukuran ulang jendela. Dipanggil kembali ketika jendela pertama kali muncul dan setiap kali jendela diukur ulang dengan lebar dan tinggi baru * /
void reshape(GLsizei
width, GLsizei height) { // GLsizei untuk bilangan bulat non-negatif
// Hitung rasio aspek dari jendela baru
if (height == 0) height = 1; // Mencegah pembagian dengan 0
GLfloat aspect = (GLfloat)width /
(GLfloat)height;
//Menyetel viewport untuk menutupi jendela baru
glViewport(0, 0, width, height);
// Menyetel rasio aspek
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
int main(int argc,
char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Menghidupkan mode dobel
glutInitWindowSize(640, 480); // Ukuran tampilan jendela
glutInitWindowPosition(50, 50); // Posisi jendela tampilan
glutCreateWindow(title); // Membuat jendela dengan judul title
glutDisplayFunc(display); // Memanggil function display
glutReshapeFunc(reshape); // Memanggil handler untuk merubah ukuran jendela
initGL(); // Inisialisasi OpenGL
glutMainLoop();
return 0;
}
Kalian juga bisa membuat objek 3D lainnya seperti kotak animasi dan mobil. Untuk contoh dan kode nya bisa dilihat dibawah ini ya.
Kotak animasi 3D
Kode:
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
//glShadeModel(GL_SMOOTH); //Enable smooth shading
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = -70.0f;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -8.0f);
//Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glRotatef(_angle, 0.0f, 1.0f, 1.0f);
//Set the colour here
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, 0.0f, 1.0f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
glNormal3f(0.0f, 0.0f, -1.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glutSwapBuffers();
}
void update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//Create the window
glutCreateWindow("Animasi Kotak 3D");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
//Add a timer
glutTimerFunc(25, update, 0);
glutMainLoop();
}
Tinggal di Compile dan Run, maka hasilnya akan seperti ini:#include <stdlib.h>
#include <GL/glut.h>
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
//glShadeModel(GL_SMOOTH); //Enable smooth shading
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _angle = -70.0f;
//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -8.0f);
//Add ambient light
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glRotatef(_angle, 0.0f, 1.0f, 1.0f);
//Set the colour here
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, 0.0f, 1.0f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
glNormal3f(0.0f, 0.0f, -1.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
glutSwapBuffers();
}
void update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
//Create the window
glutCreateWindow("Animasi Kotak 3D");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
//Add a timer
glutTimerFunc(25, update, 0);
glutMainLoop();
}
Mobil 3D
Kode:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <GL/glut.h>
int letak=0;
bool smooth = 1, bertukar = 1, state_red = 0;
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 10.0 };
GLfloat light_position1[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat light_position2[] = { -1.0, -1.0, -1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat red_light[] = {9.,0.,0.,1.};
GLfloat blue_light[] = {0.,0.,1.,1.};
GLfloat white_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
void update(int);
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
//material
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT1, GL_DIFFUSE, blue_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, red_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, blue_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glPushMatrix();
glPushMatrix(); // body
glScalef(4,1,2);
glutSolidCube(.5);
glTranslatef(-.05,.3,0);
glScalef(0.6,3,2);
glutSolidCube(.25);
glTranslatef(-.12,.001,-.001);
glScalef(1,1.8,2.48);
glRotatef(230, 0, 0, 250);
glutSolidCube(.1);
glPopMatrix();
glTranslatef(0,0,.5);
glPushMatrix();
glTranslatef(-.4,-.2,0);
glutSolidTorus(.1,.2,8,8); // wheel
glTranslatef(1,0,0);
glutSolidTorus(.1,.2,8,8); // wheel
glPopMatrix();
glTranslatef(0,0,-1);
glPushMatrix();
glTranslatef(-.4,-.2,0);
glutSolidTorus(.1,.2,8,8); // wheel
glTranslatef(1,0,0);
glutSolidTorus(.1,.2,8,8); // wheel
glPopMatrix();
glPopMatrix();
glPopMatrix();
glFlush();
}
void GL_keyboard(unsigned char key, int x, int y) // GLUT keyboard function
{
switch(key)
{case 'f':
smooth = false;
break;
case 27 : exit(0);
case 't': //ganti warna lampu
bertukar = 1;
glutTimerFunc(10,update,0);
break;
case 'w': //key w untuk maju
letak--;
glTranslatef(-0.05,0.0,0.0);
display();
break;
case 's': //key s untuk mundur
letak++;
glTranslatef(0.05,0.0,0.0);
display();
break;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-2.5*(GLfloat)w/(GLfloat)h,
2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 0., 1., -1., 1., 0., 1., 0.);
}
void update(int value)
{
if (state_red == 1)
{
glLightfv(GL_LIGHT0, GL_DIFFUSE, blue_light);
glLightfv(GL_LIGHT1, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, blue_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, red_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
state_red = 0;
}
else {
glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT1, GL_DIFFUSE, blue_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, red_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, blue_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
state_red = 1;
}
glutPostRedisplay();
if (bertukar) {
glutTimerFunc(1000,update,0);
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutKeyboardFunc(GL_keyboard);
glutMainLoop();
return 0;
}
Jalankan program nya dan hasilnya akan seperti dibawah ini:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <GL/glut.h>
int letak=0;
bool smooth = 1, bertukar = 1, state_red = 0;
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 10.0 };
GLfloat light_position1[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat light_position2[] = { -1.0, -1.0, -1.0, 0.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat red_light[] = {9.,0.,0.,1.};
GLfloat blue_light[] = {0.,0.,1.,1.};
GLfloat white_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
void update(int);
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
//material
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT1, GL_DIFFUSE, blue_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, red_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, blue_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glPushMatrix();
glPushMatrix(); // body
glScalef(4,1,2);
glutSolidCube(.5);
glTranslatef(-.05,.3,0);
glScalef(0.6,3,2);
glutSolidCube(.25);
glTranslatef(-.12,.001,-.001);
glScalef(1,1.8,2.48);
glRotatef(230, 0, 0, 250);
glutSolidCube(.1);
glPopMatrix();
glTranslatef(0,0,.5);
glPushMatrix();
glTranslatef(-.4,-.2,0);
glutSolidTorus(.1,.2,8,8); // wheel
glTranslatef(1,0,0);
glutSolidTorus(.1,.2,8,8); // wheel
glPopMatrix();
glTranslatef(0,0,-1);
glPushMatrix();
glTranslatef(-.4,-.2,0);
glutSolidTorus(.1,.2,8,8); // wheel
glTranslatef(1,0,0);
glutSolidTorus(.1,.2,8,8); // wheel
glPopMatrix();
glPopMatrix();
glPopMatrix();
glFlush();
}
void GL_keyboard(unsigned char key, int x, int y) // GLUT keyboard function
{
switch(key)
{case 'f':
smooth = false;
break;
case 27 : exit(0);
case 't': //ganti warna lampu
bertukar = 1;
glutTimerFunc(10,update,0);
break;
case 'w': //key w untuk maju
letak--;
glTranslatef(-0.05,0.0,0.0);
display();
break;
case 's': //key s untuk mundur
letak++;
glTranslatef(0.05,0.0,0.0);
display();
break;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-2.5*(GLfloat)w/(GLfloat)h,
2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 0., 1., -1., 1., 0., 1., 0.);
}
void update(int value)
{
if (state_red == 1)
{
glLightfv(GL_LIGHT0, GL_DIFFUSE, blue_light);
glLightfv(GL_LIGHT1, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, blue_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, red_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
state_red = 0;
}
else {
glLightfv(GL_LIGHT0, GL_DIFFUSE, red_light);
glLightfv(GL_LIGHT1, GL_DIFFUSE, blue_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, red_light);
glLightfv(GL_LIGHT1, GL_SPECULAR, blue_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
state_red = 1;
}
glutPostRedisplay();
if (bertukar) {
glutTimerFunc(1000,update,0);
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutKeyboardFunc(GL_keyboard);
glutMainLoop();
return 0;
}
Mobil 3D ini bisa bergerak maju dan mundur dengan menekan tombol "w" (untuk maju) dan "s" (untuk mundur).
Sekian contoh gambar Transformasi 3D dengan OpenGL. Semoga bermanfaat untuk semuanya khususnya yang lagi belajar OpenGL. Terimakasih.
Salam kelompok 5 (Grafika Komputer)
- Miqdad Abdurrohman
- Usaid Mubarak
- Wahyu Rahmana
Refrensi:
http://tysyudha.blogspot.com/2016/04/how-to-make-3d-car-move-in-opengl-c.html
http://tysyudha.blogspot.com/2016/04/how-to-make-3d-car-move-in-opengl-c.html
Comments
Post a Comment