-1

I exported the Blender Box as a Collada file, I am loading it with Assimp, yet it does not draw properly. Here is how it looks:

MyFailedBox

I have tried using .fbx and .obj without success. is this an error in my code, or is the vertex data not correct? I checked the normals, they all point in the proper direction, whats leads me to believe that the orientation of the faces is correct.

Here is my Code:

#include<assimp/Importer.hpp>
#include<assimp/postprocess.h>
#include<assimp/scene.h>
#include<vector>
#include<iostream>
#include<GL/glew.h>
#include<glm/glm.hpp>
#include<glm/gtx/quaternion.hpp>
#include<glm/gtc/quaternion.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<SFML/Window.hpp>
#include<SFML/OpenGL.hpp>
#include<SFML/Graphics.hpp>
#include"Shader.h"
#include"vertex.h"
#include"Cam.h"
#include"obj.h"

int main() {
    glewExperimental = GL_TRUE;
    if (!glewInit()) {
        std::cout << "Glew Failed to initialize" << std::endl;
        return -5;
    }
    glEnable(GL_DEPTH_TEST);


    ////////////////////////////// WINDOW SHADER CAMERA ///////////////////////////

    sf::Window window(sf::VideoMode(800, 600), "OpenGL");
    window.setActive(true);
    Shader Shader1("vss_min.glsl", "fss_min.glsl");
    Cam camera(window);
    glm::mat4 modelMatrix(1.0);
   
    modelMatrix = glm::scale(modelMatrix, glm::vec3(0.2, 0.2f, 0.2f));

    ///////////////////////////// MESH /////////////////////////////////////////

    std::vector<vertex> vertices;
    std::vector<std::uint32_t> indices;
    Assimp::Importer importer;
    const aiScene* s = importer.ReadFile("c:/meshes/ext/buntekiste.fbx",aiProcess_Debone);
    aiMesh* mesh = s->mMeshes[0];
    for (std::uint32_t it = 0; it < mesh->mNumVertices;it++) {
        vertex v;
        if (mesh->HasPositions())v.pos = vec3(mesh->mVertices[it]);
        if (mesh->HasNormals())v.normal = vec3(mesh->mNormals[it]);
        if (mesh->HasVertexColors(0))v.color = vec4(mesh->mColors[0][it]);
        if (mesh->HasTextureCoords(0))v.uv = vec2(mesh->mTextureCoords[0][it]);
        vertices.push_back(v);
    }
    for (std::uint32_t it = 0; it < mesh->mNumFaces; it++) {
        indices.push_back(mesh->mFaces[it].mIndices[0]);
        indices.push_back(mesh->mFaces[it].mIndices[1]);
        indices.push_back(mesh->mFaces[it].mIndices[2]);
    }
    
    std::uint32_t VAO, VBO, IBO, TID;

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertex), vertices.data(), GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 12 * sizeof(float), (void*)(3*sizeof(float)));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 12 * sizeof(float), (void*)(6*sizeof(float)));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 12 * sizeof(float), (void*)(10*sizeof(float)));
    glEnableVertexAttribArray(3);

    glGenBuffers(1, &IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(std::uint32_t), indices.data(), GL_STATIC_DRAW);

    /////////////////////////////// TEXTURE //////////////////////////////////////////////////////

    sf::Image img;
    img.loadFromFile("c:/textures/buntekiste.png");
    glGenTextures(1, &TID);
    glBindTexture(GL_TEXTURE_2D, TID);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    if (img.getPixelsPtr()) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.getSize().x, img.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.getPixelsPtr());
        glGenerateMipmap(GL_TEXTURE_2D);
        
    }
    else {
        std::cout << "Failed to load texture" << std::endl;
        return -3;
    }

 
    ////////////////////////////// MAINLOOP ///////////////////////////////////////////////////////
    bool running = true;
    while (running)
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }
        }

        ///////////////////// DRAWSTUFF /////////////////////////////////////////
        
        camera.update(),
        glClearColor(0.3f, 0.0f, 0.8f,0.7f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        
        glm::mat4 mvp = modelMatrix;
        Shader1.set4x4("Model", modelMatrix);
        Shader1.set4x4("View", camera.getView());
        Shader1.set4x4("Projection", camera.getProjection());
       
        glBindVertexArray(VAO);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, TID);
        Shader1.use();
        glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
        
     
        
        window.display();
    }

   

    return 0;
}

FragmentShader:

#version 440
out vec4 FragColor;
in vec4 vertexColor;
in vec3 FragPos;
in vec3 normal;
in vec2 uv;
uniform int textureSwitch;
uniform vec3 lightPos;
uniform sampler2D myTexture;
void main(){

FragColor = texture(myTexture,uv);
}

VertexShader:

#version 440 
layout(location =0) in vec3 Pos;
layout(location =1) in vec3 Normal;
layout(location =2) in vec4 Color;
layout(location =3) in vec2 TexCoord;

uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;

out vec4 vertexColor;
out vec3 normal;
out vec3 FragPos;
out vec2 uv;


void main(){
    mat4 mvp = Projection *View*Model;
    gl_Position = mvp * vec4(Pos,1.0f);
    normal = Normal;
    uv = TexCoord;

    vertexColor = Color;
    }
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
DuckPuppy
  • 123
  • 1
  • 1
  • 7
  • Are you sure that the faces in the collada file are triangles? If they aren't you might miss some indices. The general idea of you code looks fine (at least without checking the shader). Assuming that each side of the cube should have a static color, it is unlikely that orientation or depth testing are the problem. – BDL May 28 '21 at 13:37
  • Yes i am. i did both, triangulating in Blender and triangulating during preprocessing with assimp. and if i draw only the first three vertices it draws only one triangle. – DuckPuppy May 28 '21 at 13:42
  • What does "it does not draw properly" even mean here? From the image that is not clear at all. "t is unlikely that orientation or depth testing are the problem" There is no depth testing in your code. – derhass May 28 '21 at 15:16
  • 1
    Possible duplicate of [Why does OpenGl not draw my Box properly?](https://stackoverflow.com/questions/67705459/why-does-opengl-not-draw-my-box-properly) – genpfault May 28 '21 at 15:30
  • 1
    @DuckPuppy Why do you repeat the question? – Rabbid76 May 28 '21 at 15:31
  • i thought i deleated the previous question, did not know that deleted questions do not disappear completely – DuckPuppy May 28 '21 at 16:15
  • @Rabbid76 So which one do i keep working on ? – DuckPuppy May 28 '21 at 16:19

2 Answers2

0

I had the same problem and this works for me: add glEnable(GL_DEPTH_TEST);//enable depth buffer glCullFace(GL_FRONT);//for keep front faces and glDisable(GL_CULL_FACE); - if you have enabled GL_CULL_FACE

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '22 at 06:28
-1

I did call glEnable(GL_DEPTH_TEST) before creating the OpenGL Context. Calling it afterwards solves it.

Ahh finally: enter image description here

DuckPuppy
  • 123
  • 1
  • 1
  • 7
  • I would think it's much more precise and performent to do back-face culling and (since this is a convex polyhedron) skip the depth test entirely. – Neil May 30 '22 at 20:52