D3D RM - Texturing non-rectangular Faces
Hello all,
I am having just a hell of a time, and I'm really hoping someone can help out.
I have set up a group on a mesh with the following coordinates:
v[0] (-0.25, 0.25, 0)
v[1] (0.25, 0.25, 0)
v[2] (0.35, -0.25, 0)
v[3] (-0.35, -0.25, 0)
This is a trapezoid, obviously. All of the vertex normals are set to (0,0,-1). The camera is at (0,0,0) and the frame holding the mesh is at (0,0,2).
I add this with the following commands:
LargeMapMesh->AddGroup (4,1,4,fData, &Group);
LargeMapMesh->SetVertices(Group, 0, 4, v);
where v holds my vertex data, and fData = {0,1,2,3}. I then set the quality, addvisual etc. The mesh shows up fine, no problems yet.
Now I try to add in a texture to the equation. I set the tu and tv values of my vertices (in order):
(0,0)
(1,0)
(1,1)
(0,1)
The test texture I am using is a square with a series of vertical stripes (much like the test texture they use in TV studios to adjust the color). What I *want* it to do is map onto the face smoothly, such that the lines converge somewhat as they approach the top of the trapezoid. Instead of that, however, what happens is the face seems to be textured in two parts. There is an invisible line running from two of the opposite diagonal corners. Starting at the bottom of the trapezoid, all of the lines run parallel to the left edge until they hit this line, then the texture bends and the lines run parallel to the right edge, causing a "crease" in the texture, which is not at all what I am after. I know I haven't described this very well, but there is a screenshot at www.rjcyberware.com/meshcomp.htm (near the bottom. Ignore the stuff in the table. Figures 4-6 are what I am talking about).
Now, I have an idea that I need to use LargeMapMesh->SetGroupMapping(Group, D3DRMMAP_WRAPU); but when I add that line (or with D3DRMMAP_WRAPV), the texture dissapears altogether! I have also tried various wraps on the mesh itself, but none of them have gotten rid of that dratted crease.
Does anyone know what I am doing wrong, and how I can fix it?
Ron
Edited by - RonH on 1/15/00 12:03:26 AM
I''ve had that problem too. The problem is this: Ok, take your trapezoid and split it into 2 triangles. Note that one triangle is much bigger than the other one. Right beside it, draw a rectangle and split it into 2 triangles. Identical sized triangles. Ok, now imagine the rectangle one is your texture. DirectX maps one triangle of that texture to the small triangle of the trapezoid, and the other one to the big triangle. Result? One is squished and one is stretched, causing ugliness!
One possible solution is to draw a trapezoid onto your texture (assuming the small end of the trapezoid is up) your UV coordinates of the UL and UR corners should be adjusted to coincide with the UL and UR corners of this new trapezoid (so instead of being 0,0 and 1,0 you get something like 0.2,0 and 0.8,0). Get the proportions right and this should hopefully get rid of the artifact.
Hope this helps you.
-ns-
One possible solution is to draw a trapezoid onto your texture (assuming the small end of the trapezoid is up) your UV coordinates of the UL and UR corners should be adjusted to coincide with the UL and UR corners of this new trapezoid (so instead of being 0,0 and 1,0 you get something like 0.2,0 and 0.8,0). Get the proportions right and this should hopefully get rid of the artifact.
Hope this helps you.
-ns-
Thanks NightShade. I was beginning to think I was the only one who has ever attempted to texture a non-rectangular face, heh. Amazingly, I have been completely unable to find any info at all on the web about this subject. Ah well.
I like your suggestion, and I think that may work to get rid of the crease. I will give it a try tomorrow. One thing, though. With a texture coordinate at the UL of (say) 0.2,0, you are going to cut off part of the rectangular texture, and same goes for the right side. That would be bad for what I am attempting (I need to have it compress horizontally as it goes toward the narrow part at the top). But I guess I could take the rectangular texture and redraw it so it''s proportional to the face dimensions, leaving the cut off portions black or something.
Unless anyone has any better ideas?
Thanks for the tip.
Ron
I like your suggestion, and I think that may work to get rid of the crease. I will give it a try tomorrow. One thing, though. With a texture coordinate at the UL of (say) 0.2,0, you are going to cut off part of the rectangular texture, and same goes for the right side. That would be bad for what I am attempting (I need to have it compress horizontally as it goes toward the narrow part at the top). But I guess I could take the rectangular texture and redraw it so it''s proportional to the face dimensions, leaving the cut off portions black or something.
Unless anyone has any better ideas?

Thanks for the tip.
Ron
Glad I could help. I forgot to mention to resize your bitmap image to a trapezoid (leaving some borders that should probably be similar color to the inside). This is probably the easiest way assuming your 3d trapezoid doesn''t change shape. Another method would be to cut it into segments (horizontally assuming the trapezoid "points" up or down) and chop each segment into triangles, this is alot more work tho and would probably still look funny if the trapezoid sides slope alot (if its really far off from rectangular).
Another method that comes to mind that should work if the trapezoid has identical sloped sides (if its symmetrical) is this: Assuming its "pointed" up, draw a vertical line down from each top vertex, forming a rectangle and 2 triangles on either side. Split the rectangle into 2 triangles. The 2 upper verts would have tu,tv''s of 0,0 and 1,0, and the lower outer ones would have 0,1 and 1,1. The inner two lower verts would have tu,tv''s somewhere in between in proportion to the length of the bottom of the 2 outer triangles. Hey this might even work for non-symmetrical trapezoids, and will work if the trapezoid changes shape (tho you might need to recompute tu,tv).
I''m out of time. I never tried the above but it looks like it will work. gotta run!
-ns-
Another method that comes to mind that should work if the trapezoid has identical sloped sides (if its symmetrical) is this: Assuming its "pointed" up, draw a vertical line down from each top vertex, forming a rectangle and 2 triangles on either side. Split the rectangle into 2 triangles. The 2 upper verts would have tu,tv''s of 0,0 and 1,0, and the lower outer ones would have 0,1 and 1,1. The inner two lower verts would have tu,tv''s somewhere in between in proportion to the length of the bottom of the 2 outer triangles. Hey this might even work for non-symmetrical trapezoids, and will work if the trapezoid changes shape (tho you might need to recompute tu,tv).
I''m out of time. I never tried the above but it looks like it will work. gotta run!
-ns-
I believe the problem you''re getting here is not using perspective-correct texturing.
If your triangles are different sizes it''s because one is further away from the camera so at the join you will get the crease. To fix this, as the renderer is drawing the triangle it must compensate for this. To implement this you must apply perspective calculations rather than linear calculations when fetching the texels to place on the polygon. Say the top of the poly in screen-space is further away than the bottom, *more* than half of the texture should be applied to the top half of the poly in terms of screen-pixels...
I haven''t used retained mode but in immediate mode I believe there is a SetRenderState() attribute that turns perspective-correction on or off.
If your triangles are different sizes it''s because one is further away from the camera so at the join you will get the crease. To fix this, as the renderer is drawing the triangle it must compensate for this. To implement this you must apply perspective calculations rather than linear calculations when fetching the texels to place on the polygon. Say the top of the poly in screen-space is further away than the bottom, *more* than half of the texture should be applied to the top half of the poly in terms of screen-pixels...
I haven''t used retained mode but in immediate mode I believe there is a SetRenderState() attribute that turns perspective-correction on or off.
sb: It''s a good thought, but no, that isn''t the problem. If you take a look at the vertices, you will note they are all the same distance from the camera. This isn''t a rotated rectangle, but an honest-to-god trapezoid
Also, I have set the perspective correction on already. Thanks for the input, though.
NightShade: I think I''m going to end up doing something along the lines of what you suggest (splitting the trapezoid up into sub-triangles). I need to add more vertices to the mesh anyway to smooth out the overall shape of the whole object (which is a sphere). I''ll see what that does to the bends in the texture. I suspect it will improve things quite a bit, and I may not need to manually squash the textures to fit the shape of the trapezoid. But if it still looks like there are obvious creases, I can always add in a routine to change the textures, and between the two of those things, I think that ought to do it!
Ron

NightShade: I think I''m going to end up doing something along the lines of what you suggest (splitting the trapezoid up into sub-triangles). I need to add more vertices to the mesh anyway to smooth out the overall shape of the whole object (which is a sphere). I''ll see what that does to the bends in the texture. I suspect it will improve things quite a bit, and I may not need to manually squash the textures to fit the shape of the trapezoid. But if it still looks like there are obvious creases, I can always add in a routine to change the textures, and between the two of those things, I think that ought to do it!
Ron
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement