diff options
| author | Ray <[email protected]> | 2019-05-20 16:40:30 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2019-05-20 16:40:30 +0200 |
| commit | 3d7d174c70b2d00fd879ade64c5085d4ff34d4aa (patch) | |
| tree | 3b690948f186f855aa2ee8bab312b3ca28a56200 /examples/web/shaders/shaders_custom_uniform.c | |
| parent | 0b56b996bd053ec875c229e9793f7806b666839c (diff) | |
| download | raylib.com-3d7d174c70b2d00fd879ade64c5085d4ff34d4aa.tar.gz raylib.com-3d7d174c70b2d00fd879ade64c5085d4ff34d4aa.zip | |
Review and recompile ALL examples
Diffstat (limited to 'examples/web/shaders/shaders_custom_uniform.c')
| -rw-r--r-- | examples/web/shaders/shaders_custom_uniform.c | 82 |
1 files changed, 49 insertions, 33 deletions
diff --git a/examples/web/shaders/shaders_custom_uniform.c b/examples/web/shaders/shaders_custom_uniform.c index 4c3fbe1..77f84bd 100644 --- a/examples/web/shaders/shaders_custom_uniform.c +++ b/examples/web/shaders/shaders_custom_uniform.c @@ -22,25 +22,31 @@ #include <emscripten/emscripten.h> #endif +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- -int screenWidth = 800; -int screenHeight = 450; +const int screenWidth = 800; +const int screenHeight = 450; // Define the camera to look into our 3d world -Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; +Camera camera = { 0 }; -Model dwarf; // OBJ model -Texture2D texture; // Model texture -Shader shader; // Postpro shader +Model model = { 0 }; // Model data +Texture2D texture = { 0 }; // Model texture +Shader shader = { 0 }; // Postpro shader Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position -int swirlCenterLoc; -float swirlCenter[2]; +int swirlCenterLoc = 0; +float swirlCenter[2] = { 0.0f }; -RenderTexture2D target; +RenderTexture2D target = { 0 }; //---------------------------------------------------------------------------------- // Module Functions Declaration @@ -48,7 +54,7 @@ RenderTexture2D target; void UpdateDrawFrame(void); // Update and Draw one frame //---------------------------------------------------------------------------------- -// Main Enry Point +// Program Main Entry Point //---------------------------------------------------------------------------------- int main(void) { @@ -57,23 +63,31 @@ int main(void) SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable"); - dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture - dwarf.material.maps[MAP_DIFFUSE].texture = texture; // Set dwarf model diffuse texture + // Define the camera to look into our 3d world + camera.position = (Vector3){ 8.0f, 8.0f, 8.0f }; + camera.target = (Vector3){ 0.0f, 1.5f, 0.0f }; + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; + camera.fovy = 45.0f; + camera.type = CAMERA_PERSPECTIVE; + + model = LoadModel("resources/models/barracks.obj"); // Load OBJ model + texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture + + // Load postprocessing shader + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + shader = LoadShader(0, FormatText("resources/shaders/glsl%i/swirl.fs", GLSL_VERSION)); - shader = LoadShader("resources/shaders/glsl100/base.vs", - "resources/shaders/glsl100/swirl.fs"); // Load postpro shader - // Get variable (uniform) location on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 swirlCenterLoc = GetShaderLocation(shader, "center"); - + swirlCenter[0] = (float)screenWidth/2; swirlCenter[1] = (float)screenHeight/2; // Create a RenderTexture2D to be used for render to texture target = LoadRenderTexture(screenWidth, screenHeight); - + // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode @@ -82,7 +96,7 @@ int main(void) #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 { @@ -94,7 +108,7 @@ int main(void) //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture - UnloadModel(dwarf); // Unload model + UnloadModel(model); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- @@ -115,9 +129,9 @@ void UpdateDrawFrame(void) swirlCenter[1] = screenHeight - mousePosition.y; // Send new value to the shader to be used on drawing - SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2); + SetShaderValue(shader, swirlCenterLoc, swirlCenter, UNIFORM_VEC2); - UpdateCamera(&camera); // Update internal camera and our camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw @@ -127,27 +141,29 @@ void UpdateDrawFrame(void) ClearBackground(RAYWHITE); BeginTextureMode(target); // Enable drawing to texture + ClearBackground(RAYWHITE); // Clear texture background - Begin3dMode(camera); + BeginMode3D(camera); // Begin 3d mode drawing - DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture + DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid - End3dMode(); - + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode + DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); - - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - + + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + BeginShaderMode(shader); - + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); - + EndShaderMode(); - - DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY); + + // Draw some 2d text over drawn texture + DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY); DrawFPS(10, 10); |
