Membuat Gambar Rubik 3D Bertransformasi menggunakan Pelles C

Koding yang ada pada program disini sebenarnya ada semua, tapi yang akan di jelaskan hanya pada gambarnya saja, yang memang diedit. Selain pada gambarnya, kodingnya automatically generated yang berarti terbuat sendiri jika kita membuka pelles c nya.

untuk membuatnya, kita masukkan semua coding yang ada dibawah ini :

#include <windows.h>

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glfw.h>

 

void mulaiOpenGL(void);

 

int main(void)

{

//

// mRunning = TRUE, aplikasi masih berjalan

// mRunning = FALSE, ??? :p

GLuint mRunning = GL_TRUE;

 

//

// inisialisasi GLFW

if( glfwInit() == GL_FALSE )

{

MessageBox( NULL, “ERROR :: gagal menginisialisasi GLFW”, “Error!”, MB_OK);

return(0);

}

//

// buat sebuah window yang akan digunakan untuk menggambar.

if( glfwOpenWindow( 640, 480, 0, 0, 0, 0, 24, 0, GLFW_WINDOW ) == GL_FALSE )

{

MessageBox( NULL, “ERROR :: gagal membuat window”, “Error!”, MB_OK );

glfwTerminate();

return(0);

}

//

// Set judul yang ada di window dan Swap interval.

glfwSetWindowTitle( “Praktikum Grafik Komputer LabTI” );

glfwSwapInterval( 1 );

//

// mulai OpenGL (melakukan setting awal OpenGL)

mulaiOpenGL();

float sudut = 0;

//

// mulai looping utama program

while( mRunning )

{

//

// bersihkan layar dan depth buffer

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glLoadIdentity();

gluLookAt(10.0,10.0,10.0,0.0,0.0,0.0,0.0,30.0,0.0);

glRotatef(sudut,-5,4,0);

 

glScalef(2,2,2);

//

// lakukan penggambaran di sini

//————————————————————————————

//depan

glBegin(GL_QUADS);

//kotak1

glColor3f(0.7f,0.0f,2.0f);

glVertex3f(0.0f,0.0f,0.0f);

glVertex3f(1.0f,0.0f,0.0f);

glVertex3f(1.0f,1.0f,0.0f);

glVertex3f(0.0f,1.0f,0.0f);

//kotak2

glColor3f(0.7f,0.2f,0.0f);

glVertex3f(0.0f,1.1f,0.0f);

glVertex3f(1.0f,1.1f,0.0f);

glVertex3f(1.0f,2.1f,0.0f);

glVertex3f(0.0f,2.1f,0.0f);

//kotak3

glColor3f(0.0f,0.0f,3.0f);

glVertex3f(0.0f,2.2f,0.0f);

glVertex3f(1.0f,2.2f,0.0f);

glVertex3f(1.0f,3.2f,0.0f);

glVertex3f(0.0f,3.2f,0.0f);

//kotak4

glColor3f(0.0f,5.0f,0.0f);

glVertex3f(1.1f,0.0f,0.0f);

glVertex3f(2.1f,0.0f,0.0f);

glVertex3f(2.1f,1.0f,0.0f);

glVertex3f(1.1f,1.0f,0.0f);

//kotak5

glColor3f(0.7f,0.0f,7.0f);

glVertex3f(1.1f,1.1f,0.0f);

glVertex3f(2.1f,1.1f,0.0f);

glVertex3f(2.1f,2.1f,0.0f);

glVertex3f(1.1f,2.1f,0.0f);

//kotak6

glColor3f(0.0f,7.0f,7.0f);

glVertex3f(1.1f,2.2f,0.0f);

glVertex3f(2.1f,2.2f,0.0f);

glVertex3f(2.1f,3.2f,0.0f);

glVertex3f(1.1f,3.2f,0.0f);

//kotak7

glColor3f(0.0f,7.0f,0.0f);

glVertex3f(2.2f,0.0f,0.0f);

glVertex3f(3.2f,0.0f,0.0f);

glVertex3f(3.2f,1.0f,0.0f);

glVertex3f(2.2f,1.0f,0.0f);

//kotak8

glColor3f(0.7f,0.7f,0.0f);

glVertex3f(2.2f,1.1f,0.0f);

glVertex3f(3.2f,1.1f,0.0f);

glVertex3f(3.2f,2.1f,0.0f);

glVertex3f(2.2f,2.1f,0.0f);

//kotak9

glColor3f(0.7f,0.7f,0.7f);

glVertex3f(2.2f,2.2f,0.0f);

glVertex3f(3.2f,2.2f,0.0f);

glVertex3f(3.2f,3.2f,0.0f);

glVertex3f(2.2f,3.2f,0.0f);

glEnd();

 

//belakang

glBegin(GL_QUADS);

//kotak1

glColor3f(0.7f,0.7f,0.0f);

glVertex3f(0.0f,3.2f,-3.2f);

glVertex3f(1.0f,3.2f,-3.2f);

glVertex3f(1.0f,2.2f,-3.2f);

glVertex3f(0.0f,2.2f,-3.2f);

//kotak2

glColor3f(0.7f,0.7f,0.7f);

glVertex3f(0.0f,2.1f,-3.2f);

glVertex3f(1.0f,2.1f,-3.2f);

glVertex3f(1.0f,1.1f,-3.2f);

glVertex3f(0.0f,1.1f,-3.2f);

//kotak3

glColor3f(0.0f,7.0f,0.0f);

glVertex3f(0.0f,1.0f,-3.2f);

glVertex3f(1.0f,1.0f,-3.2f);

glVertex3f(1.0f,0.0f,-3.2f);

glVertex3f(0.0f,0.0f,-3.2f);

//kotak4

glColor3f(0.0f,7.0f,7.0f);

glVertex3f(1.1f,0.0f,-3.2f);

glVertex3f(2.1f,0.0f,-3.2f);

glVertex3f(2.1f,1.0f,-3.2f);

glVertex3f(1.1f,1.0f,-3.2f);

//kotak5

glColor3f(0.7f,0.0f,7.0f);

glVertex3f(1.1f,1.1f,-3.2f);

glVertex3f(2.1f,1.1f,-3.2f);

glVertex3f(2.1f,2.1f,-3.2f);

glVertex3f(1.1f,2.1f,-3.2f);

//kotak6

glColor3f(0.0f,5.0f,0.0f);

glVertex3f(1.1f,2.2f,-3.2f);

glVertex3f(2.1f,2.2f,-3.2f);

glVertex3f(2.1f,3.2f,-3.2f);

glVertex3f(1.1f,3.2f,-3.2f);

//kotak7

glColor3f(0.0f,0.0f,3.0f);

glVertex3f(2.2f,0.0f,-3.2f);

glVertex3f(3.2f,0.0f,-3.2f);

glVertex3f(3.2f,1.0f,-3.2f);

glVertex3f(2.2f,1.0f,-3.2f);

//kotak8

glColor3f(0.7f,0.2f,0.0f);

glVertex3f(2.2f,1.1f,-3.2f);

glVertex3f(3.2f,1.1f,-3.2f);

glVertex3f(3.2f,2.1f,-3.2f);

glVertex3f(2.2f,2.1f,-3.2f);

//kotak9

glColor3f(0.7f,0.0f,2.0f);

glVertex3f(2.2f,2.2f,-3.2f);

glVertex3f(3.2f,2.2f,-3.2f);

glVertex3f(3.2f,3.2f,-3.2f);

glVertex3f(2.2f,3.2f,-3.2f);

glEnd();

 

//atas

glBegin(GL_QUADS);

//kotak1

glColor3f(0.7f,0.0f,2.0f);

glVertex3f(0.0f,3.2f,0.0f);

glVertex3f(1.0f,3.2f,0.0f);

glVertex3f(1.0f,3.2f,-1.0f);

glVertex3f(0.0f,3.2f,-1.0f);

//kotak2

glColor3f(0.7f,0.2f,0.0f);

glVertex3f(0.0f,3.2f,-1.1f);

glVertex3f(1.0f,3.2f,-1.1f);

glVertex3f(1.0f,3.2f,-2.1f);

glVertex3f(0.0f,3.2f,-2.1f);

//kotak3

glColor3f(0.0f,0.0f,3.0f);

glVertex3f(0.0f,3.2f,-2.2f);

glVertex3f(1.0f,3.2f,-2.2f);

glVertex3f(1.0f,3.2f,-3.2f);

glVertex3f(0.0f,3.2f,-3.2f);

//kotak4

glColor3f(0.0f,5.0f,0.0f);

glVertex3f(1.1f,3.2f,0.0f);

glVertex3f(2.1f,3.2f,0.0f);

glVertex3f(2.1f,3.2f,-1.0f);

glVertex3f(1.1f,3.2f,-1.0f);

//kotak5

glColor3f(0.7f,0.0f,7.0f);

glVertex3f(1.1f,3.2f,-1.1f);

glVertex3f(2.1f,3.2f,-1.1f);

glVertex3f(2.1f,3.2f,-2.1f);

glVertex3f(1.1f,3.2f,-2.1f);

//kotak6

glColor3f(0.0f,7.0f,0.0f);

glVertex3f(1.1f,3.2f,-2.2f);

glVertex3f(2.1f,3.2f,-2.2f);

glVertex3f(2.1f,3.2f,-3.2f);

glVertex3f(1.1f,3.2f,-3.2f);

//kotak7

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(2.2f,3.2f,0.0);

glVertex3f(3.2f,3.2f,0.0f);

glVertex3f(3.2f,3.2f,-1.0f);

glVertex3f(2.2f,3.2f,-1.0f);

//kotak8

glColor3f(0.0f,7.0f,0.0f);

glVertex3f(2.2f,3.2f,-1.1f);

glVertex3f(3.2f,3.2f,-1.1f);

glVertex3f(3.2f,3.2f,-2.1f);

glVertex3f(2.2f,3.2f,-2.1f);

//kotak9

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(2.2f,3.2f,-2.2f);

glVertex3f(3.2f,3.2f,-2.2f);

glVertex3f(3.2f,3.2f,-3.2f);

glVertex3f(2.2f,3.2f,-3.2f);

glEnd();

 

 

//bawah

glBegin(GL_QUADS);

//kotak1

glColor3f(1.0f,1.0f,0.0f);

glVertex3f(0.0f,0.0f,0.0f);

glVertex3f(1.0f,0.0f,0.0f);

glVertex3f(1.0f,0.0f,-1.0f);

glVertex3f(0.0f,0.0f,-1.0f);

//kotak2

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(0.0f,0.0f,-1.1f);

glVertex3f(1.0f,0.0f,-1.1f);

glVertex3f(1.0f,0.0f,-2.1f);

glVertex3f(0.0f,0.0f,-2.1f);

//kotak3

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(0.0f,0.0f,-2.2f);

glVertex3f(1.0f,0.0f,-2.2f);

glVertex3f(1.0f,0.0f,-3.2f);

glVertex3f(0.0f,0.0f,-3.2f);

//kotak4

glColor3f(0.7f,10.0f,0.70f);

glVertex3f(1.1f,0.0f,0.0f);

glVertex3f(2.1f,0.0f,0.0f);

glVertex3f(2.1f,0.0f,-1.0f);

glVertex3f(1.1f,0.0f,-1.0f);

//kotak5

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(1.1f,0.0f,-1.1f);

glVertex3f(2.1f,0.0f,-1.1f);

glVertex3f(2.1f,0.0f,-2.1f);

glVertex3f(1.1f,0.0f,-2.1f);

//kotak6

glColor3f(0.7f,0.0f,10.0f);

glVertex3f(1.1f,0.0f,-2.2f);

glVertex3f(2.1f,0.0f,-2.2f);

glVertex3f(2.1f,0.0f,-3.2f);

glVertex3f(1.1f,0.0f,-3.2f);

//kotak7

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(2.2f,0.0f,0.0f);

glVertex3f(3.2f,0.0f,0.0f);

glVertex3f(3.2f,0.0f,-1.0f);

glVertex3f(2.2f,0.0f,-1.0f);

//kotak8

glColor3f(0.7f,0.7f,0.7f);

glVertex3f(2.2f,0.0f,-1.1f);

glVertex3f(3.2f,0.0f,-1.1f);

glVertex3f(3.2f,0.0f,-2.1f);

glVertex3f(2.2f,0.0f,-2.1f);

//kotak9

glColor3f(0.7f,0.0f,2.0f);

glVertex3f(2.2f,0.0f,-2.2f);

glVertex3f(3.2f,0.0f,-2.2f);

glVertex3f(3.2f,0.0f,-3.2f);

glVertex3f(2.2f,0.0f,-3.2f);

glEnd();

 

// samping kanan

glBegin(GL_QUADS);

//kotak1

glColor3f(0.7f,0.0f,2.0f);

glVertex3f(3.2f,0.0f,0.0f);

glVertex3f(3.2f,0.0f,-1.0f);

glVertex3f(3.2f,1.0f,-1.0f);

glVertex3f(3.2f,1.0f,0.0f);

//kotak2

glColor3f(0.7f,0.7f,0.7f);

glVertex3f(3.2f,1.1f,0.0f);

glVertex3f(3.2f,1.1f,-1.0f);

glVertex3f(3.2f,2.1f,-1.0f);

glVertex3f(3.2f,2.1f,0.0f);

//kotak3

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(3.2f,2.2f,0.0f);

glVertex3f(3.2f,2.2f,-1.0f);

glVertex3f(3.2f,3.2f,-1.0f);

glVertex3f(3.2f,3.2f,0.0f);

//kotak4

glColor3f(0.7f,0.0f,10.0f);

glVertex3f(3.2f,0.0f,-1.1f);

glVertex3f(3.2f,0.0f,-2.1f);

glVertex3f(3.2f,1.0f,-2.1f);

glVertex3f(3.2f,1.0f,-1.1f);

//kotak5

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(3.2f,1.1f,-1.1f);

glVertex3f(3.2f,1.1f,-2.1f);

glVertex3f(3.2f,2.1f,-2.1f);

glVertex3f(3.2f,2.1f,-1.1f);

//kotak6

glColor3f(0.7f,10.0f,0.70f);

glVertex3f(3.2f,2.2f,-1.1f);

glVertex3f(3.2f,2.2f,-2.1f);

glVertex3f(3.2f,3.2f,-2.1f);

glVertex3f(3.2f,3.2f,-1.1f);

//kotak7

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(3.2f,0.0f,-2.2f);

glVertex3f(3.2f,0.0f,-3.2f);

glVertex3f(3.2f,1.0f,-3.2f);

glVertex3f(3.2f,1.0f,-2.2f);

//kotak8

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(3.2f,1.1f,-2.2f);

glVertex3f(3.2f,1.1f,-3.2f);

glVertex3f(3.2f,2.1f,-3.2f);

glVertex3f(3.2f,2.1f,-2.2f);

//kotak9

glColor3f(1.0f,1.0f,0.0f);

glVertex3f(3.2f,2.2f,-2.2f);

glVertex3f(3.2f,2.2f,-3.2f);

glVertex3f(3.2f,3.2f,-3.2f);

glVertex3f(3.2f,3.2f,-2.2f);

glEnd();

 

//samping kiri

glBegin(GL_QUADS);

//kotak1

glColor3f(1.0f,1.0f,0.0f);

glVertex3f(0.0f,0.0f,0.0f);

glVertex3f(0.0f,0.0f,-1.0f);

glVertex3f(0.0f,1.0f,-1.0f);

glVertex3f(0.0f,1.0f,0.0f);

//kotak2

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(0.0f,1.1f,0.0f);

glVertex3f(0.0f,1.1f,-1.0f);

glVertex3f(0.0f,2.1f,-1.0f);

glVertex3f(0.0f,2.1f,0.0f);

//kotak3

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(0.0f,2.2f,0.0f);

glVertex3f(0.0f,2.2f,-1.0f);

glVertex3f(0.0f,3.2f,-1.0f);

glVertex3f(0.0f,3.2f,0.0f);

//kotak4

glColor3f(0.7f,10.0f,0.70f);

glVertex3f(0.0f,0.0f,-1.1f);

glVertex3f(0.0f,0.0f,-2.1f);

glVertex3f(0.0f,1.0f,-2.1f);

glVertex3f(0.0f,1.0f,-1.1f);

//kotak5

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(0.0f,1.1f,-1.1f);

glVertex3f(0.0f,1.1f,-2.1f);

glVertex3f(0.0f,2.1f,-2.1f);

glVertex3f(0.0f,2.1f,-1.1f);

//kotak6

glColor3f(0.7f,0.0f,10.0f);

glVertex3f(0.0f,2.2f,-1.1f);

glVertex3f(0.0f,2.2f,-2.1f);

glVertex3f(0.0f,3.2f,-2.1f);

glVertex3f(0.0f,3.2f,-1.1f);

//kotak7

glColor3f(0.7f,0.0f,0.0f);

glVertex3f(0.0f,0.0f,-2.2f);

glVertex3f(0.0f,0.0f,-3.2f);

glVertex3f(0.0f,1.0f,-3.2f);

glVertex3f(0.0f,1.0f,-2.2f);

//kotak8

glColor3f(0.7f,0.7f,0.7f);

glVertex3f(0.0f,1.1f,-2.2f);

glVertex3f(0.0f,1.1f,-3.2f);

glVertex3f(0.0f,2.1f,-3.2f);

glVertex3f(0.0f,2.1f,-2.2f);

//kotak9

glColor3f(0.7f,0.0f,2.0f);

glVertex3f(0.0f,2.2f,-2.2f);

glVertex3f(0.0f,2.2f,-3.2f);

glVertex3f(0.0f,3.2f,-3.2f);

glVertex3f(0.0f,3.2f,-2.2f);

glEnd();

 

 

 

sudut +=0.50;

 

 

 

 

//————————————————————————————

//

// tampilkan ke layar (swap double buffer)

glfwSwapBuffers();

 

//

// check input , apakah tombol esc ditekan atau tombol “close” diclick

mRunning = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );

}

 

glfwTerminate();

return(0);

}

 

void mulaiOpenGL(void)

{

//

// Set viewport ke resolusi 640×480 viewport bisa diibaratkan

// layar monitor anda

glViewport( 0, 0, 640, 480 );

//

// Set mode OpenGL ke mode pryeksi (Projection) dan set proyeksi

// menggunakan proyeksi perspective, dengan sudut pandang (Field Of

// View) 60 derajat

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

gluPerspective( 60.0f, 640.0f/480.0f, 0.1f, 1000.0f );

 

glMatrixMode( GL_MODELVIEW );

glLoadIdentity();

//

// Set mode gradasi warna halus (Smooth)

glShadeModel( GL_SMOOTH );

//

// warna yang digunakan untuk membersihkan layar

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

//

// nilai untuk membersihkan depth buffer.

glClearDepth( 1.0f );

//

// Depth test digunakan untuk menghindari polygon yang

// tumpang tindih.

glEnable( GL_DEPTH_TEST );

glDepthFunc( GL_LEQUAL );

//

// beritahu OpenGL untuk menggunakan perhitungan perspective

// yang terbaik (perhitungan ini tidak bisa selalu 100% akurat)

glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

}

 

Penjelasan

Transformasi adalah perubahan bentuk. Macam-macam transformasi:

  1. Translasi : pergeseran objek secara sejajar
  2. Rotasi : perputaran objek dengan pusat 0
  3. Dilatasi : perbesaran atau penyekalaan objek terhadap titik pusat.

 

Pada praktikum kali ini, praktikan akan membuat benda berrotasi dan bertranslasi. Pada pelles C, untuk membuat objek bertransformasi, perintah-perintahnya sebagai berikut:

  • glTranslate(f){x,y,z}: untuk membuat benda bertranslasi. (f) merupakan tipe data yang digunakan yaitu berupa Float, tipe data ini dapat diubah menjadi integer atau yang lain. {x,y,z} merupakan titik-titik skala pada sumbu x,y,dan z.
  • glRotate(f){besar_sudut,x,y,z}: untuk membuat benda berotasi. 9f0 merupakan tipe data yang digunakan, {x,y,z} merupakan titik putar atau poros rotasi objek.
  • glScale(f){x,y,z}: untuk memperbesar atau memperkecil objek berdasarkan skala tertentu, menciptakan matriks S yang menskala object berdasarkan pada vektor arah x, y, and z.

Agar objek dapat berputar, maka tentukan dulu besar sudutnya. Tanda (+) untuk perputaran berlawanan arah jarum jam, dan tanda (-) untuk perputaran searah jarum jam. Dan (0.3) merupakan ukuran seberapa cepat perputaran dilakukan, angka ini tentunya dapat diubah sesuai keinginan anda.

Bentuk-bentuk yang tersedia, antara lain:

  • glBegin(GL_POINTS) : untuk membuat titik.
  • glBegin(GL_LINES) : untuk membuat garis.
  • glBegin(GL_TRIANGLES) : untuk membuat segitiga.
  • glBegin(GL_QUADS) : untuk membuat persegi.
  • glBegin(GL_POLYGON) : untuk membuat bentuk gambar dengan banyak sudut.

kemudian user diminta untuk menentukan tipe data dari variabel-variabel tersebut. Pada program ini digunakan tipe data Float (pecahan) yang dideklarasikan dengan huruf “f”.

perintah glColor(r,g,b) digunakan untuk menentukan warna atau campuran warna; digit pertama untuk warna merah (red), digit kedua untuk warna hijau (green), dan yang ketiga untuk warna biru (blue). Untuk membuat gradasi warna, user cukup menambahkan perintah glColor di antara 2 perintah glVerteks.

Jika kita telah menetapkan beberapa titik, secara otomatis program akan menghubungkan titik-titik tersebut sehingga membentuk sebuah gambar polygon. Tidak hanya digunakan untuk bangun persegi delapan, persegi enam, atau pentagon, tapi juga bentuk-bentuk lain yang memiliki banyak titik, tanpa harus bangun datar simetris.

jika user membuat 5 titik pada program di atas atau menggunakan perintah GL_QUADS, maka secara otomatis program hanya akan mengambil 4 titik pertama saja, titik-titik yang lainnya diabaikan. begitu juga pada GL_TRIANGLES, hanya 3 vertex awal saja yang digunakan, selebihnya diabaikan.

Maka Outputnya akan seperti ini :

Gambar diatas adalah gambar model rubik 3D yang sedang berputar dengan menggunakkan transformasi.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s