Search code examples
c#opentk

OpenTK UV issue


So, I have been using OpenTK and was able to make it load an obj file but I have a problem. whenever I try to load a texture the UVs are all messed up, while In blender everything is fine. I used AssimpNet to load in the model.

Here is a screenshot of a Hat with its texture applied.

enter image description here

A sample from the Loader script, it clearly stores the Uvs.

    public RawModel LoadModel(float[] positions, float[] uvs, float[] normals, int[] indices)
        {
            int vaoID = CreateVAO();
            BindIndices(indices);
            StoreAttributes(0, 3, positions);
            StoreAttributes(1, 3, normals);
            StoreAttributes(2, 2, uvs);
            GL.BindVertexArray(0);
            return new RawModel(vaoID, indices.Length);
        }
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoords;

void main()
{
    gl_Position = vec4(aPos, 1.0) * model * view * projection;
    FragPos = vec3(vec4(aPos, 1.0) * model);
    Normal = aNormal * mat3(transpose(inverse(model)));
    TexCoords = aTexCoords;
}

This is the shader.vert script and it loads the texture coords(UVs) into the fragment shader.

#version 330 core
struct Material {
    sampler2D diffuse;
    float     shininess;
};
struct Light {
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

uniform Light light;
uniform Material material;
uniform vec3 viewPos;

out vec4 FragColor;

in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;

void main()
{
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

    // Diffuse 
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(light.position - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

    // Specular
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    vec3 specular = light.specular * spec;

    vec3 result = ambient + diffuse + specular;
    FragColor = vec4(result, 1.0);
}

This is the shader.frag script and from here we can see that it loads the diffuse(the actual texture) and runs a builtin function which makes a texture out of the diffuse and texCoord.

If anyone could help me or knows a way to fix it please tell me so! Thank you.


Solution

  • I fixed it, I have found out that in the load.cs script it added the UV's incorrectly! the second UV coord had to be negated, as from what I was told blender does the UV coords a bit different than OpenGL.