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.
Code:
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 (not FInitialized)or(Length(Textures) < 1)or(PCount < 1)or(not FHardware) then
begin
Result:= errInvalidCall;
Exit;
end;
// clip pattern number to available patterns
if (TexCoord.Pattern < 0) then TexCoord.Pattern:= 0;
if (TexCoord.Pattern > PCount - 1) then TexCoord.Pattern:= PCount - 1;
// select the specified texture
TexNum:= TexCoord.Pattern div ImgPerTex;
Texture:= Textures[TexNum];
// calculate vertex coordinates
i:= TexCoord.Pattern mod ImgPerTex;
SrcX:= (i mod ImgPerW) * PWidth;
SrcY:= ((i div ImgPerW) mod ImgPerH) * PHeight;
FloatX1:= (SrcX + TexCoord.SrcX) / TWidth;
FloatY1:= (SrcY + TexCoord.SrcX) / THeight; //here is one error: texcoord.SrcY would be right, i think
if (TexCoord.Width > 0) then
FloatX2:= ((SrcX + TexCoord.Width) / TWidth) //here is another error: it should be ((SrcX + Texcoord.SrcX + Texcoord.Width) / TWidth) to provide the right result
else FloatX2:= ((SrcX + PWidth) / TWidth);
if (TexCoord.Height > 0) then
FloatY2:= ((SrcY + TexCoord.Height) / THeight) //the same as above: ((SrcY + Texcoord.SrcY + Texcoord.Height) / THeight) should be right
else FloatY2:= ((SrcY + PHeight) / THeight);
if (TexCoord.Mirror) then
begin
VertexArray[0].tu:= FloatX2;
VertexArray[1].tu:= FloatX1;
VertexArray[2].tu:= FloatX2;
VertexArray[3].tu:= FloatX1;
end else
begin
VertexArray[0].tu:= FloatX1;
VertexArray[1].tu:= FloatX2;
VertexArray[2].tu:= FloatX1;
VertexArray[3].tu:= FloatX2;
end;
if (TexCoord.Flip) then
begin
VertexArray[0].tv:= FloatY2;
VertexArray[1].tv:= FloatY2;
VertexArray[2].tv:= FloatY1;
VertexArray[3].tv:= FloatY1;
end else
begin
VertexArray[0].tv:= FloatY1;
VertexArray[1].tv:= FloatY1;
VertexArray[2].tv:= FloatY2;
VertexArray[3].tv:= FloatY2;
end;
Result:= errNone;
end;
Bookmarks