GLSL per pixel dot shader

VC ++, OpenGL, SDL

I am wondering if there is a way to achieve smoother shading across one square of the geometry. Right now, the shading looks smooth with my spotlight, however the intensity rises along the diagonal unit [/] face. Lighting is basically an invisible intermediate vertex.

enter image description here

enter image description here

enter image description here

This is what happens when the light moves from left to right.

When I move the light on the surface, it does it sequentially. It turns brighter at each vertex and disappears from there.

Was I forced to break up the unit to achieve a smoother, more radial hue? or is there a method around this?

Here are the shaders that I use:

vert

 varying vec3 vertex_light_position; varying vec3 vertex_normal; void main() { vertex_normal = normalize(gl_NormalMatrix * gl_Normal); vertex_light_position = normalize(gl_LightSource[0].position.xyz); gl_FrontColor = gl_Color; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } 

fragmentation

 varying vec3 vertex_light_position; varying vec3 vertex_normal; void main() { float diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0); gl_FragColor = gl_Color * diffuse_value; } 

My geometry in case anyone wonders:

 glBegin(GL_QUADS); glNormal3f(0.0f, 0.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(pos_x, pos_y - size_y, depth); glTexCoord2f(1.0f, 1.0f); glVertex3f(pos_x + size_x, pos_y - size_y, depth); glTexCoord2f(1.0f, 0.0f); glVertex3f(pos_x + size_x, pos_y, depth); glTexCoord2f(0.0f, 0.0f); glVertex3f(pos_x, pos_y, depth); glEnd(); 
+4
source share
3 answers

There are several things that I see as possible problems.

If I am mistaken, you use normalize(gl_LightSource[0].position.xyz); to calculate the light vector, but this is based solely on the position of the light, and not on the top on which you work. This means that the value will be the same for each vertex and will change only on the basis of the current model representation matrix and light position. I think that computing the light vector by doing something like normalize(glLightSource[0].position.xyz - (gl_ModelViewMatrix * gl_Vertex).xyz) will be closer to what you want.

Secondly, you must normalize your vectors in the fragment shader, as well as in the vertex shader, since interpolation of two unit vectors is not guaranteed as a unit vector itself.

+5
source

I think the problem is with the light vector ...

I suggest using:

 vec3 light_vector = normalize(gl_LightSource[0].position.xyz - vertex_pos) 

vertex_pos can be calculated using:

 vertex_pos = gl_ModelViewMatrix * gl_Vertex 

Please note that all vectors must be in the same space (camera, world, object)

+2
source

Am I forced to occupy a unit to achieve a smoother, more radial shadow? or is there a method around this?

No, you can do whatever you want. The only code you need to change is the fragment shader. Try playing with him and see if you get the best score.

For example, you can do this:

 diffuse_value = pow(diffuse_value, 3.0); 

as described here .

+1
source

Source: https://habr.com/ru/post/1386661/


All Articles