Skip to content

Commit baed2b5

Browse files
committed
Added materials
1 parent 61fb4bc commit baed2b5

File tree

25 files changed

+584
-462
lines changed

25 files changed

+584
-462
lines changed

igneous/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ set(INCLUDES
4242
include/igneous/renderer/camera.hpp
4343
include/igneous/renderer/capture.hpp
4444
include/igneous/renderer/fpsCamera.hpp
45+
include/igneous/renderer/material.hpp
4546
include/igneous/renderer/model.hpp
4647
include/igneous/renderer/renderer.hpp
4748
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
#include <bgfx/bgfx.h>
6+
7+
namespace igneous {
8+
enum MATERIAL_ATTRIBUTE : uint8_t
9+
{
10+
COLOR,
11+
DIFFUSE,
12+
NORMAL,
13+
SPECULAR
14+
};
15+
16+
struct Material
17+
{
18+
bgfx::ProgramHandle shader;
19+
uint64_t state;
20+
std::vector<std::pair<bgfx::UniformHandle, void*>> uniforms;
21+
std::vector<std::pair<bgfx::UniformHandle, bgfx::TextureHandle>> textures;
22+
};
23+
24+
typedef Material* MaterialHandle;
25+
} // end namespace igneous

igneous/include/igneous/renderer/model.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
#include <bgfx/bgfx.h>
66

7+
#include "igneous/renderer/material.hpp"
8+
79
namespace igneous {
810
struct Mesh
911
{
1012
bgfx::VertexBufferHandle vbh;
1113
bgfx::IndexBufferHandle ibh;
12-
std::vector<bgfx::TextureHandle> textures;
14+
MaterialHandle material;
1315
};
1416

1517
struct Model
1618
{
1719
std::vector<Mesh> meshes;
18-
bgfx::ProgramHandle program;
1920
};
2021

2122
typedef Model* ModelHandle;

igneous/include/igneous/renderer/renderer.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ namespace renderer
1616
std::string getGpuInfo();
1717

1818
bgfx::TextureHandle loadTexture(std::string path, uint32_t _flags = 0, bool track = true);
19-
const bgfx::Memory* loadMemory(const char* filename);
20-
bgfx::ShaderHandle loadShader(const char* shader);
21-
bgfx::ProgramHandle loadProgram(const char* vs, const char* fs);
19+
const bgfx::Memory* loadMemory(const std::string& filename);
20+
bgfx::ShaderHandle loadShader(const std::string& shader);
21+
bgfx::ProgramHandle loadProgram(const std::string& vs, const std::string& fs);
22+
bgfx::ProgramHandle loadProgram(const std::string& name);
2223

23-
ModelHandle loadModel(std::string path, bgfx::ProgramHandle program);
24-
ModelHandle generateModel(std::vector<std::pair<unsigned int, unsigned int>> modelSizes, void* modelData, bgfx::VertexDecl vertexDecl, bgfx::ProgramHandle program);
24+
ModelHandle loadModel(std::string path);
25+
MaterialHandle loadMaterial(std::string name);
2526

2627
void render();
2728
void screenshot();

igneous/src/renderer/renderer.cpp

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ namespace renderer
3232
{
3333
static std::map<std::string, bgfx::TextureHandle> textures;
3434
static std::map<std::string, bgfx::ProgramHandle> programs;
35-
std::map<std::string, Model*> models;
35+
static std::map<std::string, MaterialHandle> materials;
36+
std::map<std::string, ModelHandle> models;
3637

3738
static bgfx::TextureHandle checkerBoard;
3839
static uint32_t flags = BGFX_RESET_NONE;
40+
static bgfx::UniformHandle U_COLOR;
3941

4042
void screenshotCallback(const std::string& name, const arg_list& args)
4143
{
@@ -141,6 +143,8 @@ namespace renderer
141143

142144
s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
143145

146+
U_COLOR = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
147+
144148
IG_CORE_INFO("Renderer Initialized");
145149
}
146150

@@ -217,7 +221,7 @@ namespace renderer
217221
return handle;
218222
}
219223

220-
const bgfx::Memory* loadMemory(const char* filename)
224+
const bgfx::Memory* loadMemory(const std::string& filename)
221225
{
222226
std::ifstream file(filename, std::ios::binary | std::ios::ate);
223227
if (file.fail())
@@ -235,17 +239,14 @@ namespace renderer
235239
return nullptr;
236240
}
237241

238-
bgfx::ShaderHandle loadShader(const char* shader)
242+
bgfx::ShaderHandle loadShader(const std::string& shader)
239243
{
240244
return bgfx::createShader(loadMemory(shader));
241245
}
242246

243-
bgfx::ProgramHandle loadProgram(const char* vsName, const char* fsName)
247+
bgfx::ProgramHandle loadProgram(const std::string& vsName, const std::string& fsName)
244248
{
245-
char vsPath[512];
246-
char fsPath[512];
247-
248-
const char* shaderPath = "???";
249+
std::string shaderPath;
249250

250251
switch (bgfx::getRendererType())
251252
{
@@ -262,18 +263,15 @@ namespace renderer
262263
case bgfx::RendererType::Count: break;
263264
}
264265

265-
bx::strCopy(vsPath, BX_COUNTOF(vsPath), shaderPath);
266-
bx::strCat(vsPath, BX_COUNTOF(vsPath), vsName);
267-
bx::strCat(vsPath, BX_COUNTOF(vsPath), ".bin");
268-
269-
bx::strCopy(fsPath, BX_COUNTOF(fsPath), shaderPath);
270-
bx::strCat(fsPath, BX_COUNTOF(fsPath), fsName);
271-
bx::strCat(fsPath, BX_COUNTOF(fsPath), ".bin");
266+
return bgfx::createProgram(loadShader(shaderPath + vsName + ".bin"), loadShader(shaderPath + fsName + ".bin"), true);
267+
}
272268

273-
return bgfx::createProgram(loadShader(vsPath), loadShader(fsPath), true);
269+
bgfx::ProgramHandle loadProgram(const std::string& name)
270+
{
271+
return loadProgram("vs_" + name, "fs_" + name);
274272
}
275273

276-
ModelHandle loadModel(std::string path, bgfx::ProgramHandle program)
274+
ModelHandle loadModel(std::string path)
277275
{
278276
if (models.count(path))
279277
{
@@ -325,7 +323,7 @@ namespace renderer
325323
unsigned int numMeshes;
326324
file.read((char*)&numMeshes, sizeof(unsigned int));
327325

328-
int pos = file.tellg();
326+
std::streampos pos = file.tellg();
329327

330328
uint32_t modelSize = 0;
331329
for (unsigned int i = 0; i < numMeshes; i++)
@@ -335,6 +333,11 @@ namespace renderer
335333
file.read((char*)&numVerticies, sizeof(unsigned int));
336334
file.read((char*)&numIndicies, sizeof(unsigned int));
337335
modelSize += (uint32_t)numVerticies * (uint32_t)vertexDecl.getStride() + (uint32_t)numIndicies * sizeof(uint16_t);
336+
size_t materialNameLength;
337+
file.read((char*)&materialNameLength, sizeof(size_t));
338+
std::string materialName;
339+
materialName.resize(materialNameLength);
340+
file.read(materialName.data(), materialNameLength);
338341
}
339342

340343
const void* modelData = malloc(modelSize);
@@ -350,13 +353,20 @@ namespace renderer
350353
unsigned int numIndicies;
351354
file.read((char*)&numVerticies, sizeof(unsigned int));
352355
file.read((char*)&numIndicies, sizeof(unsigned int));
356+
size_t materialNameLength;
357+
file.read((char*)&materialNameLength, sizeof(size_t));
358+
std::string materialName;
359+
materialName.resize(materialNameLength);
360+
file.read(materialName.data(), materialNameLength);
361+
353362
uint32_t verticiesSize = (uint32_t)numVerticies * (uint32_t)vertexDecl.getStride();
354363
uint32_t indiciesSize = (uint32_t)numIndicies * sizeof(uint16_t);
355364
Mesh mesh;
356365
mesh.vbh = bgfx::createVertexBuffer(bgfx::makeRef((uint8_t*)modelData + amountRead, verticiesSize), vertexDecl);
357366
amountRead += verticiesSize;
358367
mesh.ibh = bgfx::createIndexBuffer(bgfx::makeRef((uint8_t*)modelData + amountRead, indiciesSize));
359368
amountRead += indiciesSize;
369+
mesh.material = loadMaterial(materialName);
360370

361371
meshes.push_back(mesh);
362372
}
@@ -365,36 +375,48 @@ namespace renderer
365375

366376
Model* model = new Model;
367377
model->meshes = meshes;
368-
model->program = program;
369378

370379
models[path] = model;
371380
return model;
372381
}
373382

374-
ModelHandle generateModel(std::vector<std::pair<unsigned int, unsigned int>> modelSizes, void* modelData, bgfx::VertexDecl vertexDecl, bgfx::ProgramHandle program)
383+
MaterialHandle loadMaterial(std::string name)
375384
{
376-
std::vector<Mesh> meshes;
377-
uint32_t amountRead = 0;
378-
for (auto meshSize : modelSizes)
385+
if (materials.count(name))
379386
{
380-
unsigned int numVerticies = meshSize.first;
381-
unsigned int numIndicies = meshSize.second;
382-
uint32_t verticiesSize = (uint32_t)numVerticies * (uint32_t)vertexDecl.getStride();
383-
uint32_t indiciesSize = (uint32_t)numIndicies * sizeof(uint16_t);
384-
Mesh mesh;
385-
mesh.vbh = bgfx::createVertexBuffer(bgfx::makeRef((uint8_t*)modelData + amountRead, verticiesSize), vertexDecl);
386-
amountRead += verticiesSize;
387-
mesh.ibh = bgfx::createIndexBuffer(bgfx::makeRef((uint8_t*)modelData + amountRead, indiciesSize));
388-
amountRead += indiciesSize;
387+
return materials.at(name);
388+
}
389+
MaterialHandle material = new Material;
389390

390-
meshes.push_back(mesh);
391+
std::ifstream file("res/materials/" + name + ".bin", std::ios::in | std::ios::binary);
392+
393+
if (file.fail())
394+
{
395+
IG_CORE_CRITICAL("Could not open material file: {}", name);
391396
}
392397

393-
Model* model = new Model;
394-
model->meshes = meshes;
395-
model->program = program;
398+
uint8_t shaderNameLength;
399+
file.read((char*)&shaderNameLength, sizeof(uint8_t));
400+
std::string shaderName;
401+
shaderName.resize(shaderNameLength);
402+
file.read(shaderName.data(), shaderNameLength);
396403

397-
return model;
404+
material->shader = loadProgram(shaderName);
405+
material->state = BGFX_STATE_DEFAULT;
406+
407+
MATERIAL_ATTRIBUTE materialAttribute;
408+
while (file.read((char*)&materialAttribute, sizeof(materialAttribute)))
409+
{
410+
if (materialAttribute == COLOR)
411+
{
412+
float* vec4 = new float[4];
413+
file.read((char*)vec4, 4 * sizeof(float));
414+
material->uniforms.push_back(std::make_pair(U_COLOR, vec4));
415+
}
416+
}
417+
418+
materials[name] = material;
419+
return material;
398420
}
399421

400422
void reset()
@@ -413,11 +435,16 @@ namespace renderer
413435
bgfx::setTransform(&transformation);
414436
bgfx::setVertexBuffer(0, mesh.vbh);
415437
bgfx::setIndexBuffer(mesh.ibh);
416-
if (mesh.textures.size() > 0)
417-
bgfx::setTexture(0, s_tex, mesh.textures[0]);
418-
else
419-
bgfx::setTexture(0, s_tex, checkerBoard);
420-
bgfx::submit(0, model->program);
438+
for (auto uniform : mesh.material->uniforms)
439+
{
440+
bgfx::setUniform(uniform.first, uniform.second);
441+
}
442+
for (auto texture : mesh.material->textures)
443+
{
444+
bgfx::setTexture(0, texture.first, texture.second);
445+
}
446+
bgfx::setState(mesh.material->state);
447+
bgfx::submit(0, mesh.material->shader);
421448
}
422449
});
423450

samples/sandbox/.igneous

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
--HEADER
22
NAME:Sandbox
33
VERSION:0.0.0
4-
--ASSETS
5-
MODEL:barn.dae color.vtx
6-
COPY:icons
7-
COPY:audio
8-
COPY:textures
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
poly
2+
color 0.078802 0.078802 0.078802 1.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
poly
2+
color 0.202353 0.04252 0.032067 1.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
poly
2+
color 0.273903 0.056039 0.041967 1.0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
poly
2+
color 0.64 0.64 0.64 1.0

0 commit comments

Comments
 (0)