Selasa, 10 April 2012

Tugas Grafika Komputer OpenGL

iseng-iseng buka file kuliah dulu, nemu script c++ buat tugas grafika komputer yang kalo ga salah sih senter muter jadinya. .  semoga bermanfaat buat temen2.

#include <glut.h>
#include <math.h>

void cylinder(float alas,float atas,float tinggi);
void balok(float tebal,int ratiol,int ratiop);


double Loop=0;

GLfloat ambient_light[]={0.0,0.0,0.0,0.0};

GLfloat  source_light[]={1.0,1.0,1.0,0.0};

GLfloat     light_pos[]={0,0.0,0.0,4.0};


void Printer(void)
{
glClearColor(0.1,0.0,0.0,0.1);  //putih
glShadeModel(GL_SMOOTH);
glViewport(10,100,480,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 90.0, 1.5, 5.0, 100
gluPerspective(90.0,1.5,5.0,120.0);

glEnable (GL_DEPTH_TEST);
glPolygonMode   (GL_FRONT_AND_BACK,GL_FILL);
glEnable (GL_LIGHTING);
//glLightModelfv  (GL_LIGHT_MODEL_AMBIENT,ambient_light);
glLightfv (GL_LIGHT7,GL_DIFFUSE,source_light);
glLightfv (GL_LIGHT7,GL_POSITION,light_pos);
glEnable (GL_LIGHT7);
glEnable (GL_COLOR_MATERIAL);
glColorMaterial (GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
}


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


glPushMatrix();
// -3 -3 -50
glTranslatef(-3,-3,-50);
Loop++;
   glRotatef( -Loop,1.0,1.0,1.0);//Sumbu yang berotasi

glTranslatef(0.0,0.0,10.0);//tabung
glColor3f(0.0,0.0,50.0);
cylinder(6,6,20);

glRotatef(90,0.45,0.0,0.0);
glTranslatef(0.0,0.0,-1.0);//on/off
cylinder(0.5,0.5,8.9);

glRotatef(270,0.45,0.0,0.0);
glTranslatef(0.2,-5.0,2.0);//balok
balok(1,20,10);


glPopMatrix();

glFlush();
glutSwapBuffers();

}




void cylinder(float alas,float atas,float tinggi)
{
float i;
glPushMatrix();
glTranslatef(1.0,0.0,-alas/8);
glutSolidCone(alas,0,32,4);
for(i=0;i<=tinggi;i+=alas/24)
{
glTranslatef(0.0,0.0,alas/24);
glutSolidTorus(alas/4,alas-((i*(alas-atas))/tinggi),16,16);
}
glTranslatef(0.0,0.0,alas/4);
glutSolidCone(atas,0,20,1);
glColor3f(1.,0.,0.);
glPopMatrix();
}
void balok(float tebal,int ratiol,int ratiop)
{
float i,j;
glPushMatrix();
for(i=0;i<ratiop;i++)
{
glTranslatef(-(ratiol+1)*tebal/2,0.0,0.0);
for(j=0;j<ratiol;j++)
{
glTranslatef(tebal,0.0,0.0);
glutSolidCube(tebal);
}
glTranslatef(-(ratiol-1)*tebal/2,0.0,tebal);
}
glPopMatrix();
}

void tampil()
{
display();
}


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

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//|GLUT_DEPTH);
glutInitWindowPosition(10,10);
glutInitWindowSize(1000,700);
glutCreateWindow("PRINTER 3D");
glClearColor(0.5,0.5,0.0,0.0);
glutDisplayFunc(display);
gluOrtho2D(-320., 320., -240.0, 240.0);
glutIdleFunc(tampil);
Printer();
glutMainLoop();
return(0);
}

Tidak ada komentar:

Posting Komentar