Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
46 views8 pages

Final Task

This document contains code for a computer graphics program written in C++ using OpenGL. The program displays 3 shapes - a cube, sphere, and cone - with different materials applied to each. It initializes lighting and materials, defines functions to render each shape with transformations, and includes a display callback to continuously render the scene and handle user input to rotate and translate the view. The main function sets up the window and display and rendering callbacks and starts the main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views8 pages

Final Task

This document contains code for a computer graphics program written in C++ using OpenGL. The program displays 3 shapes - a cube, sphere, and cone - with different materials applied to each. It initializes lighting and materials, defines functions to render each shape with transformations, and includes a display callback to continuously render the scene and handle user input to rotate and translate the view. The main function sets up the window and display and rendering callbacks and starts the main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

TUGAS GRAFIKA KOMPUTER

MOHAMMED ZEZA FIRDAUS

13622081

#include <windows.h>

#ifdef __APPLE__

#include <GLUT/glut.h>

#else

#include <GL/glut.h>

#endif

#include <stdlib.h>

int w=400, h=400, z=-20;

int x1=0, y1=0, sudut=0, z1=0;

void kubus (){

glPushMatrix();

glTranslatef(0,0,5);

glColor3f(1,1,1);

glutSolidCube(3);

glPopMatrix();

}
void bola (){

glPushMatrix();

glTranslatef(0,0,0);

glColor3f(1,1,1);

glutSolidSphere(2,200,50);

glPopMatrix();

void cone (){

glPushMatrix();

glTranslatef(0,-1.5,-5);

glRotatef(-90,1,0,0);

glColor3f(1,0,0);

glutSolidCone(2,4,200,50);

glPopMatrix();

void setMaterial()

//set properties of surfaces material

GLfloat mat_ambient[] = {0.7f,1.7f,0.7f,5.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi
warna tertentu

GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};


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

GLfloat mat_shininess[] = {50.0f};

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,mat_shininess);

void setMaterial2()

//set properties of surfaces material

GLfloat mat_ambient[] = {1.3f,0.0f,0.0f,0.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi
warna tertentu

GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};

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

GLfloat mat_shininess[] = {50.0f};

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,mat_shininess);

void setMaterial3()

//set properties of surfaces material

GLfloat mat_ambient[] = {0.7f,0.7f,1.7f,5.0f}; // ada 4 jenis material yang dipakai, dengan kombinasi
warna tertentu

GLfloat mat_diffuse[] = {0.6f,0.6f,0.6f,1.0f};


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

GLfloat mat_shininess[] = {50.0f};

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,mat_shininess);

void renderScene(void){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClearColor(1,1,1,1);

glLoadIdentity();

glTranslatef(0,0,z);

glRotatef(sudut,x1,y1,z1);

glColor3f(0,0,0);

glEnable(GL_LIGHTING);

setMaterial();

kubus();

glDisable(GL_LIGHTING);

glEnable(GL_LIGHTING);

setMaterial2();

bola();

glDisable(GL_LIGHTING);
glEnable(GL_LIGHTING);

setMaterial3();

cone();

glDisable(GL_LIGHTING);

glutSwapBuffers();

void resize(int w1, int h1){

glViewport(0,0,w1,h1);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(45.0,(float) w1/(float) h1, 1.0,300.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

void myKeyboard(unsigned char key, int x, int y){

if (key =='a') z+=5;

else if (key == 'd') z-=5;

else if (key == 'x') {

x1=1;

y1=0;

z1=0;

sudut+=10;

}
else if (key == 'y') {

y1=1;

x1=0;

z1=0;

sudut+=-10;

else if (key == 'z') {

y1=-2;

x1=1;

z1=0;

sudut+=10;

void init(){

GLfloat LightPosition[]={10.0f, 10.0f, 20.0f, 0.0f};

GLfloat LightAmbient[]={0.0f, 1.0f, 0.0f, 1.0f};

GLfloat LightDiffuse[]={0.7f, 0.7f, 0.7f, 1.0f};

GLfloat LightSpecular[]={0.5f, 0.5f, 0.5f, 1.0f};

GLfloat Shine[]={80};

glShadeModel(GL_SMOOTH);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glClearDepth(1.0f);

glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

//glMaterialfv(GL_FRONT, GL_AMBIENT, LightAmbient);

glMaterialfv(GL_FRONT, GL_DIFFUSE, LightDiffuse);

//glMaterialfv(GL_FRONT, GL_DIFFUSE, LightAmbient);

glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);

//glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);

glEnable(GL_LIGHT0);

return;

void timer(int value){

glutPostRedisplay();

glutTimerFunc(50,timer,0);

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

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);

glutInitWindowPosition(100,100);
glutInitWindowSize(w,h);

glutCreateWindow("13622081");

glutDisplayFunc(renderScene);

glutReshapeFunc(resize);

glutKeyboardFunc(myKeyboard);

glutTimerFunc(1,timer,0);

init();

glutMainLoop(); }

You might also like