Tampilan Output:
Kode Program:
/*
* Tanggal: Minggu, 04 November 2012 18:11:00
* Program Kamera 26 Posisi
* Dikembangkan Oleh Firmansyah Maulana
* Referensi: http://www.komoto.org/opengl/sample06.html
* Pada 20 Desember 1999 Oleh Komoto Masahiro
*
*/
#include<stdlib.h>
#include<ctype.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>
//Deklarasi Variable
static GLfloat rot_y, rot_x;
static GLfloat bgn_y, bgn_x;
static int mouse_x, mouse_y;
static GLuint cubelist;
//Fungsi Tempat Menampilkan
void display_func(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0,1.0,1.0,1.0);
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();
}
//Fungsi Membuat Lingkaran
void drawCircle( float Radius, int numPoints )
{
glBegin( GL_LINE_STRIP );
int i; float PI= 3.14;
for(i=0; i<numPoints; i++ )
{
float Angle = i * (2.0*PI/numPoints); // use 360 instead of 2.0*PI if
float X = cos( Angle )*Radius; // you use d_cos and d_sin
float Y = sin( Angle )*Radius;
glVertex2f( X, Y);
}
glEnd();
}
//Fungsi Membuat BangunRuang
GLuint make_cube(void)
{
GLint list;
//Kumpulan Titik Kordinat
static GLfloat vert[][4]={
{ 2.0, 2.0, 4.0}, //0
{ 0.0, 2.0, 4.0}, //1
{-2.0, 2.0, 4.0}, //2
{ 2.0, 0.0, 4.0}, //3
{ 0.0, 0.0, 4.0}, //4
{-2.0, 0.0, 4.0}, //5
{ 2.0, -2.0, 4.0}, //6
{ 0.0, -2.0, 4.0}, //7
{-2.0, -2.0, 4.0}, //8
{ 2.0, 2.0, -4.0}, //9
{ 0.0, 2.0, -4.0}, //10
{-2.0, 2.0, -4.0}, //11
{ 2.0, 0.0, -4.0}, //12
{ 0.0, 0.0, -4.0}, //13
{-2.0, 0.0, -4.0}, //14
{ 2.0, -2.0, -4.0}, //15
{ 0.0, -2.0, -4.0}, //16
{-2.0, -2.0, -4.0}, //17
{ 2.0, 2.0, 0.0}, //18
{ 0.0, 2.0, 0.0}, //19
{-2.0, 2.0, 0.0}, //20
{ 2.0, 0.0, 0.0}, //21
{ 0.0, 0.0, 0.0}, //22
{-2.0, 0.0, 0.0}, //23
{ 2.0, -2.0, 0.0}, //24
{ 0.0, -2.0, 0.0}, //25
{-2.0, -2.0, 0.0}, //26
//{ 0.0, 0.0, 0.0}, //Titik Tengah
};
//Kumpulan Warna
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);
//Memanggil Fungsi Membuat Lingkaran
drawCircle(1, 360);
//Membuat Rangka BangunRuang
glBegin(GL_LINES);
glColor3fv(color[6]);
//Merah
//Garis Horizontal
glVertex3fv(vert[0]);
glVertex3fv(vert[2]);
glVertex3fv(vert[3]);
glVertex3fv(vert[5]);
glVertex3fv(vert[6]);
glVertex3fv(vert[8]);
//Garis Vertikal
glVertex3fv(vert[0]);
glVertex3fv(vert[6]);
glVertex3fv(vert[2]);
glVertex3fv(vert[8]);
glVertex3fv(vert[1]);
glVertex3fv(vert[7]);
//Biru
//Garis Horizontal
glVertex3fv(vert[9]);
glVertex3fv(vert[11]);
glVertex3fv(vert[12]);
glVertex3fv(vert[14]);
glVertex3fv(vert[15]);
glVertex3fv(vert[17]);
//Garis Vertikal
glVertex3fv(vert[9]);
glVertex3fv(vert[15]);
glVertex3fv(vert[11]);
glVertex3fv(vert[17]);
glVertex3fv(vert[10]);
glVertex3fv(vert[16]);
//Hijau
//Garis Horizontal
glVertex3fv(vert[18]);
glVertex3fv(vert[20]);
glVertex3fv(vert[21]);
glVertex3fv(vert[23]);
glVertex3fv(vert[24]);
glVertex3fv(vert[26]);
//Garis Vertikal
glVertex3fv(vert[18]);
glVertex3fv(vert[24]);
glVertex3fv(vert[20]);
glVertex3fv(vert[26]);
glVertex3fv(vert[19]);
glVertex3fv(vert[25]);
//Garis Penghubung
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);
//Titik Merah
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]);
//Titik Biru
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]);
//Titik Hijau
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(5); //Ukuran Titik
glEndList();
return list;
}
//Fungsi Mengatur BangunRuang
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 Mouse Ketika di-Klik
void mouse_func(int button, int stat, int x, int y)
{
if(GLUT_DOWN==stat && GLUT_LEFT==button){
mouse_x=x;
mouse_y=y;
bgn_y=rot_y;
bgn_x=rot_x;
}
}
//Fungsi Ketika di-Drag
void drag_func(int x, int y)
{
rot_x=(GLfloat)(y-mouse_y)+bgn_x;
rot_y=(GLfloat)(x-mouse_x)+bgn_y;
if(90<rot_x)
{
rot_x=90;
}
if(rot_x<-90)
{
rot_x=-90;
}
glutPostRedisplay();
}
//Fungsi Ketika Keyboard 'Q' di Tekan
void key_func(unsigned char key, int x, int y)
{
switch(toupper(key)){
case 0x1b:
case 'Q':
exit(0);
break;
}
}
//Fungsi Ketika Keyboard Arah di Tekan
void skey_func(int key, int x, int y)
{
switch(key){
case GLUT_KEY_UP:
if(rot_x<90){
rot_x+=10;
}
break;
case GLUT_KEY_DOWN:
if(-90<rot_x){
rot_x-=10;
}
break;
case GLUT_KEY_LEFT:
rot_y+=10;
break;
case GLUT_KEY_RIGHT:
rot_y-=10;
break;
}
glutPostRedisplay();
}
//Fungsi Utama
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500); //Ukuran Layar Window
glutCreateWindow("UTS | 26 Posisi Kamera - Firmansyah Maulana"); //Nama Judul Window
glutDisplayFunc(display_func); //Ambil Fungsi Tempat Menampilkan
glutReshapeFunc(reshape_func); //Ambil Fungsi Mengatur BangunRuang
glutKeyboardFunc(key_func); //Ambil Fungsi Ketika Keyboard 'Q' di Tekan
glutSpecialFunc(skey_func); //Ambil Fungsi Ketika Keyboard Arah di Tekan
glutMotionFunc(drag_func); //Ambil Fungsi Ketika di-Drag
glutMouseFunc(mouse_func); //Ambil Fungsi Ketika di-Klik
glEnable(GL_DEPTH_TEST);
cubelist=make_cube();
glutMainLoop();
return 0;
}
Tidak ada komentar:
Posting Komentar