PDA

View Full Version : Problem with texturing - textures are mirrored



Brainer
02-07-2008, 04:36 AM
:arrow:System: Windows Vista, Pentium D 805 2,66 GHz, GeForce 7600 GS
:arrow:Compiler/IDE: Delphi 2007 for Win32
:arrow:API: OpenGL, Vampyre Imaging Library

Hello men. :)

I have a little problem with my texturing unit. I've noticed that the textures are mirrored. Is it a problem with the graphics library I use or maybe OpenGL can be at fault?

Here (http://i31.tinypic.com/pnix0.jpg) is a screenshot (and for those who don't know, how it should look like, here (http://upload.wikimedia.org/wikipedia/commons/6/6d/Africa_satellite_plane.jpg) is the correct version :wink:) and below is the code I use for loading a texture. Please not that the texture file itself IS NOT mirrored.

procedure TTexture.LoadTexture(TheFile: TFileEntry);
var
ImagesArray: TDynImageDataArray;
MainLevelIndex: Integer;
ImgFormat: TImageFormat;
begin
if LoadMultiImageFromStream(TheFile.TheData, ImagesArray) and
(Length(ImagesArray) > 0) then
begin
case FTexDetail of
tdLow: MainLevelIndex := 2;
tdMedium: MainLevelIndex := 1;
tdHigh: MainLevelIndex := 0;
end;
case FTexComp of
tcNone: ImgFormat := ifUnknown;
tcDXT1: ImgFormat := ifDXT1;
tcDXT3: ImgFormat := ifDXT3;
tcDXT5: ImgFormat := ifDXT5;
end;
FTexHandle := CreateGLTextureFromMultiImage(ImagesArray, 0, 0, True,
MainLevelIndex, ImgFormat);
if (FTexHandle > 0) then
begin
Log.Log('Texture created successfully.', 'UBETexturing');
FTexFile := TheFile.FileName;
end else
Log.Log('Unable to create the texture.', 'UBETexturing');
end else
Log.Log('Unable to load texture.', 'UBETexturing');

TheFile.TheData.Seek(0, soFromBeginning);
FreeImagesInArray(ImagesArray);
end;


Thank you in advance! :)

User137
02-07-2008, 12:44 PM
It depends on your model perhaps, more precisely texture coordinates. Is it generated sphere or ready planet from some modelling tool?

If you want to test your texture with a simple quad, use glTexCoord2f(0,0) for upper left corner of texture and 1,1 for lower right.

NecroDOME
02-07-2008, 02:39 PM
I got the same problem with DirectX. The solution I use is kinda cappy, but works: I flip the image. Better would be to use 1 - TexCoord I think on only the X or Y when loading the model. Anyways if you find a solution tell me, I;m also interested!
(My problem is only when I export from 3Ds Max to .ASE or .OBJ (didn't try other formats))

Brainer
02-07-2008, 03:25 PM
Using User137's code:

procedure RectT(x1, y1, x2, y2: Single);
begin
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(x1, y1);
glTexCoord2f(0.0, 1.0); glVertex2f(x1, y2);
glTexCoord2f(1.0, 1.0); glVertex2f(x2, y2);
glTexCoord2f(1.0, 0.0); glVertex2f(x2, y1);
glEnd();
end;

This is what I get:
http://i29.tinypic.com/2gvikxv.jpg.

So I think that the texture loading is wrong, then. Any ideas how to fix it? NecroDOME's solution is good, but well, it's pretty inconvenient to flip every image I want to use. :?

NecroDOME
02-07-2008, 03:30 PM
isn't that OpenGL's coordinat system is upside down compaired with DirectX.

Brainer
02-07-2008, 03:33 PM
This is what I've found after a lighting-fast googlin' ;)


OpenGL's coordinate system makes sense: bottom-up orientation, pixel and texel centers on half-integer coordinates, normalized device coordinates are -1 to 1 in all axes. Direct3D is a mess: device coordinates are bottom-up with centers on integers — which has the nice side effect of making the projection transform viewport-dependent — but functions that take integer screen-space rects are top-down, and textures are top-down with centers on half-integers. NDC X and Y are -1 to 1 but Z is 0 to 1. Argh!

User137
02-07-2008, 03:52 PM
Using User137's code:
Sorry, i use RectT in ortho mode normally where Y axis is flipped. It doesn't matter with textures that are sideless though.

This code makes it correctly on me:
glLoadIdentity;
glDisable(GL_CULL_FACE);
glTranslatef(0,0,-3);
tex.SetTex(1);
RectT(-1,1,1,-1);
nxFlip;

Brainer
02-07-2008, 03:55 PM
Yip, that solves the problem and it seems that my sphere's coordinates are wrong. Can you please take a look at the code and tell me what's wrong?

procedure TSphere.DoRender;
var
I, J: Integer;
drho, dtheta, ds, dt, S, T, rho, srho, crho, srhodrho, crhodrho, theta,
stheta, ctheta, X, Y, Z: Single;
begin
drho := PI / FStacks;
dtheta := 2.0 * PI / FSlices;
ds := 1.0 / FSlices;
dt := 1.0 / FStacks;
T := 1.0;
S := 0.0;

for I := 0 to FStacks -1 do
begin
rho := I * drho;
srho := Sin(rho);
crho := Cos(rho);
srhodrho := Sin(rho + drho);
crhodrho := Cos(rho + drho);

glBegin(GL_TRIANGLE_STRIP);
S := 0.0;
for J := 0 to FSlices do
begin
if (J = FSlices) then
theta := 0.0
else
theta := J * dtheta;
stheta := -Sin(theta);
ctheta := Cos(theta);

X := stheta * srho;
Y := ctheta * srho;
Z := crho;

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, S, T);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, S, T);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB, S, T);
glMultiTexCoord2fARB(GL_TEXTURE3_ARB, S, T);
glNormal3f(X, Y, Z);
glVertex3f(X * 1.0, Y * 1.0, Z * 1.0);

X := stheta * srhodrho;
Y := ctheta * srhodrho;
Z := crhodrho;

glMultiTexCoord2fARB(GL_TEXTURE0_ARB, S, T - dt);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, S, T - dt);
glMultiTexCoord2fARB(GL_TEXTURE2_ARB, S, T - dt);
glMultiTexCoord2fARB(GL_TEXTURE3_ARB, S, T - dt);
S := S + ds;
glNormal3f(X, Y, Z);
glVertex3f(X * 1.0, Y * 1.0, Z * 1.0);
end;
glEnd();

T := T - dt;
end;
end;

Thanks in advance! :D

User137
02-07-2008, 04:01 PM
If all you need to do is flip texture X coordinates, set S := 1; instead of 0 and replace S := S + ds; with S := S - ds;

Brainer
02-07-2008, 04:10 PM
You're my hero, User137! :D :wink:
@NecroDOME: How about tryin' to do the same as User137 suggested me to do? Try flipping the X coordinate and it should work for you as well.

Brainer
02-07-2008, 04:20 PM
You're my hero, User137! :D :wink:
@NecroDOME: How about tryin' to do the same as User137 suggested me to do? Try flipping the X coordinate and it should work for you as well.