PDA

View Full Version : Graphical lighning



NecroDOME
08-12-2006, 06:08 PM
I'm looking for an routine/example/code snippet where the lightning is a texture. Something like static texturing, but then dynamic. Like projecting headlights of a car, but for omni's. (doensn't have to cast shadows but would be nice).

JernejL
08-12-2006, 11:05 PM
you are describing projected textures + shadow mapping.

http://en.wikipedia.org/wiki/Shadow_mapping

see: http://www.delphi3d.net/listfiles.php?category=4

Specifically these two:

http://www.delphi3d.net/download/shadowmap.zip
http://www.delphi3d.net/download/projshadow.zip

chronozphere
09-12-2006, 07:07 PM
I have been working on a planar-texturemapping routine and when i bumped into this topic, i realized that it could be usefull for shadow/lightmapping. :) :razz:

So i have made a nice 'statue of liberty' texture and i casted the shadow onto my scene. This is the result:

http://www.techzine.nl/f/g/36261phpcVogJP.jpg
http://www.techzine.nl/f/g/36261phpLqwAua.jpg

It isn't perfect.. i know, but its a start.
The images are not high-quality, but at least they are resized. ;)

So what do you think of it?? :razz:

JernejL
09-12-2006, 07:45 PM
So what do you think of it?? :razz:

it is not real-time and self-shading and dynamic like shadow mapping...

chronozphere
09-12-2006, 09:52 PM
yeah i know... but i dont know how to create a shadowmap in real time :(

does anyone know a good D3D tutorial???

JernejL
09-12-2006, 11:02 PM
yeah i know... but i dont know how to create a shadowmap in real time :(

does anyone know a good D3D tutorial???

no idea, but i posted links to two opengl ones ;)

Clootie
10-12-2006, 10:00 PM
Well, how about ShadowMap example from DirectX SDK?

JSoftware
10-12-2006, 10:32 PM
For omni lights I(at the moment) see no other way than projecting a texture for each side of a cube...

My brain can't figure out if you are actually able to project a light texture using some sort of environment mapping scheme like sphere mapping or dualparaboloid. I don't mean like you would for shadow mapping as that is not really about projecting textures but instead bending the perspective which is the opposite of what you want as i understand it

NecroDOME
10-12-2006, 11:17 PM
Thanks for your responses :D !! Had a busy weekend with my girl, so I will take a look at the stuff tomorrow!!

NecroDOME
11-12-2006, 08:26 PM
@chronozphere, I think thats what I'm looking for :D
I want to mainly use it for some effects like flickering lights, water casting on walls, etc.

Is it DirectX, any source/resources?

Currently I don't looking for any shadow things. just a picture to project on walls. The shadows comes later on...

WILL
11-12-2006, 08:30 PM
ooow, reflected light off water in a darkened room is an awesome effect! :D

chronozphere
12-12-2006, 05:55 PM
@Necrodome: I am glad you like it. :razz:

So i will explain how i did my planar texture mapping:

The whole idea is to set up the so called "UV mapping space". This is a vectorspace where the X and Y values of a vertex are equal to the desired U and V coordinates.
The Normal parameter is equal to the Z axis of this space and the XY plane of this space is the projection plane.
I used a few rotations to set this space up.. i hope you understand it. ;)

Secondly i have added a piece of code to make sure that only the desired texture tiles are projected.
The texture coords looks like this:

0.0 0.0 0.0 1.0 2.0 3.0 3.0 3.0
0.0 0.0 0.0 1.0 2.0 3.0 3.0 3.0
0.0 0.0 0.0 1.0 2.0 3.0 3.0 3.0
0.0 0.0 [0.0 1.0 2.0 3.0] 3.0 3.0
0.1 0.1 [0.1 1.1 2.1 3.1] 3.1 3.1
0.2 0.2 [0.2 1.2 2.2 3.2] 3.2 3.2
0.2 0.2 0.2 1.2 2.2 3.2 3.2 3.2
0.2 0.2 0.2 1.2 2.2 3.2 3.2 3.2
0.2 0.2 0.2 1.2 2.2 3.2 3.2 3.2

The area between brackets represents the tiles that are projected. The transparent border of the texture is stretched over the rest of the texture so nothing is visible there.

Ok?©.. here is my code;) :


{This function generates a UV mapping matrix and transforms all vertices UV mapping space.
UV mapping space is a space where the X and Y of the vertex are equal to the desired UV coordinates

Center: the center of the projection plane
Normal: the normal of the projection plane (is equal to the Z axis in UV mapping space)
sizeu,sizev: the size of each projected image/tile on the objects in the scene
uoffset,voffset: if you want to move the tiles on the plane... use these offsets.
utiles,vtiles: the ammount of images/tiles that is draw in the U or V dimention
to draw an infinit plane of tiles, pass 0 to these parameters
}
procedure Primitive_TextureMapping_plane(tex: byte; Center,Normal: TBF_Vector; sizeu,sizev,uoffset,voffset: single; utiles,vtiles: word);
var mat,mat2: TBF_Matrix;
axis,vec: TBF_Vector;
angle: single;
i: integer;
begin
//normalize plane normal
normal := Normalize(normal);

//add translation... so the "Center" will will be the actual center of our UV mapping space
D3DXMatrixTranslation(mat,-Center.x,-Center.y,-Center.z);

//convert plane normal to a z-rotation and add it to the matrix
//The axis is the intersection line between the original (to center translated) space and our plane.
//The rotation makes sure that this axis runs parallel to the X axis of our UV mapping space.
axis := Normalize(Crossproduct(Normal,BF_Vector(0,0,1)));
angle := Arccos(DotProduct(axis,BF_Vector(1,0,0)));

//make sure that the rotation is not restricted to 180 degrees
vec := Crossproduct(axis,BF_Vector(1,0,0));
if (vec.z < 0) then
angle := -angle; //invert angle

//add z rotation
D3DXMatrixRotationZ(mat2,angle);
D3DXMatrixMultiply(mat,mat,mat2);

//We rotated about the Z axis to make sure that axis runs parallel to the X axis of our UV mapping space
//We only have to Rotate about the x axis the add the slope to our space
angle := arccos(DotProduct(Normal,BF_Vector(0,0,1)));
D3DXMatrixrotationX(mat2,angle);
D3DXMatrixmultiply(mat,mat,mat2);

//add scale matrix so the XY coords are equal to UV coords
D3DXmatrixScaling(mat2,1/sizeu,(1/sizev),1);
D3DXMatrixMultiply(mat,mat,mat2);

{Matrix is created..... Lets transform into UV mapping space}

//transform vertices to vectorspace and set UV coordinates
for i:=0 to VertexCount-1 do
begin
//transform vertex/vector to UV mapping space
D3DXVec3TransformCoord(vec,BF_Vector(fVertices[i].x,fVertices[i].y,fVertices[i].z),mat);

//apply offsets to the new UV coordinates we've calculated
vec.x := vec.x + uoffset;
vec.y := vec.y + voffset;

{The following piece of code makes sure that only the given region (by UTiles,VTiles)
is textured. If UTiles and VTiles are 0.. an infinit plane of tiles is projected onto the scene
WARNING: to use UTiles and VTiles, make sure that your textures have a transparent border}

//set U coordinate
if ((vec.x >= 0) and (vec.x <utiles> utiles) then vec.x := 1;
if (vec.x <0>= 0) and (vec.y <vtiles> vtiles) then vec.y := 1;
if (vec.y < 0) then vec.y := 0;
fVertices[i].v := vec.y; //Set V
end;
end;


You should use this method in combination with a multitextured object/scene if you want to apply shadow effects or something similar. :)

I know... The implementation isn't really pro. I'm sure that there are easier ways of making such a matrix. But i couldn't think of one.
I'm 17 and i'm not a math expert, so dont expect to much.

I couldn't upload a working demo because my engine code is a bit messed up. I'm getting AV's when i close my app. Moreover i dont want to share the source of my whole engine.

I really hope that you guys understand my planar texturemapping implementation and i hope that the idea's i used or the source can be usefull to anyone. :)

Good luck.

NecroDOME
12-12-2006, 09:38 PM
So your manually project the texture on your scene?

Somewhere in the DirectX sdk theres a texture flag D3DTS_PROJECTION. This flag can be used by GetTransform and SetTransform. This transform can also be set for each texture state. Could this be used for projection? Or is there a way to setup a projection matrix?

And a new question:
What does D3DTSS_TCI_CAMERASPACENORMAL, D3DTSS_TCI_CAMERASPACEPOSITION and D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR do and how shoud they used? They are used by D3DTSS_TEXCOORDINDEX.

EDIT: How do they make car headlights? They are just textures, projected on any geometry object in the world...

chronozphere
13-12-2006, 05:55 AM
Somewhere in the DirectX sdk theres a texture flag D3DTS_PROJECTION. This flag can be used by GetTransform and SetTransform. This transform can also be set for each texture state. Could this be used for projection? Or is there a way to setup a projection matrix?

Owh... that'll probably render my code useless :cry:
But using D3DTS_PROJECTION may come with a small performance boost, and it might work more accurate than my code.
Nexttime, i need to google sometime and maybe ask you guys, before i start implementing things myself. :)