Sintak Tabung Glut C++
09.31.00
ﺑﺴﻢﷲﺍﻠﺮﺤﻤﻦﺍﻠﺮﺤﻴﻢ
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
static GLint fogMode;
static void init(void)
{
GLfloat position[] = { 0.2, 0.5, 3.0, 0.0 };
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
GLfloat mat[3] = {0.1745, 0.01175, 0.01175};
glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
glMaterialf (GL_FRONT, GL_SHININESS, 0.6*128.0);
}
glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.0, 0.0, 1.0, 0.0};
fogMode = GL_EXP;
glFogi (GL_FOG_MODE, fogMode);
glFogfv (GL_FOG_COLOR, fogColor);
glFogf (GL_FOG_DENSITY, 0.4);
glHint (GL_FOG_HINT, GL_DONT_CARE);
glFogf (GL_FOG_START, 4.0);
glFogf (GL_FOG_END, 7.0);
}
glClearColor(0.0, 0.0, 0.0, 0.0); /* memberi warna kabut */
}
// koordinat sumber cahaya/bayangan benda
float l[]={0.0,90.0,0.0};
float n[]={0.0,-1.0,0.0};
float e[]={0.0,-60.0,0.0};
void help();
// obyek yang akan digambar
void draw()
{
GLUquadricObj*obj;
obj = gluNewQuadric();
glShadeModel(GL_SMOOTH);
gluCylinder(obj,11.5,10.0,45.0,200,10);
}
//membuat proyeksi bayangan
void glShadowProjection(float*l,float*e,float*n)
{
float d, c;
float mat[16];
d = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
c = e[0]*n[0] + e[1]*n[1] + e[2]*n[2] - d;
mat[0] = l[0]*n[0]+c;
mat[4] = n[1]*l[0];
mat[8] = n[2]*l[0];
mat[12] = -l[0]*c-l[0]*d;
mat[1] = n[0]*l[1];
mat[5] = l[1]*n[1]+c;
mat[9] = n[2]*l[1];
mat[13] = -l[1]*c-l[1]*d;
mat[2] = n[0]*l[2];
mat[6] = n[1]*l[2];
mat[10] = l[2]*n[2]+c;
mat[14] = -l[2]*c-l[2]*d;
mat[3] = n[0];
mat[7] = n[1];
mat[11] = n[2];
mat[15] = -d;
glMultMatrixf(mat);
}
void render()
{
glClearColor(1.0,1.0,0.1,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// gambar bayangan
glPushMatrix();
glRotatef(300,1,0,0);
glEnable(GL_LIGHTING);
glFogi(GL_FOG_MODE, GL_EXP);
// glFogf(GL_FOG_DENSITY, fogDensity);
glColor3f(1.0,1.0,1.0); //warna benda
draw();
glPopMatrix();
//sekarang gambar bayangan yang muncul
glPushMatrix();
glShadowProjection(l,e,n);
glRotatef(60,0,1,0);
glRotatef(60,0,1,0);
glDisable(GL_LIGHTING);
glDisable(GL_FOG);
glColor3f(0.0,0.0,0.0);
draw();
glPopMatrix();
glutSwapBuffers();
}
void keypress(unsigned char c,int a,int b)
{
if ( c==27 ) exit(0);
else if ( c=='h' ) help();
}
void help()
{
printf("Proyeksi Bayangan Sebuah Tabung\n");
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
}
int main(int argc,char*argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Tabung");
glutReshapeFunc(resize);
glutReshapeWindow(400,400);
glutKeyboardFunc(keypress);
glutDisplayFunc(render);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
//glEnable(GL_FOG);
glLoadIdentity();
gluPerspective(55.0f, 1.0, 1.0, 500.0);
//Reset koordinat sebelum dimodifikasi/diubah
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-150.0);
glutMainLoop();
return 0;
}
0 komentar