Opengl Render .obj file

  .obj, c++, opengl, render

There is a problem with drawing .obj file to the screen. I am using fast_obj.h libary. I give the .obj file path and initialize mesh:

fastObjMesh* mesh = fast_obj_read(PATH);
if (!mesh) printf("Error loading %s: file not foundn", PATH); // If path isnt correct, blow a message.

And Here is my struct.

struct Vertex {
glm::vec3 position; 
glm::vec3 color;
glm::vec2 texcoord;
glm::vec3 normal; };

I have std::vector< Vertex > ObjVertices. I read data from mesh and convert it to my own struct.

for (std::size_t i = 0; i < ObjVertices.size(); ++i)
{
    //We got position datas from mesh to our Vertex Struct.
    ObjVertices[i].position.x = mesh->positions[3 * i];
    ObjVertices[i].position.y = mesh->positions[(3 * i)+1];
    ObjVertices[i].position.z = mesh->positions[(3 * i) + 2];

    //We got texcoord datas from mesh to our Vertex Struct.
    ObjVertices[i].texcoord.x = mesh->texcoords[2 * i];
    ObjVertices[i].texcoord.y = mesh->texcoords[(2 * i)+1];
   
    // //We got normal datas from mesh to our Vertex Struct.
    ObjVertices[i].normal.x = mesh->normals[3 * i];
    ObjVertices[i].normal.x = mesh->normals[(3 * i)+1];
    ObjVertices[i].normal.x = mesh->normals[(3 * i)+ 2];

    // Set white color for all vertices.
    ObjVertices[i].color = glm::vec3(1.f, 1.f, 1.f);
}

The problem is starting now. Here is my VAO,VBO, and EBO:

   GLuint VAO; // Vertex  array Object. ID for the whole model.
glCreateVertexArrays(1, &VAO);
glBindVertexArray(VAO);

GLuint VBO; // Vertex Buffer Object.
glGenBuffers(1, &VBO); 
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, ObjVertices.size()*sizeof(Vertex) , &ObjVertices[0], GL_STATIC_DRAW);


//Works fine without down 4 lines (for quad).
GLuint EBO; // Element Buffer Object. 
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, array_size(mesh->indices)*sizeof(fastObjIndex),&mesh->indices[0],GL_STATIC_DRAW);


// Set Vertex attrib points and enable.
// Find the attribute location for position. 

glVertexAttribPointer(0, 3, GL_FLOAT,GL_FALSE,sizeof(Vertex),(GLvoid*)offsetof(Vertex, position));
glEnableVertexAttribArray(0);

// Find the attribute location for color.
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
glEnableVertexAttribArray(1);

// Find the attribute location for texcoord.
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2);

// Find the attribute location for normal.
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal));
glEnableVertexAttribArray(3);

glBindVertexArray(0);

In the infinite while loop; i call this: glDrawArrays(GL_LINES, 0, ObjVertices.size()*sizeof(Vertex));

GL_TRIANGLES does not work either. glDrawElement function causes empty screen. Here is my result:
https://im.ge/i/CoZLS

This .obj file is ordinary capsule. Thx in advance.

Source: Windows Questions C++

LEAVE A COMMENT