From 9bb55ed296bd708be2b8fd6d148b38c4ed101b44 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sat, 10 Aug 2019 15:25:07 -0400 Subject: [PATCH] Add 3D --- examples/core/core_3d_camera_first_person.cpp | 93 +++++++++++++++++++ examples/text/text_raylib_fonts.cpp | 36 +++---- include/raylib/Camera3D.hpp | 8 +- include/raylib/Color.hpp | 57 ++++++++---- include/raylib/Vector3.hpp | 27 ++++++ 5 files changed, 181 insertions(+), 40 deletions(-) create mode 100644 examples/core/core_3d_camera_first_person.cpp diff --git a/examples/core/core_3d_camera_first_person.cpp b/examples/core/core_3d_camera_first_person.cpp new file mode 100644 index 0000000..7311258 --- /dev/null +++ b/examples/core/core_3d_camera_first_person.cpp @@ -0,0 +1,93 @@ +/******************************************************************************************* +* +* raylib [core] example - 3d camera first person +* +* This example has been created using raylib 1.3 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib/raylib.h" + +#define MAX_COLUMNS 20 + +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + raylib::Window w(screenWidth, screenHeight, "raylib [core] example - 3d camera first person"); + raylib::Color background(RAYWHITE); + + // Define the camera to look into our 3d world (position, target, up vector) + raylib::Camera camera(); + camera.position = raylib::Vector3(4.0f, 2.0f, 4.0f); + camera.target = raylib::Vector3(0.0f, 1.8f, 0.0f); + camera.up = raylib::Vector3(0.0f, 1.0f, 0.0f); + camera.fovy = 60.0f; + camera.type = CAMERA_PERSPECTIVE; + + // Generates some random columns + float heights[MAX_COLUMNS] = { 0.0f }; + raylib::Vector3 positions[MAX_COLUMNS] = { 0 }; + raylib::Color colors[MAX_COLUMNS] = { 0 }; + + for (int i = 0; i < MAX_COLUMNS; i++) + { + heights[i] = (float)GetRandomValue(1, 12); + positions[i] = raylib::Vector3(GetRandomValue(-15, 15), heights[i]/2, GetRandomValue(-15, 15)); + colors[i] = raylib::Color(GetRandomValue(20, 255), GetRandomValue(10, 55), 30); + } + + camera.SetMode(CAMERA_FIRST_PERSON); // Set a first person camera mode + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!w.ShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + camera.Update(); // Update camera + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + background.ClearBackground(); + + camera.BeginMode3D(); + + DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground + DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall + DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall + DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall + + // Draw some cubes around + for (int i = 0; i < MAX_COLUMNS; i++) + { + positions[i].DrawCube(2.0f, heights[i], 2.0f, colors[i]); + positions[i].DrawCubeWires(2.0f, heights[i], 2.0f, MAROON); + } + + EndMode3D(); + + DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f)); + DrawRectangleLines( 10, 10, 220, 70, BLUE); + + DrawText("First person camera default controls:", 20, 20, 10, BLACK); + DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY); + DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + return 0; +} diff --git a/examples/text/text_raylib_fonts.cpp b/examples/text/text_raylib_fonts.cpp index 4a49825..d105e07 100644 --- a/examples/text/text_raylib_fonts.cpp +++ b/examples/text/text_raylib_fonts.cpp @@ -26,6 +26,8 @@ int main() int screenHeight = 450; raylib::Window w(screenWidth, screenHeight, "raylib [text] example - raylib fonts"); + raylib::Color background(RAYWHITE); + raylib::Color textColor(DARKGRAY); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) raylib::Font fonts[MAX_FONTS] = { @@ -39,31 +41,31 @@ int main() raylib::Font("resources/fonts/jupiter_crash.png") }; - std::string messages[MAX_FONTS] = { "ALAGARD FONT designed by Hewett Tsoi", + std::string messages[MAX_FONTS] = { "ALAGARD FONT designed by Hewett Tsoi", "PIXELPLAY FONT designed by Aleksander Shevchuk", - "MECHA FONT designed by Captain Falcon", - "SETBACK FONT designed by Brian Kent (AEnigma)", - "ROMULUS FONT designed by Hewett Tsoi", + "MECHA FONT designed by Captain Falcon", + "SETBACK FONT designed by Brian Kent (AEnigma)", + "ROMULUS FONT designed by Hewett Tsoi", "PIXANTIQUA FONT designed by Gerhard Grossmann", "ALPHA_BETA FONT designed by Brian Kent (AEnigma)", "JUPITER_CRASH FONT designed by Brian Kent (AEnigma)" }; - + const int spacings[MAX_FONTS] = { 2, 4, 8, 4, 3, 4, 4, 1 }; - + Vector2 positions[MAX_FONTS]; - + for (int i = 0; i < MAX_FONTS; i++) { positions[i].x = screenWidth/2 - MeasureTextEx(fonts[i], messages[i].c_str(), fonts[i].baseSize*2, spacings[i]).x/2; positions[i].y = 60 + fonts[i].baseSize + 45*i; } - + // Small Y position corrections positions[3].y += 8; positions[4].y += 2; positions[7].y -= 8; - - Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED }; + + raylib::Color colors[MAX_FONTS] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED }; //-------------------------------------------------------------------------------------- // Main game loop @@ -78,14 +80,14 @@ int main() //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(RAYWHITE); - - DrawText("free fonts included with raylib", 250, 20, 20, DARKGRAY); - DrawLine(220, 50, 590, 50, DARKGRAY); - + background.ClearBackground(); + + textColor.DrawText("free fonts included with raylib", 250, 20, 20); + textColor.DrawLine(220, 50, 590, 50); + for (int i = 0; i < MAX_FONTS; i++) { - DrawTextEx(fonts[i], messages[i].c_str(), positions[i], fonts[i].baseSize*2, spacings[i], colors[i]); + fonts[i].DrawText(messages[i].c_str(), positions[i], fonts[i].baseSize*2, spacings[i], colors[i]); } EndDrawing(); @@ -94,4 +96,4 @@ int main() //-------------------------------------------------------------------------------------- return 0; -} \ No newline at end of file +} diff --git a/include/raylib/Camera3D.hpp b/include/raylib/Camera3D.hpp index 5741357..19bab30 100644 --- a/include/raylib/Camera3D.hpp +++ b/include/raylib/Camera3D.hpp @@ -16,6 +16,8 @@ namespace raylib { type = typeValue; }; + Camera3D() {} + inline void set(const ::Camera3D& camera) { position = camera.position; target = camera.target; @@ -39,15 +41,15 @@ namespace raylib { ::EndMode3D(); } - Matrix GetCameraMatrix() { + Matrix GetMatrix() { return ::GetCameraMatrix(*this); } - void SetCameraMode(int mode) { + void SetMode(int mode) { ::SetCameraMode(*this, mode); } - void UpdateCamera() { + void Update() { ::UpdateCamera(this); } diff --git a/include/raylib/Color.hpp b/include/raylib/Color.hpp index ba7d17e..059b201 100644 --- a/include/raylib/Color.hpp +++ b/include/raylib/Color.hpp @@ -1,6 +1,8 @@ #ifndef RAYLIB_CPP_COLOR_HPP_ #define RAYLIB_CPP_COLOR_HPP_ +#include + #include "raylib.h" #include "Vector4.hpp" #include "utils.hpp" @@ -72,31 +74,46 @@ namespace raylib { GETTERSETTER(unsigned char,B,b) GETTERSETTER(unsigned char,A,a) - Color& operator=(const ::Color& color) { - set(color); - return *this; - } + Color& operator=(const ::Color& color) { + set(color); + return *this; + } - Color& operator=(const Color& color) { - set(color); - return *this; - } + Color& operator=(const Color& color) { + set(color); + return *this; + } - void ClearBackground() { - ::ClearBackground(*this); - } + void ClearBackground() { + ::ClearBackground(*this); + } - void DrawPixel(int x, int y) { - ::DrawPixel(x, y, *this); - } + void DrawPixel(int x, int y) { + ::DrawPixel(x, y, *this); + } - void DrawPixel(::Vector2 pos) { - ::DrawPixelV(pos, *this); - } + void DrawPixel(::Vector2 pos) { + ::DrawPixelV(pos, *this); + } - void DrawText(const std::string& text, int posX, int posY, int fontSize) { - ::DrawText(text.c_str(), posX, posY, fontSize, *this); - } + void DrawText(const std::string& text, int posX, int posY, int fontSize) { + ::DrawText(text.c_str(), posX, posY, fontSize, *this); + } + void DrawText(::Font font, const std::string& text, ::Vector2 position, float fontSize, float spacing) { + ::DrawTextEx(font, text.c_str(), position, fontSize, spacing, *this); + } + + void DrawText(::Font font, const std::string& text, ::Rectangle rec, float fontSize, float spacing, bool wordWrap = false) { + ::DrawTextRec(font, text.c_str(), rec, fontSize, spacing, wordWrap, *this); + } + + void DrawRectangleLines(int posX, int posY, int width, int height) { + ::DrawRectangleLines(posX, posY, width, height, *this); + } + + void DrawRectangleLines(Rectangle rec, int lineThick) { + ::DrawRectangleLinesEx(rec, lineThick, *this); + } }; } diff --git a/include/raylib/Vector3.hpp b/include/raylib/Vector3.hpp index 4139adc..6c45e26 100644 --- a/include/raylib/Vector3.hpp +++ b/include/raylib/Vector3.hpp @@ -32,6 +32,33 @@ namespace raylib { void DrawGizmo() { ::DrawGizmo(*this); } + + void DrawLine3D(::Vector3 endPos, ::Color color) { + ::DrawLine3D(*this, endPos, color); + } + + void DrawCube(float width, float height, float length, ::Color color) { + ::DrawCube(*this, width, height, length, color); + } + void DrawCube(::Vector3 size, ::Color color) { + ::DrawCubeV(*this, size, color); + } + + + void DrawCubeWires(float width, float height, float length, ::Color color) { + ::DrawCubeWires(*this, width, height, length, color); + } + void DrawCubeWires(::Vector3 size, ::Color color) { + ::DrawCubeWiresV(*this, size, color); + } + + void DrawCubeTexture(::Texture2D texture, float width, float height, float length, ::Color color) { + ::DrawCubeTexture(texture, *this, width, height, length, color); + } + + void DrawPlane(::Vector2 size, ::Color color) { + ::DrawPlane(*this, size, color); + } }; }