No spot of light on cube - opengl

I have made three sources of light and one cube
I don't see a spot of light on faces. It's look like entire polygon is lit.
And i don't know is this posible and cube need more polygons or mayby light settings are bad.
Settings i use.
glShadeModel(GL_SMOOTH);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 150.0f);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 15.0f);

Remember that the fixed-function lighting equation is only evaluated at the vertices of a triangle and interpolated across the fragment. No per-pixel lighting unless you get creative with GL_DOT3_RGB textures.
Therefore if you want to see a nice spotlight highlight on your cube you'll need to subdivide your cube faces so that they're closer to pixel-sized:
EDIT: Also remember to pass in reasonable per-vertex normals. Lighting doesn't work too well without them :)

Related

Precompute lighting on one primitive in opengl and then use on other primitives

Say I have two triangles in an opengl 3d setup, with a specific light source. The first triangle:
glEnable(GL_LIGHT0)
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color)
glBegin(GL_TRIANGLES)
glNormal3fv(n1)
glVertex3f(p1)
glVertex3f(p2)
glVertex3f(p3)
glEnd()
Now I draw the second triangle, at a different position
glBegin(GL_TRIANGLES)
glNormal3fv(n2)
glVertex3f(p4)
glVertex3f(p5)
glVertex3f(p6)
glEnd()
but I want the light applied to this triangle AS IF it was located and had the same normal vector as the first triangle. Obviously it is easy to just change the normal vector, but it seems that I can not change the apparent position relative to the light source without also changing the true position of the triangle. Also, since the light properties of my 3d world will not change in time, I would like to precompute this in the beginning and then store it. Any tips?
Why I want to do this: essentially the vision of the player seeing the 3d world will be distorted in some way, such that the position of all 3d objects will change. However this should then not affect the color of the primitives as computed after applying a light source.

How to enable both side lighting for GL_POINTS with vertex normals

How to enable both side lighting for GL_POINTS in opengl?
It seems glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); is working for only facets, but not GL_POINTS.
What I am doing is supplying both vertices and the vertex normals with lighting enabled. With GL_LIGHT_MODEL_TWO_SIDE disabled, GL_POINTS are lit according to the normal direction. But I cannot enable both sided lighting for GL_POINTS.
Can this be done using OpenGL legacy functions? Or would I have to render both sides by negating all the normals?
Thanks in advance. Please do not comment to use modern OpenGL as that is not the answer to my question, but only a suggestion.
How to enable both side lighting for GL_POINTS in opengl?
You don't. Points and lines do not have sides. Only face primitives (triangles, quads, etc) have sides.
So if you want the lighting computation to reverse the normal if the normal is facing away or something, then yes, you will have to render your geometry twice.

glsl effect on low poly surface

I've got a vertex/fragment shader, point light and attenuation, I need to apply such shader to a cube face and I need to see a change in gradation of colours, if I use an high poly mesh
everything works quite well and the effect it's nice my goal is to have a gradient on this low poly mesh.
I tried to do this gl_FragColor = vec4(n,1) n = normal but I get a solid colour per surface
and this can be the reason why I don't see a gradation?
cheers
It is correct behaviour that you are observing. Cube is perfectly flat, thus it's normals per face vertex are the same.
Note however, that in calculations of Phong lighting you also should use the position of fragment, which is interpolated between 3 (or 4, when using quads) vertices of the given (sub)face. It can be used to calculate angle between light position and eye vector in the given fragment's position.
I've experienced similar problems lately, and I figured out that your cube really needs to shine, if you want to see something non-flat; and I mean literally. Set the shininess to reasonably high value (250-500). You should see a focused, moving point of light on the face that is reflecting directly to you. If not, your lightning shader is probably wrong.

OpenGL Lighting Quads

just a quick question. I have a quad in 3D OpenGL scene. I define a normal to the plane counter clockwise. So that the normal points out one side of the plane. In the direction of my light source. The quad is light but on both sides.
Should it not only be light on one side of the quad? Or is it the fact that a primitive like a quad is finitely thin and thus looks light from both sides. So if i wanted to make a wall I would use two quads. One for each side of the wall.
Thanks
The default OpenGL lighting behavior for two sided polygons is to calculate lighting for the front face and apply it to both sides.
You can get around this by using a front and back polygon with seperate normals for each of your double sided polygons.
Alternatively, you can enable GL_LIGHT_MODEL_TWO_SIDE for lighting calculations using glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE) . See the glLightModel reference for more information.

OpenGL: texture and plain color respond differently to ambient light?

This is a rather old problem I've had with an OpenGL application.
I have a rather complex model, some polygons in it are untextured and colored using a plain color with glColor() and others are textured. Some of the texture is the same color as the untextured polygons and there should be no visible seam between the two.
The problem is that when I turn up the ambient component of the light source, a seam between the two kinds of polygons emerge.
see this image: http://www.shiny.co.il/shooshx/colorBug2.png
The left image is without any ambient light and the right image is with ambient light of (0.2,0.2,0.2).
the RGB value of the color on the texture is identical to the RGB value of the colored faces. The textures alpha is set to 1.0 everywhere.
To shade the texture I use GL_MODULATE.
Can anyone think of a reason why that would happen and of a possible solution?
You mention that you set the color with glColor(), so I assume that GL_COLOR_MATERIAL is on? What setting do you use for glColorMaterial()? In this case it should be GL_AMBIENT_AND_DIFFUSE, so that the glColor() call affects the ambient color as well as the diffuse color. (This is the default.)
You could also try to set all material colours to white (with glMaterial()) before rendering the texture mapped faces. With some settings (don't remember which), the texture itself gets modulated by the current color.
Hope this helps or at least points you into a useful direction.