DGL
https://delphigl.com/forum/

u,v -> s,t
https://delphigl.com/forum/viewtopic.php?f=19&t=4553
Seite 1 von 1

Autor:  Stucuk [ Fr Sep 09, 2005 01:34 ]
Betreff des Beitrags:  u,v -> s,t

Im wondering how to convert u,v to s,t coords. Searching the net has come up with nothing helpful.

Autor:  BTierens [ Fr Sep 09, 2005 11:36 ]
Betreff des Beitrags: 

What are s,t coordinates?

Autor:  Lyr [ Fr Sep 09, 2005 13:34 ]
Betreff des Beitrags: 

What are u, v coords? :-)

They are just other letters for the same thing. OpenGL uses s,t,r and some graphic editors and D3D are using u,v,w. Maybe there are small varieties in the meaning of (0;0) = (left;top) or (0;0) = (right;bottom) and you have to be carefull with the usage of non power of two textures too.

Autor:  Stucuk [ Fr Sep 09, 2005 14:11 ]
Betreff des Beitrags: 

I thought u,v are in world space and s,t are in texture coords.

Zitat:
The texinfo lump (6) contains an array of texinfo_t structures:

typedef struct texinfo_s
{
float textureVecsTexelsPerWorldUnits[2][4]; // [s/t][xyz offset]
float lightmapVecsLuxelsPerWorldUnits[2][4]; // [s/t][xyz offset] - length is in units of texels/area
int flags; // miptex flags + overrides
int texdata; // Pointer to texture name, size, etc.
} texinfo_t;

Each texinfo is 72 bytes long.

The first array of floats is in essence two vectors that represent how the texture is orientated and scaled when rendered on the world geometry. The two vectors, s and t, are the mapping of the left-to-right and down-to-up directions in the texture pixel coordinate space, onto the world. Each vector has an x, y, and z component, plus an offset which is the "shift" of the texture in that direction relative to the world. The length of the vectors represent the scaling of the texture in each direction.

The 2D coordinates (u, v) of a texture pixel (or texel) are mapped to the world coordinates (x, y, z) of a point on a face by:
u = tv0,0 . x + tv0,1 . y + tv0,2 . z + tv0,3
v = tv1,0 . x + tv1,1 . y + tv1,2 . z + tv1,3

where tvA,B is textureVecs[A][B].


The U,V calculation uses the Vertex's position which doesn't give s,t coords. (Insted of having a nice texture its basicaly one colour).

Code:
  1.  
  2. P := {Normalize2}(RenderVerts[rendervertno].Position);
  3.  
  4. RenderVerts[rendervertno].TextureCoord.X := (TexInfo[Faces[x].TexInfo].TextureVects[0][0] * P.X + TexInfo[Faces[x].TexInfo].TextureVects[0][1] * P.Y + TexInfo[Faces[x].TexInfo].TextureVects[0][2] * P.Z) + TexInfo[Faces[x].TexInfo].TextureVects[0][3];
  5. RenderVerts[rendervertno].TextureCoord.Y := (TexInfo[Faces[x].TexInfo].TextureVects[1][0] * P.X + TexInfo[Faces[x].TexInfo].TextureVects[1][1] * P.Y + TexInfo[Faces[x].TexInfo].TextureVects[1][2] * P.Z) + TexInfo[Faces[x].TexInfo].TextureVects[1][3];
  6.  


The above code gives the following result:

Bild

The best i can get it is with the following code:
Code:
  1.  
  2. RenderVerts[rendervertno].TextureCoord.Y := (TexInfo[Faces[x].TexInfo].TextureVects[0][0] * P.X + TexInfo[Faces[x].TexInfo].TextureVects[0][1] * P.Y + TexInfo[Faces[x].TexInfo].TextureVects[0][2] * P.Z)/TexData[TexInfo[Faces[x].TexInfo].texdata].Height + TexInfo[Faces[x].TexInfo].TextureVects[0][3];
  3. RenderVerts[rendervertno].TextureCoord.X := -(TexInfo[Faces[x].TexInfo].TextureVects[1][0] * P.X + TexInfo[Faces[x].TexInfo].TextureVects[1][1] * P.Y + TexInfo[Faces[x].TexInfo].TextureVects[1][2] * P.Z)/TexData[TexInfo[Faces[x].TexInfo].texdata].Width + TexInfo[Faces[x].TexInfo].TextureVects[1][3];
  4.  


Bild

Autor:  Lyr [ Fr Sep 09, 2005 15:30 ]
Betreff des Beitrags: 

Ok, this is a very special usage of s,t,u and v :-).

[edit] forget it ... bullshit

[edit2] ok I don't know what exactly these values are meant for, but at least I think to normalize the vertex position isn't a good idea. But I guess you've already tried this?

Autor:  Stucuk [ Fr Sep 09, 2005 16:47 ]
Betreff des Beitrags: 

Yep, normalising it makes the texture pure one colour (dependant on the texture)

I can give the source if needed (well the VBSP unit and VTF unit. with a small amount of modification it would work in any app)

Autor:  Stucuk [ Mo Sep 12, 2005 04:42 ]
Betreff des Beitrags: 

fixed it. I forgot to change the Y and Z around in the textures rotation thingy. Correct code is:

Code:
  1.  
  2. RenderVerts[rendervertno].TextureCoord.X := (TexInfo[Faces[x].TexInfo].TextureVects[0][0] * P.X + TexInfo[Faces[x].TexInfo].TextureVects[0][2] * P.Y + -TexInfo[Faces[x].TexInfo].TextureVects[0][1] * P.Z)/TexData[TexInfo[Faces[x].TexInfo].texdata].Width + TexInfo[Faces[x].TexInfo].TextureVects[0][3]/TexData[TexInfo[Faces[x].TexInfo].texdata].Width;
  3. RenderVerts[rendervertno].TextureCoord.Y := -(TexInfo[Faces[x].TexInfo].TextureVects[1][0] * P.X + TexInfo[Faces[x].TexInfo].TextureVects[1][2] * P.Y + -TexInfo[Faces[x].TexInfo].TextureVects[1][1] * P.Z)/TexData[TexInfo[Faces[x].TexInfo].texdata].Height + 1 - TexInfo[Faces[x].TexInfo].TextureVects[1][3]/TexData[TexInfo[Faces[x].TexInfo].texdata].Height;
  4.  


And heres how it looks:
Bild

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/