Jump to content
  • Advertisement
Sign in to follow this  
Eniak19

OpenGL How to set the Lights

This topic is 3947 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

i want to ues the light in OpenGL and VC in effect,but when i done thoes code work below,nothing happens,what's going on?where is my fault? /********************************************************** // my drawing works glLoadIdentity(); glTranslatef(0, 0, m_zMove); glRotatef(m_xAngle, 1.0f,0.0f,0.0f); glRotatef(m_yAngle, 0.0f,1.0f,0.0f); glutSolidSphere(1.2,20.0,20.0); /*********************************************************** // my light setting GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 10.0, 10.0, 10.0, 0.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); GLfloat matSpecular[] = { 1.0f, 0.0f, 0.0f, 1.0f}; GLfloat matShininess[] = { 50.0f}; GLfloat matAmbient[] = { 0.25f, 0.25f, 0.25f, 1.0f}; GLfloat matDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f}; glMaterialfv(GL_FRONT, GL_SPECULAR, matSpecular); glMaterialfv(GL_FRONT, GL_SHININESS, matShininess); glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiffuse); glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);

Share this post


Link to post
Share on other sites
Advertisement
Depends on what you want to achieve.

Some thoughts:

1. I hope you set the light before rendering anyhing.

2. Your ambient is already bright white so there's nothing to light up. Try reducing the ambient component to, say, 0.1 or so. Keep in mind: without lighting you'll see all primitives in their given color, with lighting all primitives' colors are multiplied by the ambient component and then brightened where the light "touches" them.

3. Your last light position parameter is 0.0 which indicates a directional light. You'd have to specify a direction for that light.

Share this post


Link to post
Share on other sites
yeah.it simmes that my light works here,unless the words glEnable(GL_LIGHTING); add below the glEnable(GL_LIGHT0);,however,still some problem occures.

1 i set a panel use those code,but no effect occure,i can see is a only black panel,i wanne some colors
// glDisable(GL_LIGHTING);
glBegin(GL_POLYGON);
glVertex3i( 16, 0, -16);
glColor3i(0,20,100);
glVertex3i( 16, 0, 16);
glColor3i(0,20,100);
glVertex3i(-16, 0, 16);
glColor3i(0,20,100);
glVertex3i(-16, 0, -16);
glColor3i(0,20,100);
glEnd();
// glEnable(GL_LIGHTING);

2 more over,when turn to anothe side of the panel,as mentioned with side A,i can see a ball,which was drawn at the side B,i can see it throw the panel,but at some angles,i cant see,it is happens only if i took the oppsite of the normal of the panel

Share this post


Link to post
Share on other sites
1.) You need a material for the panel to be lit. Try glEnable(GL_COLOR_MATERIAL) and glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) for using the vertex colors as ambient and diffuse material.

2.) I'm not sure I understood what you mean. If you enabled backface culling (or wireframe mode for one side - back or front) that would be perfectly reasonable. Could you provide some image or screenshot of what's going on?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord_Evil
1.) You need a material for the panel to be lit. Try glEnable(GL_COLOR_MATERIAL) and glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) for using the vertex colors as ambient and diffuse material.

2.) I'm not sure I understood what you mean. If you enabled backface culling (or wireframe mode for one side - back or front) that would be perfectly reasonable. Could you provide some image or screenshot of what's going on?



first, what you told me works here,but not very looks real

second,i want to say is that sometimes,i can see the object via a opaque panel,and this occures only at some angles


/***********************************************
// those German are the Google's work

zuerst was du mir erklärtest, arbeitet hier, aber schaut nicht sehr real

zweitens möchte ich sagen bin der manchmal, kann ich den Gegenstand über eine undurchlässige Verkleidung sehen, und dieses occures nur in einigen Winkeln

Share this post


Link to post
Share on other sites
Please the FAQ for information on how to upload screenshots. It's basically: upload them somewhere and include a link to your posts.

To your other questions:

1. I'm not sure of what you mean with "it looks not very real". Don't the light show up like you want (maybe incorrect shading, maybe a spotlight on the panel with vertex lighting, ...)?

A screenshot of what you get + some description / image of what you want would be helpful.

2. Do you mean this?: You have that opaque panel and look at it from the front. At the back (behind it from your position) is a sphere. You should not see it unless you move it out of the way. Do you see it, if you only rotate the camera and the part of the panel that comes closer to the camera "suddenly" disappears? If that's the case you're like too close to the panel (it's located at height 0, is your camera still at position (0/0/0)?) and what happens is clipping at the near clipping plane (if your camera is still at (0/0/0) and your panel is what you described earlier (those (+/-16, 0, +/-16) vertices) your camera is infact "inside" the panel or too close to not have it be clipped away at the near clipping pane when looking down at it).

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!