Monday, November 18, 2013
Grafika Komputer Membuat Kubus Berputar dengan efek Lighting (672012213, 672012187, 672012044)
Annyeonghaseyoooooo... ^^
Oke teman2.. saya (Rosiana Kurniawan-672012213) beserta kelompok saya ( Imanuel Tomasila/Nyong--672012187 dan Kresna Prasmadewa--672012044) akan memberikan source code untuk membuat sebuah kubus berputar, dimana sisi2nya dibuat dari foto2 kami. Jujur saja, sebagian BESAR dari coding kami ini copas, copas dari source code dosen kami yg gagah dan kekar, Kak Evangs Mailoa, setelah itu baru kami edit2 sedikit.. bagi kalian yang ingin belajar banyak ttg grafkom juga mgkn bisa mohon izin ke Kak Evangs utk download2/liat2 ilmunya disini.. nah, langsung saja saya tampilkan kodingan terdahsyatt yang agak njlimett ini.. saya dan teman2 kelompok saya akan mencoba menjelaskan dengan cara memberi komentar (//) pada codingannya ya.. semoga kalian paham teman2.. :D
//INPUT LIBRARY
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "imageloader.h"
using namespace std;
const float BOX_SIZE = 7.0f; //Panjang tiap sisi kubus
float _angle = 1; //Rotasi terhadap box
GLuint _textureId; //pendeklarasian ID OpenGL untuk tekstur
GLuint _textureId2;
GLuint _textureId3;
GLuint _textureId4;
GLuint _textureId5;
GLuint _textureId6;
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Tekan Escape untuk EXIT
exit(0);
}
}
//Membuat gambar menjadi tekstur kemudian berikan ID pada tekstur
GLuint loadTexture(Image* image) {
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
return textureId;
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
//MEMASUKKAN FOTO PADA KUBUS, INGAT!! foto yg ingin dipakai harus berada dlm 1 folder dengan codingan kalian ya teman2...
Image* image = loadBMP("nyong.bmp");
Image* image2 = loadBMP("nyongker.bmp");
Image* image3 = loadBMP("rosi.bmp");
Image* image4 = loadBMP("rosiana.bmp");
Image* image5 = loadBMP("potongan.bmp");
_textureId = loadTexture(image);
_textureId2 = loadTexture(image2);
_textureId3 = loadTexture(image3);
_textureId4 = loadTexture(image4);
_textureId5 = loadTexture(image5);
delete image;
}
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (float)w / (float)h, 20.0, 100.0);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -30.0f);
GLfloat ambientLight[] = {0.1f, 0.1f, 0.1f, 1.0f}; //MEMBERI EFEK LIGHTING
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
GLfloat lightColor[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat lightPos[] = {-2 * BOX_SIZE, BOX_SIZE, 4 * BOX_SIZE, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glRotatef(-_angle, 30.0f, 40.0f, 20.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId); //perhatikan teksture_Id berapa yg ingin anda panggil (menentukan foto mana yg akan kalian pakai untuk sisi ini)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
//Sisi atas
glTexCoord2f(0.0f, 0.0f);
glNormal3f(0.0, 1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glEnd();
//glBegin(GL_QUADS);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId2); //perhatikan teksture_Id berapa yg ingin anda panggil (menentukan foto mana yg akan kalian pakai untuk sisi ini)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
//Sisi bawah
glTexCoord2f(0.0f, 0.0f);
glNormal3f(0.0, -1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glEnd();
//Sisi kiri
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId3); //perhatikan teksture_Id berapa yg ingin anda panggil (menentukan foto mana yg akan kalian pakai untuk sisi ini)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glEnd();
//Sisi kanan
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId4); //perhatikan teksture_Id berapa yg ingin anda panggil (menentukan foto mana yg akan kalian pakai untuk sisi ini)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(1.0, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glEnd();
//Sisi depan
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId5); //perhatikan teksture_Id berapa yg ingin anda panggil (menentukan foto mana yg akan kalian pakai untuk sisi ini)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, BOX_SIZE / 2);
glEnd();
//Sisi belakang
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId5); //perhatikan teksture_Id berapa yg ingin anda panggil (menentukan foto mana yg akan kalian pakai untuk sisi ini)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, BOX_SIZE / 2, -BOX_SIZE / 2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(BOX_SIZE / 2, -BOX_SIZE / 2, -BOX_SIZE / 2);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
//Panggil setiap 25ms
void update(int value) {
_angle += 1.0f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("CUBE");
initRendering();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
jadi begitulah teman2.. semoga bermanfaat, dan semoga kami mendapatkan nilai yg bagus.. :D
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment