Advertisement

Triangle texture interpolation?

Started by June 21, 2004 11:19 AM
10 comments, last by Tree Penguin 20 years, 5 months ago
Hi, how can i calculate the texel position of any triangle having the texel x and y value texture size vertices I tried to interpolate them in several ways but i just can't seem to find the right way. I tried this for a long time and thought i did it, now i tried to use that code in another program of mine and it seems it only works for triangles with a 90 degree corner and 2 equal sides. Can anyone help me? Thanks!
Let me get this straight -- are you trying to find the texel for any pixel on the screen of a triangle? What do you mean by 'texel position'?
- fyhuang [ site ]
Advertisement
Well, like this: a texture consists of texels right? Lets say you want to know the actual world-position of that texel, well, that's what i want to know. If that's not a texel then tell me what it is called (anyway that's what i meant), thanks.
Hmm, the 'world position'. A texel really only has an X and a Y, if you're looking for the Z, then all you have to do is some basic texturing interpolation to find the X and the Y, and a ZBuffer kind of thing to find the Z, if I understand you correctly.
- fyhuang [ site ]
For each texure image, create a lookup buffer which contains, at every texel location, the corresponding position in object space: i.e.

0) mark every texel on the lookup value as "invalid" (you can have another lookup buffer of booleans).

1) for each triangle having that texture mapped on, scan-convert the triangle in texture-space according to its vertices' texture coords;

2) for each texel you found by step (1), calculate its object-space position according to the 3 vertices' object space position and store this value in the lookup buffer (and mark it as "valid" in some manner)

When you want to know the texel's window-space position, first test if it's "valid" (i.e. the texel is really mapped on some triangle). If it is, get it's object-space position in the lookup buffer and transform this one using your modelview/projection/viewport (or use glProject() for simplicity, if you are using OpenGL) to get its window coordinates.
I think CoreMeltdown got it but i'd like to know how i can do it in software mode.

If he didn't get it right here's a better explanation of what i want to calculate:

I have:
3 vertices
3 texture coordinates
an x and a y coordinate of the texel i want to know the position of in 3d worldspace

So how does opengl interpolate the texture between the 3 vertices?

So lets say i have 3 vertices with texture coordinates 0.0,0.0 0.0,1.0 and 1.0,1.0 ... How can i calculate the position of texel 22,121 on a texture size 256x256.

Thanks in advance.

EDIT: i still think i am not clear enaugh, i mean i want a vertex specifying the modelviewspace position of a certain texel, this will have to be done by interpolating between the vertices (at least that's what i think), but how i must interpolate is the thing i don't know, so, can anyone give me some information on that? Thanks
Advertisement
OpenGL (and D3D, for that matter) interpolate the texture coordinates and other interpolants after the geometry is projected to screen-space. Maybe that fact gives you an idea on how to proceed?

Niko Suni

My first guess would be to simply interpolate between the corners of the triangle, i.e. something like:
// pseudocodenormalisedTexCoords = {texelX / textureWidth, texelY / textureHeight};texVector1 = texCoordsOfVertex2 - texCoordsOfVertex1;texVector2 = texCoordsOfVertex3 - texCoordsOfVertex1;alpha, beta = solve(texCoordsOfVertex1 + (alpha * texVector1) + (beta * texVector2) == normalisedTexCoords);worldVector1 = worldSpacePositionOfVertex2 - worldSpacePositionOfVertex1;worldVector2 = worldSpacePositionOfVertex3 - worldSpacePositionOfVertex1;position = worldSpacePositionOfVertex1 + (alpha * worldVector1) + (beta * worldVector2);


However, I suspect that would suffer problems of non-perspective correctness. Having never written a software renderer if the above doesn't work (and I suspect it probably doesn't) I can't help, except to suggest you perhaps take a look at the source code to Mesa.

Enigma
Hmm, I do have an old DOS book, the Black Art of 3D Game Programming, which does have software rendering techniques in it, including (non-perspective corrected) texturing.

I do think that software rendering experience may be of use here, which includes not me.
- fyhuang [ site ]
Compute edge equations in texture space for object space only once for each triangle:

/* All computations are in float. *//* [ui, vi] = texture coords at vertex i *//* [ui, vi] are all in range [0..1] *//* [Ai, Bi, Ci] = triangle edge equations (in texture space) */A0 = v0 - v1;B0 = u1 - u0;C0 = -0.5f * ((A0 * (u0 + u1)) + (B0 * (v0 + v1)));A1 = v1 - v2;B1 = u2 - u1;C1 = -0.5f * ((A1 * (u1 + u2)) + (B1 * (v1 + v2)));A2 = v2 - v0;B2 = u0 - u2;C2 = -0.5f * ((A2 * (u2 + u0)) + (B2 * (v2 + v0)));area = C0 + C1 + C2;oneOverArea = 1.0f / area;/* Then compute object-space edge equations *//* [xi, yi, zi] = object-space position at vertex i *//* Equation for X */t0 = x0 * oneOnverArea;t1 = x1 * oneOnverArea;t2 = x2 * oneOnverArea;Ax = (A0 * t0) + (A1 * t1) + (A2 * t2);Bx = (B0 * t0) + (B1 * t1) + (B2 * t2);Cx = (C0 * t0) + (C1 * t1) + (C2 * t2);/* Equation for Y */t0 = y0 * oneOnverArea;t1 = y1 * oneOnverArea;t2 = y2 * oneOnverArea;Ay = (A0 * t0) + (A1 * t1) + (A2 * t2);By = (B0 * t0) + (B1 * t1) + (B2 * t2);Cy = (C0 * t0) + (C1 * t1) + (C2 * t2);/* Equation for Z */t0 = z0 * oneOnverArea;t1 = z1 * oneOnverArea;t2 = z2 * oneOnverArea;Az = (A0 * t0) + (A1 * t1) + (A2 * t2);Bz = (B0 * t0) + (B1 * t1) + (B2 * t2);Cz = (C0 * t0) + (C1 * t1) + (C2 * t2);/*  Now you have:     [Ax, Bx, Cx] = equation for X component;     [Ay, By, Cy] = equation for Y component;     [Az, Bz, Cz] = equation for Z component;*/


Once you have calculated the above three edge equations for each component, you can obtain the object-space coordinates for texel at (x, y):

/* All computations are in float. *//* THIS CODE ASSUME YOUR TEXTURE COORDS ARE ALL IN RANGE [0..1] !!! *//* Add a 0.5 offset to sample texel at its center */nTX = (texelXcoord + 0.5f) / textureWidth;nTY = (texelYcoord + 0.5f) / textureHeight;texelObjectSpace.x = (Ax * nTX) + (Bx * nTY) + Cx;texelObjectSpace.y = (Ay * nTX) + (By * nTY) + Cy;texelObjectSpace.z = (Az * nTX) + (Bz * nTY) + Cz;/* texelObjectSpace holds texel position in object space */


Finally, to obtain world-space texel position, just multiply the vector texelObjectSpace by your world matrix.

The code listed doesn't need perspective correction because we are working in object space; if you want to obtain the texel window-space coordinates, just apply the usual geometric transformations (world, camera, projection, perspective division, viewport). Remember that, i.e. in OpenGL, the ModelView matrix represents the combination of world and camera matrices.

I hope this helps,

Marco.

This topic is closed to new replies.

Advertisement