summaryrefslogtreecommitdiffhomepage
path: root/examples/src/models
diff options
context:
space:
mode:
authorRay San <[email protected]>2017-10-19 14:37:22 +0200
committerRay San <[email protected]>2017-10-19 14:37:22 +0200
commitd4208de8afdc13f46ecec399f86771b249317949 (patch)
treec5c276f9e3255fcb2d4687179f61fef9cf9800d7 /examples/src/models
parent8991e0d5e068a6ad72b4ddf9a1b4b340e8c54f36 (diff)
downloadraylib.com-d4208de8afdc13f46ecec399f86771b249317949.tar.gz
raylib.com-d4208de8afdc13f46ecec399f86771b249317949.zip
New examples uploaded...
Diffstat (limited to 'examples/src/models')
-rw-r--r--examples/src/models/models_material_pbr.c195
-rw-r--r--examples/src/models/models_mesh_generation.c113
-rw-r--r--examples/src/models/models_skybox.c85
-rw-r--r--examples/src/models/models_yaw_pitch_roll.c198
4 files changed, 591 insertions, 0 deletions
diff --git a/examples/src/models/models_material_pbr.c b/examples/src/models/models_material_pbr.c
new file mode 100644
index 0000000..9f57634
--- /dev/null
+++ b/examples/src/models/models_material_pbr.c
@@ -0,0 +1,195 @@
+/*******************************************************************************************
+*
+* raylib [models] example - PBR material
+*
+* This example has been created using raylib 1.8 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2017 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "raymath.h"
+
+#define RLIGHTS_IMPLEMENTATION
+#include "rlights.h"
+
+#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
+
+// PBR material loading
+static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ 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.material = LoadMaterialPBR((Color){ 255, 255, 255, 255 }, 1.0f, 1.0f);
+
+ // Define lights attributes
+ // NOTE: Shader is passed to every light on creation to define shader bindings internally
+ Light lights[MAX_LIGHTS] = {
+ CreateLight(LIGHT_POINT, (Vector3){ LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 255, 0, 0, 255 }, model.material.shader),
+ CreateLight(LIGHT_POINT, (Vector3){ 0.0f, LIGHT_HEIGHT, LIGHT_DISTANCE }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 0, 255, 0, 255 }, model.material.shader),
+ CreateLight(LIGHT_POINT, (Vector3){ -LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 0, 0, 255, 255 }, model.material.shader),
+ CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 0.0f, LIGHT_HEIGHT*2.0f, -LIGHT_DISTANCE }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 255, 0, 255, 255 }, model.material.shader)
+ };
+
+ SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
+
+ 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
+ //----------------------------------------------------------------------------------
+ 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();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ UnloadModel(model); // Unload skybox model
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+// 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)
+{
+ Material mat = { 0 }; // NOTE: All maps textures are set to { 0 }
+
+ #define PATH_PBR_VS "resources/shaders/pbr.vs" // Path to physically based rendering vertex shader
+ #define PATH_PBR_FS "resources/shaders/pbr.fs" // Path to physically based rendering fragment shader
+
+ mat.shader = LoadShader(PATH_PBR_VS, PATH_PBR_FS);
+
+ // Get required locations points for PBR material
+ // NOTE: Those location names must be available and used in the shader code
+ mat.shader.locs[LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler");
+ mat.shader.locs[LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler");
+ mat.shader.locs[LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler");
+ mat.shader.locs[LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler");
+ mat.shader.locs[LOC_MAP_OCCUSION] = GetShaderLocation(mat.shader, "occlusion.sampler");
+ //mat.shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(mat.shader, "emission.sampler");
+ //mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
+ mat.shader.locs[LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
+ mat.shader.locs[LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
+ mat.shader.locs[LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
+
+ // Set view matrix location
+ mat.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "mMatrix");
+ mat.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
+ mat.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
+
+ // Set PBR standard maps
+ mat.maps[MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
+ mat.maps[MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
+ mat.maps[MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
+ mat.maps[MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
+ mat.maps[MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
+
+ // Set environment maps
+ #define PATH_CUBEMAP_VS "resources/shaders/cubemap.vs" // Path to equirectangular to cubemap vertex shader
+ #define PATH_CUBEMAP_FS "resources/shaders/cubemap.fs" // Path to equirectangular to cubemap fragment shader
+ #define PATH_SKYBOX_VS "resources/shaders/skybox.vs" // Path to skybox vertex shader
+ #define PATH_IRRADIANCE_FS "resources/shaders/irradiance.fs" // Path to irradiance (GI) calculation fragment shader
+ #define PATH_PREFILTER_FS "resources/shaders/prefilter.fs" // Path to reflection prefilter calculation fragment shader
+ #define PATH_BRDF_VS "resources/shaders/brdf.vs" // Path to bidirectional reflectance distribution function vertex shader
+ #define PATH_BRDF_FS "resources/shaders/brdf.fs" // Path to bidirectional reflectance distribution function fragment shader
+
+ Shader shdrCubemap = LoadShader(PATH_CUBEMAP_VS, PATH_CUBEMAP_FS);
+ Shader shdrIrradiance = LoadShader(PATH_SKYBOX_VS, PATH_IRRADIANCE_FS);
+ Shader shdrPrefilter = LoadShader(PATH_SKYBOX_VS, PATH_PREFILTER_FS);
+ Shader shdrBRDF = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS);
+
+ // Setup required shader locations
+ SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1);
+ SetShaderValuei(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, 1);
+ SetShaderValuei(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, 1);
+
+ Texture2D texHDR = LoadTexture("resources/pinetree.hdr");
+ Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE);
+ mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
+ mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
+ mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, cubemap, BRDF_SIZE);
+ UnloadTexture(cubemap);
+ UnloadTexture(texHDR);
+
+ // Unload already used shaders (to create specific textures)
+ UnloadShader(shdrCubemap);
+ UnloadShader(shdrIrradiance);
+ UnloadShader(shdrPrefilter);
+ UnloadShader(shdrBRDF);
+
+ // Set textures filtering for better quality
+ SetTextureFilter(mat.maps[MAP_ALBEDO].texture, FILTER_BILINEAR);
+ SetTextureFilter(mat.maps[MAP_NORMAL].texture, FILTER_BILINEAR);
+ SetTextureFilter(mat.maps[MAP_METALNESS].texture, FILTER_BILINEAR);
+ SetTextureFilter(mat.maps[MAP_ROUGHNESS].texture, FILTER_BILINEAR);
+ SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR);
+
+ // Enable sample usage in shader for assigned textures
+ SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, 1);
+ SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, 1);
+ SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, 1);
+ SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, 1);
+ SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, 1);
+
+ int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
+ SetShaderValuei(mat.shader, renderModeLoc, (int[1]){ 0 }, 1);
+
+ // Set up material properties color
+ mat.maps[MAP_ALBEDO].color = albedo;
+ mat.maps[MAP_NORMAL].color = (Color){ 128, 128, 255, 255 };
+ mat.maps[MAP_METALNESS].value = metalness;
+ mat.maps[MAP_ROUGHNESS].value = roughness;
+ mat.maps[MAP_OCCLUSION].value = 1.0f;
+ mat.maps[MAP_EMISSION].value = 0.5f;
+ mat.maps[MAP_HEIGHT].value = 0.5f;
+
+ return mat;
+} \ No newline at end of file
diff --git a/examples/src/models/models_mesh_generation.c b/examples/src/models/models_mesh_generation.c
new file mode 100644
index 0000000..7222215
--- /dev/null
+++ b/examples/src/models/models_mesh_generation.c
@@ -0,0 +1,113 @@
+/*******************************************************************************************
+*
+* raylib example - procedural mesh generation
+*
+* This example has been created using raylib 1.8 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2017 Ramon Santamaria (Ray San)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+#define NUM_MODELS 7 // We generate 7 parametric 3d shapes
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
+
+ // We generate a checked image for texturing
+ Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
+ Texture2D texture = LoadTextureFromImage(checked);
+ UnloadImage(checked);
+
+ Model models[NUM_MODELS];
+
+ models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5));
+ models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
+ models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
+ models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
+ models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
+ models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
+ models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
+
+ // Set checked texture as default diffuse component for all models material
+ for (int i = 0; i < NUM_MODELS; i++) models[i].material.maps[MAP_DIFFUSE].texture = texture;
+
+ // Define the camera to look into our 3d world
+ Camera camera = {{ 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
+
+ // Model drawing position
+ Vector3 position = { 0.0f, 0.0f, 0.0f };
+
+ int currentModel = 0;
+
+ SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
+
+ 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
+ //----------------------------------------------------------------------------------
+ UpdateCamera(&camera); // Update internal camera and our camera
+
+ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
+ {
+ currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
+ }
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ Begin3dMode(camera);
+
+ DrawModel(models[currentModel], position, 1.0f, WHITE);
+
+ DrawGrid(10, 1.0);
+
+ End3dMode();
+
+ DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
+ DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
+ DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
+
+ switch(currentModel)
+ {
+ case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
+ case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
+ case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
+ case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
+ case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
+ case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
+ case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
+ default: break;
+ }
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+
+ // Unload models data (GPU VRAM)
+ for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+} \ No newline at end of file
diff --git a/examples/src/models/models_skybox.c b/examples/src/models/models_skybox.c
new file mode 100644
index 0000000..46297e4
--- /dev/null
+++ b/examples/src/models/models_skybox.c
@@ -0,0 +1,85 @@
+/*******************************************************************************************
+*
+* raylib [models] example - Skybox loading and drawing
+*
+* This example has been created using raylib 1.8 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2017 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
+
+ // Define the camera to look into our 3d world
+ Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
+
+ // Load skybox model
+ Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
+ Model skybox = LoadModelFromMesh(cube);
+
+ // Load skybox shader and set required locations
+ // NOTE: Some locations are automatically set at shader loading
+ skybox.material.shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs");
+ SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, 1);
+
+ // Load cubemap shader and setup required shader locations
+ Shader shdrCubemap = LoadShader("resources/shaders/cubemap.vs", "resources/shaders/cubemap.fs");
+ SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1);
+
+ Texture2D texHDR = LoadTexture("resources/pinetree.hdr");
+ skybox.material.maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512);
+
+ UnloadShader(shdrCubemap); // Cubemap generation shader not required any more
+
+ SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
+
+ 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
+ //----------------------------------------------------------------------------------
+ UpdateCamera(&camera); // Update camera
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ Begin3dMode(camera);
+
+ DrawModel(skybox, Vector3Zero(), 1.0f, WHITE);
+
+ DrawGrid(10, 1.0f);
+
+ End3dMode();
+
+ DrawFPS(10, 10);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ UnloadModel(skybox); // Unload skybox model
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
diff --git a/examples/src/models/models_yaw_pitch_roll.c b/examples/src/models/models_yaw_pitch_roll.c
new file mode 100644
index 0000000..2bae2bf
--- /dev/null
+++ b/examples/src/models/models_yaw_pitch_roll.c
@@ -0,0 +1,198 @@
+/*******************************************************************************************
+*
+* raylib [models] example - Plane rotations (yaw, pitch, roll)
+*
+* This example has been created using raylib 1.8 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Example based on Berni work on Raspberry Pi:
+* http://forum.raylib.com/index.php?p=/discussion/124/line-versus-triangle-drawing-order
+*
+* Copyright (c) 2017 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "raymath.h"
+
+// Draw angle gauge controls
+void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color);
+
+//----------------------------------------------------------------------------------
+// Main entry point
+//----------------------------------------------------------------------------------
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ const int screenWidth = 800;
+ const int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [models] example - plane rotations (yaw, pitch, roll)");
+
+ Texture2D texAngleGauge = LoadTexture("resources/angle_gauge.png");
+ Texture2D texBackground = LoadTexture("resources/background.png");
+ Texture2D texPitch = LoadTexture("resources/pitch.png");
+ Texture2D texPlane = LoadTexture("resources/plane.png");
+
+ RenderTexture2D framebuffer = LoadRenderTexture(192, 192);
+
+ // Model loading
+ Model model = LoadModel("resources/plane.obj"); // Load OBJ model
+ model.material.maps[MAP_DIFFUSE].texture = LoadTexture("resources/plane_diffuse.png"); // Set map diffuse texture
+
+ GenTextureMipmaps(&model.material.maps[MAP_DIFFUSE].texture);
+
+ Camera camera = { 0 };
+ camera.position = (Vector3){ 0.0f, 60.0f, -120.0f };// Camera position perspective
+ camera.target = (Vector3){ 0.0f, 12.0f, 0.0f }; // Camera looking at point
+ camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
+ camera.fovy = 30.0f; // Camera field-of-view Y
+
+ float pitch = 0.0f;
+ float roll = 0.0f;
+ float yaw = 0.0f;
+
+ SetTargetFPS(60);
+ //--------------------------------------------------------------------------------------
+
+
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+
+ // Plane roll (x-axis) controls
+ if (IsKeyDown(KEY_LEFT)) roll += 1.0f;
+ else if (IsKeyDown(KEY_RIGHT)) roll -= 1.0f;
+ else
+ {
+ if (roll > 0.0f) roll -= 0.5f;
+ else if (roll < 0.0f) roll += 0.5f;
+ }
+
+ // Plane yaw (y-axis) controls
+ if (IsKeyDown(KEY_S)) yaw += 1.0f;
+ else if (IsKeyDown(KEY_A)) yaw -= 1.0f;
+ else
+ {
+ if (yaw > 0.0f) yaw -= 0.5f;
+ else if (yaw < 0.0f) yaw += 0.5f;
+ }
+
+ // Plane pitch (z-axis) controls
+ if (IsKeyDown(KEY_DOWN)) pitch += 0.6f;
+ else if (IsKeyDown(KEY_UP)) pitch -= 0.6f;
+ else
+ {
+ if (pitch > 0.3f) pitch -= 0.3f;
+ else if (pitch < -0.3f) pitch += 0.3f;
+ }
+
+ // Wraps the phase of an angle to fit between -180 and +180 degrees
+ int pitchOffset = pitch;
+ while (pitchOffset > 180) pitchOffset -= 360;
+ while (pitchOffset < -180) pitchOffset += 360;
+ pitchOffset *= 10;
+
+ Matrix transform = MatrixIdentity();
+
+ transform = MatrixMultiply(transform, MatrixRotateZ(DEG2RAD*roll));
+ transform = MatrixMultiply(transform, MatrixRotateX(DEG2RAD*pitch));
+ transform = MatrixMultiply(transform, MatrixRotateY(DEG2RAD*yaw));
+
+ model.transform = transform;
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ // Draw framebuffer texture (Ahrs Display)
+ int centerX = framebuffer.texture.width/2;
+ int centerY = framebuffer.texture.height/2;
+ float scaleFactor = 0.5f;
+
+ BeginTextureMode(framebuffer);
+
+ BeginBlendMode(BLEND_ALPHA);
+
+ DrawTexturePro(texBackground, (Rectangle){0,0,texBackground.width, texBackground.height},
+ (Rectangle){ centerX, centerY, texBackground.width*scaleFactor, texBackground.height*scaleFactor},
+ (Vector2){texBackground.width/2*scaleFactor, texBackground.height/2*scaleFactor + pitchOffset*scaleFactor}, roll, WHITE);
+
+ DrawTexturePro(texPitch, (Rectangle){ 0, 0, texPitch.width, texPitch.height },
+ (Rectangle){ centerX, centerY, texPitch.width*scaleFactor, texPitch.height*scaleFactor },
+ (Vector2){ texPitch.width/2*scaleFactor, texPitch.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
+
+ DrawTexturePro(texPlane, (Rectangle){0,0,texPlane.width, texPlane.height },
+ (Rectangle){ centerX, centerY, texPlane.width*scaleFactor, texPlane.height*scaleFactor },
+ (Vector2){texPlane.width/2*scaleFactor, texPlane.height/2*scaleFactor }, 0, WHITE);
+
+ EndBlendMode();
+
+ EndTextureMode();
+
+ // Draw 3D model (recomended to draw 3D always before 2D)
+ Begin3dMode(camera);
+
+ DrawModel(model, (Vector3){ 0, 6.0f, 0 }, 1.0f, WHITE); // Draw 3d model with texture
+ DrawGrid(10, 10.0f);
+
+ End3dMode();
+
+ // Draw 2D GUI stuff
+ DrawAngleGauge(texAngleGauge, 80, 80, roll, "roll", RED);
+ DrawAngleGauge(texAngleGauge, 190, 80, pitch, "pitch", GREEN);
+ DrawAngleGauge(texAngleGauge, 300, 80, yaw, "yaw", SKYBLUE);
+
+ DrawRectangle(30, 360, 260, 70, Fade(SKYBLUE, 0.5f));
+ DrawRectangleLines(30, 360, 260, 70, Fade(DARKBLUE, 0.5f));
+ DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 370, 10, DARKGRAY);
+ DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 390, 10, DARKGRAY);
+ DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 410, 10, DARKGRAY);
+
+ // Draw framebuffer texture
+ DrawTextureRec(framebuffer.texture, (Rectangle){ 0, 0, framebuffer.texture.width, -framebuffer.texture.height },
+ (Vector2){ screenWidth - framebuffer.texture.width - 20, 20 }, Fade(WHITE, 0.8f));
+
+ DrawRectangleLines(screenWidth - framebuffer.texture.width - 20, 20, framebuffer.texture.width, framebuffer.texture.height, DARKGRAY);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+
+ // Unload all loaded data
+ UnloadModel(model);
+
+ UnloadRenderTexture(framebuffer);
+
+ UnloadTexture(texAngleGauge);
+ UnloadTexture(texBackground);
+ UnloadTexture(texPitch);
+ UnloadTexture(texPlane);
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+// Draw angle gauge controls
+void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color)
+{
+ Rectangle srcRec = { 0, 0, angleGauge.width, angleGauge.height };
+ Rectangle dstRec = { x, y, angleGauge.width, angleGauge.height };
+ Vector2 origin = { angleGauge.width/2, angleGauge.height/2};
+ int textSize = 20;
+
+ DrawTexturePro(angleGauge, srcRec, dstRec, origin, angle, color);
+
+ DrawText(FormatText("%5.1f�", angle), x - MeasureText(FormatText("%5.1f�", angle), textSize) / 2, y + 10, textSize, DARKGRAY);
+ DrawText(title, x - MeasureText(title, textSize) / 2, y + 60, textSize, DARKGRAY);
+} \ No newline at end of file