PDA

View Full Version : bug ttexcoord



strangerranger
01-02-2004, 01:32 PM
Maybe this is already known in this forum, but since I got hit by this bug I'll post the solution.
The following code is taken from gamedev.net and solved my problem.





function TAGFImage.AcquireTexture(TexCoord: TTexCoord; var VertexArray: TVertexArray4;

var Texture: IDirect3DTexture9): Integer;

var

TexNum, i: Integer;

FloatX1, FloatY1,

FloatX2, FloatY2,

SrcX, SrcY: Single;

begin

Texture:= nil;


if &#40;not FInitialized&#41;or&#40;Length&#40;Textures&#41; < 1&#41;or&#40;PCount < 1&#41;or&#40;not FHardware&#41; then

begin

Result&#58;= errInvalidCall;

Exit;

end;


// clip pattern number to available patterns

if &#40;TexCoord.Pattern < 0&#41; then TexCoord.Pattern&#58;= 0;

if &#40;TexCoord.Pattern > PCount - 1&#41; then TexCoord.Pattern&#58;= PCount - 1;


// select the specified texture

TexNum&#58;= TexCoord.Pattern div ImgPerTex;

Texture&#58;= Textures&#91;TexNum&#93;;


// calculate vertex coordinates

i&#58;= TexCoord.Pattern mod ImgPerTex;

SrcX&#58;= &#40;i mod ImgPerW&#41; * PWidth;

SrcY&#58;= &#40;&#40;i div ImgPerW&#41; mod ImgPerH&#41; * PHeight;

FloatX1&#58;= &#40;SrcX + TexCoord.SrcX&#41; / TWidth;

FloatY1&#58;= &#40;SrcY + TexCoord.SrcX&#41; / THeight; //here is one error&#58; texcoord.SrcY would be right, i think

if &#40;TexCoord.Width > 0&#41; then

FloatX2&#58;= &#40;&#40;SrcX + TexCoord.Width&#41; / TWidth&#41; //here is another error&#58; it should be &#40;&#40;SrcX + Texcoord.SrcX + Texcoord.Width&#41; / TWidth&#41; to provide the right result

else FloatX2&#58;= &#40;&#40;SrcX + PWidth&#41; / TWidth&#41;;


if &#40;TexCoord.Height > 0&#41; then

FloatY2&#58;= &#40;&#40;SrcY + TexCoord.Height&#41; / THeight&#41; //the same as above&#58; &#40;&#40;SrcY + Texcoord.SrcY + Texcoord.Height&#41; / THeight&#41; should be right

else FloatY2&#58;= &#40;&#40;SrcY + PHeight&#41; / THeight&#41;;


if &#40;TexCoord.Mirror&#41; then

begin

VertexArray&#91;0&#93;.tu&#58;= FloatX2;

VertexArray&#91;1&#93;.tu&#58;= FloatX1;

VertexArray&#91;2&#93;.tu&#58;= FloatX2;

VertexArray&#91;3&#93;.tu&#58;= FloatX1;

end else

begin

VertexArray&#91;0&#93;.tu&#58;= FloatX1;

VertexArray&#91;1&#93;.tu&#58;= FloatX2;

VertexArray&#91;2&#93;.tu&#58;= FloatX1;

VertexArray&#91;3&#93;.tu&#58;= FloatX2;

end;


if &#40;TexCoord.Flip&#41; then

begin

VertexArray&#91;0&#93;.tv&#58;= FloatY2;

VertexArray&#91;1&#93;.tv&#58;= FloatY2;

VertexArray&#91;2&#93;.tv&#58;= FloatY1;

VertexArray&#91;3&#93;.tv&#58;= FloatY1;

end else

begin

VertexArray&#91;0&#93;.tv&#58;= FloatY1;

VertexArray&#91;1&#93;.tv&#58;= FloatY1;

VertexArray&#91;2&#93;.tv&#58;= FloatY2;

VertexArray&#91;3&#93;.tv&#58;= FloatY2;

end;


Result&#58;= errNone;

end;