Nurhanimohdriza89
12 Oktober 2011 telah menyertai
#include <GL/glut.h> /* baris untuk masukkan header files */
GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /*Red light diffuse ,Merah cahaya resap.*/
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Location of light an infinite,Lokasi cahaya yang tidak terhingga*/
GLfloat n[6][3] = { /* Normals for the 6 faces of a cube, Untuk 6 muka kiub.*/ {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube ,Indeks simpul untuk 6 muka kiub.*/ {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4}, {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3]; /*Will be filled in with X,Y,Z vertexes, Akan diisi dengan X, Y, Z vertexes.*/
void drawBox(void) /* untuk membuat segi empat tepat */ { int i; for (i = 0; i < 6; i++) { glBegin(GL_QUADS); /* Draw A Quad */ glNormal3fv(&n[i][0]); glVertex3fv(&v[faces[i][0]][0]); /* Top,Atas */ glVertex3fv(&v[faces[i][1]][0]); /* Bottom Left,Kiri Bawah */ glVertex3fv(&v[faces[i][2]][0]); /* Bottom Right,Kanan BAwah */ glVertex3fv(&v[faces[i][3]][0]); /* below the surface of the box, bawah permukaan box */ glEnd(); /* Finished Drawing The Cube, Selesai Lukisan Cube */ } }
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* clear color buffer and depth buffer,warna dan kedalaman yang jelas */ drawBox(); glutSwapBuffers(); /*swap the buffers */ }
void init(void) { /* Setup cube vertex data,Persediaan kiub bucu data. */ v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1; v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1; v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1; v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1; v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1; v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
/* Enable a single OpenGL light, Membolehkan cahaya OpenGL tunggal.*/ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); /* Specify the color of light, Tentukan warna cahaya */ glLightfv(GL_LIGHT0, GL_POSITION, light_position); /* Specify the position of light source, Tentukan kedudukan sumber cahaya */ glEnable(GL_LIGHT0); /*Enable LIGHT0, our Diffuse Light , Membolehkan LIGHT0 tersebar*/ glEnable(GL_LIGHTING); / * Enable lighting,Membolehkan pencahayaan * /
/* Use depth buffering for hidden surface elimination,Gunakan buffering mendalam bagi penghapusan permukaan tersembunyi.*/ glEnable(GL_DEPTH_TEST);
/* Setup the view of the cube,Persediaan pandangan kiub.*/ glMatrixMode(GL_PROJECTION); /* Applies subsequent matrix operations to the projection matrix stack,operasi matriks kepada susunan matriks unjuran */ gluPerspective( /* Field of view in degree ,Bidang pandangan dalam ketinggian */ 40.0, /* aspect ratio */ 1.0, /* Z near */ 1.0, /* Z far */ 10.0);
glMatrixMode(GL_MODELVIEW); /* Applies subsequent matrix operations to the modelview matrix stack,operasi matriks kepada susunan matriks pandangan model */ gluLookAt(0.0, 0.0, 5.0, /* Eye is at (0,0,5) ,mata adalah pada (0,0,5)*/ 0.0, 0.0, 0.0, /* Center is at (0,0,0) ,pertengahan di (0,0,0)*/ 0.0, 1.0, 0.); /* Up is in positive Y direction ,adalah ke arah Y yang positif */
/* Adjust cube position to be asthetic angle,Laraskan kedudukan kiub untuk menjadi sudut estetik.*/ glTranslatef(0.0, 0.0, -1.0); glRotatef(60, 1.0, 0.0, 0.0); glRotatef(-20, 0.0, 0.0, 1.0); }
int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); /* set the display to Double buffer , set display kepada double buffer */ glutCreateWindow("red 3D lighted cube"); glutDisplayFunc(display); /* use the display function to draw everything , menggunakan fungsi display untuk melukis */ init(); /* call init function,memanggil fungsi INIT */ glutMainLoop(); /* call the main loop, memanggil gelung utama */ return 0; /* ANSI C requires main to return int , ANSI C memerlukan main untuk kembali int.*/ }
-
output