Skip to main content
gamedev.net
🔒 Locked

triangle slices

Started by mike74 15 replies 950+ views
Original Post
mike74
mike74
Let's say you have a 3d triangle. Let's also say you have a 3d procedural texture generator. Now let's say you want to figure out what the triangular slice of the 3d texture looks like in 2d. Any ideas on how to do this? For instance, if your procedure function uses perlin noise to generate marble, the triangle will be like a slice of marble. mike http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Vectorg
Vectorg
mike,

When drawing the triangle, send the 3d coordinates of each pixel to the Perlin function to derive its color. Are you asking how to find the coordinates of each pixel?

What you're describing can also be accomplished very quickly with a ray-tracer, if you just want to see what it might look like.
mike74
mike74
I want an easy way to correlate the 3d triangle coordinates with the 2d coordinates in a texture map. I want to create a 2d texture of the triangle slice.

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
dimebolt
dimebolt
Quote:
Original post by mike74
I want an easy way to correlate the 3d triangle coordinates with the 2d coordinates in a texture map. I want to create a 2d texture of the triangle slice.

mike
http://www.coolgroups.com/


3D textures have 3D texture coordinates. Simply use glTexcoord3f(x, y, z) (or whatever the equivalent in Direct3D is if you use that) for each vertex where (x,y,z) is a point in the 3D texture. The 3D texture coordinates should be similar to the use of 2D textures: they are all normalized between 0.0 and 1.0. Using for example the texcoords: (0,0,1)(1,0,1)(0,1,1) on a triangle will give you part of the back quad of the texture cube (as z is maxed in all 3 vectors).

Tom
ZQJ
ZQJ
If you have 3D texture support you could just render the triangle and take a screenshot.
mike74
mike74
I don't have 3d texture support, so I need to make a 2d texture of it.

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Zakwayda
Zakwayda
Quote:
I don't have 3d texture support, so I need to make a 2d texture of it.
That sounds kind of difficult. I don't know what the standard solution is (if there is one), but here's what I would try:

1. Find the plane of the triangle.

2. Create an orthonormal basis about the normal of the plane.

3. Create a square on the plane and built around the basis that completely 'includes' the cube (I assume the 3d texture is a cube).

4. Decide on a resolution for the 2d texture.

5. 'Walk' back and forth along the square at the desired resolution. At each step, find the corresponding voxel in the 3d texture and assign that color to the texel.

6. Transform the triangle vertices to the 2d basis and find the corresponding texture coordinates.

On the other hand, if you're just interested in seeing the interior of the 3d texture, rather than some specific arbitrarily oriented slice, perhaps you could just slice along a cardinal plane. In that case pretty much all of the above complexity drops out, and you have a very simple solution.
mike74
mike74
I already have a solution to it that I used in my program at http://www.coolgroups.com/giftwrap/. The problem with it is that it's inefficient.

Right now, I'm looping through a lot of the barycentric coordinates, but I'm repeating the filling of some pixels in the texture multiple times. Currently, I'm looping 1,000,000 times to generate a triangle that only has an area of 116,281 pixels. Ideally, there would be one loop per pixel. Does anyone know the standard ways of looping through barycentric coordinates?

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
timw
timw
1m times? =) hehe to add to my previous post suppose we want to map the top point of the triangle to (.5, 1) the left most bottom to (0,0) the right most bottom to (1, 0)

I'm assuming s,t referenced left to right, down to up. it really depend on how you want to map your s,t but assume you do this, which seems that it will be more uniform then say(0,1), (0,0),(0,1) I'd pick the former but that's up to you.

loop through the image and for each pixel calculate s,t(obviously only pick s,t that are in the triangle) you can check if a s, t is in this configuration by

[s,t] = A*[.5,1] + B*[0,0] + C*[1,0]
calculating a,b,c just as you would bary coorids and if A + B + C > 0 && < 1
then it's in the triangle, so now you take that valid s,t and reference it in the triangle. if you didn't notice the A, B, C we calculated to determin if the s,t was actually in the triange, is exactly the barycentric cooridnates of the point on the triangle that maps to that s, t in the image. so P = A*V1 + B*V2 + C*V3;

so we loop throgh s, t of the image
determin A, B, C
use that A, B, C to calculate P(the point in the 3D triange that maps to the TextureCoorid s, t)

evaluate Nosie(P) and store that result to the pixel (int)s*WIDTH, (int)t*Height

you might want to take the average or 4 or so values for each pixel ro AA purposes


on second thought I think I'd pick the mapping (0,1), (0,0), (1,0) this way you can map two triangles texutres in one rectuanlar texture map.. either way you got distortion. if you got questions dont hesitate to ask.


Tim
mike74
mike74
But, how do you calculate A, B, and C?

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
timw
timw
the same way you calculate any barycentric coorids. have you heard of the area interpretation of barycentric coorids?

this states that the constants are proportional to the areas of 3 triangles that a point p breaks the triangle into..

consider a triangle and a point P within that triangle. Draw a line from each of the three vertices of the triangle to the point p. you will notice this breaks up the trinagle into 3 sub triangles with a common vertex p. it's a neat fact that barycentric tripple (A,B,C) can expressed in terms of these areas.

we know the formula these constants come from

P = v1*A + v2*B + v3*C

to calculate A for instance you look at that picture I told you to draw and look for the triangle that does NOT contain the vertex v1 and Call the area of this triangle, Z.

A is then

A = Z/T

where T is the total area of the triangle.

do the same thing to find B look at the sub triangle that does not contain the vertex v2 and call that area Z2.

B = Z2/T

now you could do C = 1 - A - B but you HAVE to know that the point is indeed in the triangle what I would do and I'm not sure if it's the most efficent way is calculate C just like A, and B. then check that A + B + C = 1, if it's not equal to 1 (or very close to it) then this (s,t) is not inside the triangle. or you could use some standard test to see if the point is inside the triangle before you calculate A, B, C or whatever.

of course we could use the cross product to calculate these areas. or we could just use algebra, because once we have determined an (s, t) mapping the sides of our triangle are fixed and with a picure we could just determin the height values for the 1/2b*h formula for a given (s,t). if this isn't clear enough I could write some code for it later but I gotta get back to work now.

Tim
mike74
mike74
I had not heard of the area interpretation of barycentric coords, but it sounds very cool and elegant. I think it will work quite well here.

Thanks a lot for telling me about it.

As for finding the area of the triangles, I will probably use Heron's formula, unless someone has a better idea.

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
timw
timw
hey, I just looked up that Heron's formula, it's really neat. I haden't heard of it before, very beautiful. I think the square root is a little much tho, the cross product in 2d is only 2 muls and one subtract and a division by 2. seems like it be a bit more efficent. unless you can get by the sqrt.

tim
mike74
mike74
I don't think there's any way around the sqrt in Heron's formula.

Can you tell me more about finding the area using the cross product?

BTW, I tried it out with Heron's formula and your area idea, and it works great. But, it could be faster.

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
mike74
mike74
I found this page on finding the area of a triangle with cross product:

http://www.jtaylor1142001.net/calcjat/Solutions/VCrossProduct/VCPATriangle.htm

It looks like you need to do a square root here too in order to find the magnitude of the cross product. Is this right?

mike
http://www.coolgroups.com/
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
timw
timw
in the general 3d case this is true, but not in 2d. cross product is only stricly defined for 3d space, because the result is a vector, we can make these 2d vectors 3d by setting the z component to zero. and taking the cross product of the two new vectors. if you do this you'll notice that the first two components of the result are zero, only the 3rd component is nonzero. so since only one component is nonzero the magnatude of that vecotr is just that component. but forget about that. here the formula for 2d vectors u, and v
U =
V =

UxV = U1*V2 - U2*V1

so the area is the absolute value of that

A = abs(UxV)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.