Jump to content

diffuse, per-vertex shader with single lightsource will not work


DudeAwesome
 Share

Recommended Posts

hey guys I´m reading opengl 4.0 shading cook book to get a feeling for shaders and learn the basics but I fail with my first simple diffuse shader example. thats my code:

 

Vertex

 

#version 400

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
out vec3 LightIntensity;
uniform vec4 LightPosition; // Light position in eye coords.
uniform vec3 Kd; // Diffuse reflectivity
uniform vec3 Ld; // Light source intensity
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 MVP; // Projection * ModelView

void main(){

// Convert normal and position to eye coords
vec3 tnorm = normalize( NormalMatrix * VertexNormal);
vec4 eyeCoords = ModelViewMatrix * vec4(VertexPosition,1.0);
vec3 s = normalize(vec3(LightPosition - eyeCoords));
// The diffuse shading equation
LightIntensity = Ld * Kd * max( dot( s, tnorm ), 0.0 );
// Convert position to clip coordinates and pass along
gl_Position = MVP * vec4(VertexPosition,1.0);
}

 

 

Fragment

 

 

#version 400

in vec3 LightIntensity;
layout( location = 0 ) out vec4 FragColor;
void main()
{
FragColor = vec4(LightIntensity, 1.0);

}

 

I attached the shader to a material and to a ground but it is still invisible and the screen is black.

It doesn´t work... why? mhmmm It works... why?

Link to comment
Share on other sites

It's not working for two reasons:

 

1) All those special variables for vertex normal, light position, transformation matrices are not part of GLSL. They are passed in by name by the engine, and every engine has its own naming convention. If you have a look in a Leadwerks shader, the uniforms there have names like vertex_normal, entity_matrix ('M' of the model-view-perspective transform) and projectioncameramatrix (the 'VP'). So the variables you're using in your code don't carry any values. But even if you used the Leadwerks variables, this concrete shader wouldn't work in Leadwerks 3.1 because

 

2) it's written for a forward renderer, and Leadwerks 3.1 has a a deferred renderer. In a deferred renderer, the lighting calculations (here the diffuse shading with the NdotL term) are done for you behind the scenes. The shader's main job is to fill the fragData G buffers for diffuse color, normal, emission etc. that the engine needs for its calculations.

  • Upvote 1
Link to comment
Share on other sites

Thx !

 

you know some good tutorials for deferred shader ? well I wanted to try to read glsl 4.0 Cook Book but its bad when I cant Test the shader Examples :/

 

I really want to get more involved into shaders but its like the holy grail.

It doesn´t work... why? mhmmm It works... why?

Link to comment
Share on other sites

Actually - no, I don't (maybe shadmar and klepto know something?) It might be better to start with a forward renderer to learn shader programming, because everything is right in front of your eyes, and playing around with different light types, lighting models etc is possible. I started digging into shaders with Leadwerks 3.0 (which has a forward renderer), and that was helpful at least for me. And it might be easier to understand what is going on in a deferred renderer if you have done everything for yourself first.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...