grafkom program baru buka lagi

30
Contoh syntax dasar membuat project atau program dengan glut di Dev C++ : #include<GL/glut.h> void display(void) { glClear(GL_COLOR_BUFFER_BIT); //membuat persegi dari glBegin…glEnd. glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0);glVertex2f(-0.5,0.5); glColor3f(0.0,1.0,0.0);glVertex2f(0.5,0.5); glColor3f(0.0,0.0,1.0);glVertex2f(0.5,-0.5); glColor3f(1.0,1.0,1.0);glVertex2f(-0.5,-0.5); glEnd(); glFlush(); } int main(int argc, char **argv) { //Inisialisasi GLUT glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //Membuat window glutInitWindowSize(250,250); //ukuran window glutCreateWindow(“Project1″); // nama window glClearColor(0.0, 0.0, 0.0, 0.0); // warna dasar window R G B A //Pemanggilan terhadap fungsi display & reshape glutDisplayFunc(display); //GLUT melakukan perulangan //untuk menunggu perintah glutMainLoop(); return 0; }

Upload: asep-arilo-rinaldo

Post on 02-Apr-2015

443 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: grafkom program baru buka lagi

Contoh syntax dasar membuat project atau program dengan glut di Dev C++ :

#include<GL/glut.h>

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

//membuat persegi dari glBegin…glEnd.

glBegin(GL_POLYGON);

glColor3f(1.0,0.0,0.0);glVertex2f(-0.5,0.5);

glColor3f(0.0,1.0,0.0);glVertex2f(0.5,0.5);

glColor3f(0.0,0.0,1.0);glVertex2f(0.5,-0.5);

glColor3f(1.0,1.0,1.0);glVertex2f(-0.5,-0.5);

glEnd();

glFlush();

}

int main(int argc, char **argv)

{

//Inisialisasi GLUT

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

//Membuat window

glutInitWindowSize(250,250); //ukuran window

glutCreateWindow(“Project1″); // nama window

glClearColor(0.0, 0.0, 0.0, 0.0); // warna dasar window R G B A

//Pemanggilan terhadap fungsi display & reshape

glutDisplayFunc(display);

//GLUT melakukan perulangan

//untuk menunggu perintah

glutMainLoop();

return 0;

}

Page 2: grafkom program baru buka lagi

OpenGL menggunakan GLUT di linux dengan bahasa CPosted on October 12, 2010 by abi71

Gue dapet tugas kampus buat bikin program Graphic pake OpenGL. pilihan

bahasa program bebas, kenapa bebas ? menurut sepengetahuan gw karena

OpenGL memang bukan bahasa program tapi sebuah library portable yang

siap pakai di bahasa pemrograman(tidak semua). Karena bebas akhirnya

pilihan jatuh pada bahasa C.

oke pertama install semua pustaka yang di butuhkan.

1root@bt:~# apt-get install freeglut3 freeglut3-dev libglew1.5 libglew1.5-dev libglu1-mesa libglu1-mesa-dev libgl1-mesa-glx libgl1-mesa-dev

jika sudah, copas contoh coding di bawah ini.

#include <GL/glut.h>

void gambar(void) {

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_LINES);

07

    glVertex2f (0.0, 0.8);

08

    glVertex2f (0.2,

Page 3: grafkom program baru buka lagi

0.10);

11

    glVertex2f (0.0, 0.8);

12

    glVertex2f (-0.2, 0.10);

15

    glVertex2f (0.0, 0.8);

16

    glVertex2f (0.8, 0.8);

19

    glVertex2f (0.8, 0.8);

20

    glVertex2f (0.8, 0.10);

    glFlus

Page 4: grafkom program baru buka lagi

h();

}

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

    glutInitWindowPosition(500,500);

    glutInitWindowSize(300,300);

    glutCreateWindow(argv[0]);

    glutDisplayFunc(gambar);

    glutMainLoop();

}

Running..

Page 5: grafkom program baru buka lagi

1gcc -lglut namaprogram.c -o namaprogram

Referensi :

http://www.google.com

http://www.lighthouse3d.com/

1.2.2.Pembuatan gambar

Didalam openGL pembuatan obyek dilakukan dengan titik-titik 3 dimensi,denganmode GL_QUARDS, maka otomatis setiap 4 titik digambar menjadi sebuah bidang segiempat,sedangkan mode GL_LINES, pada setiap 2 titik digambar manjadi sebuah garis.Didalam tahap ini setiap garis atau bidang juga dapat di atur warnanya.Funsi atau prosedur yang digunakan :mode GL_QUARDS – menggambarkan segi empatmode GL_LINES – menggambark garisglVertex3f- penentuan titik 3 dimensiglColor3f – penentuan warna

Berikut ini akan diberikan contoh script sederhana (tidak utuh,hanya sepengggaalan)beserta output yang dihasilkan,untuk menunjukkan proses pembuatan gambar atau obyekpada delphi,dengan library tambahan open GL.

Contoh pembuatan titik 3 warna :glClearColor(1,1,1,0); //warna dasar ;1,1,1,0 adalah putihglBegin(GL_POINTS); //untuk membuat titikglColor3f(1,0,0); //penentuan warna titik 1 dan posisinyaglVertex3f(-0.1,-0.1,0.1);glColor3f(0,1,0); //penentuan warna titik 2 dan posisisyaglVertex3f(0.1,-0.1,0.1);glColor3f(0,0,1); //penentuan titik 3 dan posisinyaglColor3f(0,0.1,-0.1);glEnd;

Contoh pembuatan garis :glClearColor(1,1,1,0); // warna background putihglColor3f(0,0,0); // warna garis hitam

Page 6: grafkom program baru buka lagi

glBegin(GL_LINES); //digunakan untuk membua garis tiap 2 titik yang adaglVertex3f(0,0,-0.1); //garis 1glVertex3f(0.3,0.2,0.1);glVertex3f(0.1,-0.1,-0.2); // garis 2glVertex3f(0.3,-0.2,0.1);glEnd;

Contoh pembuatan bidang segi empat :glClearColor(1,1,1,0); // warna background putihglBegin(GL_QUARDS); //untuk membuat segi empat dalam tiap4 titikglColor3f(1,0,0); //posisi titik 1 dengan gradiasi warnanyaglVertex3f(-0.2,0.2,0.2);glColor3f(1,0,0); //posisi titik 2 dengan gradiasi warnanyaglVertex3f(0.2,0.2,0.2);glColor3f(1,0,1); //posisi titik 3 dengan gradiasi warnanyaglVertex3f(0.2,-0.2,0.2);glColor3f(0,1,1); //posisi titik 4 dengan gradiasi warnanyaglVertex3f(-0.2,-0.2,0.2);glEnd;

Contoh gambar 3D menggunaan openGL pada delphi :

program BasicDelphi;

usesWindows,Messages,OpenGL;

varHGLRC_VarG : HGLRC;HDC_VarG : HDC;HWND_VarG : HWND;

keys : array [0..255] of bool;SudutPutar : single = 0.0;

//set ukuran window OpenGLLebarW : integer = 350;TinggiW : integer = 350;

function WindowProc(HWND_P : HWND; MESSAGE_P : UINT; WPARAM_P :WPARAM; LPARAM_P : LPARAM) : LRESULT; stdcall;begincase MESSAGE_P ofWM_KEYUP:

Page 7: grafkom program baru buka lagi

beginkeys[WPARAM_P] := false;result:=0;end;

WM_KEYDOWN:beginkeys[WPARAM_P] := true;result:=0;end;

end;Result := DefWindowProc(HWND_P, MESSAGE_P, WPARAM_P, LPARAM_P);end;

procedure CloseOpenGL;begin

if HGLRC_VarG <> 0 thenbeginwglMakeCurrent(HDC_VarG, 0);wglDeleteContext(HGLRC_VarG);end;

releaseDC(HWND_VarG, HDC_VarG);destroywindow(HWND_VarG);UnregisterClass('OpenGl3DwithDelphi',hInstance);

HGLRC_VarG := 0;HDC_VarG := 0;HWND_VarG := 0;end;

function CreateOpenGL(HINST_P : HINST) : boolean stdcall;variPixelFormat : integer;WCLASSEX : WNDCLASSEX;pfd : PIXELFORMATDESCRIPTOR;

beginWCLASSEX.cbSize := Sizeof(WCLASSEX);WCLASSEX.style := CS_HREDRAW or CS_VREDRAW;WCLASSEX.lpfnWndProc := @WindowProc;WCLASSEX.cbClsExtra := 0;WCLASSEX.cbWndExtra := 0;WCLASSEX.hInstance := HINST_P;WCLASSEX.hIcon := LoadIcon(0, IDI_WINLOGO);WCLASSEX.hCursor := LoadCursor(0,IDC_WAIT);

Page 8: grafkom program baru buka lagi

WCLASSEX.hbrBackground := 0;WCLASSEX.lpszMenuName := nil;WCLASSEX.lpszClassName:= 'OpenGl3DwithDelphi';

if RegisterClassEX(WCLASSEX) = 0 thenbeginMessageBox(0, 'Registrasi Window Class gagal, program dihentikan', 'Error window', MB_OK or MB_ICONERROR);Result := false;exit;end;

HWND_VarG := CreateWindowEx(0,'OpenGl3DwithDelphi','Gambar 3D(Tekan Esc untuk keluar)',0,0,0,LebarW, TinggiW,0, 0, HINST_P, nil);

if HWND_VarG = 0 thenbeginMessageBox(0, 'Pembuatan OpenGL window gagal, program dihentikan', 'Error window',MB_OK or MB_ICONERROR);Result:=false;exit;end;

pfd.nSize := SizeOf(PIXELFORMATDESCRIPTOR);pfd.nVersion := 1;pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;pfd.iPixelType := PFD_TYPE_RGBA;pfd.cColorBits := 16;pfd.cRedBits := 0;pfd.cRedShift := 0;pfd.cGreenBits := 0;pfd.cBlueBits := 0;pfd.cBlueShift := 0;pfd.cAlphaBits := 0;pfd.cAlphaShift := 0;pfd.cAccumBits := 0;pfd.cAccumRedBits := 0;pfd.cAccumGreenBits := 0;pfd.cAccumBlueBits := 0;pfd.cAccumAlphaBits := 0;pfd.cDepthBits := 16;pfd.cStencilBits := 0;pfd.cAuxBuffers := 0;pfd.iLayerType := PFD_MAIN_PLANE;pfd.bReserved := 0;pfd.dwLayerMask := 0;pfd.dwVisibleMask := 0;

Page 9: grafkom program baru buka lagi

pfd.dwDamageMask := 0;

HDC_VarG := GetDC(HWND_VarG);if HDC_VarG = 0 thenbeginMessageBox(0, 'Pembuatan Device Context gagal, program dihentikan', 'Error device',MB_OK or MB_ICONERROR);Result := false;exit;end;

iPixelFormat := ChoosePixelFormat(HDC_VarG, @pfd);

if iPixelFormat = 0 thenbeginMessageBox(0, 'Pencarian Pixel Format gagal, program dihentikan', 'Error pixel format',MB_OK or MB_ICONERROR);Result := false;exit;end;

if (not SetPixelFormat(HDC_VarG, iPixelFormat, @pfd)) thenbeginMessageBox(0, 'Set Pixel Format gagal, program dihentikan', 'Error pixel format',MB_OK or MB_ICONERROR);Result:=false;exit;end;

HGLRC_VarG := wglCreateContext(HDC_VarG);if HGLRC_VarG = 0 thenbeginMessageBox(0, 'Pembuatan Rendering Context gagal, program dihentikan', 'Error rendering context',MB_OK or MB_ICONERROR);Result := false;exit;end;

if (not wglMakeCurrent(HDC_VarG, HGLRC_VarG)) thenbeginMessageBox(0, 'Pengaktifan Rendering Context gagal, program dihentikan', 'Error rendering context',MB_OK or MB_ICONERROR);Result := false;exit;end;

ShowWindow(HWND_VarG, SW_SHOW);

Page 10: grafkom program baru buka lagi

glViewport(0, 0, LebarW, TinggiW);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0, LebarW / TinggiW, 1.0, 1000.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity;

Result := true;end;

procedure Display_Grafik;beginglClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);glLoadIdentity();

glTranslated(0.0, 0.0, -5.5);glRotated(SudutPutar, 1.0, 1.0, 1.0);

//polygon depanglBegin(GL_LINE_LOOP);glVertex3d(-1.0, -1.0, 1.0);glVertex3d( 1.0, -1.0, 1.0);glVertex3d( 1.0, 1.0, 1.0);glVertex3d(-1.0, 1.0, 1.0);glEnd();

//polygon belakangglBegin(GL_LINE_LOOP);glVertex3d(-1.0, -1.0, -1.0);glVertex3d( 1.0, -1.0, -1.0);glVertex3d( 1.0, 1.0, -1.0);glVertex3d(-1.0, 1.0, -1.0);glEnd();

//polygon kananglBegin(GL_LINE_LOOP);glVertex3d( 1.0, -1.0, 1.0);glVertex3d( 1.0, -1.0, -1.0);glVertex3d( 1.0, 1.0, -1.0);glVertex3d( 1.0, 1.0, 1.0);glEnd();

//polygon kiriglBegin(GL_LINE_LOOP);glVertex3d(-1.0, -1.0, 1.0);glVertex3d(-1.0, -1.0, -1.0);glVertex3d(-1.0, 1.0, -1.0);glVertex3d(-1.0, 1.0, 1.0);

Page 11: grafkom program baru buka lagi

glEnd();

//polygon atasglBegin(GL_LINE_LOOP);glVertex3d(-1.0, 1.0, 1.0);glVertex3d( 1.0, 1.0, 1.0);glVertex3d( 1.0, 1.0, -1.0);glVertex3d(-1.0, 1.0, -1.0);glEnd();

//polygon bawahglBegin(GL_LINE_LOOP);glVertex3d(-1.0, -1.0, 1.0);glVertex3d( 1.0, -1.0, 1.0);glVertex3d( 1.0, -1.0, -1.0);glVertex3d(-1.0, -1.0, -1.0);glEnd();

SudutPutar := SudutPutar + 0.1;

SwapBuffers(HDC_VarG);end;

function WinMain(hInstance: HINST; hPrevInstance: HINST; lpCmdLine: PChar;nCmdShow: integer): integer; stdcall;varmsg : TMSG;Selesai : Bool;

beginSelesai := false;

if not CreateOpenGL(hInstance) thenbeginCloseOpenGL;Result := 0;exit;end;

glEnable(GL_DEPTH_TEST);glClearColor(0.0, 0.5, 5.0, 0.5);

while not Selesai dobeginPeekMessage(msg, 0, 0, 0, PM_REMOVE);

if msg.message <> WM_QUIT then

Page 12: grafkom program baru buka lagi

beginDisplay_Grafik;

if keys[VK_ESCAPE] thenSelesai := true;

TranslateMessage(msg);DispatchMessage(msg);end

end;

CloseOpenGL;result := msg.wParam;end;

beginWinMain(hInstance, hPrevInst, CmdLine, CmdShow);end.

Page 13: grafkom program baru buka lagi

Membuat Object 3D Dengan GlutBerawal dari Tugas ke 3 yang diberikan oleh kakak – kakak asisten praktikum Mata kuliah Komgraf, dan sedikit trik dan manipulasi akhirnya saya bisa juga membuat Object 3D dengan glut dan menggunakan bahasa C. Hasil project yang saya buat dapat dilihat pada gambar di bawah ini beserta Codingannya :

#include <GL/glut.h>

#include <stdlib.h>

static int slices = 10;

static int stacks = 30;

static void

resize(int width, int height)

Page 14: grafkom program baru buka lagi

{

const float ar = (float) width / (float) height;

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity() ;

}

static void

display(void)

{

const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;

const double a = t*90.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3d(0.3,1,0);

glPushMatrix();

glTranslated(0,0,-6);

glRotated(10,1,0,0);

glRotated(a,0,0,1);

glutWireSphere(2,slices,stacks);

glPopMatrix();

glutSwapBuffers();

Page 15: grafkom program baru buka lagi

}

static void

key(unsigned char key, int x, int y)

{

switch (key)

{

case 27 :

case 'q':

exit(0);

break;

case '+':

slices++;

stacks++;

break;

case '-':

if (slices<3 || stacks<3)

{

slices--;

stacks--;

}

break;

}

glutPostRedisplay();

}

Page 16: grafkom program baru buka lagi

static void

idle(void)

{

glutPostRedisplay();

}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };

const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };

const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };

const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };

const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };

const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };

const GLfloat high_shininess[] = { 100.0f };

int

main(int argc, char *argv[])

{

glutInit(&argc, argv);

glutInitWindowSize(640,480);

glutInitWindowPosition(0,0);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("Tugas Object 3D == Abdul Qifli Sangadji");

glutReshapeFunc(resize);

Page 17: grafkom program baru buka lagi

glutDisplayFunc(display);

glutKeyboardFunc(key);

glutIdleFunc(idle);

glClearColor(1,1,1,1);

glEnable(GL_CULL_FACE);

glCullFace(GL_BACK);

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LESS);

glEnable(GL_LIGHT0);

glEnable(GL_NORMALIZE);

glEnable(GL_COLOR_MATERIAL);

glEnable(GL_LIGHTING);

glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);

glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);

glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);

glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);

glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);

glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

glutMainLoop();

Page 18: grafkom program baru buka lagi

return EXIT_SUCCESS;

}

Ini program adis

2. Contoh Program Polygon menggunakan bahasa C++

#include <windows.h>#include <GL/glut.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>#include <math.h>#include <iostream.h>

using namespace std;

GLsizei screenWidth=600,screenHeight=500; // define screen width and hight

class cPoint {  // class pointpublic : GLdouble X,Y;};

class cLine {  // class linepublic : GLdouble X1,Y1,X2,Y2;};

class cVector {  // class pointpublic : GLdouble X,Y;};

cLine myPillar[10][10];    // declare pillarcLine myRay;               // declare raycVector vecRay;            // declare vectorcPoint vecPoint;           // declare point

cVector vec[100][100];       // vectorcVector vecPend[100][100];   // vector perpendicular

Page 19: grafkom program baru buka lagi

GLboolean createPillar = FALSE;GLboolean createRay = FALSE;

GLdouble Thit=0;GLdouble minThit=0;cVector minPhit;cVector minVecPend;cVector vecRef;

GLint pi=0, n=0;         // point counter and line counter

GLdouble tempLineX1=0, tempLineY1=0; // temporary line x1 and y1 before mouse upGLdouble tempLineX2=0, tempLineY2=0; // temporary line x2 and y2 before mouse upGLdouble tempPointX=0, tempPointX2=0;// temporary point x and y before mouse up

GLboolean refresh = TRUE;GLboolean run = FALSE;

void myInit (void){glClear(GL_COLOR_BUFFER_BIT);    // clear the screenglClearColor (1.0, 1.0, 1.0, 0.0); // set the gb color to a bright whiteglColor3f(0.0f, 0.0f, 0.0f);    // set the drawing color to backglPointSize(3.0);       // set the point sizeglMatrixMode(GL_PROJECTION);       // set up appropriate matrices to be explainedglLoadIdentity();gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);

}

void drowString(float x, float y, char *string, void *font){int len, i;glRasterPos2f(x, y);len = (int) strlen(string);for (i = 0; i < len; i++) {glutBitmapCharacter(font, string[i]);}}

float absolute(float x){if (x<0)x = -x;

Page 20: grafkom program baru buka lagi

return (x);}

void mouse(int button, int state, int x, int y) {switch (button){case GLUT_LEFT_BUTTON:

if (state == GLUT_DOWN){tempLineX1 = x;                 // record position of mouse downtempLineY1 = screenHeight – y;} else {tempLineX2 = x;                 // record position of mouse uptempLineY2 = screenHeight – y;// if position mouse down and mouse up is sameif (tempLineX1 == tempLineX2 && tempLineY1 == tempLineY2){// then create point coordinate} else {// if position mouse down and mouse up is deferent then create line coordinateif (createRay == FALSE) {n++;if ( absolute(tempLineX2 – myPillar[pi][1].X1) < 20 && absolute(tempLineY2 – myPillar[pi][1].Y1) < 20 ) {// if distance end point with start point of polygon less than 20 close polygonmyPillar[pi][n].X1 = tempLineX1;myPillar[pi][n].Y1 = tempLineY1;myPillar[pi][n].X2 = myPillar[pi][1].X1;myPillar[pi][n].Y2 = myPillar[pi][1].Y1;} else {myPillar[pi][n].X1 = tempLineX1;myPillar[pi][n].Y1 = tempLineY1;myPillar[pi][n].X2 = tempLineX2;myPillar[pi][n].Y2 = tempLineY2;}} else {myRay.X1 = tempLineX1;myRay.Y1 = tempLineY1;myRay.X2 = tempLineX2;myRay.Y2 = tempLineY2;

// vector sinar titi sinar akhir – titik sinar akhirvecRay.X = myRay.X2 – myRay.X1;vecRay.Y = myRay.Y2 – myRay.Y1;}glutPostRedisplay();}}

Page 21: grafkom program baru buka lagi

break;case GLUT_RIGHT_BUTTON:break;}}void drawPoint(GLdouble x, GLdouble y){   // drow point functionglBegin(GL_POINTS);glVertex2i(x,y);glEnd();}

void drawLine(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2){  // drow line functionglBegin(GL_LINES);glVertex2i(x1,y1);glVertex2i(x2,y2);glEnd();}

void twoDRayTracing(void) {minThit = 0;for (int i=0; i<=pi; i++) {Thit = 0;int j = 1;while (myPillar[i][j].X2 != 0){// define vectorvec[i][j].X = myPillar[i][j].X1 – myPillar[i][j].X2;vec[i][j].Y = myPillar[i][j].Y1 – myPillar[i][j].Y2;// define vector perpendicular nivecPend[i][j].X = -vec[i][j].Y;vecPend[i][j].Y = vec[i][j].X;// (B-A)cVector ba;ba.X = myPillar[i][j].X1 – myRay.X1;ba.Y = myPillar[i][j].Y1 – myRay.Y1;// n.(B-A)GLdouble nba = (vecPend[i][j].X * ba.X) + (vecPend[i][j].Y * ba.Y);// n.cGLdouble nc = (vecPend[i][j].X * vecRay.X) + (vecPend[i][j].Y * vecRay.Y);// n.(B-A) / n.cThit = nba / nc;if (Thit > 0.0005) {// vector sinar * ThitcVector CThit;CThit.X = vecRay.X * Thit;CThit.Y = vecRay.Y * Thit;

Page 22: grafkom program baru buka lagi

// vector titik tumbuk titik awal sinar + CThit -> Phit hal 195cVector Phit;Phit.X = myRay.X1 + CThit.X;Phit.Y = myRay.Y1 + CThit.Y;

// panjang pillar yang tertumbukGLdouble lengthVec = sqrt( (vec[i][j].X*vec[i][j].X) + (vec[i][j].Y*vec[i][j].Y) );

// pengecekan apakah titik tumbuk mengenai pillar atau tidakcVector vecPhit;vecPhit.X = myPillar[i][j].X1 – Phit.X;vecPhit.Y = myPillar[i][j].Y1 – Phit.Y;

// panjang titik awal pillar sampai titik tumbukGLdouble lengthThit = sqrt((vecPhit.X*vecPhit.X) + (vecPhit.Y*vecPhit.Y));

cVector vecPhit2;vecPhit2.X = myPillar[i][j].X2 – Phit.X;vecPhit2.Y = myPillar[i][j].Y2 – Phit.Y;// panjang titik akhit pillar sampai titik tumbukGLdouble lengthThit2 = sqrt((vecPhit2.X*vecPhit2.X) + (vecPhit2.Y*vecPhit2.Y));

GLdouble lengthThitTemp=0;// jika panjang titik awal pillar ke Phit >= panjang titik akhir pillar ke Phit maka yang diambil adalah// panjang titik awal pillar ke Phit dan sebaliknyaif (lengthThit>=lengthThit2)lengthThitTemp=lengthThit;elselengthThitTemp=lengthThit2;

// jika panjang pillar yang tertumbuk >= vector terpanjangif ( lengthVec >= lengthThitTemp) {if (minThit > Thit || minThit==0 ){minThit = Thit;minPhit.X = Phit.X;minPhit.Y = Phit.Y;

// vector perpendikular dari pillar yang ditumbukminVecPend.X = vecPend[i][j].X;minVecPend.Y = vecPend[i][j].Y;}}}j++;}

Page 23: grafkom program baru buka lagi

}// vector sinar * Thit yang paling kecilcVector minCThit;minCThit.X = vecRay.X*minThit;minCThit.Y = vecRay.Y*minThit;

// panjang vector perpendikular dari pillar yang ditumbukGLdouble lengthVecPend = sqrt( (minVecPend.X*minVecPend.X) + (minVecPend.Y*minVecPend.Y));

// vector normal dari pillar yang tertumbuk// vector perpendikular / panjang vector perpendikular dari pillar yang tertumbukcVector nVec;nVec.X = minVecPend.X / lengthVecPend;nVec.Y = minVecPend.Y / lengthVecPend;

// Caksen = c – 2(c.n)n -> hal 209cVector minCThit2;// 2(c.n)nGLdouble minCThitSkl = ((minCThit.X * nVec.X) + (minCThit.Y * nVec.Y)) * 2;minCThit2.X = minCThitSkl * nVec.X;minCThit2.Y = minCThitSkl * nVec.Y;// c – 2(c.n)nvecRef.X = minCThit.X – minCThit2.X;vecRef.Y = minCThit.Y – minCThit2.Y;}

void play(void){twoDRayTracing();glutPostRedisplay();}

void reset(){   // reset screenpi=0, n=0;myRay.X1=0;refresh = TRUE;createPillar = FALSE;createRay = FALSE;run = FALSE;glutPostRedisplay();}

void myDisplay(){

Page 24: grafkom program baru buka lagi

if (refresh == TRUE) {glClear(GL_COLOR_BUFFER_BIT);// clear the screenrefresh = FALSE;}

if (myPillar[pi][n].X2 != 0) {glColor3f(0.0, 0.0, 1.0);   // set initial fill color to bluedrawLine(myPillar[pi][n].X1, myPillar[pi][n].Y1,myPillar[pi][n].X2,myPillar[pi][n].Y2);}

if (createRay == TRUE){glColor3f(1.0, 0.0, 0.0);   // set initial fill color to bluedrawLine(myRay.X1, myRay.Y1,myRay.X2,myRay.Y2);createRay = FALSE;}if (run==TRUE){glColor3f(1.0, 0.0, 0.0);   // set initial fill color to bluedrawLine(myRay.X1, myRay.Y1, minPhit.X,minPhit.Y);myRay.X1 = minPhit.X;myRay.Y1 = minPhit.Y;vecRay.X = vecRef.X;vecRay.Y = vecRef.Y;run==FALSE;}glEnd();glFlush();}

void Menu (int id){switch(id){case 1:pi++, n=0;createPillar = TRUE;break;case 2:createRay = TRUE;break;case 3:run=TRUE;play();break;case 4:reset();break;case 9:reset();

Page 25: grafkom program baru buka lagi

exit(0);}}

int main(int argc,char **argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(50,50);glutInitWindowSize(screenWidth,screenHeight);glutCreateWindow(“Tugas Kelompok”);

glutDisplayFunc(myDisplay);glutMouseFunc(mouse);

glutCreateMenu(Menu);glutAddMenuEntry(“New Pillar”,1);glutAddMenuEntry(“Create Ray”,2);glutAddMenuEntry(“Play”,3);glutAddMenuEntry(“Reset”,4);glutAddMenuEntry(“Exit”,9);glutAttachMenu(GLUT_RIGHT_BUTTON);

myInit();glutMainLoop();return 0;}

/*void test(void){myPillar[0][1].X1=10;myPillar[0][1].Y1=90;myPillar[0][1].X2=50;myPillar[0][1].Y2=10;

myPillar[0][2].X1=50;myPillar[0][2].Y1=10;myPillar[0][2].X2=140;myPillar[0][2].Y2=20;

myPillar[0][3].X1=140;myPillar[0][3].Y1=20;myPillar[0][3].X2=160;myPillar[0][3].Y2=110;

myPillar[0][4].X1=160;myPillar[0][4].Y1=110;

Page 26: grafkom program baru buka lagi

myPillar[0][4].X2=10;myPillar[0][4].Y2=90;

myRay.X1 = 90;myRay.Y1 = 20;myRay.X2 = 130;myRay.Y2 = 40;

vecRay.X = myRay.X2 – myRay.X1;vecRay.Y = myRay.Y2 – myRay.Y1;}