In many applications the parts of a model depend on one another: if we move one part, it causes other parts to move. We represent such models using trasformations. OpenGL trasformations are applied to the existing model-view matrix. Each trasformation represents a relative change from one scaling,position and orientation to another. In this post I show you a demo with a lamp. You can move some parts of the lamp using the keys 'a','s','d','f','g','h' from your keyboard. The lamp uses also a diffuse light with an emissive component. Here it is the video demo:
You can download the code here:
The demo uses the template code showed in the previous post plus other useful methods.
Inside the code you can find the drawLamp() methods with the following code:
Geometry geo;
int colors1[]={0,127,255, 102,255,0};
int colors2[]={ 255,153,51, 255,153,51, 255,153,51,
255,153,51, 255,153,51, 255,153,51};
glPushMatrix();
float HEIGHT_CYL_A = 0.5f;//BASE HEIGHT
float HEIGHT_CYL_B = 1.5f;//MIDDLE HEIGHT
float HEIGHT_CYL_C = 0.5f;//TOP HEIGHT
float SPHERE_RADIUS = 0.5f;//SPHERE RADIUS
/* Draw Base Start */
glRotatef(arm1Angle,0,1,0);
glTranslatef(0,HEIGHT_CYL_A/2,0);
geo.box(2,HEIGHT_CYL_A,2,colors2);
/* Draw Base End */
/* Draw Sphere Joint Start*/
glTranslatef(0,SPHERE_RADIUS/2+HEIGHT_CYL_A,0);
geo.sphere(SPHERE_RADIUS,10,10,colors1);
/* Draw Sphere Joint End*/
/* Draw first middle Arm Start */
glRotatef(-20,0,0,1);
glTranslatef(0,SPHERE_RADIUS+HEIGHT_CYL_B/2,0);
geo.box(1,HEIGHT_CYL_B,1,colors2);
/* Draw first middle Arm End */
/* Draw Sphere Joint Start*/
glTranslatef(0,SPHERE_RADIUS+HEIGHT_CYL_B/2,0);
geo.sphere(SPHERE_RADIUS,10,10,colors1);
/* Draw Sphere Joint End*/
/* Draw second middle Arm Start */
glRotatef(arm2Angle,0,0,1);
glTranslatef(0,SPHERE_RADIUS+HEIGHT_CYL_B/2,0);
geo.box(1,HEIGHT_CYL_B,1,colors2);
/* Draw second middle Arm End */
/* Draw Sphere Joint Start*/
glTranslatef(0,SPHERE_RADIUS+HEIGHT_CYL_B/2,0);
geo.sphere(SPHERE_RADIUS,10,10,colors1);
/* Draw Sphere Joint End*/
/* Draw third Arm Start */
glRotatef(arm3Angle,0,0,1);
glTranslatef(0,SPHERE_RADIUS+HEIGHT_CYL_C/2,0);
geo.box(2,HEIGHT_CYL_C,2,colors2);
/* Draw third Arm End */
/*Sphere emission*/
GLfloat mat_emission[] = {0.5, 0.8, 0.9, 0.0};
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
/* Draw Sphere Final Start*/
glTranslatef(0,SPHERE_RADIUS+HEIGHT_CYL_C/2,0);
geo.sphere(SPHERE_RADIUS,10,10,colors1);
glEnable(GL_LIGHT1);
/* Draw Sphere Final End*/
/*Put light near the final sphere*/
glEnable(GL_LIGHT1);
GLfloat pos[]={ 0.0, 0.0, 0.0, 1.0};
GLfloat color[]={ 1.0, 1.0, 1.0, 1.0};
//glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);// set cutoff angle
glLightfv(GL_LIGHT1, GL_POSITION, pos);
glLightfv(GL_LIGHT1, GL_DIFFUSE, color);
//glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2);
GLfloat mat_emission2[] = {0, 0, 0, 0.0};
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission2);
glPopMatrix();
In this code you can see how every part of the lamp is realized starting from the previous part. And this is an alternative demo: