Review examples

This commit is contained in:
raysan5 2021-10-14 13:37:22 +02:00
parent f9c8e31ed7
commit ef858b0dbb
3 changed files with 50 additions and 70 deletions

View file

@ -40,8 +40,8 @@ int main(void)
camera.fovy = 45.0f; // Camera field-of-view Y camera.fovy = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera mode type camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
// Load some models
Model model[MAX_MODELS] = { 0 }; Model model[MAX_MODELS] = { 0 };
model[0] = LoadModel("resources/models/gltf/raylib_32x32.glb"); model[0] = LoadModel("resources/models/gltf/raylib_32x32.glb");
model[1] = LoadModel("resources/models/gltf/rigged_figure.glb"); model[1] = LoadModel("resources/models/gltf/rigged_figure.glb");
model[2] = LoadModel("resources/models/gltf/GearboxAssy.glb"); model[2] = LoadModel("resources/models/gltf/GearboxAssy.glb");
@ -65,7 +65,7 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateCamera(&camera); UpdateCamera(&camera); // Update our camera with inputs
if (IsKeyReleased(KEY_RIGHT)) if (IsKeyReleased(KEY_RIGHT))
{ {
@ -78,6 +78,7 @@ int main(void)
currentModel--; currentModel--;
if (currentModel < 0) currentModel = MAX_MODELS - 1; if (currentModel < 0) currentModel = MAX_MODELS - 1;
} }
//----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -87,8 +88,7 @@ int main(void)
BeginMode3D(camera); BeginMode3D(camera);
DrawModelEx(model[currentModel], position, (Vector3){ 0.0f, 1.0f, 0.0f }, 180.0f, (Vector3){ 2.0f, 2.0f, 2.0f }, WHITE); DrawModel(model[currentModel], position, 1.0f, WHITE);
DrawGrid(10, 1.0f); // Draw a grid DrawGrid(10, 1.0f); // Draw a grid
EndMode3D(); EndMode3D();

View file

@ -12,22 +12,11 @@
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include "raymath.h"
#include <string.h> #include "raymath.h" // Required for: MatrixTranslate()
// VOX Files to load and view
#define NUM_VOX_FILES 3 #define NUM_VOX_FILES 3
const char* szVoxFiles[] = {
"resources/models/vox/chr_knight.vox",
"resources/models/vox/chr_sword.vox",
"resources/models/vox/monu9.vox"
};
int main(void) int main(void)
{ {
// Initialization // Initialization
@ -35,62 +24,62 @@ int main(void)
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
const char *voxFileNames[] = {
"resources/models/vox/chr_knight.vox",
"resources/models/vox/chr_sword.vox",
"resources/models/vox/monu9.vox"
};
InitWindow(screenWidth, screenHeight, "raylib [models] example - magicavoxel loading"); InitWindow(screenWidth, screenHeight, "raylib [models] example - magicavoxel loading");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
// Load MagicaVoxel files // Load MagicaVoxel files
Model models[NUM_VOX_FILES] = { 0 }; Model models[NUM_VOX_FILES] = { 0 };
for (int i = 0; i < NUM_VOX_FILES; i++) for (int i = 0; i < NUM_VOX_FILES; i++)
{ {
// Load MagicaVoxel File and build model // Load VOX file and measure time
double t0, t1; double t0 = GetTime()*1000.0;
t0 = GetTime() * 1000.0; models[i] = LoadModel(voxFileNames[i]);
double t1 = GetTime()*1000.0;
models[i] = LoadModel(szVoxFiles[i]); TraceLog(LOG_WARNING, TextFormat("[%s] File loaded in %.3f ms", voxFileNames[i], t1 - t0));
t1 = GetTime() * 1000.0; // Compute model translation matrix to center model on draw position (0, 0 , 0)
//TraceLog(LOG_INFO, TextFormat("Vox <%s> loaded in %f ms", GetFileName(szVoxFiles[i]), t1 - t0));
// Compute model's center matrix
BoundingBox bb = GetModelBoundingBox(models[i]); BoundingBox bb = GetModelBoundingBox(models[i]);
Vector3 center; Vector3 center = { 0 };
center.x = bb.min.x + (((bb.max.x - bb.min.x) / 2)); center.x = bb.min.x + (((bb.max.x - bb.min.x)/2));
center.z = bb.min.z + (((bb.max.z - bb.min.z) / 2)); center.z = bb.min.z + (((bb.max.z - bb.min.z)/2));
Matrix matP = MatrixTranslate(-center.x, 0, -center.z); Matrix matTranslate = MatrixTranslate(-center.x, 0, -center.z);
models[i].transform = matP; models[i].transform = matTranslate;
} }
// Define the camera to look into our 3d world
Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
// Model drawing position
Vector3 position = { 0.0f, 0.0f, 0.0f };
int currentModel = 0; int currentModel = 0;
SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Main game loop // Main game loop
//--------------------------------------------------------------------------------------
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
//--------------------------------------------------------------------------------------
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateCamera(&camera); // Update internal camera and our camera UpdateCamera(&camera); // Update our camera to orbit
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) // Cycle between models on mouse click
{ if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1)%NUM_VOX_FILES;
currentModel = (currentModel + 1) % NUM_VOX_FILES; // Cycle between models
}
// Cycle between models on key pressed
if (IsKeyPressed(KEY_RIGHT)) if (IsKeyPressed(KEY_RIGHT))
{ {
currentModel++; currentModel++;
@ -101,34 +90,27 @@ int main(void)
currentModel--; currentModel--;
if (currentModel < 0) currentModel = NUM_VOX_FILES - 1; if (currentModel < 0) currentModel = NUM_VOX_FILES - 1;
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
//Display model // Draw 3D model
BeginMode3D(camera); BeginMode3D(camera);
Vector3 rotAxis = { 1,0,0 }; DrawModel(models[currentModel], (Vector3){ 0, 0, 0 }, 1.0f, WHITE);
Vector3 scale = { 1,1,1 };
DrawModelEx(models[currentModel], position, rotAxis, 0, scale, WHITE);
//DrawModelWiresEx(models[currentModel], position, rotAxis, -90.0f, scale, BLACK);
DrawGrid(10, 1.0); DrawGrid(10, 1.0);
EndMode3D(); EndMode3D();
//Display debug infos // Display info
DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f)); DrawRectangle(10, 400, 310, 30, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f)); DrawRectangleLines(10, 400, 310, 30, Fade(DARKBLUE, 0.5f));
DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE); DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
DrawText(TextFormat("File: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
DrawText(GetFileName(szVoxFiles[currentModel]), 100, 10, 20, DARKBLUE);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -136,7 +118,6 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Unload models data (GPU VRAM) // Unload models data (GPU VRAM)
for (int i = 0; i < NUM_VOX_FILES; i++) UnloadModel(models[i]); for (int i = 0; i < NUM_VOX_FILES; i++) UnloadModel(models[i]);

View file

@ -79,7 +79,6 @@ int main(void)
model.transform = MatrixRotateXYZ((Vector3){ DEG2RAD*pitch, DEG2RAD*yaw, DEG2RAD*roll }); model.transform = MatrixRotateXYZ((Vector3){ DEG2RAD*pitch, DEG2RAD*yaw, DEG2RAD*roll });
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();