Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Chrome cube in OpenGL / Glut?

Tags:

opengl

glut

cube

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?

like image 255
Mithrax Avatar asked Nov 20 '25 00:11

Mithrax


1 Answers

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();
}
like image 50
John Rasch Avatar answered Nov 22 '25 19:11

John Rasch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!