DX7 Texture coords - how do I work them out

Started by
7 comments, last by risingdragon3 24 years, 1 month ago
How do I work out texture coords for imported models, actually Wavefront OBJ files?Say I have a collection of D3DVERTEXs as a trianglelist. and I want to apply a texture, I do the Settexture thing. And there I am stuck. Supposedly I guess I must work out the texture uv coords for each vertex, but how do I do this?I have avoided doing this for as long as possible, but now i have to implement to finish off my Scene class Somebody help me!!!!!
Advertisement
Hi

if you wan''t it simple, you can do somekind of planar mapping, that means take the x/y Coords ( or x/z or y/z )
of your Vertices and scale them in a way, so that they lie between 0 and 1, and then use them as u/v Coords.
That is the simplest way, but only usefull for Objects, that
are mostly Flat.

But if someone knows an Algorithm to apply Texture Coords equally onto an Object (no box, no Sphere, an highly complex one ), that would be very cool.

Lars
--------> http://www.larswolter.de <---------
Thanks a lot!
I think I will keep to planar. Maybe in the future upgrade?
I don''t know. Anyway, since all of this is inside a class, I can change it completely without anything affecting anything else (read: C++ is GOOD).
I was thinking... could I create a sphere around the object, project the vertices as a vector on to the sphere, like spokes, and take the uv coords. or is this just spherical mapping. Anyone else with good ideas?
Since I loaded objects from a .x file I created default texture coords. It is very simple. Look below. Of course, I use it only for testing my engine.

Sub FixVerts(Optional offset As Single)
On Error Resume Next
Dim I As Long
Dim V As Long
I = UBound(TriangleList)
If Not Err = 0 Then Exit Sub
For V = 0 To I - 1 Step 2
TriangleList(V).TriangleVerts(0).tu = 2 + offset
TriangleList(V).TriangleVerts(0).tv = 2 + offset

TriangleList(V).TriangleVerts(1).tu = 0 + offset
TriangleList(V).TriangleVerts(1).tv = 0 + offset

TriangleList(V).TriangleVerts(2).tu = 2 + offset
TriangleList(V).TriangleVerts(2).tv = 0 + offset


TriangleList(V + 1).TriangleVerts(0).tu = 0 + offset
TriangleList(V + 1).TriangleVerts(0).tv = 0 + offset

TriangleList(V + 1).TriangleVerts(1).tu = 2 + offset
TriangleList(V + 1).TriangleVerts(1).tv = 2 + offset

TriangleList(V + 1).TriangleVerts(2).tu = 0
TriangleList(V + 1).TriangleVerts(2).tv = 2
Next
End Sub
I tried the planar sys (see above). it was weird. the top and bottom of my test cube as right, but the sides were stretched. I think it''s because of the z coordinate not being looked at, does anyone else have other ideas to incorpate z. I need something that can do some model relatively well. Since I don''t do VB i didn''t understand 1/5 of the code posted up.
There are some basic methods you can use to map a texture to an object; however and you intending to tile the texture?
Anyway, here''s a basic alogrithm you might be able to adapt. It creates suitable texture coordinates on a grid of triangles.


CODE EXAMPLE 1
Verticies are set up like so:
0---1---2---3---4
/ // // // //
/ / / / / / / / /
5---6---7---8---9
...and texture will be mapped as the right way round (square 1 [0,1,5,6]) and flipped (square 2 [1,2,6,7]) every other square, inverting each row (i.e. imagine a checkerboard, black=right_way_round, white=opposite).

int x, // x pos    y, // y pos    width, // grid width    height; // grid heightfor (y=0;y{   for (x=0;x   {      Vertex[(y*width)+x].tu = x % 2;      Vertex[(y*width)+x].tv = y % 2;   }} 

Well even if it has no use, I will still explain my code. What it does is it loops through all of the verticies in my object in steps of 6. In other words it steps through 2 triangles at once. Within the loop it sets a defined texture coordinate. Simple. If you don''t understand then nevermind.
Ohhhhh..... so thats what it does! I seeee..... Anyway thxs for all the code, I think I will implement something like the code up. I finally can forget about texture coords and go on to something more interesting!
Thx all.
Hi

There is an alternative to the planar Mapping, but i am working at it my self, to get it working.
For that method, i am checking the Normals of the triangles, and if they point up, i asign x/z to u/v, if it points to the side i assign x/y to u/v and when it points
to the front, i take y/z. This sounded very good to me, but i have some problems alligning the texture the correct way, because it isn't enough to take just the most left, and most right point, and then scale the u/v Values by that.
But maybe that isn't so Problematic for you.

You could also try the spherical mapping you were talking about.

Lars

Edited by - Lars W. on 3/20/00 2:19:25 AM
--------> http://www.larswolter.de <---------

This topic is closed to new replies.

Advertisement