@@ -32,10 +32,12 @@ namespace renderer
32
32
{
33
33
static std::map<std::string, bgfx::TextureHandle> textures;
34
34
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;
36
37
37
38
static bgfx::TextureHandle checkerBoard;
38
39
static uint32_t flags = BGFX_RESET_NONE;
40
+ static bgfx::UniformHandle U_COLOR;
39
41
40
42
void screenshotCallback (const std::string& name, const arg_list& args)
41
43
{
@@ -141,6 +143,8 @@ namespace renderer
141
143
142
144
s_tex = bgfx::createUniform (" s_tex" , bgfx::UniformType::Sampler);
143
145
146
+ U_COLOR = bgfx::createUniform (" u_color" , bgfx::UniformType::Vec4);
147
+
144
148
IG_CORE_INFO (" Renderer Initialized" );
145
149
}
146
150
@@ -217,7 +221,7 @@ namespace renderer
217
221
return handle;
218
222
}
219
223
220
- const bgfx::Memory* loadMemory (const char * filename)
224
+ const bgfx::Memory* loadMemory (const std::string& filename)
221
225
{
222
226
std::ifstream file (filename, std::ios::binary | std::ios::ate);
223
227
if (file.fail ())
@@ -235,17 +239,14 @@ namespace renderer
235
239
return nullptr ;
236
240
}
237
241
238
- bgfx::ShaderHandle loadShader (const char * shader)
242
+ bgfx::ShaderHandle loadShader (const std::string& shader)
239
243
{
240
244
return bgfx::createShader (loadMemory (shader));
241
245
}
242
246
243
- bgfx::ProgramHandle loadProgram (const char * vsName, const char * fsName)
247
+ bgfx::ProgramHandle loadProgram (const std::string& vsName, const std::string& fsName)
244
248
{
245
- char vsPath[512 ];
246
- char fsPath[512 ];
247
-
248
- const char * shaderPath = " ???" ;
249
+ std::string shaderPath;
249
250
250
251
switch (bgfx::getRendererType ())
251
252
{
@@ -262,18 +263,15 @@ namespace renderer
262
263
case bgfx::RendererType::Count: break ;
263
264
}
264
265
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
+ }
272
268
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);
274
272
}
275
273
276
- ModelHandle loadModel (std::string path, bgfx::ProgramHandle program )
274
+ ModelHandle loadModel (std::string path)
277
275
{
278
276
if (models.count (path))
279
277
{
@@ -325,7 +323,7 @@ namespace renderer
325
323
unsigned int numMeshes;
326
324
file.read ((char *)&numMeshes, sizeof (unsigned int ));
327
325
328
- int pos = file.tellg ();
326
+ std::streampos pos = file.tellg ();
329
327
330
328
uint32_t modelSize = 0 ;
331
329
for (unsigned int i = 0 ; i < numMeshes; i++)
@@ -335,6 +333,11 @@ namespace renderer
335
333
file.read ((char *)&numVerticies, sizeof (unsigned int ));
336
334
file.read ((char *)&numIndicies, sizeof (unsigned int ));
337
335
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);
338
341
}
339
342
340
343
const void * modelData = malloc (modelSize);
@@ -350,13 +353,20 @@ namespace renderer
350
353
unsigned int numIndicies;
351
354
file.read ((char *)&numVerticies, sizeof (unsigned int ));
352
355
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
+
353
362
uint32_t verticiesSize = (uint32_t )numVerticies * (uint32_t )vertexDecl.getStride ();
354
363
uint32_t indiciesSize = (uint32_t )numIndicies * sizeof (uint16_t );
355
364
Mesh mesh;
356
365
mesh.vbh = bgfx::createVertexBuffer (bgfx::makeRef ((uint8_t *)modelData + amountRead, verticiesSize), vertexDecl);
357
366
amountRead += verticiesSize;
358
367
mesh.ibh = bgfx::createIndexBuffer (bgfx::makeRef ((uint8_t *)modelData + amountRead, indiciesSize));
359
368
amountRead += indiciesSize;
369
+ mesh.material = loadMaterial (materialName);
360
370
361
371
meshes.push_back (mesh);
362
372
}
@@ -365,36 +375,48 @@ namespace renderer
365
375
366
376
Model* model = new Model;
367
377
model->meshes = meshes;
368
- model->program = program;
369
378
370
379
models[path] = model;
371
380
return model;
372
381
}
373
382
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 )
375
384
{
376
- std::vector<Mesh> meshes;
377
- uint32_t amountRead = 0 ;
378
- for (auto meshSize : modelSizes)
385
+ if (materials.count (name))
379
386
{
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;
389
390
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);
391
396
}
392
397
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);
396
403
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;
398
420
}
399
421
400
422
void reset ()
@@ -413,11 +435,16 @@ namespace renderer
413
435
bgfx::setTransform (&transformation);
414
436
bgfx::setVertexBuffer (0 , mesh.vbh );
415
437
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 );
421
448
}
422
449
});
423
450
0 commit comments