opengl - Spot light effect does not work correctly using GLSL shaders -
i'm working on personal graphic engine , started develop spot lights. problem rendering not logical. sake of simplicity cleaned informations light , texture managing inside shaders example below.
here's vertex shader code :
#version 400 layout (location = 0) in vec3 vertexposition; uniform mat4 modelviewmatrix; uniform mat4 mvp; out vec3 vposition; void main(void) { vposition = vec3(modelviewmatrix * vec4(vertexposition, 1.0f)); //eye coordinates vertex position gl_position = mvp * vec4(vertexposition, 1.0f); }
and fragment shader code :
#version 400 in vec3 position; //in eye coordinates layout (location = 0) out vec4 fragcolor; uniform int lightcount; struct spotlight { vec4 position; //already in eye coordinates vec3 la, ld, ls; vec3 direction; float exponent; float cutoff; }; uniform spotlight lightinfos[1]; vec3 getlightintensity(void) { vec3 lightintensity = vec3(0.0f); (int idx = 0; idx < lightcount; idx++) { vec3 lightdirnorm = normalize(vec3(lightinfos[idx].position) - position); vec3 spotdirnorm = normalize(lightinfos[idx].direction); float angle = acos(dot(-lightdirnorm, spotdirnorm)); float cutoff = radians(clamp(lightinfos[idx].cutoff, 0.0f, 90.0f)); vec3 ambient = vec3(0.1f, 0.1f, 0.8f); //color out of spotlight's cone. vec3 spotcolor = vec3(0.1f, 0.8f, 0.1f); //color within spotlight's cone. if (angle < cutoff) { lightintensity = spotcolor; } else { lightintensity = ambient; } } return (lightintensity); } void main(void) { fragcolor = vec4(getlightintensity(), 1.0f); }
here's c++ code send light position properties :
glm::vec4 lightpositionvec = viewmatrix * glm::vec4(lightposition[0], lightposition[1], lightposition[2], lightposition[3]); program->setuniform(std::string("lightinfos[").append(utils::tostring<int>(idx)).append("].position").c_str(), lightpositionvec);
here's spot light properties :
<position x="0.0" y="2.0" z="0.0" w="1" /> <direction x="0.0" y="-1.0" z="0.0" /> <exponent>3.0</exponent> <cutoff>50.0</cutoff>
the view properties :
<camera name="cam1"> <perspective fovy="70.0" ratio="1.0" nearclipplane="0.1" farclipplane="1000.0"/> <lookat> <eye x="0.0" y="50.0" z="50.0" /> <target x="0.0" y="0.0" z="0.0" /> <up x="0.0" y="1.0" z="0.0"/> </lookat> </camera>
i checked these informations before sending them program shader in c++ code , correct.
and here's result :
and if place camera closer plane following camera properties :
<camera name="cam1"> <perspective fovy="70.0" ratio="1.0" nearclipplane="0.1" farclipplane="1000.0"/> <lookat> <eye x="0.0" y="10.0" z="20.0" /> <target x="0.0" y="0.0" z="0.0" /> <up x="0.0" y="1.0" z="0.0"/> </lookat> </camera>
the result following (closer reality still not correct) :
as can see, it's not logical : have spot in x=0, y=1, z=0, downward spot light direction (x=0, y=-1, z=0) don't understand result. plus, vectors in eye coordinates. have impression spot light effect depends position of camera. can me ?
worked several types of lights: directional, spotlight, point light
here code
https://code.google.com/p/jpcsp/source/browse/trunk/src/jpcsp/graphics/shader.vert?r=1639
http://en.wikibooks.org/wiki/glsl_programming/glut/specular_highlights
Comments
Post a Comment