Pemodelan Primitif dengan Cull Face

Contoh source DevC dengan GLUT untuk aplikasi pemodelan primitif.

Tugas 1

#include<windows.h>

#include<gl\glut.h>

void depan()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,0.5);

glVertex3f(0.5,0.5,0.5);

glVertex3f(-0.5,0.5,0.5);

glEnd();

}

void belakang()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,0.5,-0.5);

glVertex3f(0.5,0.5,-0.5);

glVertex3f(0.5,-0.5,-0.5);

glVertex3f(-0.5,-0.5,-0.5);

glEnd();

}

void kanan()

{

glBegin(GL_POLYGON);

glVertex3f(0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,-0.5);

glVertex3f(0.5,0.5,-0.5);

glVertex3f(0.5,0.5,0.5);

glEnd();}

void kiri()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,-0.5,0.5);

glVertex3f(-0.5,0.5,0.5);

glVertex3f(-0.5,0.5,-0.5);

glVertex3f(-0.5,-0.5,-0.5);

glEnd();

}

void bawah()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,-0.5);

glVertex3f(-0.5,-0.5,-0.5);

glEnd();

}

void atas()

{

glBegin(GL_POLYGON);

glVertex3f(0.5,0.5,0.5);

glVertex3f(-0.5,0.5,0.5);

glVertex3f(-0.5,0.5,-0.5);

glVertex3f(0.5,0.5,-0.5);

glEnd();

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

depan();

kanan();

kiri();

belakang();

glutSwapBuffers();

glutPostRedisplay();

}

void reshape(int w,int h)

{

glViewport(0,0,(GLsizei)w,(GLsizei)h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5,0.5, 1., 20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0,0,3,0,0,-0.500,0,0.5,0);

}

void init()

{

glClearColor(0.,0.,0.,0.);

glPolygonMode(GL_FRONT,GL_LINE);

glPolygonMode(GL_BACK,GL_LINE);

}

int main(int argc,char* argv[])

{

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowSize(320,240);

glutCreateWindow(“KUBUS”);

init();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop();

}

Tugas 2

#include<windows.h>

#include<gl\glut.h>

void depan()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,0.5);

glVertex3f(0.5,0.5,0.5);

glVertex3f(-0.5,0.5,0.5);

glEnd();

}

void belakang()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,0.5,-0.5);

glVertex3f(0.5,0.5,-0.5);

glVertex3f(0.5,-0.5,-0.5);

glVertex3f(-0.5,-0.5,-0.5);

glEnd();

}

void kanan()

{

glBegin(GL_POLYGON);

glVertex3f(0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,-0.5);

glVertex3f(0.5,0.5,-0.5);

glVertex3f(0.5,0.5,0.5);

glEnd();}

void kiri()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,-0.5,0.5);

glVertex3f(-0.5,0.5,0.5);

glVertex3f(-0.5,0.5,-0.5);

glVertex3f(-0.5,-0.5,-0.5);

glEnd();

}

void bawah()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,0.5);

glVertex3f(0.5,-0.5,-0.5);

glVertex3f(-0.5,-0.5,-0.5);

glEnd();

}

void atas()

{

glBegin(GL_POLYGON);

glVertex3f(0.5,0.5,0.5);

glVertex3f(-0.5,0.5,0.5);

glVertex3f(-0.5,0.5,-0.5);

glVertex3f(0.5,0.5,-0.5);

glEnd();

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

depan();

kanan();

kiri();

belakang();

glutSwapBuffers();

glutPostRedisplay();

}

void reshape(int w,int h)

{

glViewport(0,0,(GLsizei)w,(GLsizei)h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5,0.5, 1., 20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0,0,3,0,0,-0.500,0,0.5,0);

}

void init()

{

glClearColor(0.,0.,0.,0.);

glPolygonMode(GL_FRONT,GL_LINE);

glPolygonMode(GL_BACK,GL_LINE);

glCullFace(GL_BACK);

glEnable(GL_CULL_FACE);

}

int main(int argc,char* argv[])

{

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowSize(320,240);

glutCreateWindow(“BANGUN KUBUS DENGAN BACK FACE CULLING”);

init();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop();

}

Tugas 3

#include<windows.h>

#include<math.h>

#include<gl\glut.h>

#define pi  3.14

 

void bola(GLfloat radius)

{

GLdouble inc=pi/12;

GLdouble theta,phi;

for (theta=-pi/2;theta<(pi/2-inc);theta+=inc)

{

for (phi=0;phi<2*pi;phi+=inc)

{

glBegin(GL_POLYGON);

glVertex3f(radius*cos(theta)*cos(phi),radius*sin(theta),radius*cos(theta)*sin(phi));

glVertex3f(radius*cos(theta+inc)*cos(phi),radius*sin(theta+inc),radius*cos(theta+inc)*sin(phi));

glVertex3f(radius*cos(theta+inc)*cos(phi+inc),radius*sin(theta+inc),radius*cos(theta+inc)*sin(phi+inc));

glVertex3f(radius*cos(theta)*cos(phi+inc),radius*sin(theta),radius*cos(theta)*sin(phi+inc));

glEnd();

}

}

glFlush();

}

 

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.,1.,1.);

glTranslatef(0.,0.,-5.);

bola(1.5);

}

 

void reshape(int w,int h)

{

glViewport(0,0,(GLsizei)w,(GLsizei)h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5,0.5, 1., 20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0,0,3,0,0,-100,0,1,0);

}

 

 

void init()

{

glClearColor(0.,0.,0.,0.);

glPolygonMode(GL_FRONT,GL_LINE);

glPolygonMode(GL_BACK,GL_LINE);

glCullFace(GL_BACK);

glEnable(GL_CULL_FACE);

}

int main(int argc,char* argv[])

{

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(320,240);

glutCreateWindow(“BOLA DENGAN BACK FACE CULLING “);

init();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop();

}

Advertisements
Pemodelan Primitif dengan Cull Face

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s