diff options
Diffstat (limited to 'examples/web/models/models_material_pbr.c')
| -rw-r--r-- | examples/web/models/models_material_pbr.c | 100 |
1 files changed, 66 insertions, 34 deletions
diff --git a/examples/web/models/models_material_pbr.c b/examples/web/models/models_material_pbr.c index 9f57634..051384a 100644 --- a/examples/web/models/models_material_pbr.c +++ b/examples/web/models/models_material_pbr.c @@ -15,14 +15,37 @@ #define RLIGHTS_IMPLEMENTATION #include "rlights.h" +#if defined(PLATFORM_WEB) + #include <emscripten/emscripten.h> +#endif + #define CUBEMAP_SIZE 512 // Cubemap texture size #define IRRADIANCE_SIZE 32 // Irradiance texture size #define PREFILTERED_SIZE 256 // Prefiltered HDR environment texture size #define BRDF_SIZE 512 // BRDF LUT texture size +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +int screenWidth = 800; +int screenHeight = 450; + +// Define the camera to look into our 3d world +Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; + +Model model; + +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +void UpdateDrawFrame(void); // Update and Draw one frame + // PBR material loading static Material LoadMaterialPBR(Color albedo, float metalness, float roughness); +//---------------------------------------------------------------------------------- +// Main Enry Point +//---------------------------------------------------------------------------------- int main() { // Initialization @@ -33,11 +56,8 @@ int main() SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material"); - // Define the camera to look into our 3d world - Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; - // Load model and PBR material - Model model = LoadModel("resources/pbr/trooper.obj"); + model = LoadModel("resources/pbr/trooper.obj"); model.material = LoadMaterialPBR((Color){ 255, 255, 255, 255 }, 1.0f, 1.0f); // Define lights attributes @@ -51,40 +71,18 @@ int main() SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode - SetTargetFPS(60); // Set our game to run at 60 frames-per-second +#if defined(PLATFORM_WEB) + emscripten_set_main_loop(UpdateDrawFrame, 0, 1); +#else + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- - + // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { - // Update - //---------------------------------------------------------------------------------- - UpdateCamera(&camera); // Update camera - - // Send to material PBR shader camera view position - float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; - SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, 3); - //---------------------------------------------------------------------------------- - - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); - - ClearBackground(RAYWHITE); - - Begin3dMode(camera); - - DrawModel(model, Vector3Zero(), 1.0f, WHITE); - - DrawGrid(10, 1.0f); - - End3dMode(); - - DrawFPS(10, 10); - - EndDrawing(); - //---------------------------------------------------------------------------------- + UpdateDrawFrame(); } +#endif // De-Initialization //-------------------------------------------------------------------------------------- @@ -96,6 +94,40 @@ int main() return 0; } +//---------------------------------------------------------------------------------- +// Module Functions Definition +//---------------------------------------------------------------------------------- +void UpdateDrawFrame(void) +{ + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera); // Update camera + + // Send to material PBR shader camera view position + float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; + SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, 3); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + Begin3dMode(camera); + + DrawModel(model, Vector3Zero(), 1.0f, WHITE); + + DrawGrid(10, 1.0f); + + End3dMode(); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- +} + // Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps) // NOTE: PBR shader is loaded inside this function static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) |
