Projective Texture Mapping - Spotlight Flashlight

Posted By: Grafton

Projective Texture Mapping - Spotlight Flashlight - 01/31/08 14:17

PROJECTIVE TEXTURE MAPPING and PER PIXEL SPOTLIGHT SHADERS

I created a series of shaders designed to create a HLSL spotlight/ flashlight.
These stem from a side project that I have lost motivation for. Since there
have been several requests for some texture projection examples on the forum,
maybe this will help someone.

The examples were written in Lite-c, but could be rewritten in c-script quite
easily. The version I used in creating these examples was A7.06. You will need
support for PS 1.1 and VS 2.0 to run these examples.

The first four deal with creating a spotlight with "per pixel lighting" (PPL).
Its not texture projection, but I incuded these is because it is a simpler,
faster spotlight method which would also make a good fallback method for slower
machines.

The second four deal with creating a spotlight with "projective texture mapping" (PT).
The examples also demonstrate a colored texture projected onto a models surface.

EXAMPLE 1: PPL SPOTLIGHT WITH DIFFUSE AND SPECULAR
EXAMPLE 2: PPL SPOTLIGHT WITH NORMAL MAPPING
EXAMPLE 3: PPL SPOTLIGHT WITH SHADOW MAPPING
EXAMPLE 4: PPL SPOTLIGHT WITH NORMAL AND SHADOW MAPPING

EXAMPLE 5: PT SPOTLIGHT WITH DIFFUSE AND SPECULAR
EXAMPLE 6: PT SPOTLIGHT WITH WITH NORMAL MAPPING
EXAMPLE 7: PT SPOTLIGHT WITH WITH SHADOW MAPPING
EXAMPLE 8: PT SPOTLIGHT WITH WITH NORMAL AND SHADOW MAPPING

IMPORTANT: In order to keep the file size small, I have removed the models from
all the examples except the first. So you will need to copy the two models from
the first folder into the other example folders in order to run those examples.

I would appreciate any feedback on any issues you might find, so I can fix them
in case I decide to post this package at the Acknex Resource site.

Thanks!

9MB Rapidshare Link
9MB FileFront Link
Posted By: oldschoolj

Re: Projective Texture Mapping - Spotlight Flashlight - 01/31/08 14:34

I havnt looked this over yet, but the fact that you released something of this caliber, in order to further educate people is simply impressive. I will comment more when I've had a chance to further review this. Although I can't be sure, this is the first real jump in the right direction for projectors that I have seen in this community.
Posted By: Emre

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 14:36

Thank you very very very much!
Posted By: Pappenheimer

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 15:15

This is a very generous contribution!
Thanks a lot!
Posted By: ello

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 15:20

i am so curious to get home and check if it now works with my gf6800. i hope so ooo o
Posted By: lostclimate

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 15:32

can this work with a6? if so it seems like you could easily make a sun out of this......
Posted By: Emre

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 17:28

example 1&2 working. But other examples not working.
engine message:

"Error in 'MAIN' line 104: 'matViewProj' undeclared identifier
<mat_set(mtlSpotlit.matrix,matViewProj);>


can anyone help me please?

edit:Sorry.need a 7.07. Now all working.
Posted By: ello

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 18:39

indeed, it works now.. what exactly did you change since my pm? just like to know what caused the wrong shadowing...

thank you very much for your efforts!
Posted By: frazzle

Re: Projective Texture Mapping - Spotlight Flashl - 01/31/08 19:56

Great UC, will test asap

Cheers

Frazzle
Posted By: Nidhogg

Re: Projective Texture Mapping - Spotlight Flashlight - 02/01/08 01:04

This is a great contribution, thanks.

Shame about you loosing your motivation on the project though..
Posted By: oldschoolj

Re: Projective Texture Mapping - Spotlight Flashlight - 02/01/08 05:50

wow, because of you I now have projectors for my spell casting. You have helped me solve a long time problem, and saved me a good amount of money.

Thankyou.
Posted By: fogman

Re: Projective Texture Mapping - Spotlight Flashlight - 02/01/08 09:31

You are our hero!
IŽll send you some screens.
Posted By: Helghast

Re: Projective Texture Mapping - Spotlight Flashl - 02/01/08 12:52

this is great!
question though... the projection works great and all, no probs there, but shadows dont seem to be working on this pc (work computer) though on my laptop (which specs are WAAAAAAAAAAAAY lower) it runs fine, any idea why they dont show up ?

(im using example 7. PT Spotlight with Shadow Mapping)

specs (of both machines) are:

Work PC:
win XP SP2
intel core 2 extreme (quadcore) 3.0 GHZ
4 gig Ram
ATI radeon 8800GTX 768MB

Laptop
win XP SP2
dual core (not a core 2 duo) 1.7 GHZ
1 gig Ram
ATI x1600 256MB

as you can see, both should work fine, i wouldnt have been surprised if thee laptop wouldnt work, but the monster-pc doesnt work... that's weird!

got any idea how to fix this (might be the 1024x1024 render target? how do we make that smaller if needed, so it's 24bit instead of 32bit).

regards,
Posted By: Grafton

Re: Projective Texture Mapping - Spotlight Flashl - 02/01/08 19:46

I made a mistake, when looking at the version of A7 I using, I looked at about
in SED 7.06, instead of WED 7.07. Sorry about the confusion.

Ello:
This is the same as in the demo, except a few shaders were mistakenly set to be
compiled as VS 2.0 instead of 1.1.

Emre:
If you can not use Lite-c's "matViewProj", you can create this yourself like this.
Code:
  
//REPLACE THIS LINE
mat_set(mtlSpotlit.matrix,matViewProj); // store the transformation matrix to the material

//WITH THIS CODE
float my_matVP[16] = { 0.0, 0.0, 0.0, 0.0, 0.0,0.0,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
mat_set(my_matVP,matView);
mat_multiply(my_matVP,matProj);
mat_set(mtlSpotlit.matrix,my_matVP);



Otherwise, it would be very hard to write this in C-Script (I know I said you
could), because c-script dosent support materials for views.

Helghast:
Yeah that is strange! To try a smaller resolution shadowmap, just change
bmap = "#1024x1024x14"; to
bmap = "#512x512x14"; or
bmap = "#256x256x14";
Posted By: Reigel_Seven

Re: Projective Texture Mapping - Spotlight Flashl - 02/01/08 20:24

Grafton, this is so freaking awesome of you! THANKS!
I was able to convert the pixel shader examples to C-Script format, but I can't figure out a couple of things with the projection shader ones.

Could anyone help me convert this code to C-Script (wdl)? I've done most of it but I'm not sure how to change these lines:

function mtlProjection_event() // adjust matrices to size of bitmaps and pass to "matMtl" and "matEffect"
{
// create a transformation matrix
float pTexAdj[16] = { 0.5, 0.0, 0.0, 0.0, 0.0,-0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
// Adjust projection matrix for texture size
pTexAdj[12] = 0.5 + (0.5/(float)bmap_width(projectiontex));// store bitmap diminsions into matrix
pTexAdj[13] = 0.5 + (0.5/(float)bmap_height(projectiontex));
// Store the transformation matrix to the materials then adjust the matrix with the bitmap size
mat_set(mtlSpotlit.matrix,matViewProj); // store the transformation matrix to the material
mat_multiply(mtlSpotlit.matrix,pTexAdj); // adjust the matrix with the bitmap size
}

.......

function init_stages()
{
ProjectionView.stage = camera; // so projection is not lagging one frame behind
}

.......

As far as I can tell, these lines are the only thing that needs to be changed. I don't really know much about shader programming, otherwise I'd be able to do it myself. If someone could help me I'd be extremely grateful.

Thanks in advance!
Posted By: Emre

Re: Projective Texture Mapping - Spotlight Flashl - 02/02/08 04:45

Thanks again Grafton!
But child flags not working in the c-script.
VIEW ProjectionView =
{
material = mtlProjView;
flags = VISIBLE;
}
---------------------------------
< material = mtlProjView;>
Example.WDL 148:0 (): Keyword unknown ProjectionView material

< }>
Example.WDL 151:-1 (): Syntax error - ProjectionView
---------------------------------

Can somebody convert "example 5" to c-script please?
Posted By: Loopix

Re: Projective Texture Mapping - Spotlight Flashl - 02/02/08 09:12

Great contribution! Many have been waiting for something like this

Me too I'd be interested in a c-script working version! I'd compensate such a conversion by donating both my model packs to the "convertor"

Thanks!
Posted By: HeelX

Re: Projective Texture Mapping - Spotlight Flashl - 02/02/08 11:09

Thanks for your work.. this is awesome! I have some questions, though:

  • does the shader work for multiple projectors, too?
  • this is a perspective projector.. are you able to make an orthograpic projector as well?


Thanks for your work!!
Posted By: Helghast

Re: Projective Texture Mapping - Spotlight Flashl - 02/02/08 12:45

cool, i'll try that when i get back to work ^_^

in the meantime im wondering, how can i change the colour of the shadow pixel?
(im on my laptop, which has shadows working, so i can develop a bit here )
I did find how i could change the environmental fog to be more dense etc, but not the shadow color... then again im a complete shader noob, and never worked in lite-c yet :O but this is so gonna get me started!!

again, thanks

regards,
Posted By: frazzle

Re: Projective Texture Mapping - Spotlight Flashl - 02/03/08 16:42

Tested it, it works great

Cheers

Frazzle
Posted By: DoC

Re: Projective Texture Mapping - Spotlight Flashl - 02/08/08 18:48

Does anyone can convert it to wdl ? I had try it but give up ;_;
Posted By: Reigel_Seven

Re: Projective Texture Mapping - Spotlight Flashl - 02/09/08 19:02

Down below is the first Grafton example converted to WDL. Just save this code as something like "grafton1.wdl", then include it in your main script file right above your other includes, like this:

include <grafton1.wdl>;

It seems like only one entity can emit a spotlight at a time with this code, but as many entities as you need can be spotlit. You can either assign the "action Spotlight()" to your player and "action Spotlit_object()" to the entities to be spotlit, or you can do the following:

Place the following code somewhere in a while loop for the entity to emit the spotlight, like the player:

Code:
 
// send my position to shader (vecSkill1.xyzw)
mtlSpotlit.skill1 = float(my.x);
mtlSpotlit.skill2 = float(my.z); // z and y must be reversed!
mtlSpotlit.skill3 = float(my.y);
mtlSpotlit.skill4 = float(650); // spotlights range

// send my direction to shader (vecSkill2.xyzw)
vec_for_angle(temp_vec.x, vector((my.pan), my.tilt, my.roll)); //turn my angles into a dir vector
mtlSpotlit.skill5 = float(temp_vec.x);
mtlSpotlit.skill6 = float(temp_vec.z); // z and y must be reversed!
mtlSpotlit.skill7 = float(temp_vec.y);



And you can put this little code in any script linked to an entity you want spotlit:

Code:
 
my.material = mtlSpotlit;



Alternatively, you could just assign the "mtlSpotlit" material in WED to any entities you want spotlit, since many of your entites may not even have or need a C-Script.

The following is the code for the spotlight. Just use the "PP_Spot.fx" file from the Grafton example.

Code:
 
//
// By Joseph Duffey Jan 2008
// For the GameStudio Community
//
// Converted to WDL format by Reigel Seven 2008
//
// Example 1: Projective Texturing Spotlight with specular

//=======================================================================================
// VARIABLES
//=======================================================================================

var temp_vec[3]; // for temporary vectors
FONT standard_font = "Arial",1,14;

var pTexAdj[16];

//=======================================================================================
// CAMERA ACTION
//=======================================================================================

action Spectator()
{
//set(me,INVISIBLE | PASSABLE);
my.invisible = on;
my.passable = on;

// while(!player)
// {
// wait(1);
// }
vec_set(camera.x, my.x);

while(me)
{
vec_set(temp_vec,player.x);
vec_sub(temp_vec,camera.x);
vec_to_angle(camera.pan,temp_vec);
wait(1);
}
}

//=======================================================================================
// MATERIAL FOR OBJECTS LIT BY SPOTLIGHT
//=======================================================================================

MATERIAL mtlSpotlit
{
ambient_red = 12; // The ambient color - a dark grey.
ambient_green = 10;
ambient_blue = 8;
effect = "PP_Spot.fx"; // The effect file containing the vertex shader, pixel shader and technique.
}

//=======================================================================================
// SPOTLIGHT CODE
//=======================================================================================

action Spotlight()
{
//player = me;
while(1)
{
// move me with keyboard keys
// c_move(me, vector((key_w - key_s)*time_step*8,0,0), nullvector, IGNORE_PASSABLE | GLIDE);
// c_rotate(me, vector((key_cul-key_cur)*time_step*8, (key_cuu-key_cud)*time_step*8,0), IGNORE_PASSABLE );

// send my position to shader (vecSkill1.xyzw)
mtlSpotlit.skill1 = float(my.x);
mtlSpotlit.skill2 = float(my.z); // z and y must be reversed!
mtlSpotlit.skill3 = float(my.y);
mtlSpotlit.skill4 = float(650); // spotlights range

// send my direction to shader (vecSkill2.xyzw)
vec_for_angle(temp_vec.x, vector((my.pan), my.tilt, my.roll)); //turn my angles into a dir vector
mtlSpotlit.skill5 = float(temp_vec.x);
mtlSpotlit.skill6 = float(temp_vec.z); // z and y must be reversed!
mtlSpotlit.skill7 = float(temp_vec.y);

wait(1);
}
}

//=======================================================================================
// ACTION FOR OBJECTS LIT BY SPOTLIGHT
//=======================================================================================

action Spotlit_object()
{
my.material = mtlSpotlit;
}



I'm afraid I may have forgotten something, so please let me know if I did. I hope this helps somebody!

- Reigel Seven
Posted By: DoC

Re: Projective Texture Mapping - Spotlight Flashl - 02/10/08 13:25

thats really cool your my men, it works, with the PP but now someone it will works with PT? aaaand with the sportlight jpg? someone how know it?

Then Im complete happy...
but I must say it again Reigel_Seven your great
Posted By: Reigel_Seven

Re: Projective Texture Mapping - Spotlight Flashl - 02/11/08 22:16

No problem, DoC. I've been trying to convert the PT examples, but haven't had much luck.I've gotten them to run, but you can't see the projected texture. If I get it working I'll post it, but don't count on it
Posted By: DoC

Re: Projective Texture Mapping - Spotlight Flashl - 02/11/08 22:44

i think the problem is the view with the skin that dont work in wdl...
one day i learn lite-c.... but this day can wait ^__^
Posted By: Skeksis

Re: Projective Texture Mapping - Spotlight Flashl - 02/14/08 12:51

Just saw this thread for the first time, this is a great contribution, thank you grafton, i've been looking for something like this for a while. also thanks to reigel seven for converting it to c-script.
Posted By: Skeksis

Re: Projective Texture Mapping - Spotlight Flashl - 02/16/08 09:17

I was just trying the c-script version, it works great but I was wondering if it's possible to convert the shadow mapping aswell.
Also, I noticed that the material only reacts to the spotlight, I don't really know anything about shaders, but I was wondering how difficult it would be to add support for a few more lights.
Posted By: Reigel_Seven

Re: Projective Texture Mapping - Spotlight Flashl - 02/16/08 19:23

Yeah, the shadow mapping can be converted very easily, but for some reason I can't even see the shadows on my computer, whether I'm running the C-Script version or the Lite-C version. I can't figure out why.

Anywho, I'm working on adding support for a few more lights, but it may take a while, considering I know NOTHING about shader programming. I'm hoping I can basically just cut and paste from the DOOM 3 lighting shader. I'll post after I give it a try.
Posted By: Skeksis

Re: Projective Texture Mapping - Spotlight Flashl - 02/17/08 09:19

This will be an awesome contribution if you successfully add support for multiple lights. I hope you can figure it out, good luck.
Posted By: Scorpion

Re: Projective Texture Mapping - Spotlight Flashl - 02/17/08 13:19

what would be a good addition for realistic purpose:

get the dot product of the model normal and the normal from vertex to cam position and then use it with the "alpha" of the projection. So that only faces facing the light get lighted.
Posted By: Grafton

Re: Projective Texture Mapping - Spotlight Flashl - 02/20/08 04:00

Quote:

what would be a good addition for realistic purpose:

get the dot product of the model normal and the normal from vertex to cam position and then use it with the "alpha" of the projection. So that only faces facing the light get lighted.




The shaders provided already do this. Only forward (light source) facing
polygons are lit.

Adding more dynamic lights is simple, but many lights will require more than
one pass which would slow the shaders down. Plus, if you wanted all lights to
cast shadows, you will need a very high end graphics card.

The game I created the flashlight shaders for features a shader that also
supports a dynamic light that casts shadows (a "horror house" dynamic strobe
light). I used light mapping for all else.

You could create a shader which supports 2 projections without shadows and 3
lights with shadows or whatever you wish. There are numerous combinations you
can create. Hopefully the examples should get you started.

Posted By: Grafton

Re: Projective Texture Mapping - Spotlight Flashl - 02/20/08 04:37

About the troubles in conversions. The simple per pixel spotlight shader setup
can easily be converted to c-script. However, shadow mapping and projective
texturing both rely on the use of a material assigned to a view. As far as I
know, this is currently only availible when using lite-c.
Posted By: Helghast

Re: Projective Texture Mapping - Spotlight Flashl - 02/20/08 11:38

Quote:

in the meantime im wondering, how can i change the colour of the shadow pixel?




anybody, i really wanna know this!!

regards,
Posted By: croman

Re: Projective Texture Mapping - Spotlight Flashl - 02/24/08 13:55

hi, great contribution from you grafton, thanks.

check your pm's
Posted By: andibeh

Re: Projective Texture Mapping - transfer2texture? - 03/17/08 14:13

Maybe it is a super stupid question, but would it be possible to transfer the projected texture permanently to the texture of the hit model?
I would need that for a scientific simulation where I need to "print" several copies of a texture onto a model.
Posted By: Shadow969

Re: Projective Texture Mapping - transfer2texture? - 03/18/08 12:26

yeah, i wonder about this too, to make some kind of shadowmapping
Posted By: Emre

Re: Projective Texture Mapping - transfer2texture? - 04/23/08 16:35

Hey,did anyone convert "PT EXAMPLES" to c-script? (example 5-6-7-8)
Child flags(view.material&view.stage) not working with c-script.
How can i use "projective texture" with c-script. Is this impossible?
Sorry for my bad speak english.
Posted By: Satori

Re: Projective Texture Mapping - transfer2texture? - 05/02/08 23:46

How can i use this material for normal bsp map blocks ? It works only on entities \:\(
Posted By: Ilidrake

Re: Projective Texture Mapping - transfer2texture? - 05/11/08 16:36

While using the code I noticed it only handles side to side movement. Not very effective for use as a flashlight. Anyway to make it where it follows where the player is looking, side to side, up and down.
Posted By: Grafton

Re: Projective Texture Mapping - 05/11/08 17:17

I am not sure what you mean. In the demos, use the left/right cursor keys to
pan the spotlight and the up/down cursor keys to tilt the spotlight. The W/S
keys move it forwards and backwards. It can be positioned anywhere pointing in
any direction.

The purpose of these demos is to present the shaders to create spotlights,
flashlights and projective texturing with/without shadow and normal mapping
for general purposes. For a specific use, you will need to supply your own
movement/placement code.

For instance, if you want to make a flashlight that follows your camera, then
each frame you need to do something like this;

vec_set(temp_vec, vector(10,-10,-10)); // create a vector
vec_rotate (temp_vec, camera.pan); // rotate the vector to the cameras pan
vec_add (temp_vec, camera.x); // position the vector at the camera
vec_set(flashlight_mdl.x, temp_vec); // position your flashlight model
vec_set(flashlight_mdl.pan, camera.pan);

Then send the position of the flashlight model to your shader.
Posted By: Ilidrake

Re: Projective Texture Mapping - 05/11/08 20:34

Cool. Thanks man. I just wasn't sure about it because shaders and materials are something i'm inching my way into little by little.
Posted By: Satori

Re: Projective Texture Mapping - 05/12/08 12:28

Grafton help me please. Can you make a spotlight demo with normal map geometry (without entities)?
is this possible ? May be any advices ?
Posted By: Satori

Re: Projective Texture Mapping - 05/21/08 15:04

up
Posted By: Pappenheimer

Re: Projective Texture Mapping - 05/21/08 15:28

It is said, that it is too slow with normal map geometry.
I would use models. If you have an updated collision code, it works like map geometry, if you stilluse an old collision code, you can set the models passable and place blocks with the none-flag set for the collision.
Posted By: Dark_samurai

Re: Projective Texture Mapping - 05/22/08 12:11

Does this code need render to texture?

Thanks for this great contribution!

Dark_Samurai
Posted By: rayp

Re: Projective Texture Mapping - 07/12/11 23:48

Hi !

Can someone load this stuff up again ? The links wont work anymore. Would be really nice cause iam searching for a cool spotlight
Posted By: 3run

Re: Projective Texture Mapping - 07/13/11 07:01

You can find this in AUM, search them (was somewhere around 70).
Posted By: bart_the_13th

Re: Projective Texture Mapping - 07/13/11 08:13

You can find something similar in wiki
http://www.opserver.de/wiki/index.php/ProjectionTexture
It runs on all A7/8 version, even the free ones.
Posted By: rayp

Re: Projective Texture Mapping - 07/13/11 15:17

Thanks for the answers.
The stuff in the link from bart_the_13th i know. I will search the aum. If i remember right ive once seen cool screenshots with a monster in a flashlight spot with normalmapping and so on. I think this thread belongs to the screens. ok ill take a look in the aums.
© 2024 lite-C Forums