Create a Chrome cube in OpenGL / Glut?
Question
I'm attempting to make a chromed cube in GLUT / OpenGL and I'm not sure exactly how to do it.
I looked up a "Materials Table" in a textbook which showed "Chrome" as being: Ambient: (0.25, 0.25, 0.25), Diffuse: (0.4, 0.4, 0.4), and Specular: (0.774597,0.774597,0.774597).
My question is, how do I create a simple cube and apply this material/texture to it in Glut/OpenGL?
Do I use "glutSolidCube()"? If so, how do I then apply the chrome texture to it?
Can any GLUT/OpenGL people point me in the right direction?
Solution
This should be as simple as calling glMaterialfv()
before glutSolidCube()
.
I haven't done OpenGL since school, but here's some display loop code from an old project that used a sphere that should get you on the right track:
_Ambient[CHROME][0] = 0.25f;
_Ambient[CHROME][1] = 0.25f;
_Ambient[CHROME][2] = 0.25f;
_Ambient[CHROME][3] = 1.0f;
_Diffuse[CHROME][0] = 0.4f;
_Diffuse[CHROME][1] = 0.4f;
_Diffuse[CHROME][2] = 0.4f;
_Diffuse[CHROME][3] = 1.0f;
_Specular[CHROME][0] = 0.774597f;
_Specular[CHROME][1] = 0.774597f;
_Specular[CHROME][2] = 0.774597f;
_Specular[CHROME][3] = 1.0f;
_Shininess[CHROME] = 76.8f;
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, _Ambient[CHROME]);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, _Diffuse[CHROME]);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, _Specular[CHROME]);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _Shininess[CHROME]);
// Translations...
glutSolidSphere(_Radius, _Slices, _Stacks);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
OTHER TIPS
Have you looked at environment mapping? You want your object to reflect light from other objects in the environment, so you basically apply a texture that looks like the environment and generate texture coordinates based on normals. This will give the surface a mirrored appearance. You can have OpenGL automatically generate texture coordinates for each vertex based with glTexGen
.
NeHe tutorial 23 covers this with sphere mapping. You can also generate your own texture coordinates per-vertex or per-pixel using GLSL. Here's a tutorial on that.