Kinda layed out models
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <logger.h>
|
||||
|
||||
#include "display.h"
|
||||
#include "model.h"
|
||||
|
||||
int main (int argc, char** argv) {
|
||||
|
||||
|
||||
@@ -1,3 +1,43 @@
|
||||
#include "material.h"
|
||||
|
||||
Material::Material() {
|
||||
|
||||
}
|
||||
|
||||
Material::Material(std::string objPath) {
|
||||
Logger logger;
|
||||
objl::Loader loader;
|
||||
bool canLoad = loader.LoadFile(objPath);
|
||||
|
||||
if (!canLoad) {
|
||||
logger << LOGGER_ERROR << "Cannot load material '" << objPath << "'" << LOGGER_ENDL;
|
||||
return;
|
||||
}
|
||||
|
||||
loadFromObj(loader.LoadedMeshes[0]);
|
||||
}
|
||||
|
||||
Material::Material(objl::Mesh objMesh) {
|
||||
loadFromObj(objMesh);
|
||||
}
|
||||
|
||||
void Material::loadFromObj(objl::Mesh objMesh) {
|
||||
loadFromMat(objMesh.MeshMaterial);
|
||||
}
|
||||
|
||||
void Material::loadFromMat(objl::Material mat) {
|
||||
this->name = mat.name;
|
||||
OBJLVec3toGLM(mat.Ka, this->Ka);
|
||||
OBJLVec3toGLM(mat.Kd, this->Kd);
|
||||
OBJLVec3toGLM(mat.Ks, this->Ks);
|
||||
this->Ns = mat.Ns;
|
||||
this->Ni = mat.Ni;
|
||||
this->d = mat.d;
|
||||
this->illum = mat.illum;
|
||||
this->map_Ka = mat.map_Ka;
|
||||
this->map_Kd = mat.map_Kd;
|
||||
this->map_Ks = mat.map_Ks;
|
||||
this->map_Ns = mat.map_Ns;
|
||||
this->map_d = mat.map_d;
|
||||
this->map_bump = mat.map_bump;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,58 @@
|
||||
#ifndef SMHENGINE_SRC_MATERIAL_H_
|
||||
#define SMHENGINE_SRC_MATERIAL_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <OBJLoader.h>
|
||||
|
||||
#include <logger.h>
|
||||
#include "./util/util.h"
|
||||
|
||||
class Material {
|
||||
public:
|
||||
Material();
|
||||
Material(std::string objPath);
|
||||
Material(objl::Mesh objMesh);
|
||||
|
||||
void loadFromObj(objl::Mesh objMesh);
|
||||
void loadFromMat(objl::Material mat);
|
||||
|
||||
// Material Name
|
||||
std::string name;
|
||||
// Ambient Color
|
||||
glm::vec3 Ka;
|
||||
// Diffuse Color
|
||||
glm::vec3 Kd;
|
||||
// Specular Color
|
||||
glm::vec3 Ks;
|
||||
// Specular Exponent
|
||||
float Ns;
|
||||
// Optical Density
|
||||
float Ni;
|
||||
// Dissolve
|
||||
float d;
|
||||
// Illumination model 0->10
|
||||
// TODO: Make an enum for this
|
||||
int illum;
|
||||
// Ambient Texture Map
|
||||
std::string map_Ka;
|
||||
// Diffuse Texture Map
|
||||
std::string map_Kd;
|
||||
// Specular Texture Map
|
||||
std::string map_Ks;
|
||||
// Specular Hightlight Map
|
||||
std::string map_Ns;
|
||||
// Alpha Texture Map
|
||||
std::string map_d;
|
||||
// Bump Map
|
||||
std::string map_bump;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
43
src/mesh.cpp
43
src/mesh.cpp
@@ -27,18 +27,53 @@ void Mesh::loadFromObj(objl::Mesh objMesh) {
|
||||
UintToGLuint(objMesh.Indices, indices);
|
||||
}
|
||||
|
||||
void Mesh::settup() {
|
||||
void Mesh::setup() {
|
||||
glGenVertexArrays(1, &VAOid);
|
||||
glGenBuffers(1, &vertexBuffer);
|
||||
glGenBuffers(1, &indexBuffer);
|
||||
|
||||
glBindVertexArray(VAOid);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||||
|
||||
std::vector<glm::vec3> toGpu;
|
||||
toGpu.insert(toGpu.end(), vertices.begin(), vertices.end());
|
||||
toGpu.insert(toGpu.end(), normals.begin(), normals.end());
|
||||
toGpu.insert(toGpu.end(), texCoords.begin(), texCoords.end());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, toGpu.size() * sizeof(glm::vec3),
|
||||
&vertices[0], GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
|
||||
&indices[0], GL_STATIC_DRAW);
|
||||
|
||||
// Positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,
|
||||
(const void*)0);
|
||||
|
||||
// Normals
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,
|
||||
(const void*)(vertices.size() * sizeof(glm::vec3)));
|
||||
|
||||
// TexCoords
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0,
|
||||
(const void*)((vertices.size() + texCoords.size()) * sizeof(glm::vec3)));
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Mesh::bind() {
|
||||
|
||||
glBindVertexArray(VAOid);
|
||||
}
|
||||
|
||||
void Mesh::render(Shader& shader) {
|
||||
|
||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
void Mesh::unbind() {
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
@@ -24,17 +24,19 @@ public:
|
||||
Mesh(objl::Mesh objMesh);
|
||||
|
||||
void loadFromObj(objl::Mesh objMesh);
|
||||
void settup();
|
||||
void setup();
|
||||
|
||||
void bind();
|
||||
void render(Shader& shader);
|
||||
void unbind();
|
||||
static void unbind();
|
||||
|
||||
GLuint VAOid;
|
||||
|
||||
std::vector<glm::vec3> vertices;
|
||||
std::vector<glm::vec2> texCoords;
|
||||
std::vector<glm::vec3> normals;
|
||||
// This is a vec3 so it can all pop into
|
||||
//one buffer :)
|
||||
std::vector<glm::vec3> texCoords;
|
||||
std::vector<GLuint> indices;
|
||||
private:
|
||||
GLuint vertexBuffer;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "model.h"
|
||||
|
||||
|
||||
|
||||
20
src/model.h
20
src/model.h
@@ -1,12 +1,28 @@
|
||||
#ifndef SMHENGINE_SRC_MODEL_H_
|
||||
#define SMHENGINE_SRC_MODEL_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <OBJLoader.h>
|
||||
|
||||
#include <logger.h>
|
||||
|
||||
#include "mesh.h"
|
||||
#include "material.h"
|
||||
|
||||
class Mesh {
|
||||
class Model {
|
||||
public:
|
||||
|
||||
Model();
|
||||
Model(std::string loadPath);
|
||||
|
||||
std::vector<std::pair<Mesh, Material>> modelData;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
28
src/object.h
Normal file
28
src/object.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef SMHENGINE_SRC_OBJECT_H_
|
||||
#define SMHENGINE_SRC_OBJECT_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <OBJLoader.h>
|
||||
|
||||
#include <logger.h>
|
||||
|
||||
#include "model.h"
|
||||
#include "shader.h"
|
||||
|
||||
class Object {
|
||||
public:
|
||||
Object();
|
||||
|
||||
std::vector<Model> models;
|
||||
std::vector<Shader> shaders;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@
|
||||
void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
|
||||
std::vector<glm::vec3>& outVert,
|
||||
std::vector<glm::vec3>& outNorm,
|
||||
std::vector<glm::vec2>& outTexCoord) {
|
||||
std::vector<glm::vec3>& outTexCoord) {
|
||||
|
||||
for (int i = 0; i < inVertArr.size(); i++) {
|
||||
|
||||
@@ -13,7 +13,7 @@ void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
|
||||
glm::vec3 tempNorm {inVertArr[i].Normal.X, inVertArr[i].Normal.Y, inVertArr[i].Normal.Z};
|
||||
outNorm.push_back(tempNorm);
|
||||
|
||||
glm::vec2 tempTexCoord {inVertArr[i].TextureCoordinate.X, inVertArr[i].TextureCoordinate.Y};
|
||||
glm::vec3 tempTexCoord {inVertArr[i].TextureCoordinate.X, inVertArr[i].TextureCoordinate.Y, 1.0f};
|
||||
outTexCoord.push_back(tempTexCoord);
|
||||
|
||||
}
|
||||
@@ -28,3 +28,11 @@ void UintToGLuint(std::vector<unsigned int>& inIndices,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec) {
|
||||
|
||||
outVec.x = inVec.X;
|
||||
outVec.y = inVec.Y;
|
||||
outVec.z = inVec.Z;
|
||||
|
||||
}
|
||||
|
||||
@@ -9,9 +9,11 @@
|
||||
void OBJLtoGLM(std::vector<objl::Vertex>& inVertArr,
|
||||
std::vector<glm::vec3>& outVert,
|
||||
std::vector<glm::vec3>& outNorm,
|
||||
std::vector<glm::vec2>& outTexCoord);
|
||||
std::vector<glm::vec3>& outTexCoord);
|
||||
|
||||
void UintToGLuint(std::vector<unsigned int>& inIndices,
|
||||
std::vector<GLuint>& outIndices);
|
||||
|
||||
void OBJLVec3toGLM(objl::Vector3& inVec, glm::vec3& outVec);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user