Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Graphical lighning

  1. #11
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Graphical lighning

    ooow, reflected light off water in a darkened room is an awesome effect!
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #12

    Graphical lighning

    @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 :

    [pascal]
    {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;
    [/pascal]

    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.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #13
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    Graphical lighning

    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...
    NecroSOFT - End of line -

  4. #14

    Graphical lighning

    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.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •