Skip to content

Commit

Permalink
Exclude abstract Mesh class because of issues(TM)
Browse files Browse the repository at this point in the history
  • Loading branch information
vainamov committed Feb 14, 2020
1 parent f903d8e commit 488b148
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 45 deletions.
11 changes: 10 additions & 1 deletion QubGL/src/Rendering/CubeMesh.cpp
Expand Up @@ -4,7 +4,8 @@
#include "VertexArrayObject.hpp"
#include "VertexBufferObject.hpp"

CubeMesh::CubeMesh(std::vector<Vertex>* vertices, std::vector<unsigned int>* indices) {
CubeMesh::CubeMesh(std::vector<Vertex>* vertices, std::vector<unsigned int>* indices)
:m_ebo(nullptr), m_vao(nullptr), m_vbo(nullptr) {
if (!m_isLoaded) {
Load(vertices, indices);
}
Expand All @@ -24,6 +25,10 @@ void CubeMesh::Draw() const {
Unbind();
}

std::string CubeMesh::GetName() const {
return m_name;
}

void CubeMesh::Load(std::vector<Vertex>* vertices, std::vector<unsigned int>* indices) {
m_ebo = new ElementBufferObject();
m_vao = new VertexArrayObject();
Expand All @@ -46,6 +51,10 @@ void CubeMesh::Load(std::vector<Vertex>* vertices, std::vector<unsigned int>* in
m_isLoaded = true;
}

void CubeMesh::SetName(std::string name) {
m_name = name;
}

void CubeMesh::Unbind() const {
m_ebo->Unbind();
m_vao->Unbind();
Expand Down
19 changes: 11 additions & 8 deletions QubGL/src/Rendering/CubeMesh.hpp
@@ -1,25 +1,28 @@
#pragma once

#include <string>
#include <vector>

#include "Mesh.hpp"
struct Vertex;

class Vertex;

class CubeMesh : public Mesh {
class CubeMesh {
public:
CubeMesh(): m_ebo(nullptr), m_vbo(nullptr) { }
CubeMesh(): m_ebo(nullptr), m_vao(nullptr), m_vbo(nullptr) { }
CubeMesh(std::vector<Vertex>* vertices, std::vector<unsigned int>* indices);

virtual void Bind() const override;
virtual void Draw() const override;
virtual void Unbind() const override;
void Bind() const;
void Draw() const;
std::string GetName() const;
void SetName(std::string name);
void Unbind() const;

private:
void Load(std::vector<Vertex>* vertices, std::vector<unsigned int>* indices);
void Unload();

class ElementBufferObject* m_ebo;
bool m_isLoaded = false;
std::string m_name = "unnamed";
class VertexArrayObject* m_vao;
class VertexBufferObject* m_vbo;
};
46 changes: 22 additions & 24 deletions QubGL/src/Rendering/Loader.cpp
Expand Up @@ -22,15 +22,15 @@ vector<unsigned int> Loader::GetIndices() {
return m_indices;
}

vector<Mesh> Loader::GetMeshes() {
vector<CubeMesh> Loader::GetMeshes() {
return m_meshes;
}

vector<Vertex> Loader::GetVertices() {
return m_vertices;
}

void Loader::GenerateVerticesFromRawObj(vector<Vertex>& vertices, const vector<glm::vec3>& positions, const vector<glm::vec4>& coordinates, const vector<glm::vec3>& normals, string currentLine) {
void Loader::GenerateVerticesFromRawObj(vector<Vertex>& vertices, const vector<glm::vec3>& positions, const vector<glm::vec2>& coordinates, const vector<glm::vec3>& normals, string currentLine) {
vector<string> face, sVertices;
Vertex vertex;

Expand All @@ -39,47 +39,47 @@ void Loader::GenerateVerticesFromRawObj(vector<Vertex>& vertices, const vector<g
auto noNormal = false;

for (auto i = 0; i < int(face.size()); i++) {
objhelpers::VertexType vertexType;
objhelpers::VertexType vertexType = objhelpers::VertexType::None;

objhelpers::split(face[i], sVertices, "/");

if (sVertices.size() == 1) {
vertexType = objhelpers::Position;
vertexType = objhelpers::VertexType::Position;
} else if (sVertices.size() == 2) {
vertexType = objhelpers::PositionAndTexture;
vertexType = objhelpers::VertexType::PositionAndTexture;
} else if (sVertices.size() == 3) {
if (sVertices[1] == "") {
vertexType = objhelpers::PositionAndNormal;
vertexType = objhelpers::VertexType::PositionAndNormal;
} else {
vertexType = objhelpers::PositionAndTextureAndNormal;
vertexType = objhelpers::VertexType::PositionAndTextureAndNormal;
}
}

switch (vertexType) {
case objhelpers::Position: {
case objhelpers::VertexType::Position: {
vertex.Position = objhelpers::getElement(positions, sVertices[0]);
vertex.Color = glm::vec4(0.F);
vertex.TextureCoordinates = glm::vec2(0.F);
noNormal = true;
vertices.push_back(vertex);
break;
}
case objhelpers::PositionAndTexture: {
case objhelpers::VertexType::PositionAndTexture: {
vertex.Position = objhelpers::getElement(positions, sVertices[0]);
vertex.Color = objhelpers::getElement(coordinates, sVertices[1]);
vertex.TextureCoordinates = objhelpers::getElement(coordinates, sVertices[1]);
noNormal = true;
vertices.push_back(vertex);
break;
}
case objhelpers::PositionAndNormal: {
case objhelpers::VertexType::PositionAndNormal: {
vertex.Position = objhelpers::getElement(positions, sVertices[0]);
vertex.Color = glm::vec4(0.F);
vertex.TextureCoordinates = glm::vec2(0.F);
vertex.Normal = objhelpers::getElement(normals, sVertices[2]);
vertices.push_back(vertex);
break;
}
case objhelpers::PositionAndTextureAndNormal: {
case objhelpers::VertexType::PositionAndTextureAndNormal: {
vertex.Position = objhelpers::getElement(positions, sVertices[0]);
vertex.Color = objhelpers::getElement(coordinates, sVertices[1]);
vertex.TextureCoordinates = objhelpers::getElement(coordinates, sVertices[1]);
vertex.Normal = objhelpers::getElement(normals, sVertices[2]);
vertices.push_back(vertex);
break;
Expand Down Expand Up @@ -112,7 +112,7 @@ bool Loader::ParseFile(const string& objFilePath) {
m_vertices.clear();

vector<glm::vec3> positions;
vector<glm::vec4> coordinates;
vector<glm::vec2> coordinates;
vector<glm::vec3> normals;

vector<Vertex> vertices;
Expand Down Expand Up @@ -165,14 +165,12 @@ bool Loader::ParseFile(const string& objFilePath) {

if (objhelpers::firstToken(currentLine) == "vt") {
vector<string> sTexture;
glm::vec4 vTexture;
glm::vec2 vTexture;

objhelpers::split(objhelpers::tail(currentLine), sTexture, " ");

vTexture.r = stof(sTexture[0]);
vTexture.g = stof(sTexture[1]);
vTexture.b = stof(sTexture[2]);
vTexture.a = stof(sTexture[3]);
vTexture.x = stof(sTexture[0]);
vTexture.y = stof(sTexture[1]);

coordinates.push_back(vTexture);
}
Expand Down Expand Up @@ -251,7 +249,7 @@ void Loader::TriangulateVertices(vector<unsigned int>& indices, const vector<Ver
if (i == 0) {
previous = tempVertices[tempVertices.size() - 1];
} else {
previous = tempVertices[i - 1];
previous = tempVertices[(size_t)i - 1];
}

Vertex current = tempVertices[i];
Expand All @@ -260,7 +258,7 @@ void Loader::TriangulateVertices(vector<unsigned int>& indices, const vector<Ver
if (i == tempVertices.size() - 1) {
next = tempVertices[0];
} else {
next = tempVertices[i + 1];
next = tempVertices[(size_t)i + 1];
}

if (tempVertices.size() == 3) {
Expand Down Expand Up @@ -299,7 +297,7 @@ void Loader::TriangulateVertices(vector<unsigned int>& indices, const vector<Ver
break;
}

float angle = glm::angle(previous.Position - current.Position, next.Position - current.Position) * (180.F / M_PI);
float angle = glm::angle(previous.Position - current.Position, next.Position - current.Position) * (180.F / (float)M_PI);
if (angle <= 0 && angle >= 180) continue;

auto inTriangle = false;
Expand Down
13 changes: 5 additions & 8 deletions QubGL/src/Rendering/Loader.hpp
@@ -1,29 +1,26 @@
#pragma once

#include "GLM/vec3.hpp"
#include "GLM/vec4.hpp"

#include <string>
#include <vector>

class Mesh;
class Vertex;
#include "CubeMesh.hpp"
#include "Vertex.hpp"

class Loader {
public:
Loader() {}
~Loader();

std::vector<unsigned int> GetIndices();
std::vector<Mesh> GetMeshes();
std::vector<CubeMesh> GetMeshes();
std::vector<Vertex> GetVertices();
bool ParseFile(const std::string& objFilePath);

private:
void GenerateVerticesFromRawObj(std::vector<Vertex>& vertices, const std::vector<glm::vec3>& positions, const std::vector<glm::vec4>& coordinates, const std::vector<glm::vec3>& normals, std::string currentLine);
void GenerateVerticesFromRawObj(std::vector<Vertex>& vertices, const std::vector<glm::vec3>& positions, const std::vector<glm::vec2>& coordinates, const std::vector<glm::vec3>& normals, std::string currentLine);
void TriangulateVertices(std::vector<unsigned int>& indices, const std::vector<Vertex>& vertices);

std::vector<unsigned int> m_indices;
std::vector<Mesh> m_meshes;
std::vector<CubeMesh> m_meshes;
std::vector<Vertex> m_vertices;
};
4 changes: 2 additions & 2 deletions QubGL/src/Rendering/Mesh.hpp
Expand Up @@ -17,6 +17,6 @@ class Mesh {
}

protected:
std::string m_name;
class VertexArrayObject* m_vao;
std::string m_name = "unnamed";
class VertexArrayObject* m_vao = nullptr;
};
4 changes: 2 additions & 2 deletions QubGL/src/Rendering/Model.hpp
Expand Up @@ -4,13 +4,13 @@

class Model {
public:
Model(const class Mesh& mesh);
Model(const class CubeMesh& mesh);
~Model();

void Draw(const class ShaderProgram& shader);
Transform& GetTransform();

private:
const class Mesh& m_mesh;
const class CubeMesh& m_mesh;
Transform m_transform;
};

0 comments on commit 488b148

Please sign in to comment.