Jump to content

Problem with virtual light calculation in the shader


norbert
 Share

Recommended Posts

Hello There,

 

I want to let light a sphere with a virtual light. For that I have adapted a shader. But the problem is that the beam moved on the sphere.

 

maybe could give me yes explain what I'm doing wrong someone.

 

thank Norbert




https://youtu.be/ZujlT_cdUTY

i copy and change the standart default.shader

changes in Vertex Shader:
top:
out float Light_Intensity;
const vec3 LightPos = vec3(-100,0,0);

main:
vec3 ecPosition = vec3(modelvertexposition);
vec3 tnorm = normalize(nmat* vertex_normal);
vec3 lightVec2 = normalize(LightPos-ecPosition);
Light_Intensity = max(dot(lightVec2, tnorm), 0.0);


changes in Fragment Shader:
top:
in float Light_Intensity;

main:
fragData0 = outcolor * Light_Intensity; // My Code
fragData2 = vec4(vec3(fragData0),materialflags/255.0); // leuchten lassen


Complett Shader
#version 400
#define MAX_INSTANCES 256
//Uniforms
//uniform mat4 entitymatrix;
uniform vec4 materialcolordiffuse;
uniform mat4 projectioncameramatrix;
uniform mat4 camerainversematrix;
uniform instancematrices { mat4 matrix[MAX_INSTANCES];} entity;
uniform vec4 clipplane0 = vec4(0.0);
//Attributes
in vec3 vertex_position;
in vec4 vertex_color;
in vec3 vertex_normal;
in vec3 vertex_binormal;
in vec3 vertex_tangent;
in vec2 vertex_texcoords0;
//Outputs
//out vec4 ex_color;
out float ex_selectionstate;
//out vec3 ex_VertexCameraPosition;
//out vec3 ex_normal;
//out vec2 ex_texcoords0;
//Tessellation
out vec4 vPosition;
out vec2 vTexCoords0;
out vec3 vNormal;
out vec3 vBinormal;
out vec3 vTangent;
out vec4 vColor;
out float clipdistance0;

// My
out float Light_Intensity;
const vec3 LightPos = vec3(-100,0,0);

void main()
{
mat4 entitymatrix = entity.matrix[gl_InstanceID];
mat4 entitymatrix_=entitymatrix;
entitymatrix_[0][3]=0.0;
entitymatrix_[1][3]=0.0;
entitymatrix_[2][3]=0.0;
entitymatrix_[3][3]=1.0;

vColor=vec4(entitymatrix[0][3],entitymatrix[1][3],entitymatrix[2][3],entitymatrix[3][3]);

vec4 modelvertexposition = entitymatrix_ * vec4(vertex_position,1.0);
//ex_VertexCameraPosition = vec3(camerainversematrix * modelvertexposition);
//Clip planes
if (length(clipplane0.xyz)>0.0001)
{
clipdistance0 = modelvertexposition.x*clipplane0.x + modelvertexposition.y*clipplane0.y + modelvertexposition.z*clipplane0.z + clipplane0.w;
}
else
{
clipdistance0 = 0.0;
}
vPosition = modelvertexposition;
gl_Position = projectioncameramatrix * vPosition;

mat3 nmat = mat3(camerainversematrix[0].xyz,camerainversematrix[1].xyz,camerainversematrix[2].xyz);//39
nmat = nmat * mat3(entitymatrix[0].xyz,entitymatrix[1].xyz,entitymatrix[2].xyz);//40
vNormal = normalize(nmat * vertex_normal);
vBinormal = normalize(nmat * vertex_binormal);
vTangent = normalize(nmat * vertex_tangent);

vTexCoords0 = vertex_texcoords0;

vColor = vec4(entitymatrix[0][3],entitymatrix[1][3],entitymatrix[2][3],entitymatrix[3][3]);
vColor *= vec4(1.0-vertex_color.r,1.0-vertex_color.g,1.0-vertex_color.b,vertex_color.a) * materialcolordiffuse;


// My Code

vec3 ecPosition = vec3(modelvertexposition);
vec3 tnorm	 = normalize(nmat* vertex_normal);
vec3 lightVec2	 = normalize(LightPos-ecPosition);
Light_Intensity = max(dot(lightVec2, tnorm), 0.0);

}

 

#version 400
#define BFN_ENABLED 1
//Uniforms
uniform int lightingmode;
uniform vec2 buffersize;
uniform vec2 camerarange;
uniform float camerazoom;
uniform vec4 materialcolorspecular;
uniform vec4 lighting_ambient;
uniform samplerCube texture15;
//Inputs
in float ex_selectionstate;
in vec2 vTexCoords0;
in vec3 vNormal;
in vec3 vBinormal;
in vec3 vTangent;
in vec4 vColor;
in float clipdistance0;
//Outputs
out vec4 fragData0;
out vec4 fragData1;
out vec4 fragData2;
out vec4 fragData3;

// My
in float Light_Intensity;
void main(void)
{
//Clip plane discard
if (clipdistance0>0.0) discard;

vec4 outcolor = vColor;
fragData0 = outcolor * Light_Intensity; // My Code

vec3 normal=vNormal;

#if BFN_ENABLED==1
//Best-fit normals
fragData1 = texture(texture15,normalize(vec3(normal.x,-normal.y,normal.z)));
fragData1.a = fragData0.a;
#else
//Low-res normals
fragData1 = vec4(normalize(normal)*0.5+0.5,fragData0.a);
#endif
fragData1.a = materialcolorspecular.r * 0.299 + materialcolorspecular.g * 0.587 + materialcolorspecular.b * 0.114;
//int materialflags=1;
//if (ex_selectionstate>0.0) materialflags += 2;
//fragData2 = vec4(0.0,0.0,0.0,materialflags/255.0);

int materialflags=1;
if (ex_selectionstate>0.0) materialflags += 2;
//fragData2 = vec4(0.0,0.0,0.0,materialflags/255.0);
fragData2 = vec4(vec3(fragData0),materialflags/255.0); // leuchten lassen
}

Edited by norbert
Link to comment
Share on other sites

So the problem was solved .... I think ....

 

Changes in vertex shader:

 

// My Code

vec3 lightVec2 = normalize(LightPos-vec3(modelvertexposition));

mat3 normalMatrix = transpose(inverse(mat3(entitymatrix_)));

vec3 Normal2 = normalMatrix * vertex_normal;

Light_Intensity = max(dot(Normal2, lightVec2), 0.0)*10;

  • Upvote 1
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...