Kamis, 13 Desember 2012

Camera 26 sudut openGL

Hari ini saya akan memposting cara buat sudut camera dengan menggunakan glut OpenGL.

Sebelumnya saya berterima kasih dengan Kotomo hakase  (kotomo.org) yang telah menshare ilmunya.

ini Source codenya :





 /***************************************************************************************
  *                               Deklarasi pemanggilan                                 *
  ***************************************************************************************/

#include<stdlib.h>
#include<ctype.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>


static GLfloat rot_y, rot_x;
static GLfloat bgn_y, bgn_x;
static int mouse_x, mouse_y;
static GLuint cubelist;







/***************************************************************************************
 *                               Pemanggilan layar sisplay                             *
 ***************************************************************************************/

void display_func(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);                       //warna background window
glPushMatrix();
glTranslatef(0.0, 0.0, -15.0);
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glCallList(cubelist);
glPopMatrix();
glutSwapBuffers();
}







/***************************************************************************************
 *                           Titik Koordinat pada kubus                                *
 ***************************************************************************************/

GLuint make_cube(void)
{
GLint list;

static GLfloat vert[][4]=
{

{ 3.0,  3.0,  3.0},
{ 0.0,  3.0,  3.0},
{-3.0,  3.0,  3.0},
{ 3.0,  0.0,  3.0},
{ 0.0,  0.0,  3.0},
{-3.0,  0.0,  3.0},
{ 3.0, -3.0,  3.0},
{ 0.0, -3.0,  3.0},
{-3.0, -3.0,  3.0},

{ 3.0,  3.0, -3.0},
{ 0.0,  3.0, -3.0},
{-3.0,  3.0, -3.0},
{ 3.0,  0.0, -3.0},
{ 0.0,  0.0, -3.0},
{-3.0,  0.0, -3.0},
{ 3.0, -3.0, -3.0},
{ 0.0, -3.0, -3.0},
{-3.0, -3.0, -3.0},

{ 3.0,  3.0,  0.0},
{ 0.0,  3.0,  0.0},
{-3.0,  3.0,  0.0},
{ 3.0,  0.0,  0.0},
{ 0.0,  0.0,  0.0},
{-3.0,  0.0,  0.0},
{ 3.0, -3.0,  0.0},
{ 0.0, -3.0,  0.0},
{-3.0, -3.0,  0.0},


};


static GLfloat color[][4]=
{
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 1.0, 1.0, 0.0},
{1.0, 0.0, 1.0, 0.0},
{1.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0},
};


list=glGenLists(1);
glNewList(list, GL_COMPILE);


glutSolidTeapot(1);            //membuat gambar teapot


glBegin(GL_LINES);
glColor3fv(color[6]);

glVertex3fv(vert[0]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glVertex3fv(vert[5]);
glVertex3fv(vert[6]);
glVertex3fv(vert[8]);

glVertex3fv(vert[0]);
glVertex3fv(vert[6]);
glVertex3fv(vert[2]);
glVertex3fv(vert[8]);
glVertex3fv(vert[1]);
glVertex3fv(vert[7]);



glVertex3fv(vert[9]);
glVertex3fv(vert[11]);
glVertex3fv(vert[12]);
glVertex3fv(vert[14]);
glVertex3fv(vert[15]);
glVertex3fv(vert[17]);

glVertex3fv(vert[9]);
glVertex3fv(vert[15]);
glVertex3fv(vert[11]);
glVertex3fv(vert[17]);
glVertex3fv(vert[10]);
glVertex3fv(vert[16]);


glVertex3fv(vert[18]);
glVertex3fv(vert[20]);
glVertex3fv(vert[21]);
glVertex3fv(vert[23]);
glVertex3fv(vert[24]);
glVertex3fv(vert[26]);

glVertex3fv(vert[18]);
glVertex3fv(vert[24]);
glVertex3fv(vert[20]);
glVertex3fv(vert[26]);
glVertex3fv(vert[19]);
glVertex3fv(vert[25]);


glVertex3fv(vert[0]);
glVertex3fv(vert[9]);
glVertex3fv(vert[1]);
glVertex3fv(vert[10]);
glVertex3fv(vert[2]);
glVertex3fv(vert[11]);
glVertex3fv(vert[3]);
glVertex3fv(vert[12]);
glVertex3fv(vert[4]);
glVertex3fv(vert[13]);
glVertex3fv(vert[5]);
glVertex3fv(vert[14]);
glVertex3fv(vert[6]);
glVertex3fv(vert[15]);
glVertex3fv(vert[7]);
glVertex3fv(vert[16]);
glVertex3fv(vert[8]);
glVertex3fv(vert[17]);

glEnd();

glBegin(GL_POINTS);

glColor3fv(color[0]);

glVertex3fv(vert[0]);
glVertex3fv(vert[1]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glVertex3fv(vert[4]);
glVertex3fv(vert[5]);
glVertex3fv(vert[6]);
glVertex3fv(vert[7]);
glVertex3fv(vert[8]);


glColor3fv(color[2]);
glVertex3fv(vert[9]);
glVertex3fv(vert[10]);
glVertex3fv(vert[11]);
glVertex3fv(vert[12]);
glVertex3fv(vert[13]);
glVertex3fv(vert[14]);
glVertex3fv(vert[15]);
glVertex3fv(vert[16]);
glVertex3fv(vert[17]);


glColor3fv(color[1]);
glVertex3fv(vert[18]);
glVertex3fv(vert[19]);
glVertex3fv(vert[20]);
glVertex3fv(vert[21]);
glVertex3fv(vert[22]);
glVertex3fv(vert[23]);
glVertex3fv(vert[24]);
glVertex3fv(vert[25]);
glVertex3fv(vert[26]);

glEnd();
glPointSize(10); //Size untuk tiap titik
glEndList();

return list;
}





/***************************************************************************************
 *                               fungsi buat bangun ruang                              *
 ***************************************************************************************/

void reshape_func(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
}






/***************************************************************************************
 *                               Fungsi gerak pada keyboard                            *
 ***************************************************************************************/
void skey_func(int key, int x, int y)
{
switch(key){
case GLUT_KEY_UP:
if(rot_x<90){
rot_x+=5;
}
break;
case GLUT_KEY_DOWN:
if(-90<rot_x){
rot_x-=5;
}
break;
case GLUT_KEY_LEFT:
rot_y+=5;
break;
case GLUT_KEY_RIGHT:
rot_y-=5;
break;
}

glutPostRedisplay();
}







/***************************************************************************************
 *                               Fungsi renderscene teapot                             *
 ***************************************************************************************/
void renderScene(void)
{

 glutWireTeapot(4);
 glutSwapBuffers();
}







/***************************************************************************************
 *                                   Main Function                                     *
 ***************************************************************************************/
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);    //Ukuran Layar Window
glutCreateWindow("CAMERA TEAMARC X KOTOMO");  //Nama Window yang ingin ditampilkan
glutDisplayFunc(display_func); //fungsi untuk memunculkan window
glutReshapeFunc(reshape_func);        //Fungsi Membuat garis kubus
glutSpecialFunc(skey_func); //Fungsi untuk Keyboard Arah
cubelist=make_cube();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
glLoadIdentity();


return 0;
}


Terus dicompile, dan jangan lupa simpan dengan ekstensi cpp.


Salam PETOK!!!.





Tidak ada komentar:

Posting Komentar