Mapping texture on 3DS VAO turns out mirrored

徘徊边缘 提交于 2019-12-11 05:50:06

问题


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:


回答1:


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



来源:https://stackoverflow.com/questions/14156476/mapping-texture-on-3ds-vao-turns-out-mirrored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!