# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #61 2009-12-03 05:58:31

Joseph
Member
Registered: 2009-09-04
Posts: 14

### Re: Yet Another Idiotic Light And Shadow Engine

so xot, did you come up with anything new to add to your shadows?  like...casting over models?
also, is this going to speed up your engine any?  I wouldn't think so, but it's doing something with a dll so I thought maybe it could be taking some of the load off, or is it adding to it?

Offline

## #62 2009-12-03 22:17:25

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

I haven't worked on it since I posted the demo (although that demo was about 8 iteration out of date when I posted it). Currently shadows can and are cast onto models from other models. To do so requires two versions of any model receiving shadows, one textured normally, and one textured for environmental mapping. The shadows aren't totally realistic in every case but they look good for the most part. The big limitation is that dynamic geometry does not cast shadows onto other dynamic geometry. I'm not sure that will ever be possible.

The surface fix thing is an interesting development. Being able to use the zbuffer should be a huge help in certain cases, like first-person or low-angle views. Without that I have to sort the polys manually and that is really slow. Without sorting you have to live with certain depth order glitches, which are generally not a problem for a top-down view, but make a first-person view impossible. The zbuffer solves a lot of problems.

The surface fix also allows the ability to render a perspective view to a surface. That could be used here to completely eliminate the complicated shadow geometry. It also might make better wall shadows more feasible but I haven't had time to think about exactly how that might work.

There are some special effects possibilities for the surface fix that I'm also looking forward to trying.

Abusing forum power since 1986.

Offline

## #63 2009-12-04 06:04:52

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

### Re: Yet Another Idiotic Light And Shadow Engine

>>There are some special effects possibilities for the surface fix that I'm also looking forward to trying.

Yes, I'm working on an idea to draw 3d on surfaces and distort the game field with other models. A black hole for example...

Or some sort of weapon beam that distorst the field.

The only problem I cant figure who to use premade models (like the river flow, lake, lava thing I did) on the playing field, for something that moves and rotates. Like an energy beam. I still need to calculate whe uv according to the placement and rotation of the beam....

I figure I could keep the uvs within a certain radius inside the texture and rotate the draw on the surface accordingly (top down 3d game) to the uvs specified in the model(s) map to the righ image bits.

Unfortunatly, I find myself with little energy to do anything these days.

Offline

## #64 2009-12-04 19:44:39

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

I'm not sure if this will help you, but I had a similar problem with making models receive shadows from the environment map. Recomputing the model's many UV coordinates each frame wasn't feasible. So instead, I remapped the texture coordinates indirectly. It's actually a very simple technique but it's kind of hard to explain.

A: That's the textured model. It is UV mapped from the top view with a z-axis projection.

B: This is the area of the environment we need to copy. You can see the actual region in the main image to the left. This is the final image, so the spider has already been drawn. The environment map itself obviously doesn't have the spider rendered to it, just lighting, and that's what we see on the right. Compute the environment map's UV coordinates at the four corners labeled "???,???".

C: Then we render that section to a new surface/texture with a pair of textured triangles. That orients the texture section to the right angle and scale to match the model's existing UV map.

D: We use the new texture for the multiplicative lighting pass to get the properly shaded model. Draw it in the correct position and orientation and it should match the environment.

The obvious advantage is that only four UV coordinates need to be computed for a model of any complexity, as long as it uses a simple texture projection like this. So there you have dynamic UV mapping with fast rendering static models.

Abusing forum power since 1986.

Offline

## #65 2009-12-04 20:41:30

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

### Re: Yet Another Idiotic Light And Shadow Engine

Similar to what I was planing to do. Thanks.

Offline

## #66 2009-12-07 02:33:27

Joseph
Member
Registered: 2009-09-04
Posts: 14

### Re: Yet Another Idiotic Light And Shadow Engine

I don't get how to project the texture onto the model...uv maps aren't perfect squares so it's not like you can just draw it overtop and expect it to form around it.
could you explain this further please?

Offline

## #67 2009-12-07 14:13:09

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

You have two models. One is textured "normally" (which is what you must mean be UV maps not being perfect squares) and the other is texture mapped specifically for environmental lighting. In this case environmental lighting means copying the floor lighting directly below the model.

If you look at figure A, that indicates a UV mapping for the environmental model. For the purposes of illustration we'll say the z-axis is the "up" axis and it is that axis which we are looking directly down in the figure. For any vertex in the model, the UV mapping of it is roughly u = x/width and v = y/height. That's a z-axis planar image projection, sized to fit the model over the UV intervals [0,1]. In practice you'll need to offset the x,y coordinates to reposition the UV map origin to the "top-left" corner of model.

The result is not unlike pointing a film projector at an object. In this case, the projector is above and the image being projected is the lighting on the floor. The limitation of this type of texture projection is that it can't encode detail along the projection axis. That leads to tell-tale texture smearing around the sides of model. With careful control of the view angle and/or geometry this problem is mitigated. In this case the top-down perspective hides these problems.

Since to model has static UV coordinates, the projected image itself has to be dynamic. Each step, the image is copied from the master lighting texture using UV coordinates calculated for the position of the "corners" of the model within the master texture.

In the example I don't actually use two models. I use the planar image projection for the color texture as well.

Abusing forum power since 1986.

Offline

## #68 2009-12-09 22:40:28

IamCalle
Member
Registered: 2007-10-20
Posts: 23

### Re: Yet Another Idiotic Light And Shadow Engine

That is an interesting technique, nice.

Offline

## #69 2009-12-10 03:37:44

Joseph
Member
Registered: 2009-09-04
Posts: 14

### Re: Yet Another Idiotic Light And Shadow Engine

alright, I kind of understand.  it's still way beyond me how it actually works though.

Offline

## #70 2009-12-11 14:35:16

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

I'll try to make a demo that illustrates the technique better than I can in words. Can't get to it right now. Major computer problems here this last week, rebuilding the system from scratch. I have no software installed at the moment (beyond this web browser I'm using) but Game Maker is pretty high on my list of things to immediately reinstall. My sanity depends on it.

Abusing forum power since 1986.

Offline

## #71 2009-12-12 08:17:19

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

Here's a demonstration of a z-axis planar projection UV map used for environmental shading. It ended up looking a little more complex than I wanted but hopefully it will shed some light on things.

http://www.host-a.net/u/xot/planar-projection.gmk

Move the mouse to move the cube. Scroll the mouse wheel to tilt the camera angle. Cube is drawn slightly darker than background to increase its visibility. Upper-left corner shows the cube texture, which is copied from the background texture. The small circles at the corners of the cube show the region being copied.

Abusing forum power since 1986.

Offline

## #72 2009-12-15 16:39:45

Joseph
Member
Registered: 2009-09-04
Posts: 14

### Re: Yet Another Idiotic Light And Shadow Engine

Holy crap...I see it, I can read, but I have absolutely no idea how any of it works.  I'll have to study this for a while before I get it, but once I do, I know my shadows will be a million times better.  THANKS!!

The more I look at this the more I love it...it's just so awesome!  I seriously hope it's not as complicated as it looks...

So...with your spider models, did you have to find all of the triangle coordinates for it too?  and explode them from a string like you did this cube?
so every model I want to cast shadows on, I must find every triangle position and...somehow write it into a string?  Is there some way to...I don't know, calculate everything at game start?

Last edited by Joseph (2009-12-15 16:47:01)

Offline

## #73 2009-12-16 05:30:41

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

So...with your spider models, did you have to find all of the triangle coordinates for it too?  and explode them from a string like you did this cube?

I used strings here for my own convenience. I'm kind of sorry I did because it adds a couple of layers of complication that are not necessary. I only did it to save myself some typing. The spider model was not produced this way.

It's been a long time but for the spider I think what I did was model it in LightWave3D and export it as an Wavefront OBJ file. Then I imported it into Marzipan and I texture mapped it there. As far as I can remember setting up a planar texture projection was a built-in function of Marzipan. It's possible I used a different tool but I think it was Marzipan. I don't know if icuurd12b42 has similar functionality in his tools.

You can make your own tool for this. If you can save your GM model with d3d_model_save, it's not too difficult to work out the file format. It's basically text file with a series of GML commands, one on each line, with an integer replacing the GML function name which is followed by some reals for its arguments.

0 5.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
9 0.0000 -225.0000 360.0000 0.0000 -0.9029 0.4299 0.2500 0.3585 16777215.0000 1.0000
9 0.0000 -247.2000 312.8400 0.0000 -0.9086 0.4177 0.2500 0.3628 16777215.0000 1.0000
9 71.7840 -213.6960 360.0000 0.2805 -0.8579 0.4306 0.3003 0.3583 16777215.0000 1.0000
9 78.8667 -234.7807 312.8400 0.2822 -0.8633 0.4184 0.3003 0.3626 16777215.0000 1.0000
9 133.6320 -182.0880 360.0000 0.5313 -0.7292 0.4313 0.3502 0.3581 16777215.0000 1.0000
9 146.8170 -200.0540 312.8400 0.5347 -0.7338 0.4191 0.3502 0.3624 16777215.0000 1.0000
9 182.0880 -133.6320 360.0000 0.7292 -0.5313 0.4313 0.3998 0.3581 16777215.0000 1.0000
9 200.0540 -146.8170 312.8400 0.7338 -0.5347 0.4191 0.3998 0.3624 16777215.0000 1.0000
9 213.6960 -71.7840 360.0000 0.8579 -0.2805 0.4306 0.4497 0.3583 16777215.0000 1.0000
9 234.7807 -78.8667 312.8400 0.8633 -0.2822 0.4184 0.4497 0.3626 16777215.0000 1.0000
9 225.0000 0.0000 360.0000 0.9029 0.0000 0.4299 0.5000 0.3585 16777215.0000 1.0000
9 247.2000 0.0000 312.8400 0.9086 0.0000 0.4177 0.5000 0.3628 16777215.0000 1.0000
1 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000

I haven't bothered to completely decipher the file format myself, and this is only a small portion of a test file, but it looks like the first line here starts a new model primitive with command "0" and I assume "5.0000" means pr_trianglestrip. The next 12 lines start with "9" which must mean d3d_model_vertex_normal_texture_color and on each of these lines are the arguments {x,y,z,nx,ny,nz,xtex,ytex,col,alpha}. Then comes a line starting with "1 0.0000 ..." and that has to mean d3d_model_primitive_end. I haven't verified that all of this is true but it looks pretty straightforward. There are a couple of other lines at the beginning. The first line in my test file is "100" and that might be a magic number to indicate that the file is a model. The next line in my file is "280", and that seems to be the number of lines (GML function calls) remaining in the file.

With that information it should not be difficult to find the extents of a model and then use that with the x,y information of each vertex to create new UV coordinates.

U = (x - left) / (right - left)
V = (y - top) / (bottom - top)

Replace the existing UV coordinates for each vertex, save it to a new file, and you'll have a model textured for planar projection ready to load at the start of the game.

Abusing forum power since 1986.

Offline

## #74 2009-12-16 20:01:40

Joseph
Member
Registered: 2009-09-04
Posts: 14

### Re: Yet Another Idiotic Light And Shadow Engine

very confusing but it's making a bit more sense.  I see how the format works, if it is what you say.  "You said The first line in my test file is 100"...what test file is this?  was this just a some random model you made to play with?  where was the 100?  at the 0 or 5.0000?  so if I make a cube in a modeling program, save it as an obj, and bring it into marzipan to save as a d3d, and open it up, it'll show data much like what you've shown above?

or, what were you talking about with marzipan already set up planar projection?  I don't see any thing in the menu's, only a couple uv mapping options.

Last edited by Joseph (2010-04-19 01:13:56)

Offline

## #75 2010-04-17 18:04:12

Joseph
Member
Registered: 2009-09-04
Posts: 14

### Re: Yet Another Idiotic Light And Shadow Engine

hey xot, I wanted to show you something.
http://host-a.net/JosephCh7/planar.zip *updated since edit* password is ak47

I figured out an easy way to set up planar mapping just by uv mapping from 'top' projection.
It took me a while to figure it out but I did and it looks great.
It's not perfect, something about the size that doesn't line up perfectly (even with exact dimensions) but it works.

I also threw in my shadows but they don't seem to copy from surface to surface...not sure why.
Anyway, hopefully I can figure something out.  This would be a great effect for any game.

edit.
I decided to just do an ortho render to background so everything casting a shadow will have one directly below (for now).
Also, I managed to fix the surface uv mapping so it's almost pixel perfect with my model!
Just wanted to thank you xot for making that planar example in the first place.

Last edited by Joseph (2010-04-19 01:17:53)

Offline

## #76 2011-06-10 07:30:19

Rani_sputnik
Member
Registered: 2011-04-24
Posts: 18

### Re: Yet Another Idiotic Light And Shadow Engine

Hey xot I hope you're still working on this, it is absolutely incredible and with the 3d improvements to GM8.1 it's made it even faster.

With blurring set to 2, All lights at dynamic, Flickering etc all the bells and whistles I get a guaranteed 150fps and it's usually hovering around 175,
With everything fleshed out, kill enabled etc i get well over 400fps!!!

I'm running a NVidea Quadro FX1800M -> sucks for Game Maker

Please continue with this, it's brilliant.
Ryan.

Offline

## #77 2011-06-10 09:09:48

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

Thanks for the info and the support, Ryan. I can't believe that demo is almost 2 years old. I should work on this again. With surfaces now enabled in 3D mode, it may be possible to make some additional improvements. It really needs to be rebuilt from the ground up. I've got some ideas for some indirect texture/shadow mapping that could allow more flexibility with the shadows cast onto walls.

Abusing forum power since 1986.

Offline

## #78 2011-06-10 19:28:00

Rani_sputnik
Member
Registered: 2011-04-24
Posts: 18

### Re: Yet Another Idiotic Light And Shadow Engine

Yeah I remember I tried this out ages ago and looking through this topic was what convinced me to sign up to gmljoint in the first place. Theres so much innovation in your work, as much as I wish I could just pick up where you left off, there's no way my efforts would come anywhere near what you can achieve... I'm gonna be writing my own version of this to help me understand it, hopefully it all goes well... I'll edit here with any problems I encounter.

Thanks for the info and the support, Ryan

I was sitting here wondering how you knew my name, then I realised I signed my post with it XD, that's too funny. Anyways more to the point, thank YOU for such an inspiring topic, I can't stand it when people complain "Gamemaker sucks! it's too slow," etc. It's just fast enough imo.

EDIT:
Oooookay I've ended up going a similar but completely different way to you. The way I do it is I take a top down perspective of the environment for each light but I extend the geometry so that it's height is ridiculous. The simple act of looking at the geometry, drawn in black, from above when the geometry has near infinite height gives me the shadow map. Now to optimise and see if this is actually anywhere near as good as yours.... *Clings to hope desperately*

Last edited by Rani_sputnik (2011-06-10 21:34:54)

Offline

## #79 2011-06-10 21:45:25

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Yet Another Idiotic Light And Shadow Engine

The way you describe yours is exactly how mine was intended to work. I've had to deviate somewhat from that because of the limitation surfaces imposed at the time of writing it. Instead of using a simple perspective view to build the shadows, I had to bake my own perspective projection into the model and used a transform to "correct" the geometry for any particular viewpoint/light. Now a lot of that extra work is unnecessary.

And see how fast you got it going? It's really not that complicated, I just somehow make it sound complicated when I try to explain it.

I'm glad someone has finally decided to take on this method for themselves. That was the entire point of this topic.

Abusing forum power since 1986.

Offline

## #80 2011-06-10 23:55:28

Rani_sputnik
Member
Registered: 2011-04-24
Posts: 18

### Re: Yet Another Idiotic Light And Shadow Engine

I figured this was the way you probably wanted to do it in the first place, but unfortunately there are a couple of problems.

Turns out the surface *fix* that was supposed to sort out drawing surfaces in 3d is not all it's cracked up to be, trying to implement this engine is such a pain in the **** because of all the little exceptions there are surrounding how these things work. For instance, it would appear that creating surfaces and using them on the fly in 3d is not allowed. I haven't tested thoroughly yet but the exact same code, one using a surface created and destroyed in the step/draw event and one using a surface created in the create event, perform in completely different ways. Creating them in the step/draw leads to all sorts of depth issues when drawing any other geometry.... grr. That's one issue, there's another one where surface_get_width was giving me random values even though the surface I was giving it was actually non-existant. Though I can't reproduce that one so it might have been my fault...

All complaints aside it runs at about 280fps with 115 cubes on the screen, so not too bad, I don't think it's much faster though tbh, certainly not at this stage, but I'll upload it once I get it to a stage I am comfortable with. Please let me know if I'm treading on your turf a bit to much though xot, this was your idea pretty much after all...

Offline