I want to create a class in c++ that loads a model with the obj format.The model gets generated from Blender(3D modeling program).When i try do draw something on the screen without the class(all the code is in main) it works fine.But somehow my class doesnt work.It only shows a black screen.Heres my code:
main.cpp
#include "global.h"
using namespace std;
int main()
{
glewExperimental = GL_TRUE;
glewInit();
glViewport(0,0,800,800);
mesh.loadMesh("models/1/cube.obj");
shaders.push_back(Shader("shaders/default/default.vert", "shaders/default/default.frag"));
while (window.isOpen())
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
while (window.pollEvent(Event))
{
if (Event.type == sf::Event::Closed)
window.close();
}
mesh.draw(shaders[0]);
window.display();
}
return 0;
}
vertex shader
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;
void main()
{
gl_Position = vec4(position.x, position.y, position.z, 1.0f);
}
fragment shader
#version 330 core
out vec4 color;
void main()
{
color = vec4(1.0,0.0,0.0,1.0);
}
As you can see i dont use the normals and the textures yet..all i want for now is to draw something red on the screen.
The shader class its a class that i dowloaded from learnopengl.com .It just uses OpenGL standard fuctions to create a shader.Since my program works when i put everything in main,the shader class is not the problem.
and now the mesh class mesh.h
#ifndef MESH_H
#define MESH_H
#include "Includes.h"
#include "Shader.h"
struct Vertex
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoords;
Vertex(glm::vec3 p,glm::vec3 n,glm::vec2 t)
{
position=p;
normal=n;
texCoords=t;
}
};
struct Texture
{
GLuint id;
string type;
string path;
};
class Mesh
{
public:
void draw(Shader &shader);
void loadMesh(const char* fileName);
void loadMaterials(const char* fileName);
private:
GLuint VAO, VBO, EBO;
vector<Vertex> vertex;
vector<GLuint> indices;
vector<Texture> texture;
void setupMesh();
};
#endif // MESH_H
and mesh.cpp
void Mesh::loadMesh(const char* fileName)
{
int i,nrObj=-1,nr=0,number,number1,number2,number3;
char text[256],matName[256];
ifstream fin(fileName);
vector<string>line;
vector<glm::vec3>position;
vector<glm::vec3>normal;
vector<glm::vec2>texCoord;
while(!fin.eof())
{
fin.getline(text,256);
line.push_back(text);
}
//loadMaterials(line[2].c_str());
glm::vec3 v;
glm::vec2 t;
for(i=4;line[i][0]=='v'&& line[i][1]==' ';i++)
{
sscanf(line[i].c_str(),"v %f %f %f",&v.x,&v.y,&v.z);
position.push_back(v);
}
for(;line[i][0]=='v'&& line[i][1]=='t';i++)
{
sscanf(line[i].c_str(),"vt %f %f",&t.x,&t.y);
texCoord.push_back(t);
}
for(;line[i][0]=='v'&& line[i][1]=='n';i++)
{
sscanf(line[i].c_str(),"vn %f %f %f",&v.x,&v.y,&v.z);
normal.push_back(v);
}
for(;i<line.size();i++)
{
if(line[i][0]=='u'&&line[i][1]=='s'&&line[i][2]=='e')
sscanf(line[i].c_str(),"usemtl %s",&matName);
i++;
if(line[i][0]=='s'&&line[i][1]==' ')
i++;
for(i;line[i][0]=='f'&&line[i][1]==' ';i++)
{
indices.resize(indices.size()+3);
sscanf(line[i].c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",&number1,&number,&number,&number2,&number,&number,&number3,&number,&number);
indices[nr*3]=number1-1;
indices[nr*3+1]=number2-1;
indices[nr*3+2]=number3-1;
nr++;
}
}
for(i=0;i<position.size();i++)
vertex.push_back(Vertex(position[i],glm::vec3(0,0,0),glm::vec2(0,0)));
setupMesh();
fin.close();
}
void Mesh::loadMaterials(const char* fileName)
{
}
void Mesh::setupMesh()
{
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->vertex.size() * sizeof(Vertex), &this->vertex[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);
// Set the vertex attribute pointers
// Vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
// Vertex Normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal));
// Vertex Texture Coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoords));
glBindVertexArray(0);
}
void Mesh::draw(Shader &shader)
{
shader.Use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
I checked to see if my class reads corectly the obj file and it does. Pls tell me whats wrong.