Jump to content
  • Advertisement
Sign in to follow this  
Chrono1081

OpenGL All objects being lit even though they are each being pushed to matrix stack?

This topic is 2697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I don't think I am understanding the matrix stack for OpenGL correctly. I thought that whenever you pushed something to the stack anything you did to it would not affect the rest of the stack. The theory works on transformations but not with lighting. Am I doing something wrong with my lighting? Or is this the way it is? When I apply lighting effects it affects all of my objects in the same way but I want each object to be affected differently. (I'm experimenting in lighting and would like to compare each object).

Here is my code, and attached is a picture of what is happening. Notice all of the teapots are shiny even though only the first one has the call to make it shiny.


[font="Menlo"][color="#3C2480"][font="arial, verdana, tahoma, sans-serif"][color="#000000"][color="#008210"]//Function to render Teapots

[color="#b933a1"]void renderTeapots()

{

[color="#008210"] //Row1

[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#78482c"] [color="#3c2480"]glMaterialfv[color="#000000"](GL_FRONT_AND_BACK[color="#000000"], GL_SPECULAR[color="#000000"], [color="#508187"]whiteSpecularMaterial[color="#000000"]);

[color="#78482c"] [color="#3c2480"]glMaterialfv[color="#000000"](GL_FRONT_AND_BACK[color="#000000"], GL_SHININESS[color="#000000"], [color="#508187"]mShininess[color="#000000"]);

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"]glTranslatef(-[color="#2038d6"]0.6f, [color="#2038d6"]0.7f, [color="#2038d6"]0.0f);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#008210"] //glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, greenEmissiveMaterial);

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"] glTranslatef[color="#000000"]([color="#2038d6"]0.0f[color="#000000"], [color="#2038d6"]0.7f[color="#000000"], [color="#2038d6"]0.0f[color="#000000"]);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"] glTranslatef[color="#000000"]([color="#2038d6"]0.6f[color="#000000"], [color="#2038d6"]0.7f[color="#000000"], [color="#2038d6"]0.0f[color="#000000"]);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#008210"] //Row2

[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"]glTranslatef(-[color="#2038d6"]0.6f, [color="#2038d6"]0.0f, [color="#2038d6"]0.0f);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"] glTranslatef[color="#000000"]([color="#2038d6"]0.0f[color="#000000"], [color="#2038d6"]0.0f[color="#000000"], [color="#2038d6"]0.0f[color="#000000"]);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"] glTranslatef[color="#000000"]([color="#2038d6"]0.6f[color="#000000"], [color="#2038d6"]0.0f[color="#000000"], [color="#2038d6"]0.0f[color="#000000"]);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#008210"] //Row3

[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"]glTranslatef(-[color="#2038d6"]0.6f, -[color="#2038d6"]0.7f, [color="#2038d6"]0.0f);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"]glTranslatef([color="#2038d6"]0.0f, -[color="#2038d6"]0.7f, [color="#2038d6"]0.0f);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



[color="#3c2480"] glPushMatrix[color="#000000"]();

[color="#2038d6"] [color="#3c2480"]glColor3f[color="#000000"](1.0f[color="#000000"], 0.0f[color="#000000"], 0.0f[color="#000000"]);

[color="#3c2480"]glTranslatef([color="#2038d6"]0.6f, -[color="#2038d6"]0.7f, [color="#2038d6"]0.0f);

[color="#3c2480"]glRotatef([color="#508187"]angle, [color="#2038d6"]0.0f, [color="#2038d6"]1.0f, [color="#2038d6"]0.0f);

[color="#3c2480"] glutSolidTeapot[color="#000000"]([color="#2038d6"]0.15[color="#000000"]);

[color="#3c2480"] glPopMatrix[color="#000000"]();



}

[/font][/font]


I tried adding information before I made the pushMatrix calls as kind of a "reset" but the lighting still will creep across to all the objects. Anyone have any ideas? Attached is a picture. ALso sorry for the lack of code tags, it completely ruins my code when I try and use them.

Share this post


Link to post
Share on other sites
Advertisement
PushMatrix and Popmatrix only effect the transform matrix stack, it doesn't do anything with any enables or lighting options that you set.

If you want to push/pop things like glMaterial, you need to make use of glPushAttrib/glPopAttrib;

For lighting options, this would be done by:


glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT); //<-------------
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, whiteSpecularMaterial);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess);
glColor3f(1.0f, 0.0f, 0.0f);
glTranslatef(-0.6f, 0.7f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glutSolidTeapot(0.15);
glPopAttrib(); //<----------------
glPopMatrix();


Alternatively you could just disable the lighting options after you're done with them.

Share this post


Link to post
Share on other sites
Thank you so much! I hadn't come across that in any of my tutorials yet so I thought maybe I was doing something wrong :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!