feat: refactor wavefront to support new mesh + material system
This commit is contained in:
@@ -9,8 +9,7 @@
|
|||||||
|
|
||||||
#include "engine/renderer/shader.h"
|
#include "engine/renderer/shader.h"
|
||||||
#include "engine/renderer/renderer.h"
|
#include "engine/renderer/renderer.h"
|
||||||
#include "engine/renderer/material.h"
|
#include "engine/3d/mesh.hpp"
|
||||||
#include "engine/renderer/mesh.h"
|
|
||||||
#include "engine/opengl/buffers.h"
|
#include "engine/opengl/buffers.h"
|
||||||
|
|
||||||
#include "engine/export.h"
|
#include "engine/export.h"
|
||||||
@@ -20,40 +19,44 @@ namespace Core {
|
|||||||
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
||||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||||
|
|
||||||
class ENGINE_API Object {
|
class ENGINE_API Object : public MeshGroup {
|
||||||
friend class Renderer;
|
friend class Renderer;
|
||||||
private:
|
private:
|
||||||
static inline int NormalizeIndex(int idx, int baseCount);
|
static inline int NormalizeIndex(int idx, int baseCount);
|
||||||
|
|
||||||
private:
|
|
||||||
Object();
|
Object();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~Object() = default;
|
~Object() = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Object* LoadFile(const std::string& filename);
|
static Object* LoadFile(const std::string& filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void LoadMaterials(const std::filesystem::path& filename);
|
void LoadMTL(const std::filesystem::path& filename);
|
||||||
private:
|
|
||||||
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
|
||||||
std::shared_ptr<Material> GetMaterial(std::string name);
|
|
||||||
private:
|
private:
|
||||||
|
void CreateNewMesh();
|
||||||
|
void CreateNewMesh(const Material& material);
|
||||||
Mesh& GetLastMesh();
|
Mesh& GetLastMesh();
|
||||||
void CreateNewMesh(const std::string& materialName);
|
|
||||||
|
void AddMaterial(MaterialID id, const Material& material);
|
||||||
|
Material* GetMaterial(const MaterialID& id);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Render(Shader& shader, unsigned int count);
|
void Render(Shader& shader, unsigned int count);
|
||||||
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void EnableBatch(const OpenGL::InstanceBuffer* instanceBuffer);
|
void EnableBatch(const OpenGL::InstanceBuffer* instanceBuffer);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::vector<glm::vec3> m_vertices;
|
std::vector<glm::vec3> m_vertices;
|
||||||
std::vector<glm::vec3> m_normals;
|
std::vector<glm::vec3> m_normals;
|
||||||
std::vector<glm::vec2> m_texCoords;
|
std::vector<glm::vec2> m_texCoords;
|
||||||
|
|
||||||
std::vector<Mesh> m_meshes;
|
std::unordered_map<MaterialID, Material> m_materials;
|
||||||
|
|
||||||
std::unordered_map<std::string, std::shared_ptr<Material>> m_materials;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,24 +8,10 @@
|
|||||||
#include "engine/renderer/wavefront.h"
|
#include "engine/renderer/wavefront.h"
|
||||||
|
|
||||||
#include "engine/IO/parser.h"
|
#include "engine/IO/parser.h"
|
||||||
#include "engine/renderer/mesh.h"
|
#include "engine/3d/mesh.hpp"
|
||||||
|
|
||||||
#define DEFAULT_MATERIAL_NAME "default"
|
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
// ObjElement toElement(const std::string &s) {
|
|
||||||
// if (s == "#") return ObjElement::OHASH;
|
|
||||||
// if (s == "mtllib") return ObjElement::MTLLIB;
|
|
||||||
// if (s == "usemtl") return ObjElement::USEMTL;
|
|
||||||
// if (s == "o") return ObjElement::O;
|
|
||||||
// if (s == "v") return ObjElement::V;
|
|
||||||
// if (s == "vn") return ObjElement::VN;
|
|
||||||
// if (s == "vt") return ObjElement::VT;
|
|
||||||
// if (s == "f") return ObjElement::F;
|
|
||||||
// return ObjElement::OUNKNOWN;
|
|
||||||
// }
|
|
||||||
|
|
||||||
inline ObjElement toElement(const char* s) {
|
inline ObjElement toElement(const char* s) {
|
||||||
switch (s[0]) {
|
switch (s[0]) {
|
||||||
case '#': return ObjElement::OHASH;
|
case '#': return ObjElement::OHASH;
|
||||||
@@ -42,22 +28,6 @@ inline ObjElement toElement(const char* s) {
|
|||||||
return ObjElement::OUNKNOWN;
|
return ObjElement::OUNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
// MtlElement toMtlElement(const std::string &s) {
|
|
||||||
// if (s == "#") return MtlElement::MHASH;
|
|
||||||
// if (s == "newmtl") return MtlElement::NEWMTL;
|
|
||||||
// if (s == "Ns") return MtlElement::NS;
|
|
||||||
// if (s == "Ka") return MtlElement::KA;
|
|
||||||
// if (s == "Ks") return MtlElement::KS;
|
|
||||||
// if (s == "Kd") return MtlElement::KD;
|
|
||||||
// if (s == "Ni") return MtlElement::NI;
|
|
||||||
// if (s == "d") return MtlElement::D;
|
|
||||||
// if (s == "illum") return MtlElement::ILLUM;
|
|
||||||
// if (s == "map_Kd") return MtlElement::MAP_KD;
|
|
||||||
// if (s == "map_Ka") return MtlElement::MAP_KA;
|
|
||||||
// // if (s == "map_Ke") return MtlElement::MAP_KE;
|
|
||||||
// return MtlElement::MUNKNOWN;
|
|
||||||
// }
|
|
||||||
|
|
||||||
inline MtlElement toMtlElement(const char* s) {
|
inline MtlElement toMtlElement(const char* s) {
|
||||||
switch (s[0]) {
|
switch (s[0]) {
|
||||||
case '#': return MtlElement::MHASH;
|
case '#': return MtlElement::MHASH;
|
||||||
@@ -104,7 +74,11 @@ Object::Object() {
|
|||||||
m_texCoords = std::vector<glm::vec2>();
|
m_texCoords = std::vector<glm::vec2>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::LoadMaterials(const std::filesystem::path& filename) {
|
void Object::AddMaterial(MaterialID id, const Material& material) {
|
||||||
|
m_materials.insert(std::make_pair(id, material));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Object::LoadMTL(const std::filesystem::path& filename) {
|
||||||
std::ifstream file(filename);
|
std::ifstream file(filename);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
std::cerr << "Failed to open MTL file: " << filename << std::endl;
|
std::cerr << "Failed to open MTL file: " << filename << std::endl;
|
||||||
@@ -112,7 +86,8 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string currentMaterialName;
|
std::string currentMaterialName;
|
||||||
std::shared_ptr<Material> currentMaterial;
|
Material currentMaterial;
|
||||||
|
bool hasCurrent = false;
|
||||||
|
|
||||||
char line[1024]; // buffer per line
|
char line[1024]; // buffer per line
|
||||||
|
|
||||||
@@ -128,15 +103,15 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
case MtlElement::NEWMTL:
|
case MtlElement::NEWMTL:
|
||||||
{
|
{
|
||||||
// If a material was being built, commit it first
|
// If a material was being built, commit it first
|
||||||
if (currentMaterial) {
|
if (hasCurrent) {
|
||||||
AddMaterial(currentMaterialName, std::move(currentMaterial));
|
AddMaterial(currentMaterialName, currentMaterial);
|
||||||
currentMaterial = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* materialName = p.TakeWord();
|
char* materialName = p.TakeWord();
|
||||||
if (materialName) {
|
if (materialName) {
|
||||||
currentMaterialName = materialName;
|
currentMaterialName = materialName;
|
||||||
currentMaterial = std::make_shared<Material>();
|
currentMaterial = Material(currentMaterialName);
|
||||||
|
hasCurrent = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -144,7 +119,7 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
case MtlElement::NS: // specular weight
|
case MtlElement::NS: // specular weight
|
||||||
{
|
{
|
||||||
float weight = p.TakeFloat();
|
float weight = p.TakeFloat();
|
||||||
if (currentMaterial) currentMaterial->SetSpecularWeight(weight);
|
if (hasCurrent) currentMaterial.SetSpecularWeight(weight);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +128,7 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
float r = p.TakeFloat();
|
float r = p.TakeFloat();
|
||||||
float g = p.TakeFloat();
|
float g = p.TakeFloat();
|
||||||
float b = p.TakeFloat();
|
float b = p.TakeFloat();
|
||||||
if (currentMaterial) currentMaterial->SetAmbientColor(glm::vec3(r, g, b));
|
if (hasCurrent) currentMaterial.SetAmbientColor(glm::vec3(r, g, b));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +137,7 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
float r = p.TakeFloat();
|
float r = p.TakeFloat();
|
||||||
float g = p.TakeFloat();
|
float g = p.TakeFloat();
|
||||||
float b = p.TakeFloat();
|
float b = p.TakeFloat();
|
||||||
if (currentMaterial) currentMaterial->SetSpecularColor(glm::vec3(r, g, b));
|
if (hasCurrent) currentMaterial.SetSpecularColor(glm::vec3(r, g, b));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,21 +146,21 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
float r = p.TakeFloat();
|
float r = p.TakeFloat();
|
||||||
float g = p.TakeFloat();
|
float g = p.TakeFloat();
|
||||||
float b = p.TakeFloat();
|
float b = p.TakeFloat();
|
||||||
if (currentMaterial) currentMaterial->SetDiffuseColor(glm::vec3(r, g, b));
|
if (hasCurrent) currentMaterial.SetDiffuseColor(glm::vec3(r, g, b));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case MtlElement::D: // opacity
|
case MtlElement::D: // opacity
|
||||||
{
|
{
|
||||||
float d = p.TakeFloat();
|
float d = p.TakeFloat();
|
||||||
if (currentMaterial) currentMaterial->SetOpacity(d);
|
if (hasCurrent) currentMaterial.SetOpacity(d);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case MtlElement::ILLUM: // illumination model
|
case MtlElement::ILLUM: // illumination model
|
||||||
{
|
{
|
||||||
int illum = p.TakeInt();
|
int illum = p.TakeInt();
|
||||||
if (currentMaterial) currentMaterial->SetIllumination(illum);
|
if (hasCurrent) currentMaterial.SetIllumination(illum);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +168,7 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
{
|
{
|
||||||
// take rest of line as texture path (can contain spaces)
|
// take rest of line as texture path (can contain spaces)
|
||||||
char* texPath = p.TakeUntil('\0');
|
char* texPath = p.TakeUntil('\0');
|
||||||
if (texPath && currentMaterial) {
|
if (texPath && hasCurrent) {
|
||||||
// trim trailing spaces
|
// trim trailing spaces
|
||||||
size_t len = std::strlen(texPath);
|
size_t len = std::strlen(texPath);
|
||||||
while (len > 0 && (texPath[len - 1] == ' ' || texPath[len - 1] == '\t'))
|
while (len > 0 && (texPath[len - 1] == ' ' || texPath[len - 1] == '\t'))
|
||||||
@@ -201,7 +176,7 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
|
|
||||||
std::filesystem::path texturePath = filename.parent_path() / texPath;
|
std::filesystem::path texturePath = filename.parent_path() / texPath;
|
||||||
|
|
||||||
currentMaterial->SetDiffuseTexture(Texture::LoadFile(texturePath.string()));
|
currentMaterial.SetDiffuseTexture(Texture::LoadFile(texturePath.string()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -209,13 +184,13 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
case MtlElement::MAP_KA: // ambient texture map
|
case MtlElement::MAP_KA: // ambient texture map
|
||||||
{
|
{
|
||||||
char* texPath = p.TakeUntil('\0');
|
char* texPath = p.TakeUntil('\0');
|
||||||
if (texPath && currentMaterial) {
|
if (texPath && hasCurrent) {
|
||||||
size_t len = std::strlen(texPath);
|
size_t len = std::strlen(texPath);
|
||||||
while (len > 0 && (texPath[len - 1] == ' ' || texPath[len - 1] == '\t'))
|
while (len > 0 && (texPath[len - 1] == ' ' || texPath[len - 1] == '\t'))
|
||||||
texPath[--len] = '\0';
|
texPath[--len] = '\0';
|
||||||
|
|
||||||
// optional: handle ambient texture
|
// optional: handle ambient texture
|
||||||
// currentMaterial->SetAmbientTexture(Texture::LoadFile(texPath));
|
// currentMaterial.SetAmbientTexture(Texture::LoadFile(texPath));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -227,41 +202,36 @@ void Object::LoadMaterials(const std::filesystem::path& filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Commit last material if pending
|
// Commit last material if pending
|
||||||
if (currentMaterial) {
|
if (hasCurrent) {
|
||||||
AddMaterial(currentMaterialName, std::move(currentMaterial));
|
AddMaterial(currentMaterialName, currentMaterial);
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::AddMaterial(std::string name, std::shared_ptr<Material> material)
|
Material* Object::GetMaterial(const MaterialID& id)
|
||||||
{
|
{
|
||||||
m_materials.insert(std::make_pair(std::move(name), std::move(material)));
|
auto material = m_materials.find(id);
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Material> Object::GetMaterial(std::string name)
|
|
||||||
{
|
|
||||||
auto material = m_materials.find(name);
|
|
||||||
if (material == m_materials.end()) return nullptr;
|
if (material == m_materials.end()) return nullptr;
|
||||||
return material->second;
|
return &material->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::CreateNewMesh(const std::string& materialName)
|
void Object::CreateNewMesh(const Material& material)
|
||||||
{
|
{
|
||||||
Mesh mesh;
|
EmplaceBack(material);
|
||||||
mesh.materialName = materialName;
|
}
|
||||||
m_meshes.push_back(mesh);
|
|
||||||
|
void Object::CreateNewMesh()
|
||||||
|
{
|
||||||
|
EmplaceBack();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh& Object::GetLastMesh()
|
Mesh& Object::GetLastMesh()
|
||||||
{
|
{
|
||||||
if (m_meshes.empty()) {
|
if (Empty()) {
|
||||||
auto material = std::make_shared<Material>();
|
CreateNewMesh(Material::Default());
|
||||||
material->SetAmbientColor(glm::vec3(0.52f, 0.52f, 0.52f));
|
|
||||||
AddMaterial(DEFAULT_MATERIAL_NAME, std::move(material));
|
|
||||||
CreateNewMesh(DEFAULT_MATERIAL_NAME);
|
|
||||||
}
|
}
|
||||||
return m_meshes.back();
|
return Back();
|
||||||
}
|
}
|
||||||
|
|
||||||
Object* Object::LoadFile(const std::string& filename) {
|
Object* Object::LoadFile(const std::string& filename) {
|
||||||
@@ -289,7 +259,7 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
if (mtlFile) {
|
if (mtlFile) {
|
||||||
std::filesystem::path fullPath = filename;
|
std::filesystem::path fullPath = filename;
|
||||||
std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile);
|
std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile);
|
||||||
obj->LoadMaterials(mtlPath);
|
obj->LoadMTL(mtlPath);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -298,11 +268,16 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
{
|
{
|
||||||
char* materialName = p.TakeWord();
|
char* materialName = p.TakeWord();
|
||||||
if (materialName) {
|
if (materialName) {
|
||||||
auto& mesh = obj->GetLastMesh();
|
auto material = obj->GetMaterial(materialName);
|
||||||
if (mesh.materialName != materialName) {
|
if (!material) {
|
||||||
Mesh newMesh;
|
// Not defined material being used.
|
||||||
newMesh.materialName = materialName;
|
std::cerr << "[WARN] WavefrontError: use of undefined material '"
|
||||||
obj->m_meshes.push_back(newMesh);
|
<< materialName << "'" << std::endl;
|
||||||
|
material = new Material();
|
||||||
|
}
|
||||||
|
auto mesh = obj->FindMeshByMaterial(material);
|
||||||
|
if (mesh == obj->End()) {
|
||||||
|
obj->CreateNewMesh(*material);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -349,8 +324,11 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
case ObjElement::F: // face
|
case ObjElement::F: // face
|
||||||
{
|
{
|
||||||
auto& mesh = obj->GetLastMesh();
|
auto& mesh = obj->GetLastMesh();
|
||||||
int raw_vi, raw_ti, raw_ni;
|
|
||||||
|
|
||||||
|
std::vector<uint32_t> faceIndices;
|
||||||
|
faceIndices.reserve(8);
|
||||||
|
|
||||||
|
int raw_vi, raw_ti, raw_ni;
|
||||||
while (p.TakeFaceIndices(raw_vi, raw_ti, raw_ni)) {
|
while (p.TakeFaceIndices(raw_vi, raw_ti, raw_ni)) {
|
||||||
// Convert raw OBJ indices to 0-based / -1 sentinel
|
// Convert raw OBJ indices to 0-based / -1 sentinel
|
||||||
int vi = Object::NormalizeIndex(raw_vi, (int)obj->m_vertices.size());
|
int vi = Object::NormalizeIndex(raw_vi, (int)obj->m_vertices.size());
|
||||||
@@ -362,15 +340,22 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 vert = obj->m_vertices[vi];
|
Vertex v;
|
||||||
glm::vec3 norm(0.0f);
|
v.position = obj->m_vertices[vi];
|
||||||
glm::vec2 texCoord(0.0f);
|
v.normal = (ni >= 0) ? obj->m_normals[ni] : glm::vec3(0.0f);
|
||||||
|
v.uv = (ti >= 0) ? obj->m_texCoords[ti] : glm::vec3(0.0f);
|
||||||
|
|
||||||
if (ni >= 0) norm = obj->m_normals[ni];
|
uint32_t idx = mesh.PushVertex(v);
|
||||||
if (ti >= 0) texCoord = obj->m_texCoords[ti];
|
faceIndices.push_back(idx);
|
||||||
|
// mesh.m_vertexBuffer.emplace_back(vert, norm, texCoord);
|
||||||
|
// mesh.m_indexBuffer.push_back(mesh.m_vertexBuffer.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
mesh.m_vertexBuffer.emplace_back(vert, norm, texCoord);
|
// triangulate polygon (fan)
|
||||||
mesh.m_indexBuffer.push_back(mesh.m_vertexBuffer.size() - 1);
|
if (faceIndices.size() >= 3) {
|
||||||
|
for (size_t i = 1; i + 1 < faceIndices.size(); ++i) {
|
||||||
|
mesh.PushTriangle(faceIndices[0], faceIndices[i], faceIndices[i+1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -385,130 +370,108 @@ Object* Object::LoadFile(const std::string& filename) {
|
|||||||
std::cout << "Vertices count: " << obj->m_vertices.size() << std::endl;
|
std::cout << "Vertices count: " << obj->m_vertices.size() << std::endl;
|
||||||
std::cout << "Normals count: " << obj->m_normals.size() << std::endl;
|
std::cout << "Normals count: " << obj->m_normals.size() << std::endl;
|
||||||
std::cout << "TexCoords count: " << obj->m_texCoords.size() << std::endl;
|
std::cout << "TexCoords count: " << obj->m_texCoords.size() << std::endl;
|
||||||
std::cout << "Meshes count: " << obj->m_meshes.size() << std::endl;
|
std::cout << "Meshes count: " << obj->GetSize() << std::endl;
|
||||||
std::cout << "Materials count: " << obj->m_materials.size() << std::endl;
|
std::cout << "Materials count: " << obj->m_materials.size() << std::endl;
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
for (auto &mesh : obj->m_meshes) {
|
// FIXME:
|
||||||
mesh.Upload();
|
|
||||||
}
|
// for (auto it = obj->Begin(); it != obj->End(); ++it) {
|
||||||
|
// it->Upload();
|
||||||
|
// }
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::EnableBatch(const OpenGL::InstanceBuffer* instanceBuffer) {
|
void Object::EnableBatch(const OpenGL::InstanceBuffer* instanceBuffer) {
|
||||||
for (auto &mesh : m_meshes) {
|
// FIXME:
|
||||||
mesh.Bind();
|
|
||||||
|
|
||||||
instanceBuffer->StartConfigure();
|
// for (auto &mesh : m_meshes) {
|
||||||
std::size_t vec4Size = sizeof(glm::vec4);
|
// mesh.Bind();
|
||||||
for (int i = 0; i < 4; ++i) {
|
|
||||||
glEnableVertexAttribArray(3 + i); // use locations 3,4,5,6 for instance matrix
|
|
||||||
glVertexAttribPointer(3 + i, 4, GL_FLOAT, GL_FALSE,
|
|
||||||
sizeof(glm::mat4), (void*)(i * vec4Size));
|
|
||||||
glVertexAttribDivisor(3 + i, 1); // IMPORTANT: one per instance, not per vertex
|
|
||||||
}
|
|
||||||
instanceBuffer->EndConfigure();
|
|
||||||
|
|
||||||
mesh.Unbind();
|
// instanceBuffer->StartConfigure();
|
||||||
}
|
// std::size_t vec4Size = sizeof(glm::vec4);
|
||||||
|
// for (int i = 0; i < 4; ++i) {
|
||||||
|
// glEnableVertexAttribArray(3 + i); // use locations 3,4,5,6 for instance matrix
|
||||||
|
// glVertexAttribPointer(3 + i, 4, GL_FLOAT, GL_FALSE,
|
||||||
|
// sizeof(glm::mat4), (void*)(i * vec4Size));
|
||||||
|
// glVertexAttribDivisor(3 + i, 1); // IMPORTANT: one per instance, not per vertex
|
||||||
|
// }
|
||||||
|
// instanceBuffer->EndConfigure();
|
||||||
|
|
||||||
|
// mesh.Unbind();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// void Object::Render(Shader& shader)
|
|
||||||
// {
|
|
||||||
// for (auto &mesh : m_meshes) {
|
|
||||||
// auto material = GetMaterial(mesh.materialName);
|
|
||||||
|
|
||||||
// shader.setFloat("ambientStrength", 0.2f);
|
|
||||||
// shader.setFloat("shininess", material->GetSpecularWeight());
|
|
||||||
// shader.setFloat("opacity", material->GetOpacity());
|
|
||||||
// shader.setBool("useSpecular", material->GetIllumination() >= 2);
|
|
||||||
// shader.setFloat("specularStrength", 1.0f);
|
|
||||||
// shader.setVec3("ambientColor", material->GetAmbientColor());
|
|
||||||
// shader.setVec3("diffuseColor", material->GetDiffuseColor());
|
|
||||||
// shader.setVec3("specularColor", material->GetSpecularColor());
|
|
||||||
|
|
||||||
// if (material->HasDiffuseTexture()) {
|
|
||||||
// shader.setBool("useTexture", true);
|
|
||||||
// glActiveTexture(GL_TEXTURE0);
|
|
||||||
// glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
|
||||||
// shader.setInt("diffuseTex", 0);
|
|
||||||
// } else {
|
|
||||||
// shader.setBool("useTexture", false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// mesh.Render();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
void Object::Render(Shader& shader, unsigned int count)
|
void Object::Render(Shader& shader, unsigned int count)
|
||||||
{
|
{
|
||||||
for (auto &mesh : m_meshes)
|
// FIXME:
|
||||||
{
|
|
||||||
auto material = GetMaterial(mesh.materialName);
|
|
||||||
|
|
||||||
// --- Basic material properties ---
|
// for (auto &mesh : m_meshes)
|
||||||
shader.setFloat("opacity", material->GetOpacity());
|
// {
|
||||||
|
// auto material = GetMaterial(mesh.GetMaterialName());
|
||||||
|
|
||||||
// Albedo (base color)
|
// // --- Basic material properties ---
|
||||||
shader.setVec3("albedo", material->GetDiffuseColor());
|
// shader.setFloat("opacity", material->GetOpacity());
|
||||||
|
|
||||||
// Metallic and roughness (defaults)
|
// // Albedo (base color)
|
||||||
shader.setFloat("metallic", 0.8f);
|
// shader.setVec3("albedo", material->GetDiffuseColor());
|
||||||
shader.setFloat("roughness", 0.5f);
|
|
||||||
shader.setFloat("ao", 1.0f); // default ambient occlusion if none
|
|
||||||
|
|
||||||
// --- Optional textures ---
|
// // Metallic and roughness (defaults)
|
||||||
int texUnit = 0;
|
// shader.setFloat("metallic", 0.8f);
|
||||||
|
// shader.setFloat("roughness", 0.5f);
|
||||||
|
// shader.setFloat("ao", 1.0f); // default ambient occlusion if none
|
||||||
|
|
||||||
// Albedo texture
|
// // --- Optional textures ---
|
||||||
if (material->HasDiffuseTexture()) {
|
// int texUnit = 0;
|
||||||
shader.setBool("useAlbedoMap", true);
|
|
||||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
|
||||||
shader.setInt("albedoTex", texUnit++);
|
|
||||||
} else {
|
|
||||||
shader.setBool("useAlbedoMap", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Metallic texture
|
// // Albedo texture
|
||||||
// if (material->HasMetallicTexture()) {
|
// if (material->HasDiffuseTexture()) {
|
||||||
if (false) {
|
// shader.setBool("useAlbedoMap", true);
|
||||||
shader.setBool("useMetallicMap", true);
|
// glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
// glBindTexture(GL_TEXTURE_2D, material->GetDiffuseTexture()->GetID());
|
||||||
// glBindTexture(GL_TEXTURE_2D, material->GetMetallicTexture()->GetID());
|
// shader.setInt("albedoTex", texUnit++);
|
||||||
shader.setInt("metallicTex", texUnit++);
|
// } else {
|
||||||
} else {
|
// shader.setBool("useAlbedoMap", false);
|
||||||
shader.setBool("useMetallicMap", false);
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
// Roughness texture
|
// // Metallic texture
|
||||||
// if (material->HasRoughnessTexture()) {
|
// // if (material->HasMetallicTexture()) {
|
||||||
if (false) {
|
// if (false) {
|
||||||
shader.setBool("useRoughnessMap", true);
|
// shader.setBool("useMetallicMap", true);
|
||||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
// glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
// glBindTexture(GL_TEXTURE_2D, material->GetRoughnessTexture()->GetID());
|
// // glBindTexture(GL_TEXTURE_2D, material->GetMetallicTexture()->GetID());
|
||||||
shader.setInt("roughnessTex", texUnit++);
|
// shader.setInt("metallicTex", texUnit++);
|
||||||
} else {
|
// } else {
|
||||||
shader.setBool("useRoughnessMap", false);
|
// shader.setBool("useMetallicMap", false);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// AO texture
|
// // Roughness texture
|
||||||
// if (material->HasAoTexture()) {
|
// // if (material->HasRoughnessTexture()) {
|
||||||
if (false) {
|
// if (false) {
|
||||||
shader.setBool("useAoMap", true);
|
// shader.setBool("useRoughnessMap", true);
|
||||||
glActiveTexture(GL_TEXTURE0 + texUnit);
|
// glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
// glBindTexture(GL_TEXTURE_2D, material->GetAoTexture()->GetID());
|
// // glBindTexture(GL_TEXTURE_2D, material->GetRoughnessTexture()->GetID());
|
||||||
shader.setInt("aoTex", texUnit++);
|
// shader.setInt("roughnessTex", texUnit++);
|
||||||
} else {
|
// } else {
|
||||||
shader.setBool("useAoMap", false);
|
// shader.setBool("useRoughnessMap", false);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// --- Render mesh ---
|
// // AO texture
|
||||||
mesh.Render(count);
|
// // if (material->HasAoTexture()) {
|
||||||
}
|
// if (false) {
|
||||||
|
// shader.setBool("useAoMap", true);
|
||||||
|
// glActiveTexture(GL_TEXTURE0 + texUnit);
|
||||||
|
// // glBindTexture(GL_TEXTURE_2D, material->GetAoTexture()->GetID());
|
||||||
|
// shader.setInt("aoTex", texUnit++);
|
||||||
|
// } else {
|
||||||
|
// shader.setBool("useAoMap", false);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // --- Render mesh ---
|
||||||
|
// mesh.Render(count);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user