开发者

make a 3D spotlight cone matrix

How to create the transformation matrix (4x4) that transforms a cylinder (of height 1 and diameter 1) into a cone that represents my spotlight (position, direction and cutoff angle) ?

--edit--

In other words: how to draw the cone that represents my spotlight by drawing a cylinder through a suitable transformation matrix.

--edit2--

The following (pseudo)code gives me the expected result:

PushMatrix();
LoadIdentity();
Perspective(lightCutoffAngle * 2, 1, 0.001, 10000); // fov, aspect, near, far
LookAt(lightPos.x,lightPos开发者_StackOverflow.y,lightPos.z, lightDir.x,lightDir.y,lightDir.z, 0,0,1);
Matrix44 mat = GetModelViewMatrix();
mat.Invert();
PopMatrix();
MultMatrix(mat);
DrawCone(1,1); // diameter, height

HIH


You don't normally transform cylinder into the cone using matrices. This is because it involves scaling of 2 components where scale factor is dependent on the 3rd component.

I think you can build matrix like that by building "look at" matrix (gluLookAt or D3DXMatrxiLookAtLH) and multiplying it with perspective matrix (gluPerspective or D3DXMatrixPerspectiveFovLH), but I seriously doubt you will be able to render that cylinder with either OpenGL or D3D. This is because in order for projection to work 3D rendering uses 4th vector component - W which is calculated by multiplying vectors with projection matrix. W component is typically hidden, and available only through shaders. Messing with this component normally screws up any geometry, and you can't transform cylinder into cone without using w component. I.e. if you transform cylinder into cone, you will have to use W, this will screw up projection transform.

I suggest to build cylinder normally - from vertices. This isn't hard in both D3D and OpenGL.

If you really want to transform cylinder into cone, writing vertex shader for transforming cylinder will be easier, although making sure all normals are all right might be a problem.

If you want to know which objects hit light cone, use math and collision detection. This isn't hard.

If you want to render objects visible within light cone, make additional render target (or framebuffer), viewport or whatever, and render objects visible from light's point of view.

About light cutoff angle.

Given point P, light position L, and light direction LDir, and cutoff angle you can easily check if P is within light cone. To do that, you'll need cosine of either full or half of cutoff angle (depends on whether cutoff angle is calculated relative to light direction, or determines width of the cone). Make vector PDir = P - L, normalize PDir, and calculate dot product between normalized PDir and normalized LDir, this will give cosine of angle between light direction at point P. IF cosine (between PDir and Ldir) is larger cosine of light cone, then point is within light cone.


I do beleave that you want a matrix something like

x/y 0   0  0
 0  y   0  0
 0  0  z/y 0
 0  0   0  1

or possibly

x(1-y) 0   0    0
  0    1   0    0
  0    0 z(1-y) 0
  0    0   0    1

If you cone is a circle centred at the origin, lying on the z-x axis that has been extruded 1 unit along the y-axis. One of these matrices should work, as the points of the cone move further up the y axis, the more you want to them to move towards the y-axis.

Hopefully this should sort you out, or at least give you a good start... now to rest my head.

UPDATE

As pointed out in the comments, this want work. I think I half jumped the gun. If you replace the x ans z with 1, then multiply the matrix by your vector. you would get something like

x = x/y  or x(1-y)
y =  y
z = z/y  or z(1-y)

UPDATE

To take into account the angle of light, we will scale the radius of our cone but keep it unit length. if we have our angle of light L (this being from its centre point, not edge to edge) we can say that the radius our cone needs to be is tan^-1(L)

so if we factor that in, our final maths should be something like

x = tan^-1(L) * [ x/y  or x(1-y) ]
y =  y
z = tan^-1(L) * [ z/y  or z(1-y) ]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜