Added asset loading and OBJ parsing, including indexing

This commit is contained in:
grayson committed 2021-08-30 21:26:06 -05:00
1 parent 086f8a1670
commit 9fcd8240e4
16 files changed
+249 -79

No files matched your search

+29 -10
View File
@@ -1,5 +1,9 @@
#include "Entity.h"
#include<vector>
#include "Utility.h"
namespace nf {
Entity::Entity() :
m_model(nullptr),
@@ -10,25 +14,40 @@ namespace nf {
}
void Entity::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureName) {
void Entity::create(Asset* modelAsset, Asset* textureAsset) {
AModel& model = *(AModel*)modelAsset;
if (model.alreadyLoaded) {
m_model = model.loadedModel;
return;
}
if (!textureAsset)
Error("No texture given to Entity create function on new model load!");
ATexture& texture = *(ATexture*)textureAsset;
std::string obj = model.data;
m_model = new Model;
m_model->create(vertexBufferData, vertexBufferSize, indexBufferData, indexBufferCount, textureCoordinatesBufferData, textureCoordinatesBufferSize, textureName);
//TODO: Replace this with getting the information from the AGeometry and ATexture structs
std::vector<float> vb;
std::vector<unsigned int> ib;
size_t ibCount = 0;
std::vector<float> tc;
parseOBJ(obj, vb, ib, ibCount, tc);
m_model->create(&vb[0], vb.size() * sizeof(float), &ib[0], ibCount, &tc[0], tc.size() * sizeof(float), texture.data, texture.size);
model.alreadyLoaded = true;
model.loadedModel = m_model;
}
void Entity::setPosition(float x, float y, float z) {
void Entity::setPosition(double x, double y, double z) {
m_position = { x, y, z };
}
void Entity::setRotation(float x, float y, float z) {
void Entity::setRotation(double x, double y, double z) {
m_rotation = { x, y, z };
}
void Entity::setScale(float x) {
void Entity::setScale(double x) {
m_rotation = { x, x, x };
}
void Entity::setScale(float x, float y, float z) {
void Entity::setScale(double x, double y, double z) {
m_scale = { x, y, z };
}
@@ -45,9 +64,9 @@ namespace nf {
void Entity::setModelMatrix(Shader* shader) {
glm::mat4 model(1.0f);
model = glm::translate(model, glm::vec3(m_position.x, m_position.y, m_position.z));
model = glm::rotate(model, glm::radians(m_rotation.x), glm::vec3(1.0, 0.0, 0.0));
model = glm::rotate(model, glm::radians(m_rotation.y), glm::vec3(0.0, 1.0, 0.0));
model = glm::rotate(model, glm::radians(m_rotation.z), glm::vec3(0.0, 0.0, 1.0));
model = glm::rotate(model, glm::radians((float)m_rotation.x), glm::vec3(1.0, 0.0, 0.0));
model = glm::rotate(model, glm::radians((float)m_rotation.y), glm::vec3(0.0, 1.0, 0.0));
model = glm::rotate(model, glm::radians((float)m_rotation.z), glm::vec3(0.0, 0.0, 1.0));
model = glm::scale(model, glm::vec3(m_scale.x, m_scale.y, m_scale.z));
shader->setUniform("model", model);
}
+4 -5
View File
@@ -6,18 +6,17 @@ namespace nf {
Model::Model() {
}
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureName) {
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const unsigned char* textureData, size_t textureSize) {
m_vao = new VertexArray;
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
m_vao->push<float>(2);
//TODO: Change this to 3
m_vao->push<float>(3);
m_vao->finishBufferLayout();
if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureName) {
if (textureCoordinatesBufferData && textureCoordinatesBufferSize && textureData) {
m_vao->addBuffer(textureCoordinatesBufferData, textureCoordinatesBufferSize);
m_vao->push<float>(2);
m_vao->finishBufferLayout();
m_texture = new Texture;
m_texture->create(textureName);
m_texture->create(textureData, textureSize);
}
m_ib = new IndexBuffer(indexBufferData, indexBufferCount);
}
+4 -5
View File
@@ -11,18 +11,17 @@ namespace nf {
glGenTextures(1, &m_id);
}
void Texture::create(const char* textureName) {
void Texture::create(const unsigned char* textureData, size_t textureSize) {
int width, height, nChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* texture = stbi_load(textureName, &width, &height, &nChannels, 0);
//TODO: Load from memory
unsigned char* texture = stbi_load_from_memory(textureData, textureSize, &width, &height, &nChannels, 0);
if (!texture)
Error("Texture \"" + (std::string)textureName + "\" failed to load from memory!");
Error("Texture failed to load from memory!");
glBindTexture(GL_TEXTURE_2D, m_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
//glGenerateMipmap(GL_TEXTURE_2D);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(texture);
}