Decals again

Started by
10 comments, last by Basiror 17 years ago
Hi, Sorry to bother about decals again. I placed the same question a little while ago, but the thread died, and I'm still curious! A couple of guys who helped me were talking about "projected decals". They tried to explain me, but I think I didn't really understand it. Sorry for that. When talking about projective textures, I always think about projecting a flashlight or something on a wall. But I thought decals were just simple meshes, with a texture on it. The only thing that makes it more difficult, is when you need to build the mesh if you want it to "wrap" around a bumpy surface, a corner, or even an animated character. I think these are 2 different techniques. Which one is used nowadays (the blood decals in Doom3 for example)? And how is it made? greetings, Rick
Advertisement
Lets say that you know wich triangles are affected by the decal.

Get those triangles, copy them into a mesh and give them UV coordinates with projecting.

How?

First, determine the impact point and the decals radius. Then see wich triangles are in that sphere.
After that, make two vertices:
left = impactvector *cross* Y-axis;
up = impactvector *cross* left;
normalize up and left

For every vector of the affected triangles:
U= vector *dot* left
V= vector *dot* up


Thats all.
-----------------------------------"After you finish the first 90% of a project, you have to finish the other 90%." - Michael Abrashstickman.hu <=my game (please tell me your opinion about it)
take care about front and back facing triangles


as for texture coordinates, setup a projection matrix
from decal-origin,to decal direction, up vector

e.g.: gluLookAt will do this for you and create a viewport of some size e.g. 512*512(this will have an impact on size of the rendered decal)
move the decal origin along the decal direction, so your object fits exactly into the frustrum (the silhouette touches the side clipping planes)
this can be done with simple trigonometry ( triangles & 90° angles as a key word)
make sure the near and far clipping planes enclose the decal receiver object completely to avoid unforeseen projection artifacts

now transform your decal-hit position with the 3 matrices (modelview,projection,viewport) and do the same with the rest of the vertices

then in world coordinates do a sphere comparsion and store those triangles in a new mesh as Gagyi said above when the following conditions match

a) triangles are within decal radius in world space
b) triangles are front facing

the window coordinates represent the decal uv coordinates, but you need to scale them with a factor


- calculate a 2d bounding box of the uv coordinates
- find the 2 vertices for min & max
- calculate their distance "d" in worldspace coordinates
- d/(2*decalradius) = ratio

scale the decal uv coordinates with "ratio"

this should give you the projected uv coordinates

here is a link explaining the different coordinates spaces (e.g.: normalized device coordinates, window coordinates)
9.011 How are coordinates transformed? What are the different coordinate spaces?

Once you got this running you will have a pretty solid system
http://www.8ung.at/basiror/theironcross.html
Ah, now I see what you guys mean with "projective". If I understand it right, this will be used to calculate proper texture coordinates for the decal mesh, no matter how that mesh looks like. But I'm not that far yet, I need triangles to render the decal on, but how to setup that mesh?

I indeed know on which triangles the decal is placed. In many cases, these polygons are way larger than the decal itself. I could make all that stuff invisible by just drawing the tranparent decal texture at the right spot. But I guess this is not so good for the fill-rate.

Therefore I want the decal meshes to be as small as possible. For example, a bullet hole gets a 10x10 cm rectangle mesh that is "wrapped" upon the surface. First I copy those surface triangles, but how to clip them to a 10x10 cm decal?


I'm sorry if I miss things, my (technical) English isn't that good.

greetings,
Rick
well where is the problem? just clip the "too large polygons" against the sphere you used to determine the triangles that are affected by the decal
just project the clipped vertices too done

for simplicity I would triangulate your meshes


http://www.8ung.at/basiror/theironcross.html
Quote:Original post by spek
Ah, now I see what you guys mean with "projective". If I understand it right, this will be used to calculate proper texture coordinates for the decal mesh, no matter how that mesh looks like. But I'm not that far yet, I need triangles to render the decal on, but how to setup that mesh?

I indeed know on which triangles the decal is placed. In many cases, these polygons are way larger than the decal itself. I could make all that stuff invisible by just drawing the tranparent decal texture at the right spot. But I guess this is not so good for the fill-rate.

Therefore I want the decal meshes to be as small as possible. For example, a bullet hole gets a 10x10 cm rectangle mesh that is "wrapped" upon the surface. First I copy those surface triangles, but how to clip them to a 10x10 cm decal?


I'm sorry if I miss things, my (technical) English isn't that good.

greetings,
Rick



The technique I used was to create a frustum ( 6 planes ) defining the slab inwhich the decal should appear. The left, right, top and bottom are the edges, the front and back are the thickness of the decal.

First I find all the triangles inside a sphere that bounds the frustum, to reduce my working set. Then I use the six planes to clip the triangles. Once done I can project my texture coordinates onto the triangles. I cache these new triangles in a vertex list for later emission so that I don't have to re-clip them every time.
Exactly, I don't know how to do the clipping thing :) Maybe I should have asked this in the first place, instead of starting about decals, but I was not sure I should build meshes and clip them anyway. But ok, the question basically is:

If I have a sphere, and 1 triangle, how to make the triangle fit into that sphere in case it sticking outside the sphere?

Is there an example somewhere? I saw articles about clipping, but it was ussually on non-convex triangles, and a lot of math. Unfortunately, math is not my strongest point as you might have discovered by now :)

Thanks for helping,
Rick
Quote:Original post by spek
Exactly, I don't know how to do the clipping thing :) Maybe I should have asked this in the first place, instead of starting about decals, but I was not sure I should build meshes and clip them anyway. But ok, the question basically is:

If I have a sphere, and 1 triangle, how to make the triangle fit into that sphere in case it sticking outside the sphere?

Is there an example somewhere? I saw articles about clipping, but it was ussually on non-convex triangles, and a lot of math. Unfortunately, math is not my strongest point as you might have discovered by now :)

Thanks for helping,
Rick


I use the ubiquitous Sotherland-Hodgman clipper:

http://hpcc.engin.umich.edu/CFD/users/charlton/Thesis/html/node90.html

It is described as a polygon clipper - of course a triangle is a polygon, too!

It clips polygons against planes, so take each triangle and clip it against the 6 planes of the frustum. Anything left over inside the planes is in your decal. After clipping you will have a polygon, not a triangle, so walk the polygon edges and make a fan of the polygon. Luckily the polygon will always be convex with an orthographic frustum, so you can just loop like this:

if it has 4 vertices ( a, b, c, d ) make a triangle with these points:

a,b,c and a,c,d.


if it has 5 vertices ( a, b, c, d,e ) make a triangle with these points:

a,b,c and a,c,d. a,d,e.

With all the triangles, project the texture coordinates onto it.

Once you have the triangles, you can render them all using triangle list primitives.

Unfortunately I don't have my code here - it's at work, so I can't give you samples - writing a sutherland hodgman clipper is not too hard - you can find java samples on the web that do it interactively.

Best of luck.
Quote:Original post by spek
Exactly, I don't know how to do the clipping thing :) Maybe I should have asked this in the first place, instead of starting about decals, but I was not sure I should build meshes and clip them anyway. But ok, the question basically is:

If I have a sphere, and 1 triangle, how to make the triangle fit into that sphere in case it sticking outside the sphere?

Is there an example somewhere? I saw articles about clipping, but it was ussually on non-convex triangles, and a lot of math. Unfortunately, math is not my strongest point as you might have discovered by now :)

Thanks for helping,
Rick


a triangle consists of 3 vertices and 3 edges, to clip(simple version) a triangle against a sphere you have 2 simple cases
a) 2 vertices are outside, in this case the clipped triangle is a single triangle again

b) 1 vertex is outside, so you get a quad, thus 2 triangles

to clip a triangle against a sphere you can simply determine the edge intersections( google: ray sphere intersection)
and handle the 2 cases i mentioned above

this isn t a 100% clean clip, but it is sufficient enough for decals

you don t need any fancy clipping algorithms


I think this should be enough information to get you started

just for the case that your clipped triangles are too small to enclose the entire decal texture, you can add a delta to your radius while clipping to counter these artifacts
http://www.8ung.at/basiror/theironcross.html
While this thread is here, ill ask something thats been bugging me; how would I go about applying a decal to an animated mesh?

obviously I can no longer simply create a few vertices wrapped around the mesh and uv map it, since in the next frame, those vertices that I just created the decal's mesh from will have moved. I thought about having each animated mesh have its own "decal texture" and every time a new decal needs to be applied, it would render to that texture and then that texture is mapped onto the object; however, that could use alot of texture memory, especially if there are alot of individual animated objects.

I see games like far cry and HL2 which are able to pull this off (animated mesh decals), so there must be an efficient way to do it..

This topic is closed to new replies.

Advertisement