2017-08-04 14:38:21 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
2017-09-30 01:45:03 +03:00
|
|
|
* raylib [models] example - Plane rotations (yaw, pitch, roll)
|
2017-08-04 14:38:21 +03:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*
|
2019-05-14 16:34:23 +03:00
|
|
|
* Example contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
|
2017-08-04 14:38:21 +03:00
|
|
|
*
|
2019-05-14 16:34:23 +03:00
|
|
|
* Copyright (c) 2017 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
|
2017-08-04 14:38:21 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
#include "raymath.h"
|
|
|
|
|
|
|
|
// Draw angle gauge controls
|
|
|
|
void DrawAngleGauge(Texture2D angleGauge, int x, int y, float angle, char title[], Color color);
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2017-08-04 14:38:21 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
|
|
|
|
2017-09-30 01:45:03 +03:00
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [models] example - plane rotations (yaw, pitch, roll)");
|
2017-08-04 14:38:21 +03:00
|
|
|
|
2018-03-13 19:04:49 +03:00
|
|
|
Texture2D texAngleGauge = LoadTexture("resources/angle_gauge.png");
|
2017-08-04 14:38:21 +03:00
|
|
|
Texture2D texBackground = LoadTexture("resources/background.png");
|
2018-03-13 19:04:49 +03:00
|
|
|
Texture2D texPitch = LoadTexture("resources/pitch.png");
|
2017-08-04 14:38:21 +03:00
|
|
|
Texture2D texPlane = LoadTexture("resources/plane.png");
|
|
|
|
|
|
|
|
RenderTexture2D framebuffer = LoadRenderTexture(192, 192);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
// Model loading
|
2019-03-29 22:22:50 +03:00
|
|
|
Model model = LoadModel("resources/plane.obj"); // Load OBJ model
|
|
|
|
model.materials[0].maps[MAP_DIFFUSE].texture = LoadTexture("resources/plane_diffuse.png"); // Set map diffuse texture
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2019-03-29 22:22:50 +03:00
|
|
|
GenTextureMipmaps(&model.materials[0].maps[MAP_DIFFUSE].texture);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
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
|
2018-04-11 11:13:00 +03:00
|
|
|
camera.type = CAMERA_PERSPECTIVE; // Camera type
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
float pitch = 0.0f;
|
|
|
|
float roll = 0.0f;
|
|
|
|
float yaw = 0.0f;
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2017-08-04 14:38:21 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
// Main game loop
|
2017-08-04 14:38:21 +03:00
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-09-30 01:45:03 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
// 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;
|
|
|
|
|
2019-08-09 18:04:52 +03:00
|
|
|
/* matrix transform done with multiplication to combine rotations
|
2017-08-04 14:38:21 +03:00
|
|
|
Matrix transform = MatrixIdentity();
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
transform = MatrixMultiply(transform, MatrixRotateZ(DEG2RAD*roll));
|
|
|
|
transform = MatrixMultiply(transform, MatrixRotateX(DEG2RAD*pitch));
|
|
|
|
transform = MatrixMultiply(transform, MatrixRotateY(DEG2RAD*yaw));
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
model.transform = transform;
|
2019-08-09 18:04:52 +03:00
|
|
|
*/
|
|
|
|
// matrix created from multiple axes at once
|
|
|
|
model.transform = MatrixRotateXYZ((Vector3){DEG2RAD*pitch,DEG2RAD*yaw,DEG2RAD*roll});
|
2017-08-04 14:38:21 +03:00
|
|
|
|
2019-08-09 18:04:52 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
2017-08-04 14:38:21 +03:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
ClearBackground(RAYWHITE);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
// 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);
|
|
|
|
|
2017-11-05 19:06:50 +03:00
|
|
|
DrawTexturePro(texBackground, (Rectangle){ 0, 0, texBackground.width, texBackground.height },
|
2017-08-04 14:38:21 +03:00
|
|
|
(Rectangle){ centerX, centerY, texBackground.width*scaleFactor, texBackground.height*scaleFactor},
|
2017-11-05 19:06:50 +03:00
|
|
|
(Vector2){ texBackground.width/2*scaleFactor, texBackground.height/2*scaleFactor + pitchOffset*scaleFactor }, roll, WHITE);
|
2017-08-04 14:38:21 +03:00
|
|
|
|
|
|
|
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);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-11-05 19:06:50 +03:00
|
|
|
DrawTexturePro(texPlane, (Rectangle){ 0, 0, texPlane.width, texPlane.height },
|
2018-03-13 19:04:49 +03:00
|
|
|
(Rectangle){ centerX, centerY, texPlane.width*scaleFactor, texPlane.height*scaleFactor },
|
2017-11-05 19:06:50 +03:00
|
|
|
(Vector2){ texPlane.width/2*scaleFactor, texPlane.height/2*scaleFactor }, 0, WHITE);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
EndBlendMode();
|
|
|
|
|
|
|
|
EndTextureMode();
|
|
|
|
|
|
|
|
// Draw 3D model (recomended to draw 3D always before 2D)
|
2018-05-04 17:54:05 +03:00
|
|
|
BeginMode3D(camera);
|
2017-08-04 14:38:21 +03:00
|
|
|
|
|
|
|
DrawModel(model, (Vector3){ 0, 6.0f, 0 }, 1.0f, WHITE); // Draw 3d model with texture
|
|
|
|
DrawGrid(10, 10.0f);
|
|
|
|
|
2018-05-04 17:54:05 +03:00
|
|
|
EndMode3D();
|
2017-08-04 14:38:21 +03:00
|
|
|
|
|
|
|
// Draw 2D GUI stuff
|
2017-11-05 19:06:50 +03:00
|
|
|
DrawAngleGauge(texAngleGauge, 80, 70, roll, "roll", RED);
|
|
|
|
DrawAngleGauge(texAngleGauge, 190, 70, pitch, "pitch", GREEN);
|
|
|
|
DrawAngleGauge(texAngleGauge, 300, 70, yaw, "yaw", SKYBLUE);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
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
|
2018-03-13 19:04:49 +03:00
|
|
|
DrawTextureRec(framebuffer.texture, (Rectangle){ 0, 0, framebuffer.texture.width, -framebuffer.texture.height },
|
2017-08-04 14:38:21 +03:00
|
|
|
(Vector2){ screenWidth - framebuffer.texture.width - 20, 20 }, Fade(WHITE, 0.8f));
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
DrawRectangleLines(screenWidth - framebuffer.texture.width - 20, 20, framebuffer.texture.width, framebuffer.texture.height, DARKGRAY);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
// Unload all loaded data
|
|
|
|
UnloadModel(model);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
UnloadRenderTexture(framebuffer);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
|
|
|
UnloadTexture(texAngleGauge);
|
2017-08-04 14:38:21 +03:00
|
|
|
UnloadTexture(texBackground);
|
2018-03-13 19:04:49 +03:00
|
|
|
UnloadTexture(texPitch);
|
2017-08-04 14:38:21 +03:00
|
|
|
UnloadTexture(texPlane);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
2018-03-13 19:04:49 +03:00
|
|
|
|
2017-08-04 14:38:21 +03:00
|
|
|
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);
|
2018-03-13 19:04:49 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|