Created
March 17, 2021 15:48
-
-
Save JeffM2501/08d20cdd931456ad0e52905401cc34af to your computer and use it in GitHub Desktop.
Model merge for raylib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************************* | |
* | |
* raylib [core] example - Basic window | |
* | |
* Welcome to raylib! | |
* | |
* To test examples, just press F6 and execute raylib_compile_execute script | |
* Note that compiled executable is placed in the same folder as .c file | |
* | |
* You can find all basic examples on C:\raylib\raylib\examples folder or | |
* raylib official webpage: www.raylib.com | |
* | |
* Enjoy using raylib. :) | |
* | |
* This example has been created using raylib 1.0 (www.raylib.com) | |
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |
* | |
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5) | |
* | |
********************************************************************************************/ | |
#include "raylib.h" | |
#include "raymath.h" | |
Model MergeModels(Model* models, size_t count) | |
{ | |
Model outModel = { 0 }; | |
// count the meshes and materials in all models | |
for (size_t i = 0; i < count; ++i) | |
{ | |
outModel.meshCount += models[i].meshCount; | |
outModel.materialCount += models[i].materialCount; | |
} | |
outModel.transform = MatrixIdentity(); | |
// allocate buffers for all combined meshes/materials | |
outModel.meshes = MemAlloc(sizeof(Mesh) * outModel.meshCount); | |
outModel.materials = MemAlloc(sizeof(Material) * outModel.materialCount); | |
outModel.meshMaterial = MemAlloc(sizeof(int) * outModel.meshCount); | |
// ingore animations | |
outModel.boneCount = 0; | |
outModel.bones = NULL; | |
outModel.bindPose = NULL; | |
// merge the meshes | |
int meshOffset = 0; | |
int matOffset = 0; | |
for (size_t i = 0; i < count; ++i) | |
{ | |
for (int meshIndex = 0; meshIndex < models[i].meshCount; ++meshIndex) | |
{ | |
outModel.meshes[meshIndex + meshOffset] = models[i].meshes[meshIndex]; | |
outModel.meshMaterial[meshIndex + meshOffset] = models[i].meshMaterial[meshIndex] + matOffset; | |
} | |
for (int matIndex = 0; matIndex < models[i].materialCount; ++matIndex) | |
{ | |
outModel.materials[matIndex + matOffset] = models[i].materials[matIndex]; | |
} | |
// delete memory buffers used by old models | |
MemFree(models[i].meshes); | |
models[i].meshes = NULL; | |
MemFree(models[i].materials); | |
models[i].materials = NULL; | |
MemFree(models[i].meshMaterial); | |
models[i].meshMaterial = NULL; | |
meshOffset += models[i].meshCount; | |
matOffset += models[i].materialCount; | |
} | |
return outModel; | |
} | |
int main(void) | |
{ | |
// Initialization | |
//-------------------------------------------------------------------------------------- | |
const int screenWidth = 800; | |
const int screenHeight = 450; | |
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); | |
Camera cam = { 0 }; | |
cam.fovy = 45; | |
cam.position.z = -25; | |
cam.position.y = 10; | |
cam.up.y = 1; | |
Model models[2]; | |
models[0] = LoadModel("resources/models/well.obj"); | |
models[0].materials[0].maps[MAP_DIFFUSE].texture = LoadTexture("resources/models/well_diffuse.png"); | |
models[1] = LoadModel("resources/models/cube.obj"); | |
models[1].materials[0].maps[MAP_DIFFUSE].texture = LoadTexture("resources/models/cube_diffuse.png"); | |
Model mergedModel = MergeModels(models, 2); | |
SetTargetFPS(60); // Set our game to run at 60 frames-per-second | |
//-------------------------------------------------------------------------------------- | |
// Main game loop | |
while (!WindowShouldClose()) // Detect window close button or ESC key | |
{ | |
// Update | |
//---------------------------------------------------------------------------------- | |
// TODO: Update your variables here | |
//---------------------------------------------------------------------------------- | |
// Draw | |
//---------------------------------------------------------------------------------- | |
BeginDrawing(); | |
ClearBackground(RAYWHITE); | |
BeginMode3D(cam); | |
DrawModel(mergedModel, Vector3Zero(), 1, WHITE); | |
EndMode3D(); | |
EndDrawing(); | |
//---------------------------------------------------------------------------------- | |
} | |
// De-Initialization | |
//-------------------------------------------------------------------------------------- | |
CloseWindow(); // Close window and OpenGL context | |
//-------------------------------------------------------------------------------------- | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment