-1

I am trying to load a textured model cube made in Blender using my custom importer made in OpenGL, C++ and assimp. But the Cube is not textured properly as shown in Blender.

This is my Texture : enter image description here

This is how it looks in BLender :enter image description here

This is how it looks in my application;enter image description here

In my application it is rendering the texture but the background of it(the background of my texture is white) so it is seen as a white cube

MY texture loader: It uses SFML to load the texture

GLuint Model_importer::loadTextureFromFile(const char* path) {
    sf::Image texture;
    if (texture.loadFromFile(path)) {
        GLuint tex;
        glGenTextures(1, &tex);
        glBindTexture(GL_TEXTURE_2D, tex);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.getSize().x, texture.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getPixelsPtr());
        
        glGenerateMipmap(GL_TEXTURE_2D);

        return tex;
    }
    else {
        std::cout << "Image loading Error : Image loading failed successfully" << std::endl;
        return GLuint(0);
    }
}

My function which extracts the vertices, indices and the texture Coordinates :

void Model_importer::processMesh(aiMesh* mesh) {
    for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
        MI::Vertex vertex{};
        vertex.position.x = mesh->mVertices[i].x;
        vertex.position.y = mesh->mVertices[i].y;
        vertex.position.z = mesh->mVertices[i].z;
        if (mesh->HasNormals()) {
            vertex.normals.x = mesh->mNormals[i].x;
            vertex.normals.y = mesh->mNormals[i].y;
            vertex.normals.z = mesh->mNormals[i].z;
        }
        if (mesh->mTextureCoords[0]) {
            glm::vec2 texC{};
            texC.x = mesh->mTextureCoords[0][i].x;
            texC.y = mesh->mTextureCoords[0][i].y;
            vertex.textureCoords = texC;
        }
        this->vertices.push_back(vertex);
    }
    if (mesh->mMaterialIndex >= 0) {
        aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
        std::vector<MI::Texture> diffuseTextures = addTexture(material, aiTextureType_DIFFUSE, "texture_diffuse");
        this->textures.insert(textures.end(), diffuseTextures.begin(), diffuseTextures.end());
    }
    for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
        aiFace face = mesh->mFaces[i];
        for (unsigned int j = 0; j < face.mNumIndices; j++) {
            this->indices.push_back(face.mIndices[j]);
        }
    }
}

My function which add the textures :

std::vector<MI::Texture> Model_importer::addTexture(aiMaterial* material, aiTextureType material_type, std::string typeName) {
    std::vector<MI::Texture> textures;
    for (unsigned int i = 0; i < material->GetTextureCount(material_type); i++) {
        aiString string;
        material->GetTexture(material_type, i, &string);
        MI::Texture texture;
        texture.id = loadTextureFromFile(string.C_Str());
        texture.type = typeName;
        texture.path = string.C_Str();
        textures.push_back(texture);
    }
    return textures;
}

My fragment shader :

#version 430 core
out vec4 fragcolor;

uniform sampler2D texture_diffuse1;
in vec2 texpos;

void main() {
    fragcolor = texture(texture_diffuse1, texpos);
}

My Vertex shader if needed :

#version 430 core

layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 texPos;
out vec2 texpos;

uniform mat4 perspective;
uniform mat4 view;

void main() {
    gl_Position = perspective * view * vec4(pos, 1.0);
    texpos = texPos;
}

My drawing function(if needed) :


    void Model_importer::draw(Shaders& shader) {
    int diffuseNo = 1;
    for (unsigned int i = 0; i < this->textures.size(); i++) {
        glActiveTexture(GL_TEXTURE0 + i);

        std::string number{};
        std::string name = textures[i].type;
        if (name == "texture_diffuse")
            number = std::to_string(diffuseNo++);

        shader.setInt(i, name + number);
        glBindTexture(GL_TEXTURE_2D, textures[i].id);
    }
    glBindVertexArray(vao);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

I tried changing the fragment shader : texture(texture_diffuse1, vec2(texpos.x, 1 - texpos.y)) but it also didn't work;

I can't figure out what's wrong in my code, I tried changing the WRAP_T and WRAP_S values, the MIN and MAG filters, the mipmap, etc. but still I face the same issue.

Shloak
  • 15
  • 4
  • @Rabbid76 I am setting it as 'name + diffuseNoStr' which results in '"texture_diffuse1"' I can't figure it out, btw thanks for helping, should i provide my draw function? – Shloak Sep 02 '23 at 16:41

0 Answers0