OBJ Loader不会“创建”或“渲染”对象

问题描述 投票:-1回答:1

我想实现一个obj loader,该loader将从混合器和渲染器导出的obj文件中获取'v'(顶点),'vt'(纹理),'vn'(法线)和'f'(面)坐标。它在我的程序上。但是,在实施它之后,我发现它存在问题。创建时没有任何显示。现在,自从我开始工作以来,网格代码一直是我的难题,因此我希望有人可以找到我无法找到的东西。不仅如此,纹理代码还声明了(在终端中)Trying to access the pixels of an empty image。但是,我给它的图像有像素,我完全不知道为什么会发生这种情况。

// Chest.cpp 
objLoader.loadObjModel("res/models/test.obj");
mesh.loadToVAO(objLoader.getVertices(), objLoader.getTextures(), objLoader.getNormals(), objLoader.getIndices());
mesh.loadTexture(Texture::get("res/images/wallpaper.png"));
program = Program::get(BASIC_VERTEX_SHADER, BASIC_FRAGMENT_SHADER);

这将从test.obj加载obj模型,然后在mesh.loadToVAO中获取顶点,纹理,法线和索引。然后,网格物体也将获得纹理位置。

// Mesh.cpp // Loading 
void Mesh::loadToVAO(std::vector<glm::vec3> vertices, std::vector<glm::vec2> textures, std::vector<glm::vec3> normals, std::vector<int> indices) {
    // create a VAO
    GLuint vaoID = createVAO();
    indicesSize = indices.size();
    bindIndicesBuffer(indices.data(), indicesSize);

    // Store the data in attribute lists
    storeDataInAttrubeList(0, 3, &vertices[0], vertices.size() * sizeof(glm::vec3));
    storeDataInAttrubeList(1, 2, &textures[0], textures.size() * sizeof(glm::vec2));
    storeDataInAttrubeList(2, 3, &normals[0], normals.size() * sizeof(glm::vec3));
    unbindVAO();
}

// Rendering
void Mesh::renderVAO() {
    // Texture
    texture->useTexture();

    // Binding
    glBindVertexArray(VAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO);

    // Rendering
    glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_INT, 0);

    // Unbinding
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

// createVAO/storeDataInAttrubeList/bindIndicesBuffer
GLuint Mesh::createVAO() {
    GLuint vaoID;

    glGenVertexArrays(1, &vaoID);
    VAO = vaoID;
    glBindVertexArray(vaoID);

    return vaoID;
}

void Mesh::storeDataInAttrubeList(GLuint attribNumber, int attribSize, void* data, int dataSize) {
    GLuint vboID;
    // Create a new buffer
    glGenBuffers(1, &vboID);
    // Store the buffer in the list
    VBO = vboID;
    // Bind the buffer to use it
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    // Store the data in the buffer
    glBufferData(GL_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
    // Tell OpenGL how and where to store this VBO in the VAO
    glVertexAttribPointer(attribNumber, attribSize, GL_FLOAT, GL_FALSE, 0, nullptr);
}

void Mesh::bindIndicesBuffer(int* indices, int& count) {
    GLuint vboID;
    // Generate a buffer and bind it for use
    glGenBuffers(1, &vboID);
    // Store the buffer in the list
    VBO = vboID;
    // Bind the buffer to use it
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
    // Store the indices in the buffer
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * count, indices, GL_STATIC_DRAW);
} 

这遍历每一行并找到适当的坐标。

while (fgets(line, 256, file) != NULL) {
        token = NULL;
        type = strtok_s(line, " ", &token);

        // Vertex
        if (type[0] == 'v' && type[1] == NULL) {
            x = strtod(token, &stop);
            token = stop + 1; 
            y = strtod(token, &stop);
            token = stop + 1; 
            z = strtod(token, &stop);

            vertices.push_back(glm::vec3(x, y, z));
        }

        // Textures
        else if (type[0] == 'v' && type[1] == 't') {
            x = strtod(token, &stop);
            token = stop + 1; 
            y = 1 - strtod(token, &stop);

            tempTextures.push_back(glm::vec2(x, y));
        }

        // Normals
        else if (type[0] == 'v' && type[1] == 'n') {
            x = strtod(token, &stop);
            token = stop + 1; 
            y = strtod(token, &stop);
            token = stop + 1; 
            z = strtod(token, &stop);

            tempNormals.push_back(glm::vec3(x, y, z));
        }

        // Faces
        else if (type[0] == 'f') {
            if (indices.size() == 0) {
                // Set size of the array
                textures.resize(vertices.size());
                normals.resize(vertices.size());
            }

            // Process vertices data
            processVertices(token, indices, tempTextures, textures, tempNormals, normals);
        }
    }
// This method process the vertice and indice information 
void OBJLoader::processVertices(char* vertexData, std::vector<int>& indices, std::vector<glm::vec2>& tempTextures, 
    std::vector<glm::vec2>& textures, std::vector<glm::vec3>& tempNormals, std::vector<glm::vec3>& normals) {

    char* stop;
    int vertexPointer;

    for (unsigned int i = 0; i < 3; i++) {
        // Get and store index
        vertexPointer = strtol(vertexData, &stop, 10) - 1;
        indices.push_back(vertexPointer);
        vertexData = stop + 1; 

        textures[vertexPointer] = tempTextures[strtol(vertexData, &stop, 10) - 1];
        vertexData = stop + 1; 

        normals[vertexPointer] = tempNormals[strtol(vertexData, &stop, 10) - 1];
        vertexData = stop + 1; 
    }
}

Chest.cpp中,您可以看到我正在调用名为get(something)的方法,这就是它的样子

std::vector<glm::vec3> getVertices() { return vertices; } // Note that vertices and etc. are vectors

所以最后,这不是渲染还是只是没有创建?我知道代码很长,但是我不完全知道哪个部分对我的问题最重要或最负责。

c++ opengl glm-math wavefront
1个回答
1
投票

我不知道这是否可以解决您代码中的所有问题,但是我注意到了一些事情:

  1. Mesh::storeDataInAttrubeList()确实为每个属性数组创建了一个新的VBO(这在GL中是可能的,但是您不需要这样做。将整个对象数据打包到一个VBO中)。但是更糟糕的是它会覆盖(Mesh类属性?)变量VBO。因此,您基本上会丢失之前创建的VBO的ID。更糟糕的是,渲染时稍后使用VBO作为元素数组缓冲区,这将完全破坏渲染。
  2. Mesh::renderVAO()更改元素缓冲区绑定并将其每次重置为0。这不是必需的。 GL_ELEMENT_ARRAY_BUFFER绑定存储在VAO中,无需为每个绘图调用对其进行设置和重置。
  3. 您的OBJ解析器无法正确复制顶点。您需要为每个唯一的vertexID/texcoordID/normalID三元组创建一个顶点。但是,您要做的只是用新的texcoords和法线覆盖vertexID处的顶点数据,而不是创建一个全新的顶点。
© www.soinside.com 2019 - 2024. All rights reserved.