Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working a spotlight for my deferred renderer and I'm having trouble with matching the mesh to the visual representation of the light. Right now my mesh is a cone, the apex of the cone is at (0,0,0), it has a height of 1 and a radius of 1. The direction of this cone is (0,-1,0) when the rotation is (0,0,0).

The relevant GLSL code:

float spot_alpha = dot(-l,normalize(vec3(0,-1,0) )); // <------
float inner_alpha = cos(light.falloff);
float outer_alpha = cos(light.radius);
float spot = clamp((spot_alpha - outer_alpha) / (inner_alpha - outer_alpha),0.,1.);

As you can see, the GLSL code uses a direction to define the area to be lit, so I could get this direction as a uniform, but I would need to find a rotation from that for the mesh to follow, or I can get the rotation and find a direction, but I don't know how to do either of these things.

Can I rotate a direction? How do I do it? If I can't, is there another solution for this problem?

share|improve this question
    
If I understand correctly, you should construct a rotation matrix and upload it to the shader. What language or engine is being used to manage the shader? –  DBRalir Feb 5 '13 at 13:21
    
@DBRalir I'm using C++, but I tried vec3(0,-1,0) * model3x3 //without scale and it didn't work, is that what you mean? –  Luke B. Feb 5 '13 at 13:27

1 Answer 1

up vote 3 down vote accepted

Assuming C++ with GLM...

C++:

//Construct a rotation matrix to rotate 'degrees' around a certain axis
glm::mat4 rotation = glm::rotate(glm::mat4(1.f), degrees, glm::vec3(/* axis */));

//Upload to shader 'program' as uniform 'rotation'
GLint loc = glGetUniformLocation(program, "rotation");
glUniformMatrix4fv(loc, 1, GL_FALSE, &rotation[0][0]);

GLSL:

uniform mat4 rotation;

...
//Rotate the vector
vec4 result = rotation*vec4(0,-1,0,1);
//Chop off the W component and normalize
vec3 direction = normalize(result.xyz);
//Use the rotated vector
float spot_alpha = dot(-l,direction);
...

Of course, you could also calculate direction entirely in the C++ code and just upload it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.