Question

I implemented a 3ds modelloader following this tutorial. Currently my program uses VAOs do display things. This worked so far for simple cubes and tiles. Textures were displaying correctly.

However, when I load the 3DS model into a VAO its a different sotry. The model looks correct concerning the vertices, but the texture is mirrored along the Y axis for some reason.

This is how i read the UV:

case 0x4140:

qty = reader.ReadUInt16();        // 2 bytes

UV[] texcoords = new UV[qty];

for (i = 0; i < qty; i++)
{
    texcoords[i] = new UV
    {
        U = reader.ReadSingle(),  // 4 bytes
        V = reader.ReadSingle()   // 4 bytes
    };
}

tb = new TexCoordBuffer(texcoords);

And the UV:

[StructLayout(LayoutKind.Sequential)]
struct UV
{
    public float U { get; set; }
    public float V { get; set; }
}

Creating the Attribute:

GL.EnableVertexAttribArray(2);
texCoordBuffer.Bind();
GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, Vector2.SizeInBytes, 0);
GL.BindAttribLocation(shaderHandle, 2, "in_texture");

How the Texture is "created":

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapNearest);

GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Replace);

GL.TexImage2D(
    TextureTarget.Texture2D,
    0,
    PixelInternalFormat.Rgba,
    bmpData.Width,
    bmpData.Height,
    0,
    PixelFormat.Bgra,
    PixelType.UnsignedByte,
    bmpData.Scan0);

GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);

This is how it turns out: Example left, my version right: Comparision

Was it helpful?

Solution

Yes you should flip V texture coordinate when importing model for OpenGL. UV maps in 3DS model and OpenGL are different. OpenGL texture coordinates should be (U; 1-V)

This can be fixed either in import model code or by selecting corresponding option for flipping UV in your 3D modelling software.

You can take a look at answer and comments to understand this behavior in this related question: https://stackoverflow.com/a/8621571/405681

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top