Texture mapping on 3DS VAO is mirrored
I have implemented 3ds modelloader following this tutorial . My program is currently using VAOs that display things. This has worked so far for simple cubes and tiles. Textures were displayed correctly.
However, when I load the 3DS model in VAO it is different from sotry. The model looks correct in relation to the vertices, but for some reason the texture on the y-axis is mirrored.
This is how I read 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 UV:
[StructLayout(LayoutKind.Sequential)]
struct UV
{
public float U { get; set; }
public float V { get; set; }
}
Attribute creation:
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 on the left, my version is right:
source to share
Yes, you must flip the V-texture coordinate when importing the 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 the import model code or by selecting the appropriate option to switch UVs in your 3D modeling software.
You can take a look at the answer and comments to understand this behavior in this related question: fooobar.com/questions/1990340 / ...
source to share