/*Bismillahirrohmanirrohim.. 5302413025_Fatimah_Rombel 2_Projek_Akhir_Komgraf*/
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "SOIL.h" #include
float z_pos = -5.0f; float xRot, yRot, zRot; float rot = 0.0f; //openGL yang digunakan untuk mempengaruhi lighting pada objek //terdiri dari 4 parameter yaitu RGBA GLfloat LightAmbient[] = {1.0f, .5f, 0.5f, 1.0f}; GLfloat LightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f}; GLfloat LightPosition[] = {1.0f, 50.0f, 2.0f, 1.0f};
// untuk penyimpanan teksture GLuint tex_2d; GLuint texture1; GLuint texture2; GLuint texture3; GLuint texture4; GLuint texture5; GLuint texture6; GLuint texture7; GLuint texture8; //GLuint texture[1];
//GLuint texture[2]; GLuint LoadGLTextures(); void init(); void myKeyboard(unsigned char, int, int); void myDisplay(void); void resize(int, int);
//bagian main int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(650, 650); glutInitWindowPosition(0, 0); glutCreateWindow("5302413025_Proyek_Komgraf_Ka'bah"); glutDisplayFunc(myDisplay); glutIdleFunc(myDisplay); glutKeyboardFunc(myKeyboard); glutReshapeFunc(resize);
init(); glutMainLoop(); return 0; }
//membuat gambar menjadi teksture kemudian memberikan ID pada teksture GLuint LoadGLTextures(char* namaFile) // ID openGL untuk teksture {
tex_2d = SOIL_load_OGL_texture(namaFile, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);//bagian dari penyimpanan SOIL //untuk mengecek eror saat pemrosesan berlangsung if(tex_2d == 0) { printf( "SOIL loading error: '%s'\n", SOIL_last_result() ); } glBindTexture(GL_TEXTURE_2D, tex_2d);//openGL untuk memasang teksture pd objek 2D glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); return tex_2d; }
//funggsi untuk dipanggil saat inisialisasi void init() { //melakukan inisialisasi texture1 = LoadGLTextures("samping.bmp"); texture2 = LoadGLTextures("muka.bmp"); texture3 = LoadGLTextures("atas.png"); texture4 = LoadGLTextures("kaba.bmp"); texture5 = LoadGLTextures("tanah.bmp"); texture6 = LoadGLTextures("kiri.bmp"); texture7 = LoadGLTextures("kiri.bmp"); texture8 = LoadGLTextures("kiri.bmp");
//fungsi untuk mengaktifkan pencahayaan GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 0.2 }; GLfloat mat_shininess[] = { 100.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 }; glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClearDepth(1.0f); //membersihkan layar glEnable(GL_DEPTH_TEST); glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT1); }
//fungsi interkasi keyboard terhadap opengl void myKeyboard(unsigned char key, int x, int y) { switch(key) { case '<': case ',': z_pos -= 0.1f; //posisi z -0.1 (objek mengecil) break;
case 'a': case 'A': xRot += 0.1f; //posisi kamera diata objek karena x berotasi +0.1 break; case 's': case 'S': yRot += 0.1f; // posisi kamera mendatar (dari kanan berotasi ke kiri) krn y berotasi +0.1 break; case 'd': yRot -= 0.1f; //memperlambat putaran yg berjalan otomatis break; case 'f': xRot -= 0.1f; //posisi kamera dibawah objek karena x berotasi -0.1 break; case '>': case '.': z_pos += 0.1f; //posisi objek mendekati kamera (membesar) break; case 27: exit(0); //keluar break; default: break; } }
//methode yang digunakan untuk proses tampilan layar void myDisplay(void) {
glClearColor(0.1f, 0.45f, 0.7f, 0.5f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glLoadIdentity(); glTranslatef(0.0f, 0.0f, z_pos); glRotatef(xRot,1.0f,0.0f,0.0f); // rotasi pada sumbu X glRotatef(yRot,0.0f,1.0f,0.0f); // rotasi pada sumbu Y glRotatef(zRot,0.0f,0.0f,1.0f); // rotasi pada sumbu Z glEnable(GL_LIGHTING);
//BAGIAN FUNGSI UNTUK MENGGAMBAR KA'BAH dan TEMBOK // tampilan muka glBindTexture(GL_TEXTURE_2D, texture2); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -1.0f, 1.0f);//untuk membuat koordinat tekstur pd sebuah objek glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 1.0f, 1.0f); glEnd();
// tampilan belakang glBindTexture(GL_TEXTURE_2D, texture1); glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS);
glDisable(GL_BLEND); glNormal3f( 1.0f, 1.0f,1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glEnd();
// tampilan atas glBindTexture(GL_TEXTURE_2D, texture3); glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 1.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glEnd();
// tampilan bawah glBindTexture(GL_TEXTURE_2D, texture5); glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f,1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, -1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f, -1.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -1.0f, 1.0f); glEnd();
// tampilan kanan glBindTexture(GL_TEXTURE_2D, texture1); glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// tampilan kiri glNormal3f(1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 1.0f, 0.0f); glEnd();
// tulisan glBindTexture(GL_TEXTURE_2D, texture4); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -2.0f, 0.5f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -2.0f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -0.5f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 1.0f); glEnd();
//lantai glBindTexture(GL_TEXTURE_2D, texture5); glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f,1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-10.0f, -2.0f, -8.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 10.0f, -2.0f, -8.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 10.0f, -2.0f, 8.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-10.0f, -2.0f, 8.0f); glEnd();
//tembok belakang glBindTexture(GL_TEXTURE_2D, texture6); glBegin(GL_QUADS); glDisable(GL_BLEND); glNormal3f( 1.0f, 1.0f,1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, -8.0f, -8.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-10.0f, 10.0f, -8.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0f, 10.0f, -8.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 10.0f, -8.0f, -8.0f); glEnd();
//tembok kanan glBindTexture(GL_TEXTURE_2D, texture7);
glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS); glNormal3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 10.0f, -5.0f, -8.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 10.0f, 10.0f, -8.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 10.0f, 10.0f, 8.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 10.0f, -5.0f, 8.0f); glEnd();
//tembok kiri glBindTexture(GL_TEXTURE_2D, texture8); glEnable(GL_DEPTH_TEST); glBegin(GL_QUADS); glEnable(GL_BLEND); glNormal3f(1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-10.0f, -5.0f, -8.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-10.0f, -5.0f, 8.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-10.0f, 10.0f, 8.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-10.0f, 10.0f, -8.0f); glEnd();
//untuk melakukan rotasi otomatis pada objek //xRot+=0.1f; // rotasi pada sumbu X yRot+=0.005f; // rotasi pada sumbu Y //zRot+=0.1f; // rotasi pada sumbu Z glFlush(); //untuk memastikan apakah gambar bisa dieksekusi glutSwapBuffers(); //fungsi perpindahan atau ditukar }
//fungsi yang digunakan untuk meresize objek yang sudah dibuat void resize(int width, int height) { glViewport(0, 0, width, height);//keseluruhan dr window //fungsi setup dalam pemetaan window glMatrixMode(GL_PROJECTION); glLoadIdentity(); //fungsi yg digunakan untuk merubah ukuran window yg digunakan dlm openGL //angka 100 merupakan lebar jarak terjauh dan 200 merupakan tinggi jarak terjauh gluPerspective(100.0, (GLdouble)width / (GLdouble)height, 1.0, 200.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }