From a8abb4afc973a21b585544215d462ec3d9845688 Mon Sep 17 00:00:00 2001 From: elipzer Date: Mon, 8 Oct 2018 15:19:48 -0400 Subject: [PATCH] Added a Sprite class to render 2D things The sprite inherits from Poseable. It is intended to allow for pure 2D rendering or integration of sprites/particles into 3D scenes. Eventually an ImageScene should be created to test out the image loading, sprite movement and rotation, and 2D camera functionality for the builtin engine. --- OpenGLEngine/ImageLoader.h | 9 +- OpenGLEngine/ImageLoaderImage.h | 16 --- OpenGLEngine/OpenGLEngine.vcxproj | 4 +- OpenGLEngine/OpenGLEngine.vcxproj.filters | 126 +++++++++++++--------- OpenGLEngine/Poseable.cpp | 15 +++ OpenGLEngine/Poseable.h | 2 + OpenGLEngine/Sprite.cpp | 28 +++++ OpenGLEngine/Sprite.h | 22 ++++ OpenGLEngine/SpriteBatch.h | 26 +++++ 9 files changed, 179 insertions(+), 69 deletions(-) delete mode 100644 OpenGLEngine/ImageLoaderImage.h create mode 100644 OpenGLEngine/Sprite.cpp create mode 100644 OpenGLEngine/Sprite.h create mode 100644 OpenGLEngine/SpriteBatch.h diff --git a/OpenGLEngine/ImageLoader.h b/OpenGLEngine/ImageLoader.h index 5cb3a7f..8434051 100644 --- a/OpenGLEngine/ImageLoader.h +++ b/OpenGLEngine/ImageLoader.h @@ -1,11 +1,18 @@ #pragma once -#include "ImageLoaderImage.h" +#include namespace charcoal { namespace image_loader { + struct ImageRGBA + { + std::vector data; + unsigned int width; + unsigned int height; + }; + ImageRGBA load_file(const std::string& filename); } } \ No newline at end of file diff --git a/OpenGLEngine/ImageLoaderImage.h b/OpenGLEngine/ImageLoaderImage.h deleted file mode 100644 index b6f27f8..0000000 --- a/OpenGLEngine/ImageLoaderImage.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -namespace charcoal -{ - namespace image_loader - { - struct ImageRGBA - { - std::vector data; - unsigned int width; - unsigned int height; - }; - } -} \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine.vcxproj index 651a02a..33e1c9d 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine.vcxproj @@ -182,6 +182,7 @@ + NotUsing NotUsing @@ -211,7 +212,6 @@ - @@ -228,6 +228,8 @@ + + diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index a1d5c64..55aeb25 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -70,36 +70,12 @@ {b3bc2383-d099-4fac-a51c-d2d4a8e7dea3} - - {2f2c0657-4ba3-4314-86dd-a600f533f746} - - - {f3f94b58-31cf-4a57-bc2a-0c3a33a6b9ee} - - - {fa76cc6e-e866-4987-8263-85abac1ac2c6} - - - {2e0e6381-fca9-42aa-87a9-04495a753104} - {a9e42739-f380-4358-83e5-f994cab7e55c} {f9faae2c-4381-4a2e-a0be-27920a52dc4d} - - {a67acfda-71a8-46cf-8207-bfdece4228ac} - - - {283b2135-b031-48f0-b3cc-564a864e13bd} - - - {ca87b30d-8b69-4c09-90a5-1fe317322c4c} - - - {4c6497d4-160a-45a1-a23b-7bf905de0824} - {ca004137-6425-4863-b91a-cf32988855be} @@ -121,6 +97,48 @@ {b8357bff-b6be-4ee1-b45e-2f1e0fee90e1} + + {32da6379-79e0-4266-9f92-896845c98850} + + + {e9fecf63-116f-4e49-a6a0-aee4bdb9c09a} + + + {2f2c0657-4ba3-4314-86dd-a600f533f746} + + + {f3f94b58-31cf-4a57-bc2a-0c3a33a6b9ee} + + + {fa76cc6e-e866-4987-8263-85abac1ac2c6} + + + {2e0e6381-fca9-42aa-87a9-04495a753104} + + + {af291666-8e83-495d-979d-8eaa9aacc295} + + + {6b8de3ed-e727-4558-8522-07d2b397c6c8} + + + {da4a1131-4c15-462b-91c9-625d59096f73} + + + {ca87b30d-8b69-4c09-90a5-1fe317322c4c} + + + {283b2135-b031-48f0-b3cc-564a864e13bd} + + + {a67acfda-71a8-46cf-8207-bfdece4228ac} + + + {4c6497d4-160a-45a1-a23b-7bf905de0824} + + + {492583f7-cffd-4d22-82c6-88be804e2f2f} + @@ -178,31 +196,31 @@ Source Files\Engine\Baseline\Rendering - Source Files\Engine\builtin\Scenes\Basic + Source Files\Engine\builtin\Scenes\3D\Basic - Source Files\Engine\builtin\Scenes\Basic + Source Files\Engine\builtin\Scenes\3D\Basic - Source Files\Engine\builtin\Scenes\Lit + Source Files\Engine\builtin\Scenes\3D\Lit - Source Files\Engine\builtin\Scenes\Lit + Source Files\Engine\builtin\Scenes\3D\Lit - Source Files\Engine\builtin\Scenes\Textured + Source Files\Engine\builtin\Scenes\3D\Textured - Source Files\Engine\builtin\Scenes\Textured + Source Files\Engine\builtin\Scenes\3D\Textured Source Files\Example\Application - Source Files\Engine\builtin\Scenes\LitShadowed + Source Files\Engine\builtin\Scenes\3D\LitShadowed - Source Files\Engine\builtin\Scenes\LitShadowed + Source Files\Engine\builtin\Scenes\3D\LitShadowed Source Files\Example\Application @@ -231,6 +249,9 @@ Source Files\Engine\Plugins\LodePNG + + Source Files\Engine\Baseline + @@ -297,31 +318,31 @@ Header Files\Example\Application - Header Files\Engine\builtin\Scenes\Basic + Header Files\Engine\builtin\Scenes\3D\Basic - Header Files\Engine\builtin\Scenes\Basic + Header Files\Engine\builtin\Scenes\3D\Basic - Header Files\Engine\builtin\Scenes\Basic + Header Files\Engine\builtin\Scenes\3D\Basic - Header Files\Engine\builtin\Scenes\Lit + Header Files\Engine\builtin\Scenes\3D\Lit - Header Files\Engine\builtin\Scenes\Lit + Header Files\Engine\builtin\Scenes\3D\Lit - Header Files\Engine\builtin\Scenes\Lit + Header Files\Engine\builtin\Scenes\3D\Lit - Header Files\Engine\builtin\Scenes\Textured + Header Files\Engine\builtin\Scenes\3D\Textured - Header Files\Engine\builtin\Scenes\Textured + Header Files\Engine\builtin\Scenes\3D\Textured - Header Files\Engine\builtin\Scenes\Textured + Header Files\Engine\builtin\Scenes\3D\Textured Header Files\Example\Application @@ -330,28 +351,28 @@ Header Files\Engine\Baseline\Rendering - Header Files\Engine\builtin\Scenes\LitShadowed + Header Files\Engine\builtin\Scenes\3D\LitShadowed - Header Files\Engine\builtin\Scenes\LitShadowed + Header Files\Engine\builtin\Scenes\3D\LitShadowed - Header Files\Engine\builtin\Scenes\LitShadowed + Header Files\Engine\builtin\Scenes\3D\LitShadowed Header Files\Example\Application - Header Files\Engine\builtin\Scenes\Basic + Header Files\Engine\builtin\Scenes\3D\Basic - Header Files\Engine\builtin\Scenes\Lit + Header Files\Engine\builtin\Scenes\3D\Lit - Header Files\Engine\builtin\Scenes\Textured + Header Files\Engine\builtin\Scenes\3D\Textured - Header Files\Engine\builtin\Scenes\LitShadowed + Header Files\Engine\builtin\Scenes\3D\LitShadowed Header Files\Engine\Baseline @@ -428,12 +449,15 @@ Header Files\Engine\Baseline\ImageLoader - - Header Files\Engine\Baseline\ImageLoader - Header Files\Engine\Plugins\LodePNG + + Header Files\Engine\builtin\General + + + Header Files\Engine\Baseline + diff --git a/OpenGLEngine/Poseable.cpp b/OpenGLEngine/Poseable.cpp index eb73ef4..ee82193 100644 --- a/OpenGLEngine/Poseable.cpp +++ b/OpenGLEngine/Poseable.cpp @@ -18,6 +18,21 @@ namespace charcoal ) {} + void Poseable::reset_orientation() + { + m_orientation_matrix[0][0] = 1.0f; + m_orientation_matrix[0][1] = 0.0f; + m_orientation_matrix[0][2] = 0.0f; + + m_orientation_matrix[1][0] = 0.0f; + m_orientation_matrix[1][1] = 1.0f; + m_orientation_matrix[1][2] = 0.0f; + + m_orientation_matrix[2][0] = 0.0f; + m_orientation_matrix[2][1] = 0.0f; + m_orientation_matrix[2][2] = 1.0f; + } + void Poseable::update_position(const vec3& position) { m_orientation_matrix[3][0] = position.x; diff --git a/OpenGLEngine/Poseable.h b/OpenGLEngine/Poseable.h index de6910a..062efd4 100644 --- a/OpenGLEngine/Poseable.h +++ b/OpenGLEngine/Poseable.h @@ -17,6 +17,8 @@ namespace charcoal const vec3& right = vec3(1.0f, 0.0f, 0.0f) ); + void reset_orientation(); + void update_position(const vec3& position); // Assumes that forward, up, and right are orthogonal and normalized void update_orientation(const vec3& forward, const vec3& up, const vec3& right); diff --git a/OpenGLEngine/Sprite.cpp b/OpenGLEngine/Sprite.cpp new file mode 100644 index 0000000..cc7b528 --- /dev/null +++ b/OpenGLEngine/Sprite.cpp @@ -0,0 +1,28 @@ +#include "Sprite.h" + +namespace charcoal +{ + Sprite::Sprite(const vec2& position) : Poseable(vec3(position, 0.0f)) + {} + + void Sprite::update_position(const vec2& position) + { + Poseable::update_position(vec3(position, 0.0f)); + } + + void Sprite::update_rotation(float angle) + { + Poseable::reset_orientation(); + rotate(angle); + } + + void Sprite::translate(const vec2& translation) + { + Poseable::translate(vec3(translation, 0.0f)); + } + + void Sprite::rotate(float angle) + { + Poseable::rotate(vec3(0.0f, 0.0f, 1.0f), angle); + } +} \ No newline at end of file diff --git a/OpenGLEngine/Sprite.h b/OpenGLEngine/Sprite.h new file mode 100644 index 0000000..8bdb8e4 --- /dev/null +++ b/OpenGLEngine/Sprite.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +#include "Poseable.h" + +namespace charcoal +{ + class Sprite : private Poseable + { + public: + Sprite(const vec2& position = vec2(0.0f, 0.0f)); + + void update_position(const vec2& position); + void update_rotation(float angle); + + void translate(const vec2& translation); + void rotate(float angle); + + const mat4& get_orientation_matrix() const { return Poseable::get_orientation_matrix(); } + }; +} \ No newline at end of file diff --git a/OpenGLEngine/SpriteBatch.h b/OpenGLEngine/SpriteBatch.h new file mode 100644 index 0000000..2315fcf --- /dev/null +++ b/OpenGLEngine/SpriteBatch.h @@ -0,0 +1,26 @@ +#pragma once + +#include "BuiltinBatch.h" + +namespace charcoal +{ + namespace builtin + { + template > + class SpriteBatch : public builtin::Batch + { + SpriteBatch( + RenderableT* renderable, + int element_count + ) : PoseableBatch(renderable, element_count, element_count) + {} + + SpriteBatch( + RenderableT* renderable, + int element_count, + int element_render_count + ) : builtin::Batch(renderable, element_render_count), m_pose_elements(element_count) + {} + }; + } +}