PDA

View Full Version : Can not load .obj files



Otak64
01-26-2018, 07:41 AM
I'm trying to load .obj files with vbo indexing, but the only result is that ...


2637

The file should be a plane:sorrow:
Here is the code:

-obj.cpp
#include "obj.h"

/* Material */
Material::Material(const float r, const float g, const float b, const char *Name)
{
coul = glm::vec3(r, g, b);
this->Name = Name;
}

Material::Material(Material &mat)
{
this->coul = mat.coul;
this->Name = mat.Name;
}

Material::~Material()
{

}

/* Mesh Obj */
MeshObj::MeshObj(const char *FileName)
{
LoadObj(FileName);

glGenVertexArrays(1, &VertexArrayId);
glBindVertexArray(VertexArrayId);

glGenBuffers(1, &VertexBufferId);
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
glBufferData(GL_ARRAY_BUFFER, ver.size() * sizeof(GLfloat), ver.data(), GL_STATIC_DRAW);

glGenBuffers(1, &VertexBufferIndicesId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VertexBufferIndicesId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iv.size() * sizeof(GLuint), iv.data(), GL_STATIC_DRAW);
}

MeshObj::~MeshObj()
{
for(unsigned int i = 0; i < materials.size(); i++)
delete materials[i];
materials.clear();

glDeleteVertexArrays(1, &VertexArrayId);
glDeleteBuffers(1, &VertexBufferId);
glDeleteBuffers(1, &VertexBufferIndicesId);
}

void MeshObj::LoadObj(const char *FileName)
{
std::string line;
std::ifstream file(FileName, std::ios::in);

if(file)
{
do
{
getline(file, line);
std::cout << line << std::endl;

if(line[0] == 'v')
{
if(line[1] == ' ')//Vertex
{
GLfloat x, y, z;
sscanf(line.c_str(), "v %lf %lf %lf", &x, &y, &z);
ver.push_back(glm::vec3(x, y, z));
}
else if(line[1] == 't')//Texture
{
GLfloat x, y;
sscanf(line.c_str(), "vt %lf %lf", &x, &y);
tex.push_back(glm::vec3(x, y, 0));
}
else if(line[1] == 'n')//Normale
{
GLfloat x, y, z;
sscanf(line.c_str(), "vn %lf %lf %lf", &x, &y, &z);
nor.push_back(glm::vec3(x, y, z));
}
}
else if(line[0] == 'f') //Indice faces
{
line = DoubleSlash(line);
std::vector<std::string> termes = SplitSpace(line.substr(2));

for(unsigned int i = 0; i < termes.size(); i++)
{
std::cout << termes[i] << std::endl;
GLuint x, y, z;
sscanf(termes[i].c_str(), "%ld/%ld/%ld/", &x, &y, &z);
iv.push_back(x -1);
it.push_back(y);
in.push_back(z);
}
}
else if(line[0] == 'm')
LoadMtl((getDirectory(FileName) + line.substr(7)).c_str());
} while(!file.eof());

/*for(unsigned int i = 0; i < ver.size(); i++)
std::cout << ver[i] << std::endl;*/
/*for(unsigned int i = 0; i < ver.size(); i++)
std::cout << ver[i].x << " : " << ver[i].y << " : " << ver[i].z << std::endl;
for(unsigned int i = 0; i < iv.size(); i++)
std::cout << iv[i] << std::endl;*/
}
else
std::cerr << "Error couldn't open : " << FileName << std::endl;
}

void MeshObj::LoadMtl(const char *FileName)
{

}

void MeshObj::Draw(bool nor, bool tex)
{
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
glVertexAttribLPointer(0, 3, GL_FLOAT, GL_FALSE, nullptr);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VertexBufferIndicesId);
glDrawElements(GL_TRIANGLES, iv.size(), GL_UNSIGNED_INT, nullptr);

glDisableVertexAttribArray(0);
}

std::string MeshObj::DoubleSlash(const std::string& s)
{
std::string s1 = "";
for(unsigned int i = 0; i < s.size(); i++)
{
if(i < s.size() -1 && s[i] == '/' && s[i+1] == '/')
{
s1 += "/1/";
i++;
}
else
{
s1 += s[i];
}
}
return s1;
}

std::string MeshObj::SpaceSlash(const std::string& s)
{
std::string s1 = "";
for(unsigned int i = 0; i < s.size(); i++)
{
if(s[i] == '/')
s1 += ' ';
else
s1 += s[i];
}
return s1;
}

std::vector<std::string> MeshObj::SplitSpace(const std::string& s)
{
std::vector<std::string> ret;
std::string s1 = "";
for(unsigned int i = 0; i < s.size(); i++)
{
if(s[i] == ' ' || i == s.size() -1)
{
if(i == s.size() -1)
s1 += s[i];
ret.push_back(s1);
s1 = "";
}
else
s1 += s[i];
}
return ret;
}

std::string MeshObj::getDirectory(const std::string& s)
{
std::string s1 = "", s2 = "";
for(unsigned int i = 0; i < s.size(); i++)
{
if(s[i] == '/' || s[i] == '\\')
{
s1 += s2 + "/";
s2 = "";
}
else
s2 += s[i];
}
return s1;
}

-obj.h
#ifndef OBJ_H
#define OBJ_H

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <glm/glm.hpp>
#include "texture.h"

class Material
{
public:
Material(const float r, const float g, const float b, const char *Name);
Material(Material &mat);
~Material();

glm::vec3 coul;
const char *Name;
};

class MeshObj
{
public:
MeshObj(const char *FileName);
~MeshObj();

void LoadObj(const char *FileName);
void LoadMtl(const char *FileName);
void Draw(bool nor = true, bool tex = false);
void setMaterialAndTexture(std::vector<Material*> mats, Texture tex);
std::string DoubleSlash(const std::string& s);
std::string SpaceSlash(const std::string& s);
std::vector<std::string> SplitSpace(const std::string& s);
std::string getDirectory(const std::string& s);

private:
Texture texture;
std::vector<glm::vec3> ver, nor, tex, col;
std::vector<GLuint> iv, it, in;
std::vector<Material*> materials;

GLuint VertexArrayId;
GLuint VertexBufferId;
GLuint ColorBufferId;
GLuint UvBufferId;

GLuint VertexBufferIndicesId;
GLuint ColorBufferIndicesId;
GLuint UvBufferIndicesId;
};

#endif

and the main.cpp
#include <iostream>
#include <GL/glew.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>

#include <GLFW/glfw3.h>

#include "window.h"
#include "mesh.h"
#include "shader.h"
#include "transform.h"
#include "texture.h"
#include "camera.h"
#include "obj.h"

#define SCREEN_SIZE_X 1024
#define SCREEN_SIZE_Y 768


const char * GetGLErrorStr(GLenum err)
{
switch (err)
{
case GL_NO_ERROR: return "No error";
case GL_INVALID_ENUM: return "Invalid enum";
case GL_INVALID_VALUE: return "Invalid value";
case GL_INVALID_OPERATION: return "Invalid operation";
case GL_STACK_OVERFLOW: return "Stack overflow";
case GL_STACK_UNDERFLOW: return "Stack underflow";
case GL_OUT_OF_MEMORY: return "Out of memory";
default: return "Unknown error";
}
}

void CheckGLError()
{
const GLenum err = glGetError();
std::cout << "GL Error: " << GetGLErrorStr(err) << ", Code : " << err << std::endl;
}

/* Variable used in event */
Timer CubeTimer;

/* Event key gestion */
static void on_key(GLFWwindow *window, int key, int button, int action, int mods)
{
if(key == GLFW_KEY_F2 && action == GLFW_PRESS)
{
if(!IsConsoleVisible())
ShowConsole();
else if(IsConsoleVisible())
HideConsole();

glfwFocusWindow(window);
}

if(key == GLFW_KEY_SPACE && action == GLFW_PRESS)
{
if(CubeTimer.Stoped)
CubeTimer.Play();
else
CubeTimer.Stop();
}

if(key == GLFW_KEY_F3 && action == GLFW_PRESS)
{
CheckGLError();
}
}

int main()
{
float Vertex[] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f};
float Color[] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};

float CubeVertex[] = { -1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f,-1.0f, 1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,-1.0f, 1.0f,-1.0f, 1.0f, -1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 1.0f,-1.0f, 1.0f,-1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,-1.0f, -1.0f, 1.0f,-1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f,-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,-1.0f, 1.0f};

float CubeColor[] = {0.583f, 0.771f, 0.014f,0.609f, 0.115f, 0.436f,0.327f, 0.483f, 0.844f,0.822f, 0.569f, 0.201f,0.435f, 0.602f, 0.223f,0.310f, 0.747f, 0.185f,0.597f, 0.770f, 0.761f,0.559f, 0.436f, 0.730f,0.359f, 0.583f, 0.152f,0.483f, 0.596f, 0.789f,0.559f, 0.861f, 0.639f,0.195f,
0.548f, 0.859f,0.014f, 0.184f, 0.576f,0.771f, 0.328f, 0.970f,0.406f, 0.615f, 0.116f,0.676f, 0.977f, 0.133f,0.971f, 0.572f, 0.833f,0.140f, 0.616f, 0.489f, 0.997f, 0.513f, 0.064f,0.945f, 0.719f, 0.592f, 0.543f, 0.021f, 0.978f,0.279f, 0.317f, 0.505f,0.167f, 0.620f, 0.077f,0.347f, 0.857f,
0.137f,0.055f, 0.953f, 0.042f,0.714f, 0.505f, 0.345f,0.783f, 0.290f, 0.734f,0.722f, 0.645f, 0.174f,0.302f, 0.455f, 0.848f,0.225f, 0.587f, 0.040f,0.517f, 0.713f, 0.338f,0.053f, 0.959f, 0.120f, 0.393f, 0.621f, 0.362f,0.673f, 0.211f, 0.457f,0.820f, 0.883f, 0.371f, 0.982f, 0.099f, 0.879f};

float CubeUv[] = {0.000059f, 1.0f-0.000004f,0.000103f, 1.0f-0.336048f,0.335973f, 1.0f-0.335903f,1.000023f, 1.0f-0.000013f,0.667979f, 1.0f-0.335851f,0.999958f, 1.0f-0.336064f,0.667979f, 1.0f-0.335851f,0.336024f, 1.0f-0.671877f,0.667969f, 1.0f-0.671889f,1.000023f, 1.0f-0.000013f,0.668104f, 1.0f-0.000013f,0.667979f, 1.0f-0.335851f,0.000059f, 1.0f-0.000004f,
0.335973f, 1.0f-0.335903f,0.336098f, 1.0f-0.000071f,0.667979f, 1.0f-0.335851f,0.335973f, 1.0f-0.335903f,0.336024f, 1.0f-0.671877f,1.000004f, 1.0f-0.671847f,0.999958f, 1.0f-0.336064f,0.667979f, 1.0f-0.335851f,0.668104f, 1.0f-0.000013f,0.335973f, 1.0f-0.335903f,0.667979f, 1.0f-0.335851f,0.335973f, 1.0f-0.335903f,0.668104f, 1.0f-0.000013f,0.336098f, 1.0f-0.000071f,
0.000103f, 1.0f-0.336048f,0.000004f, 1.0f-0.671870f,0.336024f, 1.0f-0.671877f,0.000103f, 1.0f-0.336048f,0.336024f, 1.0f-0.671877f,0.335973f, 1.0f-0.335903f,0.667969f, 1.0f-0.671889f,1.000004f, 1.0f-0.671847f,0.667979f, 1.0f-0.335851f};

/** always create the window first because of the Init **/
MainWindow window(SCREEN_SIZE_X, SCREEN_SIZE_Y, "Super Title");
window.setWindowIcon("./res/icon/keyboard.png");

Mesh Cube(CubeVertex, sizeof(CubeVertex));
Cube.AddTexture(CubeUv, sizeof CubeUv);

Mesh Cube2(CubeVertex, sizeof(CubeVertex));
Cube2.AddColor(CubeColor, sizeof CubeColor);

Shader shaderTexture("./res/Shader/Base.vs", "./res/Shader/Texture.fs");
shaderTexture.AddUniformLocation("MVP");

Shader shaderColor("./res/Shader/Base.vs", "./res/Shader/Color.fs");
shaderColor.AddUniformLocation("MVP");

Shader shaderBase("./res/Shader/Base.vs", "./res/Shader/Base.fs");
shaderBase.AddUniformLocation("MVP");

Transform CubeTransform;

Texture CubeTexture("./res/Texture/box.jpg");

/* Key event gestion */
glfwSetKeyCallback(window.getWindow(), on_key);

glClearColor(0.4f, 0.4f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS);

Camera cam(glm::vec3(3.0, 3.0, 3.0));

bool test = false;

Timer ShaderReloadTime;
Timer MouseTime;
bool Mouse = true;

MeshObj object("./res/Obj/cube.obj");

/* Main loop (Update) */
while (!window.Closed())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(window.getKey(GLFW_KEY_ESCAPE))
break;

CubeTimer.Update();

/* function to reload the shader */
ShaderReloadTime.Update();
if(window.getKey(GLFW_KEY_F4) && ShaderReloadTime.getTime() > 1.0f)
{
shaderTexture.Reload("./res/Shader/Base.vs", "./res/Shader/Texture.fs");
shaderColor.Reload("./res/Shader/Base.vs", "./res/Shader/Color.fs");
shaderBase.Reload("./res/Shader/Base.vs", "./res/Shader/Base.fs");
ShaderReloadTime.Reset();
}

//cam.setPoint(glm::vec3(0.0, 0.0, 0.0));

if(window.getButton(GLFW_MOUSE_BUTTON_LEFT))
{
window.setInputMode(GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
if(!Mouse)
cam.Orientate(window.getMousePositionX() * 0.4, window.getMousePositionY() * 0.4);

window.setMousePosition(0.0, 0.0);
Mouse = false;
}
else
{
if(!Mouse)
window.setMousePosition(SCREEN_SIZE_X / 2, SCREEN_SIZE_Y / 2);
window.setInputMode(GLFW_CURSOR, GLFW_CURSOR_NORMAL);
Mouse = true;
}

if(window.getKey(GLFW_KEY_S))
{
cam.Move(CAMERA_DIRECTION_BACK);
//cam.Move(CAMERA_DIRECTION_DOWN);
}
if(window.getKey(GLFW_KEY_W))
{
cam.Move(CAMERA_DIRECTION_FRONT);
//cam.Move(CAMERA_DIRECTION_UP);
}

if(window.getKey(GLFW_KEY_A))
{
cam.Move(CAMERA_DIRECTION_LEFT);
}
if(window.getKey(GLFW_KEY_D))
{
cam.Move(CAMERA_DIRECTION_RIGHT);
}

/* Drawing */
shaderTexture.Use();
CubeTexture.Bind();
CubeTransform.setPosition(glm::vec3(0.0f, 0.0f, 0.0f));
CubeTransform.Rotate(glm::vec3((float)CubeTimer.ge tTime(), (float)CubeTimer.getTime() * 1.5, 0.0f));
//MVP = Projection * View * CubeTransform.getModel();
shaderTexture.UniformMatrix4fv(0, cam.getVP() * CubeTransform.getModel());
Cube.Draw();

shaderColor.Use();
CubeTransform.setPosition(glm::vec3(-2.0f, -1.0f, -3.0f));
//MVP = Projection * View * CubeTransform.getModel();
shaderColor.UniformMatrix4fv(0, cam.getVP() * CubeTransform.getModel());
Cube2.Draw();

shaderBase.Use();
CubeTransform.setPosition(glm::vec3(-4.0f, -4.0f, -4.0f));
CubeTransform.Rotate(glm::vec3(0.0f, 0.0f, 0.0f));
shaderBase.UniformMatrix4fv(0, cam.getVP() * CubeTransform.getModel());
object.Draw();

glfwSwapBuffers(window.getWindow());
glfwPollEvents();
//std::cout << glfwGetTime() << std::endl;
}

return 0;
}

Help !

Thanks for the translation :p