This document contains C++ code for rendering a torus shape in 3D using OpenGL. It defines functions for drawing the torus, setting the material properties, lighting, viewport, and camera. The main function initializes the GLUT window and rendering context and calls the display function, which renders the torus by calling the other functions. It enables lighting, depth testing, and other OpenGL features before entering the main loop.
1 of 2
Download to read offline
More Related Content
pelajaran buat grafika komputer
1. #include <Windows.h>
#include <iostream>
#include <glGL.h>
#include <glGLU.h>
#include <glglut.h>
#include <math.h>
void torus()
{
glPushMatrix();
glTranslated(0,0,0);
glScaled(1.0,1.0,1.0);
glutSolidTorus(0.3,0.1,20,20);
glPopMatrix();
}
void setMaterial()
{
//set properties of surfaces material
GLfloat mat_ambient[] = {0.6f,0.6f,0.7f,0.0f}; // ada 4 jenis material yang
dipakai, dengan kombinasi warna tertentu
GLfloat mat_diffuse[] = {0.2f,0.1f,1.0f,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 setLighting()
{
//set light sources
//GLfloat lightIntensity[] = {0.7f,0.7f,0.7f,1.0f};//mensetting pencahayaan
GLfloat light_position[] = {2.0f,6.0f,3.0f,0.0f};
//glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void setViewport()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,0.1,100.0);
}
void setCamera()
{
//set the camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3.3,3.3,3.3,0,0.25,0,0.0,0.0,1.0);
}
void displayObject()
{
setMaterial();
setLighting();
setViewport();
setCamera();
//startDrawing
2. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
torus();//memanggil fungsi menggambar kubus
glFlush();//mengirim smua objek untuk dirender
}
main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow(" 3 dimensi sederhana");
glutDisplayFunc(displayObject);//fungsi dari display object yang menggabungkan
kubus lighting material dan kamera
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(1.0f,1.0f,1.0f,1.0f);
glViewport(0,0,640,480);
glutMainLoop();
}