mirror of
				https://github.com/raysan5/raylib.git
				synced 2025-10-26 12:27:01 +00:00 
			
		
		
		
	REVIEWED: example: shaders_mesh_instancing
Simplified example
This commit is contained in:
		| @@ -11,7 +11,7 @@ | |||||||
| * | * | ||||||
| *   LICENSE: zlib/libpng | *   LICENSE: zlib/libpng | ||||||
| * | * | ||||||
| *   Copyright (c) 2017-2020 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5) | *   Copyright (c) 2017-2022 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5) | ||||||
| * | * | ||||||
| *   This software is provided "as-is", without any express or implied warranty. In no event | *   This software is provided "as-is", without any express or implied warranty. In no event | ||||||
| *   will the authors be held liable for any damages arising from the use of this software. | *   will the authors be held liable for any damages arising from the use of this software. | ||||||
| @@ -45,22 +45,22 @@ | |||||||
| // Light data | // Light data | ||||||
| typedef struct {    | typedef struct {    | ||||||
|     int type; |     int type; | ||||||
|  |     bool enabled; | ||||||
|     Vector3 position; |     Vector3 position; | ||||||
|     Vector3 target; |     Vector3 target; | ||||||
|     Color color; |     Color color; | ||||||
|     bool enabled; |  | ||||||
|      |      | ||||||
|     // Shader locations |     // Shader locations | ||||||
|     int enabledLoc; |     int enabledLoc; | ||||||
|     int typeLoc; |     int typeLoc; | ||||||
|     int posLoc; |     int positionLoc; | ||||||
|     int targetLoc; |     int targetLoc; | ||||||
|     int colorLoc; |     int colorLoc; | ||||||
| } Light; | } Light; | ||||||
|  |  | ||||||
| // Light type | // Light type | ||||||
| typedef enum { | typedef enum { | ||||||
|     LIGHT_DIRECTIONAL, |     LIGHT_DIRECTIONAL = 0, | ||||||
|     LIGHT_POINT |     LIGHT_POINT | ||||||
| } LightType; | } LightType; | ||||||
|  |  | ||||||
| @@ -128,27 +128,12 @@ Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shade | |||||||
|         light.target = target; |         light.target = target; | ||||||
|         light.color = color; |         light.color = color; | ||||||
|  |  | ||||||
|         // TODO: Below code doesn't look good to me,  |         // NOTE: Lighting shader naming must be the provided ones | ||||||
|         // it assumes a specific shader naming and structure |         light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount)); | ||||||
|         // Probably this implementation could be improved |         light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount)); | ||||||
|         char enabledName[32] = "lights[x].enabled\0"; |         light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount)); | ||||||
|         char typeName[32] = "lights[x].type\0"; |         light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount)); | ||||||
|         char posName[32] = "lights[x].position\0"; |         light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount)); | ||||||
|         char targetName[32] = "lights[x].target\0"; |  | ||||||
|         char colorName[32] = "lights[x].color\0"; |  | ||||||
|          |  | ||||||
|         // Set location name [x] depending on lights count |  | ||||||
|         enabledName[7] = '0' + lightsCount; |  | ||||||
|         typeName[7] = '0' + lightsCount; |  | ||||||
|         posName[7] = '0' + lightsCount; |  | ||||||
|         targetName[7] = '0' + lightsCount; |  | ||||||
|         colorName[7] = '0' + lightsCount; |  | ||||||
|  |  | ||||||
|         light.enabledLoc = GetShaderLocation(shader, enabledName); |  | ||||||
|         light.typeLoc = GetShaderLocation(shader, typeName); |  | ||||||
|         light.posLoc = GetShaderLocation(shader, posName); |  | ||||||
|         light.targetLoc = GetShaderLocation(shader, targetName); |  | ||||||
|         light.colorLoc = GetShaderLocation(shader, colorName); |  | ||||||
|  |  | ||||||
|         UpdateLightValues(shader, light); |         UpdateLightValues(shader, light); | ||||||
|          |          | ||||||
| @@ -168,7 +153,7 @@ void UpdateLightValues(Shader shader, Light light) | |||||||
|  |  | ||||||
|     // Send to shader light position values |     // Send to shader light position values | ||||||
|     float position[3] = { light.position.x, light.position.y, light.position.z }; |     float position[3] = { light.position.x, light.position.y, light.position.z }; | ||||||
|     SetShaderValue(shader, light.posLoc, position, SHADER_UNIFORM_VEC3); |     SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3); | ||||||
|  |  | ||||||
|     // Send to shader light target position values |     // Send to shader light target position values | ||||||
|     float target[3] = { light.target.x, light.target.y, light.target.z }; |     float target[3] = { light.target.x, light.target.y, light.target.z }; | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ | |||||||
| * | * | ||||||
| *   LICENSE: zlib/libpng | *   LICENSE: zlib/libpng | ||||||
| * | * | ||||||
| *   Copyright (c) 2017-2020 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5) | *   Copyright (c) 2017-2022 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5) | ||||||
| * | * | ||||||
| *   This software is provided "as-is", without any express or implied warranty. In no event | *   This software is provided "as-is", without any express or implied warranty. In no event | ||||||
| *   will the authors be held liable for any damages arising from the use of this software. | *   will the authors be held liable for any damages arising from the use of this software. | ||||||
| @@ -45,22 +45,22 @@ | |||||||
| // Light data | // Light data | ||||||
| typedef struct {    | typedef struct {    | ||||||
|     int type; |     int type; | ||||||
|  |     bool enabled; | ||||||
|     Vector3 position; |     Vector3 position; | ||||||
|     Vector3 target; |     Vector3 target; | ||||||
|     Color color; |     Color color; | ||||||
|     bool enabled; |  | ||||||
|      |      | ||||||
|     // Shader locations |     // Shader locations | ||||||
|     int enabledLoc; |     int enabledLoc; | ||||||
|     int typeLoc; |     int typeLoc; | ||||||
|     int posLoc; |     int positionLoc; | ||||||
|     int targetLoc; |     int targetLoc; | ||||||
|     int colorLoc; |     int colorLoc; | ||||||
| } Light; | } Light; | ||||||
|  |  | ||||||
| // Light type | // Light type | ||||||
| typedef enum { | typedef enum { | ||||||
|     LIGHT_DIRECTIONAL, |     LIGHT_DIRECTIONAL = 0, | ||||||
|     LIGHT_POINT |     LIGHT_POINT | ||||||
| } LightType; | } LightType; | ||||||
|  |  | ||||||
| @@ -128,27 +128,12 @@ Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shade | |||||||
|         light.target = target; |         light.target = target; | ||||||
|         light.color = color; |         light.color = color; | ||||||
|  |  | ||||||
|         // TODO: Below code doesn't look good to me,  |         // NOTE: Lighting shader naming must be the provided ones | ||||||
|         // it assumes a specific shader naming and structure |         light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount)); | ||||||
|         // Probably this implementation could be improved |         light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount)); | ||||||
|         char enabledName[32] = "lights[x].enabled\0"; |         light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount)); | ||||||
|         char typeName[32] = "lights[x].type\0"; |         light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount)); | ||||||
|         char posName[32] = "lights[x].position\0"; |         light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount)); | ||||||
|         char targetName[32] = "lights[x].target\0"; |  | ||||||
|         char colorName[32] = "lights[x].color\0"; |  | ||||||
|          |  | ||||||
|         // Set location name [x] depending on lights count |  | ||||||
|         enabledName[7] = '0' + lightsCount; |  | ||||||
|         typeName[7] = '0' + lightsCount; |  | ||||||
|         posName[7] = '0' + lightsCount; |  | ||||||
|         targetName[7] = '0' + lightsCount; |  | ||||||
|         colorName[7] = '0' + lightsCount; |  | ||||||
|  |  | ||||||
|         light.enabledLoc = GetShaderLocation(shader, enabledName); |  | ||||||
|         light.typeLoc = GetShaderLocation(shader, typeName); |  | ||||||
|         light.posLoc = GetShaderLocation(shader, posName); |  | ||||||
|         light.targetLoc = GetShaderLocation(shader, targetName); |  | ||||||
|         light.colorLoc = GetShaderLocation(shader, colorName); |  | ||||||
|  |  | ||||||
|         UpdateLightValues(shader, light); |         UpdateLightValues(shader, light); | ||||||
|          |          | ||||||
| @@ -168,7 +153,7 @@ void UpdateLightValues(Shader shader, Light light) | |||||||
|  |  | ||||||
|     // Send to shader light position values |     // Send to shader light position values | ||||||
|     float position[3] = { light.position.x, light.position.y, light.position.z }; |     float position[3] = { light.position.x, light.position.y, light.position.z }; | ||||||
|     SetShaderValue(shader, light.posLoc, position, SHADER_UNIFORM_VEC3); |     SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3); | ||||||
|  |  | ||||||
|     // Send to shader light target position values |     // Send to shader light target position values | ||||||
|     float target[3] = { light.target.x, light.target.y, light.target.z }; |     float target[3] = { light.target.x, light.target.y, light.target.z }; | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ | |||||||
| * | * | ||||||
| *   Example contributed by @seanpringle and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5) | *   Example contributed by @seanpringle and reviewed by Max (@moliad) and Ramon Santamaria (@raysan5) | ||||||
| * | * | ||||||
| *   Copyright (c) 2020-2021 @seanpringle, Max (@moliad) and Ramon Santamaria (@raysan5) | *   Copyright (c) 2020-2022 @seanpringle, Max (@moliad) and Ramon Santamaria (@raysan5) | ||||||
| * | * | ||||||
| ********************************************************************************************/ | ********************************************************************************************/ | ||||||
|  |  | ||||||
| @@ -18,8 +18,8 @@ | |||||||
| #define RLIGHTS_IMPLEMENTATION | #define RLIGHTS_IMPLEMENTATION | ||||||
| #include "rlights.h" | #include "rlights.h" | ||||||
|  |  | ||||||
| #include <stdlib.h> | #include <stdlib.h>         // Required for: calloc(), free() | ||||||
| #include <math.h> | #include <math.h>           // Required for:  | ||||||
|  |  | ||||||
| #if defined(PLATFORM_DESKTOP) | #if defined(PLATFORM_DESKTOP) | ||||||
|     #define GLSL_VERSION            330 |     #define GLSL_VERSION            330 | ||||||
| @@ -27,7 +27,7 @@ | |||||||
|     #define GLSL_VERSION            100 |     #define GLSL_VERSION            100 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #define MAX_INSTANCES  10000 | #define MAX_INSTANCES  8000 | ||||||
|  |  | ||||||
| //------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------ | ||||||
| // Program main entry point | // Program main entry point | ||||||
| @@ -38,18 +38,9 @@ int main(void) | |||||||
|     //-------------------------------------------------------------------------------------- |     //-------------------------------------------------------------------------------------- | ||||||
|     const int screenWidth = 800; |     const int screenWidth = 800; | ||||||
|     const int screenHeight = 450; |     const int screenHeight = 450; | ||||||
|     const int fps = 60; |  | ||||||
|  |  | ||||||
|     SetConfigFlags(FLAG_MSAA_4X_HINT);  // Enable Multi Sampling Anti Aliasing 4x (if available) |  | ||||||
|     InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing"); |     InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing"); | ||||||
|  |  | ||||||
|     int speed = 30;                 // Speed of jump animation |  | ||||||
|     int groups = 2;                 // Count of separate groups jumping around |  | ||||||
|     float amp = 10;                 // Maximum amplitude of jump |  | ||||||
|     float variance = 0.8f;          // Global variance in jump height |  | ||||||
|     float loop = 0.0f;              // Individual cube's computed loop timer |  | ||||||
|     float x = 0.0f, y = 0.0f, z = 0.0f; // Used for various 3D coordinate & vector ops |  | ||||||
|  |  | ||||||
|     // Define the camera to look into our 3d world |     // Define the camera to look into our 3d world | ||||||
|     Camera camera = { 0 }; |     Camera camera = { 0 }; | ||||||
|     camera.position = (Vector3){ -125.0f, 125.0f, -125.0f }; |     camera.position = (Vector3){ -125.0f, 125.0f, -125.0f }; | ||||||
| @@ -58,113 +49,64 @@ int main(void) | |||||||
|     camera.fovy = 45.0f; |     camera.fovy = 45.0f; | ||||||
|     camera.projection = CAMERA_PERSPECTIVE; |     camera.projection = CAMERA_PERSPECTIVE; | ||||||
|  |  | ||||||
|  |     // Define mesh to be instanced | ||||||
|     Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); |     Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); | ||||||
|  |  | ||||||
|     Matrix *rotations = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix));    // Rotation state of instances |     // Define transforms to be uploaded to GPU for instances | ||||||
|     Matrix *rotationsInc = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix)); // Per-frame rotation animation of instances |     Matrix *transforms = (Matrix *)RL_CALLOC(MAX_INSTANCES, sizeof(Matrix));   // Pre-multiplied transformations passed to rlgl | ||||||
|     Matrix *translations = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix)); // Locations of instances |  | ||||||
|  |  | ||||||
|     // Scatter random cubes around |     // Translate and rotate cubes randomly | ||||||
|     for (int i = 0; i < MAX_INSTANCES; i++) |     for (int i = 0; i < MAX_INSTANCES; i++) | ||||||
|     { |     { | ||||||
|         x = (float)GetRandomValue(-50, 50); |         Matrix translation = MatrixTranslate((float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50), (float)GetRandomValue(-50, 50)); | ||||||
|         y = (float)GetRandomValue(-50, 50); |         Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) }); | ||||||
|         z = (float)GetRandomValue(-50, 50); |  | ||||||
|         translations[i] = MatrixTranslate(x, y, z); |  | ||||||
|  |  | ||||||
|         x = (float)GetRandomValue(0, 360); |  | ||||||
|         y = (float)GetRandomValue(0, 360); |  | ||||||
|         z = (float)GetRandomValue(0, 360); |  | ||||||
|         Vector3 axis = Vector3Normalize((Vector3){ x, y, z }); |  | ||||||
|         float angle = (float)GetRandomValue(0, 10)*DEG2RAD; |         float angle = (float)GetRandomValue(0, 10)*DEG2RAD; | ||||||
|  |         Matrix rotation = MatrixRotate(axis, angle); | ||||||
|          |          | ||||||
|         rotationsInc[i] = MatrixRotate(axis, angle); |         transforms[i] = MatrixMultiply(rotation, translation); | ||||||
|         rotations[i] = MatrixIdentity(); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     Matrix *transforms = RL_MALLOC(MAX_INSTANCES*sizeof(Matrix));   // Pre-multiplied transformations passed to rlgl |     // Load lighting shader | ||||||
|  |     Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting_instancing.vs", GLSL_VERSION), | ||||||
|     Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/base_lighting_instanced.vs", GLSL_VERSION), |  | ||||||
|                                TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION)); |                                TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION)); | ||||||
|  |     // Get shader locations | ||||||
|     // Get some shader loactions |  | ||||||
|     shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp"); |     shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp"); | ||||||
|     shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); |     shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); | ||||||
|     shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform"); |     shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform"); | ||||||
|  |  | ||||||
|     // Ambient light level |     // Set shader value: ambient light level | ||||||
|     int ambientLoc = GetShaderLocation(shader, "ambient"); |     int ambientLoc = GetShaderLocation(shader, "ambient"); | ||||||
|     SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4); |     SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4); | ||||||
|  |  | ||||||
|  |     // Create one light | ||||||
|     CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader); |     CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 50.0f, 50.0f, 0.0f }, Vector3Zero(), WHITE, shader); | ||||||
|  |  | ||||||
|     // NOTE: We are assigning the intancing shader to material.shader |     // NOTE: We are assigning the intancing shader to material.shader | ||||||
|     // to be used on mesh drawing with DrawMeshInstanced() |     // to be used on mesh drawing with DrawMeshInstanced() | ||||||
|     Material material = LoadMaterialDefault(); |     Material matInstances = LoadMaterialDefault(); | ||||||
|     material.shader = shader; |     matInstances.shader = shader; | ||||||
|     material.maps[MATERIAL_MAP_DIFFUSE].color = RED; |     matInstances.maps[MATERIAL_MAP_DIFFUSE].color = RED; | ||||||
|  |  | ||||||
|     SetCameraMode(camera, CAMERA_ORBITAL);  // Set an orbital camera mode |     // Create a defult material with default internal shader for non-instanced mesh drawing | ||||||
|  |     Material matDefault = LoadMaterialDefault(); | ||||||
|  |     matDefault.maps[MATERIAL_MAP_DIFFUSE].color = BLUE; | ||||||
|  |  | ||||||
|     int textPositionY = 300; |     // Set an orbital camera mode | ||||||
|     int framesCounter = 0;                  // Simple frames counter to manage animation |     SetCameraMode(camera, CAMERA_ORBITAL);   | ||||||
|  |  | ||||||
|     SetTargetFPS(fps);                      // 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 | ||||||
|         //---------------------------------------------------------------------------------- |         //---------------------------------------------------------------------------------- | ||||||
|         textPositionY = 300; |         UpdateCamera(&camera); | ||||||
|         framesCounter++; |  | ||||||
|  |  | ||||||
|         if (IsKeyDown(KEY_UP)) amp += 0.5f; |  | ||||||
|         if (IsKeyDown(KEY_DOWN)) amp = (amp <= 1)? 1.0f : (amp - 1.0f); |  | ||||||
|         if (IsKeyDown(KEY_LEFT))  variance = (variance <= 0.0f)? 0.0f : (variance - 0.01f); |  | ||||||
|         if (IsKeyDown(KEY_RIGHT)) variance = (variance >= 1.0f)? 1.0f : (variance + 0.01f); |  | ||||||
|         if (IsKeyDown(KEY_ONE)) groups = 1; |  | ||||||
|         if (IsKeyDown(KEY_TWO)) groups = 2; |  | ||||||
|         if (IsKeyDown(KEY_THREE)) groups = 3; |  | ||||||
|         if (IsKeyDown(KEY_FOUR)) groups = 4; |  | ||||||
|         if (IsKeyDown(KEY_FIVE)) groups = 5; |  | ||||||
|         if (IsKeyDown(KEY_SIX)) groups = 6; |  | ||||||
|         if (IsKeyDown(KEY_SEVEN)) groups = 7; |  | ||||||
|         if (IsKeyDown(KEY_EIGHT)) groups = 8; |  | ||||||
|         if (IsKeyDown(KEY_NINE)) groups = 9; |  | ||||||
|         if (IsKeyDown(KEY_W)) { groups = 7; amp = 25; speed = 18; variance = 0.70f; } |  | ||||||
|  |  | ||||||
|         if (IsKeyDown(KEY_EQUAL)) speed = (speed <= (fps*0.25f))? (int)(fps*0.25f) : (int)(speed*0.95f); |  | ||||||
|         if (IsKeyDown(KEY_KP_ADD)) speed = (speed <= (fps*0.25f))? (int)(fps*0.25f) : (int)(speed*0.95f); |  | ||||||
|  |  | ||||||
|         if (IsKeyDown(KEY_MINUS)) speed = (int)fmaxf(speed*1.02f, speed + 1); |  | ||||||
|         if (IsKeyDown(KEY_KP_SUBTRACT)) speed = (int)fmaxf(speed*1.02f, speed + 1); |  | ||||||
|  |  | ||||||
|         // Update the light shader with the camera view position |         // Update the light shader with the camera view position | ||||||
|         float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; |         float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; | ||||||
|         SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3); |         SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3); | ||||||
|  |  | ||||||
|         // Apply per-instance transformations |  | ||||||
|         for (int i = 0; i < MAX_INSTANCES; i++) |  | ||||||
|         { |  | ||||||
|             rotations[i] = MatrixMultiply(rotations[i], rotationsInc[i]); |  | ||||||
|             transforms[i] = MatrixMultiply(rotations[i], translations[i]); |  | ||||||
|  |  | ||||||
|             // Get the animation cycle's framesCounter for this instance |  | ||||||
|             loop = (float)((framesCounter + (int)(((float)(i%groups)/groups)*speed))%speed)/speed; |  | ||||||
|  |  | ||||||
|             // Calculate the y according to loop cycle |  | ||||||
|             y = (sinf(loop*PI*2))*amp*((1 - variance) + (variance*(float)(i%(groups*10))/(groups*10))); |  | ||||||
|  |  | ||||||
|             // Clamp to floor |  | ||||||
|             y = (y < 0)? 0.0f : y; |  | ||||||
|  |  | ||||||
|             transforms[i] = MatrixMultiply(transforms[i], MatrixTranslate(0.0f, y, 0.0f)); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         UpdateCamera(&camera); |  | ||||||
|         //---------------------------------------------------------------------------------- |         //---------------------------------------------------------------------------------- | ||||||
|  |  | ||||||
|         // Draw |         // Draw | ||||||
| @@ -174,41 +116,20 @@ int main(void) | |||||||
|             ClearBackground(RAYWHITE); |             ClearBackground(RAYWHITE); | ||||||
|  |  | ||||||
|             BeginMode3D(camera); |             BeginMode3D(camera); | ||||||
|                 //DrawMesh(cube, material, MatrixIdentity()); |  | ||||||
|                 DrawMeshInstanced(cube, material, transforms, MAX_INSTANCES); |                 // Draw cube mesh with default material (BLUE) | ||||||
|  |                 DrawMesh(cube, matDefault, MatrixTranslate(-10.0f, 0.0f, 0.0f)); | ||||||
|  |  | ||||||
|  |                 // Draw meshes instanced using material containing instancing shader (RED + lighting), | ||||||
|  |                 // transforms[] for the instances should be provided, they are dynamically | ||||||
|  |                 // updated in GPU every frame, so we can animate the different mesh instances | ||||||
|  |                 DrawMeshInstanced(cube, matInstances, transforms, MAX_INSTANCES); | ||||||
|  |  | ||||||
|  |                 // Draw cube mesh with default material (BLUE) | ||||||
|  |                 DrawMesh(cube, matDefault, MatrixTranslate(10.0f, 0.0f, 0.0f)); | ||||||
|  |  | ||||||
|             EndMode3D(); |             EndMode3D(); | ||||||
|  |  | ||||||
|             DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, MAROON); |  | ||||||
|             DrawText("PRESS KEYS:", 10, textPositionY, 20, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("1 - 9", 10, textPositionY += 25, 10, BLACK); |  | ||||||
|             DrawText(": Number of groups", 50, textPositionY , 10, BLACK); |  | ||||||
|             DrawText(TextFormat(": %d", groups), 160, textPositionY , 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("UP", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": increase amplitude", 50, textPositionY, 10, BLACK); |  | ||||||
|             DrawText(TextFormat(": %.2f", amp), 160, textPositionY , 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("DOWN", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": decrease amplitude", 50, textPositionY, 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("LEFT", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": decrease variance", 50, textPositionY, 10, BLACK); |  | ||||||
|             DrawText(TextFormat(": %.2f", variance), 160, textPositionY , 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("RIGHT", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": increase variance", 50, textPositionY, 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("+/=", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": increase speed", 50, textPositionY, 10, BLACK); |  | ||||||
|             DrawText(TextFormat(": %d = %f loops/sec", speed, ((float)fps / speed)), 160, textPositionY , 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("-", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": decrease speed", 50, textPositionY, 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawText("W", 10, textPositionY += 15, 10, BLACK); |  | ||||||
|             DrawText(": Wild setup!", 50, textPositionY, 10, BLACK); |  | ||||||
|  |  | ||||||
|             DrawFPS(10, 10); |             DrawFPS(10, 10); | ||||||
|  |  | ||||||
|         EndDrawing(); |         EndDrawing(); | ||||||
| @@ -217,11 +138,7 @@ int main(void) | |||||||
|  |  | ||||||
|     // De-Initialization |     // De-Initialization | ||||||
|     //-------------------------------------------------------------------------------------- |     //-------------------------------------------------------------------------------------- | ||||||
|     // Free allocated matrices |     RL_FREE(transforms);    // Free transforms | ||||||
|     RL_FREE(rotations); |  | ||||||
|     RL_FREE(rotationsInc); |  | ||||||
|     RL_FREE(translations); |  | ||||||
|     RL_FREE(transforms); |  | ||||||
|  |  | ||||||
|     CloseWindow();          // Close window and OpenGL context |     CloseWindow();          // Close window and OpenGL context | ||||||
|     //-------------------------------------------------------------------------------------- |     //-------------------------------------------------------------------------------------- | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Ray
					Ray