Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Membuat Grafik Segitiga 3D dengan OpenGL

Membuat Grafik Segitiga 3D dengan OpenGL

#include [<]Windows.h[>]
#include
[<]stdio.h[>]
#include [<]GL\glut.h[>]
#include [<]GL\freeglut.h[>]

//Deklarasi variabel global
float sudut_putar;
float kecepatan_putar;
__int32 waktu_awal, waktu_sekarang, waktu_selisih;

//Deklarasi functions
void glut_idle_F();
void glut_display_F();

//Main//
int main(int argc, char **argv){
int L_Window = 750, T_Window=500;

//inisialisasi variabel global
sudut_putar = 0;
kecepatan_putar = 30.0f;
waktu_selisih = 500;

//inisialisasi OpenGL
printf("Inisialisasi OpenGL\n");
glutInitWindowSize(L_Window, T_Window);//mendisplay object 3D
glutInit(&argc, argv);//parameter sama yang di main

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);//inisialisasi model dari tampilan
glutCreateWindow("Belajar Grafik segitiga 3D");

/*membuat object yang jauh tertutup oleh object yang
lebih dekat pada saat object berada pada satu garis lurus*/
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);//mengubah model matrix yang sedang aktif
/*(float) -> konversi dari int ke float*/
gluPerspective(45.0, (float)L_Window/(float)T_Window, 1.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);

glutIdleFunc(glut_idle_F);
glutDisplayFunc(glut_display_F);

glutMainLoop(); //looping sampai window di close oleh user

return 0;
}//endmain

//function
void glut_idle_F(){
waktu_awal = timeGetTime();
do{
waktu_sekarang = timeGetTime();
}while((waktu_sekarang - waktu_awal) <= waktu_selisih);

printf("waktu sekarang: %d\n", waktu_sekarang - waktu_awal);

//menambah sudut putar agar segitiga berputar mengelilingi sumbu Y
sudut_putar = sudut_putar + kecepatan_putar;
if(sudut_putar>360.0)
sudut_putar = 0.0f;
//panggil fungsi glut_display_F() agar object segitiga ditampilkan kembali
glutPostRedisplay();

}//endfunction


void glut_display_F(){
printf("Display object \n");
//clear the buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//sebelum object 3d ditampilkan maka matrix harus di set le matrix identity
glTranslatef(0.0f, 0.0f, -8.0f);
glRotatef(15, 1.0f, 0.0f, 0.0f);
glRotatef(-15, 1.0f, 0.0f, 0.0f);

/*object disini*/

//gambar sumbu +X_
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINE);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(3.0f,0.0f,0.0f);
glEnd();

//gambar sumbu -X
glEnable(GL_LINE_STIPPLE); //garis putus2
glLineStipple(1,0x7733); //pattern untuk garis putus2
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-3.0, 0.0f, 0.0f);
glEnd();
glDisable(GL_LINE_STIPPLE);//nonaktifkan garis putus2

//gambar sumbu +Y
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 3.0f, 0.0f);
glEnd();

//gambar sumbu -Y
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x7733);
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, -3.0f,0.0f);
glEnd();
glDisable(GL_LINE_STIPPLE);

//gambar sumbu +Z
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,3.0f);
glEnd();

//gambar sumbu -Z
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x7733);
glBegin(GL_LINE_STRIP);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-3.0f);
glEnd();
glDisable(GL_LINE_STIPPLE);

//buat agar segitiga berputar
glRotatef(sudut_putar, 0.0f, 1.0f, 0.0f);

//buat segitiga
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.7f, 1.0f, 0.7f);
glEnd();

//jangan lupa untuk swap the buffer
glutSwapBuffers();
}//endfunction


catatan : jika mau ketik ulang harus diperhatikan penulisannya...karena case sensitive. di include siku buka siku tutupnya dikurung biar tampil di blog. karena dianggapnya tag...



Download Source Code

Kategori

Kategori