kumpulan program grafkom

26
Nama : Fakhrurrozi NIM : 10018172 Jam praktikum:Kamis jam 09.00-10.30 KUMPULAN PROGRAM POSTEST PRAKTIKUM 1-10 GRAFIKA KOMPUTER 1. PRAKTIKUM 1 (Membuat Kotak) /* praktikum 01 *pengenalan openGL */ #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <glut.h> void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); //menghapus layar glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glVertex2f(-0.10, -0.5); glVertex2f(-0.5, -0.10); glVertex2f(-0.10, -0.10); glVertex2f(0.10, -0.5); glEnd();

Upload: dabit-psim

Post on 26-Oct-2014

90 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: Kumpulan Program Grafkom

Nama : Fakhrurrozi

NIM : 10018172

Jam praktikum:Kamis jam 09.00-10.30

KUMPULAN PROGRAM

POSTEST PRAKTIKUM 1-10 GRAFIKA KOMPUTER

1. PRAKTIKUM 1 (Membuat Kotak)

/* praktikum 01*pengenalan openGL*/

#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <glut.h>

void mydisplay(){

glClear(GL_COLOR_BUFFER_BIT); //menghapus layarglBegin(GL_POLYGON);glVertex2f(-0.5, -0.5);glVertex2f(-0.5, 0.5);glVertex2f(0.5, 0.5);glVertex2f(0.5, -0.5);

glVertex2f(-0.10, -0.5);glVertex2f(-0.5, -0.10);glVertex2f(-0.10, -0.10);glVertex2f(0.10, -0.5);

glEnd();glFlush();

}

int main(int argc, char** argv){

printf("contoh sederhana kotak");glutCreateWindow("praktikum 01");glutDisplayFunc(mydisplay);

Page 2: Kumpulan Program Grafkom

glutMainLoop();}

2. PRAKTIKUM 2

#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <glut.h>#define drawOneLine(x1,y1,x2,y2) glBegin(GL_LINES); glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();void init(void){

glClearColor(0.0, 0.0, 0.0, 0.0);glShadeModel (GL_FLAT);

}void display(void){

int i;glClear (GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);glEnable(GL_LINE_STIPPLE);glLineStipple(1, 0x00FF);drawOneLine (150.0, 125.0, 300.0, 125.0);glLineStipple(1, 0x00FF);drawOneLine (300.0, 125.0, 300.0, 10.0);glLineStipple(1, 0x00FF);drawOneLine (150.0, 125.0, 150.0, 10.0);glLineStipple(1, 0x00FF);drawOneLine (150.0, 10.0, 300.0, 10.0);

Page 3: Kumpulan Program Grafkom

glDisable(GL_LINE_STIPPLE);glFlush();

}void reshape (int w, int h){

glViewport(0,0,(GLsizei) w,(GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity ();gluOrtho2D (0.0, (GLdouble) w, 0.0,(GLdouble) h);

}int main(int argc, char** argv){

glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(400,150);glutInitWindowPosition(100,100);glutCreateWindow(argv[0]);init ();glutDisplayFunc(display);glutReshapeFunc(reshape);glutMainLoop();return 0;

}

3. PRAKTIKUM 3

/* Praktikum 03 * Membuat objek primitif : poligon */

#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <glut.h>

Page 4: Kumpulan Program Grafkom

void display(void) { GLubyte fly[] = { 0x0f, 0xc0, 0x00, 0x00, 0x0f, 0xc0, 0x01, 0xC0, 0x0f, 0xc0, 0x06, 0x20, 0xc0, 0xc0, 0x18, 0x20, 0xc0, 0xc0, 0x60, 0x20, 0xc0, 0xc0, 0x80, 0x22, 0xc0, 0xc0, 0x80, 0x22, 0xc0, 0xc0, 0x80, 0x22, 0xc0, 0xc0, 0x80, 0x66, 0xc0, 0xc0, 0x81, 0x98, 0xc0, 0xc0, 0x87, 0xe0, 0xc0, 0xc0, 0x8c, 0xc0, 0xc0, 0xc0, 0x26, 0x60, 0xc0, 0xfc, 0x33, 0x18, 0xc0, 0xfc, 0xC6, 0x08, 0xc0, 0xfc, 0x18, 0x08; GLubyte halftone[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55}; glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); /* draw one solid, unstippled rectangle, */ /* then two stippled rectangles */ glRectf (25.0, 25.0, 125.0, 125.0); glEnable (GL_POLYGON_STIPPLE); glPolygonStipple (fly); glRectf (125.0, 25.0, 225.0, 125.0); glPolygonStipple (halftone); glRectf (225.0, 25.0, 325.0, 125.0); glDisable (GL_POLYGON_STIPPLE); glFlush (); } void init (void)

Page 5: Kumpulan Program Grafkom

{ glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (350, 150); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }

4. PRAKTIKUM 4

/* Praktikum 04* Membuat objek garis dengan DDA dan Bresenham*/

#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <glut.h>#include <math.h>

void display(void){

Page 6: Kumpulan Program Grafkom

//set display-window background color to whiteglClearColor(1.0,1.0,1.0,0.0);//set projection parametersglMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,300.0,0.0,300.0);}void setPixel (GLint xCoordinate, GLint yCoordinate){glBegin (GL_POINTS);glVertex2i (xCoordinate, yCoordinate);glEnd();glFlush();}//Procedure Bresenham line-drawing untuk |m| < 1.0void lineBres (GLint x0, GLint y0, GLint xEnd, GLint yEnd){GLint dx = (float) fabs ((float) xEnd - x0);GLint dy = (float) fabs ((float) yEnd - y0);GLint p = 2*dy-dx;GLint twoDy = 2*dy;GLint twoDyMinusDx = 2*(dy-dx);GLint x,y;//determine which endpoint to use as start positionif(x0 > xEnd){x=xEnd;y=yEnd;xEnd=x;}else{x=x0;y=y0;}setPixel(x,y);while(x<xEnd){x++;if(p<0)p+=twoDy;else{y++;p+=twoDyMinusDx;}setPixel(x,y);}}void drawMyLine(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,0.0);glPointSize(4.0);GLint x0 = 50;GLint y0 = 100;

Page 7: Kumpulan Program Grafkom

GLint xEnd = 100;GLint yEnd = 150;lineBres(x0,y0,xEnd,yEnd);

glColor3f(1.0,0.0,0.0);glPointSize(4.0);GLint x1 = 50;GLint y1 = 100;GLint x2 = 150;GLint y2 = 150;lineBres(x1,y1,x2,y2);

glColor3f(0.0,2.0,0.0);glPointSize(4.0);GLint x3 = 150;GLint y3 = 150;GLint x4 = 200;GLint y4 = 200;lineBres(x3,y3,x4,y4);

glColor3f(0.0,0.0,3.0);glPointSize(4.0);GLint x5 = 100;GLint y5 = 150;GLint x6 = 200;GLint y6 = 200;lineBres(x5,y5,x6,y6);

}int main(int argc, char** argv){//initialize GLUTglutInit(&argc,argv);//initialize display modeglutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//set display-window width & heightglutInitWindowSize(400,400);//set display-window upper-left positionglutInitWindowPosition(0,0);//create diplay-window with a titleglutCreateWindow("Digital Differential Analyzer Algorithm");//initialize OpenGLdisplay();//call graphics to be displayed on the windowglutDisplayFunc(drawMyLine);//display everything and waitglutMainLoop();return 0;}

Page 8: Kumpulan Program Grafkom

5. PRAKTIKUM 5

#include <iostream>#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <glut.h> #include<math.h> using namespace std; typedef unsigned char uchar; // number of line segmentsstatic int num_lines = 20;static int num_liness = 4;

// callback prototypesvoid disp(void);void keyb(uchar k, int x, int y);void reshape(int x, int y); // mainint main(int argc, char **argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow("circle.cpp"); glClearColor(0.0,0.0,0.0,0.0); glutDisplayFunc(disp); glutKeyboardFunc(keyb); glutReshapeFunc(reshape); glutMainLoop(); return 0;

Page 9: Kumpulan Program Grafkom

} // dispvoid disp(void){ double angle; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINE_LOOP); for(int i =0;i<num_lines;i++){ angle = i*2*3.14/num_lines; glVertex2f(cos(angle),sin(angle)); } glEnd();

glBegin(GL_LINE_LOOP); for(int i =0;i<num_lines;i++){ angle = i*2*3.14/num_lines; glVertex2f(cos(angle)/2,sin(angle)/2); } glEnd();

glBegin(GL_LINE_LOOP); for(int i =0;i<num_lines;i++){ angle = i*2*3.14/num_lines; glVertex2f(cos(angle)/3,sin(angle)/3); } glEnd();

glBegin(GL_LINE_LOOP); for(int i =0;i<num_lines;i++){ angle = i*2*3.14/num_lines; glVertex2f(cos(angle)/4,sin(angle)/4); } glEnd(); glutSwapBuffers();}// keybvoid keyb(uchar k, int x, int y){ switch (k){ case 'q': exit(0); break; case '+': if(num_lines < 99){ num_lines++; cout << "Circle consists of " << num_lines << " lines " << endl; glutPostRedisplay(); } break; case '-': if(num_lines >3){

Page 10: Kumpulan Program Grafkom

num_lines--; cout << "Circle consists of " << num_lines << " lines " << endl; glutPostRedisplay(); } break; }} // reshapevoid reshape(int x,int y){ if(x<y) glViewport(0,(y-x)/2,x,x); else glViewport((x-y)/2,0,y,y);}

6. PRAKTIKUM 6

#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <glut.h>

void mydisplay(){

glClear(GL_COLOR_BUFFER_BIT); //menghapus layarglMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();

Page 11: Kumpulan Program Grafkom

glBegin(GL_POLYGON);glColor3f(1.0f, 0.0f, 0.0);glVertex3f(-0.1f, -0.1f, 0.0f);glVertex3f(-0.1f, 0.1f, 0.0f);glVertex3f(0.1f, 0.1f, 0.0f);glVertex3f(0.1f, -0.1f, 0.0f);glEnd();

glPopMatrix();glutSwapBuffers();

glFlush;}int main(int argc, char** argv){

glutInitWindowSize(400, 400);glutCreateWindow("Praktikum06");glutDisplayFunc(mydisplay);glutMainLoop();

}

7. PRAKTIKUM 7

#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <glut.h> #include <glu.h>

GLsizei winWidth = 500, winHeight = 500;

void init(void)

Page 12: Kumpulan Program Grafkom

{glClearColor(1.0, 1.0, 1.0, 0.0);glShadeModel(GL_SMOOTH);

}

void displayWirePolyhedra(void){

// Set up the projection parametersglMatrixMode(GL_PROJECTION);glLoadIdentity();glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);

// Set up the position of our virtual cameraglMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Clear the windowglClear(GL_COLOR_BUFFER_BIT);

// Draw a wireframe cubeglColor3f(0.0, 1.0, 0.0); // Set colour to blueglTranslatef(2.0, 0.0, 0.0);//glScalef(2.0, 2.0, 2.0);glutWireCube(1.0);

glColor3f(1.0, 1.0, 0.0); // Set colour to blueglTranslatef(-2.0, 0.0, 0.0);//glScalef(1.5, 1.0, 1.0);glRotatef(45, 0.0, 0.0, 1.0);glutWireCube(1.0);

glColor3f(0.0, 1.0, 1.0); // Set colour to blueglTranslatef(0.0, 0.0, 2.0);//glScalef(1.5, 1.0, 1.0);glRotatef(45, 1.0, 0.0, 0.0);glutWireCube(1.0);

glFlush();}

// Resize the viewport when the window is resizedvoid winReshapeFcn(GLint newWidth, GLint newHeight){

glViewport(0, 0, newWidth, newHeight);glClear(GL_COLOR_BUFFER_BIT);

} int main(int argc, char** argv) { printf("Contoh Sederhana Kubus");

Page 13: Kumpulan Program Grafkom

glutCreateWindow("Praktikum07"); glutDisplayFunc(displayWirePolyhedra); glutMainLoop(); }

8. PRAKTIKUM 8

#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <glut.h> #include <glu.h>

void initRendering() {glEnable(GL_DEPTH_TEST);glEnable(GL_COLOR_MATERIAL);glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH);

}

void handleResize(int w, int h) {glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0, (double)w / (double)h,7.0, 100.0);glMatrixMode (GL_MODELVIEW);

}

void drawScene() {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();

Page 14: Kumpulan Program Grafkom

glTranslatef(0.0f, 0.0f, -8.0f);

GLfloat ambientColor[] = {0.0f, 0.0f, 0.0f, 1.0f}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

GLfloat lightColor0[] = {1.0f, 1.0f, 2.0f, 2.0f}; GLfloat lightPos0[] = {3.0f, 10.0f, 2.0f, 4.0f}; glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);gluLookAt (2.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);//glScalef (2.0, 2.0, 2.0); glutSolidCube(1.0);

glTranslatef(1.0, 1.0, 0.0); //glScalef (2.0, 2.0, 2.0);

glutSolidCube(1.0);

glTranslatef(-2.0, 0.0, 0.0); //glScalef (2.0, 2.0, 2.0);

glutSolidCube(1.0);

glTranslatef(1.0, 0.0, 0.0); //glScalef (2.0, 2.0, 2.0);

glutSolidCube(1.0);

glTranslatef(0.0, 1.0, 0.0); //glScalef (2.0, 2.0, 2.0);

glutSolidCube(1.0);

glFlush ();glEnd();

glutSwapBuffers();

}

int main(int argc, char** argv) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(400, 400);glutCreateWindow("praktikum_8");initRendering();glutDisplayFunc(drawScene);glutReshapeFunc(handleResize);glutMainLoop();return 0;

}

Page 15: Kumpulan Program Grafkom

9. PRAKTIKUM 9

#include <windows.h> #include <iostream>#include <stdlib.h>#include <stdio.h> #include <string.h> #include <stdarg.h> #include <glut.h> #include <glu.h>

void initRendering() {glEnable(GL_DEPTH_TEST);glEnable(GL_COLOR_MATERIAL);glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_NORMALIZE); glShadeModel(GL_SMOOTH);

}

void handleResize(int w, int h) {glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);

}

float _angle = 60.0f;

void drawScene() {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);

Page 16: Kumpulan Program Grafkom

glLoadIdentity();glTranslatef(0.0f, 0.0f, -8.0f);

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

//radiusGLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f};

GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

glRotatef(_angle, 0.0f, 1.0f, 0.0f);

glBegin(GL_QUADS);

//FrontglColor3f(1.0f, 1.0f, 0.0f);glVertex3f(-1.5f, -1.0f, 1.5f);glVertex3f(1.5f, -1.0f, 1.5f);glVertex3f(1.5f, 1.0f, 1.5f);glVertex3f(-1.5f, 1.0f, 1.5f);

//biruglColor3f(0.0f, 0.0f, 1.0f);glVertex3f(1.5f, -1.0f, 1.5f);glVertex3f(1.5f, -1.0f, -1.5f);glVertex3f(1.5f, 1.0f, -1.5f);glVertex3f(1.5f, 1.0f, 1.5f);

//merahglColor3f(1.0f, 0.0f, 0.0f);glVertex3f(1.5f, 1.0f, -1.5f);glVertex3f(-1.5f,1.0f, -1.5f);glVertex3f(-1.5f, -1.0f, -1.5f);glVertex3f(1.5f, -1.0f, -1.5f);

//unguglColor3f(1.0f, 0.0f, 1.0f);glVertex3f(-1.5f, 1.0f, -1.5f);glVertex3f(-1.5f, 1.0f, 1.5f);glVertex3f(-1.5f, -1.0f, 1.5f);glVertex3f(-1.5f, -1.0f, -1.5f);

glEnd();

Page 17: Kumpulan Program Grafkom

glutSwapBuffers();}

void update(int value) {_angle += 1.5f;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("Lighting");initRendering();glutDisplayFunc(drawScene);glutReshapeFunc(handleResize);glutTimerFunc(25, update, 0);

glutMainLoop();return 0;

}

Page 18: Kumpulan Program Grafkom

10. PRAKTIKUM 10

/* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above notice and this permission notice shall be included in all copies * or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *//* File for "Textures" lesson of the OpenGL tutorial on * www.videotutorialsrock.com */

#include <iostream>#include <stdlib.h>

#ifdef __APPLE__#include <OpenGL/OpenGL.h>#include <GLUT/glut.h>#else#include <glut.h>#endif

#include "imageloader.h"

using namespace std;

Page 19: Kumpulan Program Grafkom

void handleKeypress(unsigned char key, int x, int y) {switch (key) {

case 27: //Escape keyexit(0);

}}

//Makes the image into a texture, and returns the id of the textureGLuint loadTexture(Image* image) {

GLuint textureId;glGenTextures(1, &textureId); //Make room for our textureglBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which

texture to edit//Map the image to the textureglTexImage2D(GL_TEXTURE_2D, //Always

GL_TEXTURE_2D 0, //0 for now GL_RGB, //Format

OpenGL uses for image image->width, image->height, //Width and

height 0, //The border

of the image GL_RGB, //GL_RGB, because pixels are stored

in RGB format GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE,

because pixels are stored //as unsigned numbers image->pixels); //The actual

pixel datareturn textureId; //Returns the id of the texture

}

GLuint _textureId; //The id of the texture

void initRendering() {glEnable(GL_DEPTH_TEST);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_NORMALIZE);glEnable(GL_COLOR_MATERIAL);

Image* image = loadBMP("vtr.bmp");_textureId = loadTexture(image);delete image;

}

void handleResize(int w, int h) {glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();

Page 20: Kumpulan Program Grafkom

gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);}

void drawScene() {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

glTranslatef(0.0f, 1.0f, -6.0f);

GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

GLfloat directedLight[] = {0.7f, 0.7f, 0.7f, 1.0f};GLfloat directedLightPos[] = {-10.0f, 15.0f, 20.0f, 0.0f};glLightfv(GL_LIGHT0, GL_DIFFUSE, directedLight);glLightfv(GL_LIGHT0, GL_POSITION, directedLightPos);

glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, _textureId);

//BottomglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,

GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,

GL_NEAREST);//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,

GL_LINEAR);//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,

GL_LINEAR);glColor3f(1.0f, 0.2f, 0.2f);glBegin(GL_QUADS);

glNormal3f(0.0, 1.0f, 0.0f);glTexCoord2f(0.0f, 0.0f);glVertex3f(-2.5f, -2.5f, 2.5f);glTexCoord2f(1.0f, 0.0f);glVertex3f(2.5f, -2.5f, 2.5f);glTexCoord2f(1.0f, 1.0f);glVertex3f(2.5f, -2.5f, -2.5f);glTexCoord2f(0.0f, 1.0f);glVertex3f(-2.5f, -2.5f, -2.5f);

glEnd();

//BackglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,

GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,

GL_LINEAR);

Page 21: Kumpulan Program Grafkom

glColor3f(1.0f, 1.0f, 1.0f);glBegin(GL_TRIANGLES);

glNormal3f(0.0f, 0.0f, 1.0f);glTexCoord2f(0.0f, 0.0f);glVertex3f(-2.5f, -2.5f, -2.5f);glTexCoord2f(5.0f, 5.0f);glVertex3f(0.0f, 2.5f, -2.5f);glTexCoord2f(10.0f, 0.0f);glVertex3f(2.5f, -2.5f, -2.5f);

glEnd();

//LeftglDisable(GL_TEXTURE_2D);glColor3f(1.0f, 0.7f, 0.3f);glBegin(GL_QUADS);

glNormal3f(1.0f, 0.0f, 0.0f);glVertex3f(-2.5f, -2.5f, 2.5f);glVertex3f(-2.5f, -2.5f, -2.5f);glVertex3f(-2.5f, 2.5f, -2.5f);glVertex3f(-2.5f, 2.5f, 2.5f);

//kotakglVertex2f(-0.5, -0.5);glVertex2f(-0.5, 0.5);glVertex2f(0.5, 0.5);glVertex2f(0.5, -0.5);

glVertex2f(-0.10, -0.5);glVertex2f(-0.5, -0.10);glVertex2f(-0.10, -0.10);glVertex2f(0.10, -0.5);

glEnd();

glutSwapBuffers();}

int main(int argc, char** argv) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(400, 400);

glutCreateWindow("Textures");initRendering();

glutDisplayFunc(drawScene);glutKeyboardFunc(handleKeypress);glutReshapeFunc(handleResize);

Page 22: Kumpulan Program Grafkom

glutMainLoop();return 0;

}