【问题标题】:OpenGL does not draw Model properlyOpenGL 无法正确绘制模型
【发布时间】:2021-08-16 17:13:42
【问题描述】:

我将 Blender Box 导出为 Collada 文件,我正在使用 Assimp 加载它,但它无法正确绘制。 这是它的外观:

我尝试使用 .fbx 和 .obj 没有成功。 这是我的代码中的错误,还是顶点数据不正确? 我检查了法线,它们都指向正确的方向, 是什么让我相信面孔的方向是正确的。

这是我的代码:

#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;
}

片段着色器:

#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);
}

顶点着色器:

#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;
    }

【问题讨论】:

  • 您确定 collada 文件中的面是三角形吗?如果不是,您可能会错过一些索引。您的代码的总体思路看起来不错(至少在不检查着色器的情况下)。假设立方体的每一面都应该有一个静态颜色,那么方向或深度测试不太可能是问题。
  • 是的,我是。我两者都做了,在 Blender 中进行三角剖分,并在使用 assimp 进行预处理期间进行三角剖分。如果我只画前三个顶点,它只画一个三角形。
  • 这里的“绘制不正确”是什么意思?从根本不清晰的图像。 “方向或深度测试不太可能是问题”您的代码中没有深度测试。
  • @DuckPuppy 你为什么要重复这个问题?

标签: c++ opengl collada assimp


【解决方案1】:

我确实打电话给glEnable(GL_DEPTH_TEST) 创建 OpenGL 上下文之前。 之后调用它可以解决它。

啊终于:

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 2011-04-11
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2012-10-05
    相关资源
    最近更新 更多