0
\$\begingroup\$

I was working on implementing a renderer for SYOYO's tiny obj loader and when it came to testing time, it failed to render. Here is an image of the result:

enter image description here

I believe that i have made the error with my renderer, in which i have no idea of what is wrong.

Here is the code for the renderer:

void flushTerrain() {
            while (!pr_Models.empty()) {
                const Model3D* model = pr_Models.front();

                model->getVAO()->bind();

                glm::mat4 a;
                glm::translate(a, glm::vec3(model->getPosition()));

                model->getShader().setUniformMat4("ml_matrix", a);
                glDrawArrays(GL_TRIANGLES, 0, model->getShapeVerticies().size());

                model->getVAO()->unbind();

                pr_Models.pop_front();
            }
        }

and the code for the Model3D Constructor:

Model3D(std::vector<tinyobj::shape_t> shapes, std::vector<tinyobj::material_t> materials ,glm::vec3 position, Shader& shader) :
            pr_shapes(shapes), pr_materials(materials), pr_position(position), pr_shader(shader)
        {
            pr_VAO = new VertexArray();
            for (tinyobj::shape_t shape : pr_shapes) {
                pr_VAO->addBuffer(new Buffer(shape.mesh.positions, shape.mesh.positions.size(), 3), 1);

                pr_VAO->addBuffer(new Buffer(shape.mesh.texcoords, shape.mesh.texcoords.size(), 2), 2);
                pr_VAO->addBuffer(new Buffer(shape.mesh.normals, shape.mesh.normals.size(), 3), 3);
            }
        }

and finally, for the Buffer and VertexArray class:

Buffer::Buffer(tinyobj::shape_t data, GLsizei count, GLuint componentCount) {
        glGenBuffers(1, &pr_BufferID);
        glBindBuffer(GL_ARRAY_BUFFER, pr_BufferID);
        glBufferData(GL_ARRAY_BUFFER, count * sizeof(tinyobj::shape_t), &data, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

void VertexArray::addBuffer(Buffer* buffer, GLuint index) {
        bind();
        buffer->bind();

        glEnableVertexAttribArray(index);
        glVertexAttribPointer(index, buffer->getCount(), GL_FLOAT, GL_FALSE, 0, 0);

        buffer->unbind();
        unbind();
    }
\$\endgroup\$
  • \$\begingroup\$ What is the expected result? \$\endgroup\$ – Vaillancourt Jul 23 '16 at 6:16
-1
\$\begingroup\$

Make sure: Number of vertexes=3*Number of Triangles when using gldrawelement(GL_TRANGLES,...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.