GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
|
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
|
void glLightModel{if}( GLenum pname, TYPE param );
void glLightModel{if}v( GLenum pname, TYPE *param );
|
Parameter Name | Default Value | Meaning |
|---|---|---|
GL_LIGHT_MODEL_AMBIENT | (0.2, 0.2, 0.2, 1.0) | ambient RGBA intensity of the entire scene |
GL_LIGHT_MODEL_LOCAL_VIEWER | 0.0 or GL_FALSE | how specular reflection angles are computed |
GL_LIGHT_MODEL_TWO_SIDE | 0.0 or GL_FALSE | choose between one-sided or two-sided lighting |
| GL_LIGHT_MODEL_COLOR_CONTROL | GL_SINGLE_COLOR | whether specular color is calculated separately from ambient and diffuse |
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
|
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff );
|
glMaterial*()
void glMaterial{if} (GLenum face, GLenum pname, TYPE param);
void glMaterial{if}v (GLenum face, GLenum pname, TYPE *param);
face -> { GL_FRONT, GL_BACK, GL_FRONT_AND_BACK }
Non-vector version only works for GL_SHINNESS.
Like colors, material properties are 'state' oriented.
|
Parameter Name | Default Value | Meaning |
|---|---|---|
GL_AMBIENT | (0.2, 0.2, 0.2, 1.0) | ambient color of material |
GL_DIFFUSE | (0.8, 0.8, 0.8, 1.0) | diffuse color of material |
GL_AMBIENT_AND_DIFFUSE |
| ambient and diffuse color of material |
GL_SPECULAR | (0.0, 0.0, 0.0, 1.0) | specular color of material |
GL_SHININESS | 0.0 | specular exponent |
GL_EMISSION | (0.0, 0.0, 0.0, 1.0) | emissive color of material |
GL_COLOR_INDEXES | (0,1,1) | ambient, diffuse, and specular color indices |
Example:Different Material Properties
//material.cpp
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* draw sphere in first row, first column
* diffuse reflection only; no ambient or specular
*/
glPushMatrix();
glTranslatef (-3.75, 3.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(1.0, 16, 16);
glPopMatrix();
/* draw sphere in first row, second column
* diffuse and specular reflection; low shininess; no ambient
*/
glPushMatrix();
glTranslatef (-1.25, 3.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(1.0, 16, 16);
glPopMatrix();
/* draw sphere in first row, third column
* diffuse and specular reflection; high shininess; no ambient
*/
glPushMatrix();
glTranslatef (1.25, 3.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
glutSolidSphere(1.0, 16, 16);
glPopMatrix();
/* draw sphere in first row, fourth column
* diffuse reflection; emission; no ambient or specular refl.
*/
glPushMatrix();
glTranslatef (3.75, 3.0, 0.0);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glutSolidSphere(1.0, 16, 16);
glPopMatrix();
|
|
No ambient reflection
Grey ambient reflection
Blue ambient reflection
|
Figure column:
More teapots:
In summary, the default OpenGL lighting math model ('redbook' page. 220 - page. 225)
vertex color = emissionmaterial +
ambientlight model * ambientmaterial +

[ambientlight *ambientmaterial +
(max { L (dot) n , 0} ) * diffuselight * diffusematerial +
(max { s (dot) n , 0} )shininess * specularlight * specularmaterial ] i
| int glutCreateWindow(char *name) | Creates a new top-level window |
| int glutCreateSubWindow(int win, int x, int y, int width, int height) |
Creates a sub-window |
| void glutSetWindow(int winId) | Set the window with ID winId as current window |
| int glutGetWindow(void) | Requests the identifier for the current window |
| void glutDestroyWindow(int winId) | Destroys the window specified by winId |
| void glutPostRedisplay(void) | Tells the GLUT event processor that the current window needs to be redisplayed |
| void glutSwapBuffers(void) | Swaps the buffers for the current window |
| void glutPositionWindow(int x, int y) | Requests a change in the position of the window |
| void glutReshapeWindow(int width, int height) | Requests a change in the size of the window |
| void glutFullScreen() | Requests that the current window be made full screen |
| void glutPopWindow(void) void glutPushWindow(void) |
Push or Pop the current window relative to others in the stack |
| void glutShowWindow(void) void glutHideWindow(void) void glutIconifyWindow(void) |
Show, hide or iconify the current window |
| void glutSetWindowTitle(char *name) void glutSetIconTitle(char *name) |
Set title bar in window or iconified window |
"GLUI is a GLUT-based C++ user interface library which provides controls such as buttons, checkboxes, radio buttons, and spinners to OpenGL applications. It is window-system independent, relying on GLUT to handle all system-dependent issues, such as window and mouse management." - GLUI website
http://glui.sourceforge.net/
GLubyte * loadTexture (char *fileName, int width, int height, int depth)
{
GLubyte *raw_bitmap, *reversed_bitmap;
FILE *texFile;
int byteSize, textureSize;
byteSize = sizeof(GLubyte);
textureSize = width * height * depth * byteSize;
texFile = fopen(fileName, "rb");
if (texFile == NULL)
{
printf ("Texture %s not found\n", fileName);
exit(1);
}
raw_bitmap = (GLubyte *) malloc (textureSize);
reversed_bitmap = (GLubyte *) malloc (textureSize);
if ((raw_bitmap == NULL) || (reversed_bitmap == NULL))
{
printf("Cannot allocate memory for texture %s\n", fileName);
fclose(texFile);
exit(1);
}
fread (raw_bitmap , width * height * depth, 1 , texFile );
fclose (texFile);
/* need to reverse the texture horizontally here */
/************************************************/
int i, j, k;
for (i=0; i < height;i++)
for (j=0; j < width*depth;j+=depth)
for (k=0;k < depth;k++)
reversed_bitmap[i*width*depth + (j+k)] =
raw_bitmap[(i+1)*width*depth-(j+depth)+k];
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
free (raw_bitmap);
return (reversed_bitmap);
}
|