Wednesday 13 June 2018

Example Projek OpenGL Lampion merah dengan Codeblock C++

ini adalah sebuah projek membuat lampion dengan efek memutar, menjauhkan layar dengan tombol, mendekatkan dengan layar , merubah putaran ke arah sumbu x, y atau z. hasil akhir dari projek ini adalah seperti gambar dibawah ini.


langkah-langkagh untuk membuatnya adalah sebagai berikut.

  1. download terlebih dahulu openGL disini : View openGL
  2. setting codeblock dengan openGL caranya dapat dilihat disini: view
  3. buka codeblock dan buat projek baru caranya dapat dilihat disini : view
  4. copy semua sourcode yang tersedia di blog ini.
adapun sourcode yang tersedia adalah sebagai berikut ini :


#include <GL/glut.h>
#include <windows.h>
int w=400, h=400, z=0;
int x1=0, y1=0, z1=0, sudut=0;
void renderScene (void) {
static float alpha =0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor (0, 0, 0, 0);
glLoadIdentity ();
glTranslatef (0, 0, z);
glRotatef (sudut, x1, y1, z1);
glRotatef (alpha,1,1,1);
glColor3f(1, 1, 0);
alpha = alpha +0.5;
//glutWireCube (3);//fungsi kubus
//glutSolidCube(3);//kubus penuh warna
glutWireSphere(2,90,90);//fungsi bola
//glutWireCone(2, 4, 25, 25);//fungsi kerucut
glColor3f(1,0,0);
glutWireTorus (4, 2, 90, 30);//fungsi donat
//glutWireTeapot (4);//fungsi ceret
//glutSolidIcosahedron ();//fungsi delima
//glutWireDodecahedron ();//fungsi bola
//glutWireTetrahedron();//fungsi piramida
glutSwapBuffers ();
}
void resize (int w1, int h1) {
glViewport (0, 0, w1, h1);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (45.0,(float) w1/(float) h1,1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void Keyboard (GLubyte key, GLint x, GLint y) {
if (key == 'a' || key == 'A') z+=2;
if (key == 'd' || key == 'D') z-=2;
if (key == 'x' || key == 'X') {
x1=1;
y1=0;
z1=0;
sudut +=10;
}
if (key == 'y' || key == 'Y') {
x1=0;
y1=1;
z1=0;
sudut +=-10;
}
if (key == 'z'|| key == 'Z') {
x1=0;
y1=0;
z1=1;
sudut +=-10;
}
if ( key == 'f'|| key == 'F') {
glutFullScreen ();
}
{
int foo;
foo = x + y;
if ('q' == key || 'Q' == key || 27 == key)
exit (0);
}
}
void timer (int value) {
glutPostRedisplay ();
glutTimerFunc (1,timer,0);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition (100,100);
glutInitWindowSize (w,h);
glutCreateWindow ("lampion");
gluOrtho2D (-w/2,w/2,-h/2,h/2);
glutDisplayFunc (renderScene);
glutReshapeFunc (resize);
glutKeyboardFunc (Keyboard);
glutTimerFunc (1,timer,0);
glutMainLoop ();
}


apabila sourcode ini dijalankan akan menghasilkan hasil seperti dibawah ini. berupa lampion merah dengan putaran-putarananya


dan berikut ini adalah tombol yang dapat digunakan untuk merubah arahnya.

a. tombol A untuk zoom (+) objek 
b.tombol D untuk zoom ( - )objek 
c.tombol X untuk rotasi sumbu x 
d. tombol Y untuk rotasi sumbu y 
e. tombol Z untuk rotasi sumbu y



  1. http://arsipbertuah.blogspot.co.id/2015/01/tugas-transformasi-2d-dan-3d-opengl-c.html




Contoh Projek 3D lainnya.


Baca Juga Projek OpenGL dengan C++ Lainnya  dibawah ini :

 Contoh Projek 2D openGL

Baca juga Animasi 2D untuk projek openGL lainnya dibawah ini

Dasar OpenGL

Baca juga Dasar Dari OpenGL dibawah ini jika kalian ingin menekuninya.


No comments:

Post a Comment

Tingkat keanekaragaman hayati tingkat gen

Tingkat keanekaragaman hayati tingkat gen -- Keanekaragaman hayati adalah sebuah istilah untuk menggambarkan keanekaragaman mahluk hidup di ...