[GLSL] Spotlight an fester Position in der Szene
-
Hallo,
ich arbeite gerade an einen einfachen Spotlight Shader.
Alles funktioniert,bis auf die Positionierung des Spotlights.
Ich will,dass das Spotlight an einem festen Punkt in der Szene bleibt, was nicht klappt (das Licht scheint sich relativ zur Kamera zu bewegen).Ich bin für jeden Tipp dankbar!
uniform mat4 matView; // view matrix uniform vec4 lightPositionOC ; // in object coordinates uniform vec3 spotDirectionOC; // in object coordinates uniform float spotCutoff; // in degrees void main(void) { vec3 lightPosition; vec3 spotDirection; vec3 lightDirection; float angle; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // Transforms light position and direction into eye coordinates lightPosition = (lightPositionOC * gl_ModelViewMatrix).xyz; spotDirection = vec3( vec4(spotDirectionOC,1) * matView); spotDirection = normalize(spotDirection); // Calculates the light vector (vector from light position to vertex) vec4 vertex = gl_ModelViewMatrix * gl_Vertex; lightDirection = normalize(lightPosition.xyz -vertex.xyz); // Calculates the angle between the spot light direction vector and the light vector angle = dot(normalize(spotDirection), normalize(lightDirection)); angle = max(angle,0); // Test whether vertex is located in the cone if(acos (angle) < radians(spotCutoff)) gl_FrontColor = vec4(1,1,0,1); // lit (yellow) else gl_FrontColor = vec4(0,0,0,1); // unlit(black) }
-
weil du alles in den view space transformierst. mach die beleuchtung in world space und dann sollte es um einiges einfacher sein und problemlos funzen.
-
rapso schrieb:
weil du alles in den view space transformierst. mach die beleuchtung in world space und dann sollte es um einiges einfacher sein und problemlos funzen.
Kannst du das bitte konkretisieren bzw an Code zeigen?
Danke!
-
rapso schrieb:
weil du alles in den view space transformierst. mach die beleuchtung in world space und dann sollte es um einiges einfacher sein und problemlos funzen.
Hab ich so gemacht und funktioniert. Danke!
**Ich hab jetzt nur noch ein paar Verständnisfragen:
* Warum muss das im World Space sein bzw. warum ist es einfacher als im View Space?
* Bin ich richtig in der Annahme,dass das mit dem World Space auch analog für Point Lights gilt?
* Was müsste ich machen,wenn ich die Beleuchtung im View Space berechnen will?**
Entschuldigung für meine blöden Fragen,aber ich tu mir mit den verscheidenen Coordinate Spaces noch schwer :( (eine Anschauliche Erklärung wäre hilfreich) Hier der verbesserte und funktionierende Code: void main(void) { vec3 lightPosition; vec3 spotDirection; vec3 lightDirection; float angle; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // Transforms light position and direction into eye coordinates lightPosition = (lightPositionOC * matWorld).xyz; spotDirection = vec3(vec4(spotDirectionOC,1) * matWorld); spotDirection = normalize(spotDirection); // Calculates the light vector (vector from light position to vertex) vec4 vertex = gl_ModelViewMatrix * gl_Vertex; lightDirection = normalize( (gl_Vertex * matWorld).xyz - lightPosition.xyz); // Calculates the angle between the spot light direction vector and the light vector angle = dot( normalize(spotDirection), normalize(lightDirection)); angle = max(angle,0); // Test whether vertex is located in the cone if(acos (angle) > radians(spotCutoff)) gl_FrontColor = vec4(0,0,0,1); // unlit(black) else gl_FrontColor = vec4(1,1,0,1); // lit (yellow) }
Vielen Dank im Vorraus!
-
lightPosition = (lightPositionOC * matWorld).xyz; spotDirection = vec3(vec4(spotDirectionOC,1) * matWorld);
1. dein licht sollte doch schon im world space sein, wozu noch eine transformation? was soll "matWorld" ueberhaupt in diesem fall sein? bitte lass es nicht die transformationsmatrix vom objekt sein das du beleuchten willst
2. eigentlich sollte LightDirection.w==0 und LightPosition.w==1 (du scheinst es genau andersrum zu haben)lightPosition = lightPositionOC.xyz; spotDirection = spotDirectionOC.xyz;
_Jemand schrieb:
* Warum muss das im World Space sein bzw. warum ist es einfacher als im View Space?
muss es nicht, aber licht ist doch schon im world space, wozu also transformieren?
_Jemand schrieb:
* Bin ich richtig in der Annahme,dass das mit dem World Space auch analog für Point Lights gilt?
ja, alle lichter sollten damit funktionieren.
_Jemand schrieb:
* Was müsste ich machen,wenn ich die Beleuchtung im View Space berechnen will?
dann solltest du nur mit dem viewspace transoformieren, nicht noch mit dem model space, das macht (wie ganz oben schon gesagt) eigentlich keinen sinn.
-
Ok, alles klar. Danke!
rapso schrieb:
lightPosition = (lightPositionOC * matWorld).xyz; spotDirection = vec3(vec4(spotDirectionOC,1) * matWorld);
1. dein licht sollte doch schon im world space sein, wozu noch eine transformation? was soll "matWorld" ueberhaupt in diesem fall sein? bitte lass es nicht die transformationsmatrix vom objekt sein das du beleuchten willst
2. eigentlich sollte LightDirection.w==0 und LightPosition.w==1 (du scheinst es genau andersrum zu haben)lightPosition = lightPositionOC.xyz; spotDirection = spotDirectionOC.xyz;
matWorld war (zufällig) die Einheitsmatrix,deshalb hat es funktionert.