Archived

This topic is now archived and is closed to further replies.

EasyGino

Texture Mapping

Recommended Posts

Hello all, Ok, I''ve moved on from drawing some primitive objects like a triangle to a more advanced "pyramid" and now im looking at wrapping it up in the DX9 tutorial "banana" texture. However in there example they wrap it around a cylinder where my object is a pyramid. So I have this to define my pyramid. CUSTOMVERTEX g_Vertices[] = { { -.50f, 1.0f, -0.0f, 0xff000fff, }, { -1.0f, .0f, -.5f, 0xff000fff, }, { -.50f, .0f, 0.0f, 0xff0fffff, }, { -.5f, .0f,0.0f, 0xff000fff, }, { -.50f, 1.0f, -0.0f, 0xff000fff, }, { 0.0f, .0f, -0.5f, 0xff0fffff, }, { -1.0f, .0f, -.5f, 0xff000fff, }, { .0f, .0f, -.5f, 0xff000fff, }, { -.5f, 1.0f, .0f, 0xff0fffff,}, { -1.0f, .0f, -.5f, 0xff000fff, }, { .0f, .0f, -.5f, 0xff000fff, }, { -.5f, .0f, .0f,0xff0fffff, }, }; for( int i=0; i<12; i++) { #ifndef TEXTURE g_Vertices.tu = -1.0f; g_Vertices[i].tv = -1.0f; #endif } HOWEVER i''m honestly unsure on what tu and tv is suppose to be set too. I''ve tried mimicing the x,y coordinates of my g_Vertices, but that doesn''t work either. Help please! Mike

Share this post


Link to post
Share on other sites
Well, first of all, the tu and tv coordinates (henceforth known as uv coords) will need to be different for each vertex. That loop you''ve got assigns identical uv coords to all the vertices, which will result in an object with a uniform color.

Look here for a brief introduction to uv mapping.

Share this post


Link to post
Share on other sites
Ok can anyone else offer me some insight on what I need to do? I can''t read a tutorial like that and understand fully, I need to be able to see the code for what they are doing. It''s a simple pyramid with the above vertices and im confused on where to start using these UV settings. argh.

Share this post


Link to post
Share on other sites
Think a texture as a 2d plane with area (0,0)-(1,1).
Now, every vertex needs to include a coordinate corresponding to one point in this plane. The hardware calculates, by these coordinates, at which points of the texture it should get the colors of the pixels of the rasterized triangles.

Sorry, i can''t explain it easier, but i hope this helps.

-Nik

Share this post


Link to post
Share on other sites
Im such a pain,

Ok for example this triangle here, just a plane triangle.

{ -.50f, 1.0f, 0.0f, 0xff000fff, }
{ -1.0f, .0f, 0.0f, 0xff000fff, }
{ -.50f, .0f, 0.0f, 0xff0fffff, }

now what would the value of my pu, pv? -1.0f, 1.0f? that would cover almost all the triangle? Or am I not looking at this in the right way?

Share this post


Link to post
Share on other sites
Ok so what you are saying is, for a triangle I need 3 pu, pv coords right? each corresponding to each vertex.. respectively.

so a [0,0] [0,1] [1,1] and that will draw a half triangle BUT completely distort my image, right? but it''ll draw it onto the triangle?

Share this post


Link to post
Share on other sites
Essentially would this work?


for (int i=0; i<4; i++){
g_Vertices.tu = 0.0f;
g_Vertices[i].tv = 0.0f;
g_Vertices[i + 1].tu = 0.0f;
g_Vertices[i + 1].tv = 1.0f;
g_Vertices[i + 2].tu = 1.0f;
g_Vertices[i + 2].tv = 1.0f;
}

Share this post


Link to post
Share on other sites
I suggest that until you grasp the concept of texture mapping, you just work with a single triangle rather than a pyramid. Once you get the idea, then you''ll be able to expand to a pyramid or anything else.
The thing to keep in mind with texture mapping is that the uv coords of your vertices are completely independant of the actual position of the vertices in space. The uv coords are only used to determine what part of a texture appears on the triangle, regardless of where the triangle is located or how big it is.

Now, to actual code. Forget that loop, it is not helping you. It was only useful when you were assigning the same uv coords to each vertex, but since that is not what you want to do, you should abandon it and assign the coords by hand, like you did with the physical coords. For a single triangle, try something like this:

CUSTOMVERTEX g_Vertices[] =
{
{ 0.5f, 0.0f, 0.0f, 0xff000fff, },
{ 0.0f, 1.0f, 0.0, 0xff000fff, },
{ -0.5f, 0.0f, 0.0f, 0xff0fffff, }
};

#ifndef TEXTURE
g_Vertices[0].tu = 0.0f;
g_Vertices[0].tv = 0.0f;
g_Vertices[1].tu = 0.0f;
g_Vertices[1].tv = 1.0f;
g_Vertices[2].tu = 1.0f;
g_Vertices[2].tv = 1.0f;
#endif

Now, if you were using a texture like the smiley face in nik02''s post, your triangle will be covered by the bottom left half of the texture, showing most of the mouth and a little of the left eye. It will look a bit skewed, since the actual shape of the triangle does not exactly match the right-triangle traced by the uv coords. Once you have that working, play around with the shape of the triangle, and with the uv coords, and you will soon get the idea of how it works.

By the way, when posting code snippets, put them within "code" or "source" tags, which will prevent the strange formatting you''ve been getting in your posts, and make it more readable for the rest of us. Precede the code snippet with the word "code" or "source" inside square brackets, and finish it with the same word preceded by a forward slash, also inside square brackets. Code tags merely preserve your formatting, source tags create that nice little scrollable text box.

Share this post


Link to post
Share on other sites