Jump to content
  • Advertisement

kfriddile

Member
  • Content Count

    7
  • Joined

  • Last visited

Community Reputation

100 Neutral

About kfriddile

  • Rank
    Newbie
  1. kfriddile

    [glsl] more spotlight problems

    Quote:Original post by karwosts Oh ok I think I have another idea, I think you're doing things in the vertex shader that you should be doing in the fragment shader. Take this for example: vec3 lightVector = lightPosition - gl_Position.xyz; vertexLightDistance = length( lightVector ); You're probably assuming that for every fragment this is going to give you the distance between the light and the fragment, but it's not working that way. Assume for a moment that your light is fixed in world position at 0,1,0, and all your four vertices are located at (+/-1,0,+/-1). The way the attributes work is that it calculates the value at each vertex, and interpolates them to get the value per fragment. But if you look in this case, the distance from the light to each vertex is exactly the same. Therefore when you interpolate the corners into your fragment shader, you will have the exact same vertexLightDistance for each fragment, when the distance is clearly less for the vertices nearer the camera. I think you may want to try calculating the light vector inside the fragment shader as well, I think this is not necessarily correct for this operation either. This was the problem...it all works now! I have to wonder why all of the tutorials I found computed those things in the vertex shader though, including the most common one...
  2. kfriddile

    [glsl] more spotlight problems

    Quote:Original post by karwosts Well yes but it's possible it looks correct in the client but that they aren't being loaded correctly. You can clearly see that there is some difference between the two triangles on your ground plane. The only data that varies per triangle are your attributes, which are position, normal, and texture coordinate, so I think something must be incorrect about your attributes. If the problem was in your fragment shader only, you would not see the discontinuity from one face to another. It's just a theory, but that's what looks suspect to me based on the video. I hardcoded the normal in the vertex shader like so: vertexNormal = mat3x3(modelViewMatrix) * vec3(0,1,0); // We assume no non-uniform scaling here and the lighting on the plane looks exactly the same :(
  3. kfriddile

    [glsl] more spotlight problems

    Quote:Original post by karwosts Something wrong with the normals maybe? You can clearly see a discontinuity between the two triangles where it's getting different data. The only thing that I can think of that could be different from one triangle to the next is the normals and the positions, and the positions look correct. Post your opengl client code maybe? Thanks for the response. The normal for each vertex of the plane is (0,1,0) which I have verified in the debugger at the point where they are uploaded to the shader.
  4. I've run into another problem with my per-pixel spotlight shader. I've attached a video that demonstrates the issue pretty clearly. As you can see, the plane is made up of two large triangles. As I move the light over the plane, it distorts strangely. The light is always pointing straight down at the plane. The guy standing on the plane is fairly high-poly, and the the lit area on him looks like it might be correct, but maybe I just can't tell because he's so high poly? All values being passed to the shader have been verified in the debugger. I'm really pulling my hair out over this one because my shader code looks like every tutorial I've seen. Here's the video: video You can't see the wireframe well in the video, but the split between the plane's triangles runs from the lower-left to the upper-right (you can see the lit area distort when moving across it) light parameters: "ambientColor" : [0,0,0,255], "diffuseColor" : [255,255,255,255], "specularColor" : [255,255,255,255], "coneAngle" : 15, // degrees "rateOfDecay" : 0, "constantAttenuation" : 1, "linearAttenuation" : 0, "quadraticAttenuation" : 0 vertex shader: #version 130 uniform mat4x4 projectionMatrix; uniform mat4x4 modelViewMatrix; uniform mat2x2 textureMatrix; uniform vec4 globalAmbient; uniform vec3 lightPosition; // in view space uniform vec4 lightAmbient; uniform vec4 lightDiffuse; uniform vec4 materialAmbient; uniform vec4 materialDiffuse; in vec4 position; in vec2 textureCoordinate; in vec3 normal; out vec2 vertexTextureCoordinate; out vec3 vertexNormal; out vec4 vertexGlobalAmbient; out vec3 vertexLightDirection; out float vertexLightDistance; out vec4 vertexAmbientColor; out vec4 vertexDiffuseColor; void main() { vertexNormal = mat3x3(modelViewMatrix) * normal; // We assume no non-uniform scaling here // Compute the light direction gl_Position = modelViewMatrix * position; vec3 lightVector = lightPosition - gl_Position.xyz; vertexLightDirection = normalize( lightVector ); vertexLightDistance = length( lightVector ); // Compute ambient and diffuse colors vertexGlobalAmbient = globalAmbient * materialAmbient; vertexAmbientColor = materialAmbient * lightAmbient; vertexDiffuseColor = materialDiffuse * lightDiffuse; gl_Position = projectionMatrix * gl_Position; vertexTextureCoordinate = textureMatrix * textureCoordinate; } fragment shader: #version 130 uniform sampler2D texture; uniform vec4 lightSpecular; uniform float lightConstantAttenuation; uniform float lightLinearAttenuation; uniform float lightQuadraticAttenuation; uniform vec3 lightDirection; // in view space uniform vec3 lightHalfVector; // in view space uniform float lightConeAngleCosine; uniform uint lightRateOfDecay; uniform vec4 materialSpecular; uniform int materialShininess; in vec2 vertexTextureCoordinate; in vec3 vertexNormal; in vec4 vertexGlobalAmbient; in vec3 vertexLightDirection; in float vertexLightDistance; in vec4 vertexAmbientColor; in vec4 vertexDiffuseColor; out vec4 fragmentColor; void main() { fragmentColor = vertexGlobalAmbient; vec3 normal = normalize( vertexNormal ); float normalDotLightDirection = max( dot( normal, normalize(vertexLightDirection) ), 0 ); if( normalDotLightDirection > 0 ) { float spotEffect = dot( normalize(lightDirection), normalize(-vertexLightDirection) ); if( spotEffect > lightConeAngleCosine ) { spotEffect = pow( spotEffect, lightRateOfDecay ); float attenuation = spotEffect / ( lightConstantAttenuation + lightLinearAttenuation * vertexLightDistance + lightQuadraticAttenuation * vertexLightDistance * vertexLightDistance ); fragmentColor += attenuation * ( vertexDiffuseColor * normalDotLightDirection + vertexAmbientColor ); float normalDotHalfVector = max( dot(normal, lightHalfVector), 0 ); fragmentColor += attenuation * materialSpecular * lightSpecular * pow( normalDotHalfVector, materialShininess ); } } vec4 texel = texture2D( texture, vertexTextureCoordinate ); fragmentColor *= texel; fragmentColor.a = texel.a; }
  5. I'm having a lot of trouble getting my per-pixel spotlight shader to work right. I've attached the code as well as a video demonstrating the problem. The little floating guy in the video represents the position and orientation of the light. As you can see, the problem is that the circle on the ground from the light is the wrong shape, and actually changes shape when I rotate the camera even though the light is never moved. This only happens when I rotate the camera, not when I simply translate it. I've verified that the normals of the ground's vertices are all [0,1,0] as they should be. The relevant values for the light are: cone angle : 15, rate of decay : 0, constant attenuation : 1, linear attenuation" : 0, quadratic attenuation : 0 video link vertex shader: #version 130 uniform mat4x4 projectionMatrix; uniform mat4x4 modelViewMatrix; uniform mat2x2 textureMatrix; uniform vec4 globalAmbient; uniform vec3 lightPosition; // in view space uniform vec4 lightAmbient; uniform vec4 lightDiffuse; uniform vec4 materialAmbient; uniform vec4 materialDiffuse; in vec4 position; in vec2 textureCoordinate; in vec3 normal; out vec2 vertexTextureCoordinate; out vec3 vertexNormal; out vec4 vertexGlobalAmbient; out vec3 vertexLightDirection; out vec3 vertexHalfVector; out float vertexLightDistance; out vec4 vertexAmbientColor; out vec4 vertexDiffuseColor; void main() { vertexNormal = mat3x3(modelViewMatrix) * normal; // We assume no non-uniform scaling here // Compute the light direction gl_Position = modelViewMatrix * position; vec3 lightVector = lightPosition - gl_Position.xyz; vertexLightDirection = normalize( lightVector ); vertexLightDistance = length( lightVector ); vertexHalfVector = normalize( vertexLightDirection + gl_Position.xyz ); // Compute ambient and diffuse colors vertexGlobalAmbient = globalAmbient * materialAmbient; vertexAmbientColor = materialAmbient * lightAmbient; vertexDiffuseColor = materialDiffuse * lightDiffuse; gl_Position = projectionMatrix * gl_Position; vertexTextureCoordinate = textureMatrix * textureCoordinate; } fragment shader: #version 130 uniform sampler2D texture; uniform vec4 lightSpecular; uniform float lightConstantAttenuation; uniform float lightLinearAttenuation; uniform float lightQuadraticAttenuation; uniform vec3 lightDirection; uniform float lightConeAngleCosine; uniform uint lightRateOfDecay; uniform vec4 materialSpecular; uniform int materialShininess; in vec2 vertexTextureCoordinate; in vec3 vertexNormal; in vec4 vertexGlobalAmbient; in vec3 vertexLightDirection; in vec3 vertexHalfVector; in float vertexLightDistance; in vec4 vertexAmbientColor; in vec4 vertexDiffuseColor; out vec4 fragmentColor; void main() { fragmentColor = vertexGlobalAmbient; vec3 normal = normalize( vertexNormal ); float normalDotLightDirection = max( dot( normal, normalize(vertexLightDirection) ), 0 ); if( normalDotLightDirection > 0 ) { float spotEffect = dot( normalize(lightDirection), normalize(-vertexLightDirection) ); if( spotEffect > lightConeAngleCosine ) { spotEffect = pow( spotEffect, lightRateOfDecay ); float attenuation = spotEffect / ( lightConstantAttenuation + lightLinearAttenuation * vertexLightDistance + lightQuadraticAttenuation * vertexLightDistance * vertexLightDistance ); fragmentColor += attenuation * ( vertexDiffuseColor * normalDotLightDirection + vertexAmbientColor ); vec3 halfVector = normalize( vertexHalfVector ); float normalDotHalfVector = max( dot(normal, halfVector), 0 ); fragmentColor += attenuation * materialSpecular * lightSpecular * pow( normalDotHalfVector, materialShininess ); } } vec4 texel = texture2D( texture, vertexTextureCoordinate ); fragmentColor *= texel; fragmentColor.a = texel.a; }
  6. My per-pixel spotlight shader isn't working right. The lit area moves around when I move the camera even though the light's position doesn't change. Even then, the lit area distorts strangely as it moves over the geometry. Here's the vertex shader: #version 130 uniform mat4x4 projectionMatrix; uniform mat4x4 modelViewMatrix; uniform mat2x2 textureMatrix; uniform vec4 globalAmbient; uniform vec3 lightPosition; // in view space uniform vec4 lightAmbient; uniform vec4 lightDiffuse; uniform vec4 materialAmbient; uniform vec4 materialDiffuse; in vec4 position; in vec2 textureCoordinate; in vec3 normal; out vec2 vertexTextureCoordinate; out vec3 vertexNormal; out vec4 vertexGlobalAmbient; out vec3 vertexLightDirection; out vec3 vertexHalfVector; out float vertexLightDistance; out vec4 vertexAmbientColor; out vec4 vertexDiffuseColor; void main() { vertexNormal = mat3x3(modelViewMatrix) * normal; // We assume no non-uniform scaling here // Compute the light direction gl_Position = modelViewMatrix * position; vec3 lightVector = lightPosition - gl_Position.xyz; vertexLightDirection = normalize( lightVector ); vertexLightDistance = length( lightVector ); vertexHalfVector = normalize( vertexLightDirection + gl_Position.xyz ); // Compute ambient and diffuse colors vertexGlobalAmbient = globalAmbient * materialAmbient; vertexAmbientColor = materialAmbient * lightAmbient; vertexDiffuseColor = materialDiffuse * lightDiffuse; gl_Position = projectionMatrix * gl_Position; vertexTextureCoordinate = textureMatrix * textureCoordinate; } and the fragment shader: #version 130 uniform sampler2D texture; uniform vec4 lightSpecular; uniform float lightConstantAttenuation; uniform float lightLinearAttenuation; uniform float lightQuadraticAttenuation; uniform vec3 lightDirection; uniform float lightConeAngleCosine; uniform uint lightRateOfDecay; uniform vec4 materialSpecular; uniform int materialShininess; in vec2 vertexTextureCoordinate; in vec3 vertexNormal; in vec4 vertexGlobalAmbient; in vec3 vertexLightDirection; in vec3 vertexHalfVector; in float vertexLightDistance; in vec4 vertexAmbientColor; in vec4 vertexDiffuseColor; out vec4 fragmentColor; void main() { fragmentColor = vertexGlobalAmbient; vec3 normal = normalize( vertexNormal ); float normalDotLightDirection = max( dot( normal, normalize(vertexLightDirection) ), 0 ); if( normalDotLightDirection > 0 ) { float spotEffect = dot( normalize(lightDirection), normalize(-vertexLightDirection) ); if( spotEffect > lightConeAngleCosine ) { spotEffect = pow( spotEffect, lightRateOfDecay ); float attenuation = spotEffect / ( lightConstantAttenuation + lightLinearAttenuation * vertexLightDistance + lightQuadraticAttenuation * vertexLightDistance * vertexLightDistance ); fragmentColor += attenuation * ( vertexDiffuseColor * normalDotLightDirection + vertexAmbientColor ); vec3 halfVector = normalize( vertexHalfVector ); float normalDotHalfVector = max( dot(normal, halfVector), 0 ); fragmentColor += attenuation * materialSpecular * lightSpecular * pow( normalDotHalfVector, materialShininess ); } } vec4 texel = texture2D( texture, vertexTextureCoordinate ); fragmentColor *= texel; fragmentColor.a = texel.a; }
  • 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!