#### Archived

This topic is now archived and is closed to further replies.

# Lighting Spotlights

This topic is 5627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I''m trying to make a spotlight show up on a simple triangle on the screen (black in a blue background). I''m trying to make a white spotlight show on the triangle, not light the whole thing. Here is the triangle: pVertices[0].position = D3DXVECTOR3 (-1.0f, -1.0f, 0.0f); pVertices[0].normal = D3DXVECTOR3 (-1.0f, 0.0f, 0.0f); pVertices[1].position = D3DXVECTOR3 (1.0f, -1.0f, 0.0f); pVertices[1].normal = D3DXVECTOR3 (1.0f, 0.0f, 0.0f); pVertices[2].position = D3DXVECTOR3 (0.0f, 1.0f, 0.0f); pVertices[2].normal = D3DXVECTOR3 (0.0f, 0.0f, 0.0f); Here''s the struct for it: struct SpotlightTriangle { D3DXVECTOR3 position; D3DXVECTOR3 normal; }; I tried going though the Direct3D tutorials and it didn''t explain it like I wanted. =/ I''m not entirely sure the normals are right either. Any small little code to just light the triangle with a SPOTLIGHT light would be appriciated. Thanks alot

##### Share on other sites
You can''t get a spot light on a polygon. D3D lights vertices only, not the individual pixels on the polygon. You can get the spotlight''s brightness at each of the three points, but that''s it.

And yes, your normals are wrong. Very wrong. I''m not going to get into the details...

##### Share on other sites
quote:
Original post by Namethatnobodyelsetook
You can''t get a spot light on a polygon. D3D lights vertices only, not the individual pixels on the polygon. You can get the spotlight''s brightness at each of the three points, but that''s it.

And yes, your normals are wrong. Very wrong. I''m not going to get into the details...

Where can I find tutorial about normals ?
Could you explain ?

##### Share on other sites
Here''s how you calculate the normal:
vector a = vertices[2] - vertices[0];
vector b = vertices[1] - vertices[0];
normal = crossproduct(a, b);

Proceeding on a brutal rampage is the obvious choice.

##### Share on other sites
Ok, here goes the basics.Normals are used in 3D solely for lighting calculations.  Theyare always a unit vector, ie:  They all have a length of 1.0For example (1,0,0), (0,1,0), (0.7071, 0, 0.7071) are allvectors of length 1... sqrt(x*x+y*y+z*z) = 1.0When DirectX renders a polygon, the normal at each vertex isused to calculate lighting.  The 3 light values are theninterpolated across the polygon as each pixel is drawn.Normals point outward from the object.  Ignoring the front andback sides, the normals on the top, bottom, left and right wouldbe as follows: +-X    |     (0,1,0) (0,1,0)         Y        |       |(-1,0,0)--+-------+--(1,0,0)          |       |          |       |          |       |(-1,0,0)--+-------+--(1,0,0)          |       |       (0,-1,0)(0,-1,0)    The front face would be pointing towards you, so (0,0,-1), andthe back face would be pointing away from you, so (0,0,1).  Notethat in order to get a cube lit correctly, you'll need 3 copiesof each corner vertex to hold the correct normals.  For examplein the top, right, back corner, you'll need one with a normal tolight the top correctly, one to light the right side correctly,and one to light the back side correctly.  These vertices willall have the same position, but different normals.Now, most meshes aren't as hard edged as cubes, and as such youwon't have to duplicate vertices to get new normals.  You'llwant the normals shared between faces on a curve.  For example,take this arc.  Imagine it's a top down view of a wall, so youcan't see the polygons making it up... but you can see thenormals, as they poke outwards.  You want the object to appearlike a smooth curve, even though it's just a bunch of triangles. +-X            |             (0,0,1) Z                |  (-.5, 0, .87) __+__ (.5, 0, .87)            +-~~     ~~-+           /             -          /               -         +                 +     (-.7,0,.7)            (.7,0,.7)When DX draws the polygons with normals like these, the lightingvalues at each vertex will be calculated as though it was a flatsurface pointing in the direction of the normal.  Then theselighting values are interpolated across the polygon giving it asmooth light change, rather than an abrupt change at eachpolygon.[/CODE][edited by - Namethatnobodyelsetook on January 21, 2003 10:09:12 PM]

##### Share on other sites
So...

D3DXVec3Normalize() just basically scales it down to 1 unit?

##### Share on other sites
quote:
Original post by Anonymous Poster
So...

D3DXVec3Normalize() just basically scales it down to 1 unit?

yes

##### Share on other sites
To add one more thing: if you really want a spotlight on an individual triangle, you must tesselate the triangle. Tesselation means that you break one big triangle into many little triangles. In this way, D3D''s per-vertex lighting will show the spotlight correctly, as it will calculate the lighting for many different points on the large triangle.

Firebird Entertainment

##### Share on other sites
a good way to immitate spotlights over a lot of geometry is a projected texture, it will show on everything that you render with the projected texture stage on (provided its within the projection matrix of the texture) and it can be pretty fast if you can multitexture it well.

• 45
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631373
• Total Posts
2999629
×