From ae7fcc901193020b3f38f116cd248dfd2f98f34f Mon Sep 17 00:00:00 2001 From: elipzer Date: Sat, 15 Sep 2018 17:40:49 -0400 Subject: [PATCH] Starting Textured Scene Want to use seperate textures and samplers for the scene to allow for more modularization. Currently have created a texture class and started creating a scene class. --- OpenGLEngine/BasicScene.h | 2 +- OpenGLEngine/BuiltinTypes.h | 22 ++- OpenGLEngine/MyApplication.cpp | 8 +- OpenGLEngine/MyApplication.h | 2 + OpenGLEngine/MyBuiltinTexturedScene.cpp | 55 ++++++ OpenGLEngine/MyBuiltinTexturedScene.h | 19 +++ OpenGLEngine/MySimple3DScene.h | 2 +- OpenGLEngine/MySimpleCubeScene.h | 2 +- OpenGLEngine/OpenGLEngine.vcxproj | 9 + OpenGLEngine/OpenGLEngine.vcxproj.filters | 89 +++++++--- OpenGLEngine/Sampler.cpp | 21 +++ OpenGLEngine/Sampler.h | 48 ++++++ OpenGLEngine/Texture.h | 199 ++++++++++++++++++++++ OpenGLEngine/TexturedBatch.cpp | 32 ++++ OpenGLEngine/TexturedBatch.h | 30 ++++ OpenGLEngine/TexturedFS.glsl | 6 + OpenGLEngine/TexturedScene.cpp | 45 +++++ OpenGLEngine/TexturedScene.h | 39 +++++ OpenGLEngine/TexturedShaderProgram.h | 15 ++ OpenGLEngine/TexturedVS.glsl | 11 ++ 20 files changed, 621 insertions(+), 35 deletions(-) create mode 100644 OpenGLEngine/MyBuiltinTexturedScene.cpp create mode 100644 OpenGLEngine/MyBuiltinTexturedScene.h create mode 100644 OpenGLEngine/Sampler.cpp create mode 100644 OpenGLEngine/Sampler.h create mode 100644 OpenGLEngine/Texture.h create mode 100644 OpenGLEngine/TexturedBatch.cpp create mode 100644 OpenGLEngine/TexturedBatch.h create mode 100644 OpenGLEngine/TexturedFS.glsl create mode 100644 OpenGLEngine/TexturedScene.cpp create mode 100644 OpenGLEngine/TexturedScene.h create mode 100644 OpenGLEngine/TexturedShaderProgram.h create mode 100644 OpenGLEngine/TexturedVS.glsl diff --git a/OpenGLEngine/BasicScene.h b/OpenGLEngine/BasicScene.h index 1a64ffc..61ca859 100644 --- a/OpenGLEngine/BasicScene.h +++ b/OpenGLEngine/BasicScene.h @@ -6,7 +6,7 @@ #include "BasicShaderProgram.h" #include "BuiltinTypes.h" -#include "Camera2D.h" +#include "Camera.h" #include "Batched.h" #include "BasicBatch.h" diff --git a/OpenGLEngine/BuiltinTypes.h b/OpenGLEngine/BuiltinTypes.h index 4e5dbab..c961e21 100644 --- a/OpenGLEngine/BuiltinTypes.h +++ b/OpenGLEngine/BuiltinTypes.h @@ -79,15 +79,6 @@ namespace charcoal Position position; }; - struct PNVertex - { - void set_position(const Position& position) { this->position = position; } - void set_normal(const Normal& normal) { this->normal = normal; } - - Position position; - Normal normal; - }; - struct PNMVertex { void set_position(const Position& position) { this->position = position; } @@ -99,6 +90,15 @@ namespace charcoal Material material; }; + struct PTVertex + { + void set_position(const Position& position) { this->position = position; } + void set_uv(const UV& uv) { this->uv = uv; } + + Position position; + UV uv; + }; + // typedefs for builtin types typedef PVertex BasicVertex; @@ -108,5 +108,9 @@ namespace charcoal typedef PNMVertex LitVertex; typedef Index LitIndex; typedef Renderable LitRenderable; + + typedef PTVertex TexturedVertex; + typedef Index TexturedIndex; + typedef Renderable TexturedRenderable; } } \ No newline at end of file diff --git a/OpenGLEngine/MyApplication.cpp b/OpenGLEngine/MyApplication.cpp index 89a409f..b5765dc 100644 --- a/OpenGLEngine/MyApplication.cpp +++ b/OpenGLEngine/MyApplication.cpp @@ -7,7 +7,8 @@ MyApplication::MyApplication(int width, int height) m_simple_3d_scene(*this), m_simple_cube_scene(*this), m_builtin_basic_cube_scene(*this), - m_builtin_lit_scene(*this) + m_builtin_lit_scene(*this), + m_builtin_textured_scene(*this) {} void MyApplication::init() @@ -18,6 +19,7 @@ void MyApplication::init() m_simple_cube_scene.init(); m_builtin_basic_cube_scene.init(); m_builtin_lit_scene.init(); + m_builtin_textured_scene.init(); m_p_current_scene = &m_basic_scene; m_p_current_scene->use(); @@ -49,6 +51,10 @@ void MyApplication::update(float delta_time, clock_t clock) { swap_scene(&m_builtin_lit_scene); } + else if (m_glfw_input_manager.is_key_pressed(GLFW_KEY_7)) + { + swap_scene(&m_builtin_textured_scene); + } m_p_current_scene->update(delta_time, clock); } diff --git a/OpenGLEngine/MyApplication.h b/OpenGLEngine/MyApplication.h index b7ad209..6a0323e 100644 --- a/OpenGLEngine/MyApplication.h +++ b/OpenGLEngine/MyApplication.h @@ -7,6 +7,7 @@ #include "MySimpleCubeScene.h" #include "MyBuiltinCubeScene.h" #include "MyBuiltinLitScene.h" +#include "MyBuiltinTexturedScene.h" using namespace charcoal; @@ -37,5 +38,6 @@ private: MySimpleCubeScene m_simple_cube_scene; MyBuiltinCubeScene m_builtin_basic_cube_scene; MyBuiltinLitScene m_builtin_lit_scene; + MyBuiltinTexturedScene m_builtin_textured_scene; }; diff --git a/OpenGLEngine/MyBuiltinTexturedScene.cpp b/OpenGLEngine/MyBuiltinTexturedScene.cpp new file mode 100644 index 0000000..5de5f5a --- /dev/null +++ b/OpenGLEngine/MyBuiltinTexturedScene.cpp @@ -0,0 +1,55 @@ +#include "MyBuiltinTexturedScene.h" + +#include "MeshGenerator.h" + +#include "constants.h" + +MyBuiltinTexturedScene::MyBuiltinTexturedScene(Application& application) + : TexturedScene(application), + m_shape(meshgenerator::gen_cube_p(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DrawMode::DRAW_TRIANGLES), + m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f)), + m_batch(add_batch(&m_shape, 1)) +{ + add_prerenderable(&m_camera); + set_camera(&m_camera); +} + +void MyBuiltinTexturedScene::update(float delta_time, clock_t clock) +{ + float brightness; + float radians; + + clock_t c; + const clock_t intervals = 512 * CLOCKS_PER_SEC / 100; + const clock_t half_interval = 256 * CLOCKS_PER_SEC / 100; + c = clock % intervals; + if (c < half_interval) + brightness = (float)c / half_interval; + else + brightness = (float)(intervals - c) / half_interval; + + radians = (float)TAU * c / intervals; + + { + Poseable& pose = m_batch.get_pose(0); + pose.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_2 * delta_time); + pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f)); + } + + vec3 camera_translation(0.0f, 0.0f, 0.0f); + + if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1; + if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1; + if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1; + if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1; + if (m_input_manager.is_key_down(GLFW_KEY_Q)) camera_translation.z -= 1; + if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1; + + float camera_rotation = 0.0f; + if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1; + if (m_input_manager.is_key_down(GLFW_KEY_C)) camera_rotation -= 1; + + m_camera.translate(camera_translation * delta_time); + m_camera.rotate(vec3(0.0f, 1.0f, 0.0f), camera_rotation * (float)TAU_1_8 * delta_time); +} + diff --git a/OpenGLEngine/MyBuiltinTexturedScene.h b/OpenGLEngine/MyBuiltinTexturedScene.h new file mode 100644 index 0000000..d0fd181 --- /dev/null +++ b/OpenGLEngine/MyBuiltinTexturedScene.h @@ -0,0 +1,19 @@ +#pragma once + +#include "TexturedScene.h" +#include "BuiltinCamera3D.h" + +using namespace charcoal; +using namespace charcoal::builtin; + +class MyBuiltinTexturedScene : public TexturedScene +{ +public: + MyBuiltinTexturedScene(Application& application); + + void update(float delta_time, clock_t clock) override; +private: + TexturedRenderable m_shape; + builtin::Camera3D m_camera; + TexturedBatch& m_batch; +}; \ No newline at end of file diff --git a/OpenGLEngine/MySimple3DScene.h b/OpenGLEngine/MySimple3DScene.h index c4b3da4..2070f95 100644 --- a/OpenGLEngine/MySimple3DScene.h +++ b/OpenGLEngine/MySimple3DScene.h @@ -31,6 +31,6 @@ private: MySimpleShaderProgram m_shader_program; MySimpleShaderProgram::Renderable m_shape; MyBatch m_batch; - Camera3D m_camera; + charcoal::Camera3D m_camera; }; diff --git a/OpenGLEngine/MySimpleCubeScene.h b/OpenGLEngine/MySimpleCubeScene.h index 9809285..80daf5e 100644 --- a/OpenGLEngine/MySimpleCubeScene.h +++ b/OpenGLEngine/MySimpleCubeScene.h @@ -31,6 +31,6 @@ private: MySimpleShaderProgram m_shader_program; MySimpleShaderProgram::Renderable m_shape; MyBatch m_batch; - Camera3D m_camera; + charcoal::Camera3D m_camera; }; diff --git a/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine.vcxproj index 875c13c..7aca24e 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine.vcxproj @@ -166,6 +166,7 @@ + @@ -179,6 +180,8 @@ NotUsing NotUsing + + @@ -205,8 +208,12 @@ + + + + @@ -240,6 +247,8 @@ + + diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index ff626ea..fa8a887 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -61,6 +61,24 @@ {ca004137-6425-4863-b91a-cf32988855be} + + {f3f94b58-31cf-4a57-bc2a-0c3a33a6b9ee} + + + {fa76cc6e-e866-4987-8263-85abac1ac2c6} + + + {2e0e6381-fca9-42aa-87a9-04495a753104} + + + {283b2135-b031-48f0-b3cc-564a864e13bd} + + + {a67acfda-71a8-46cf-8207-bfdece4228ac} + + + {4c6497d4-160a-45a1-a23b-7bf905de0824} + @@ -120,18 +138,12 @@ Source Files\Engine\builtin - - Source Files\Engine\builtin - Source Files\Example\Application Source Files\Engine\builtin - - Source Files\Engine\builtin - Source Files\Example\Application @@ -139,10 +151,25 @@ Source Files\Engine\Rendering - Source Files\Engine\builtin + Source Files\Engine\builtin\Basic + + + Source Files\Engine\builtin\Basic - Source Files\Engine\builtin + Source Files\Engine\builtin\Lit + + + Source Files\Engine\builtin\Lit + + + Source Files\Engine\builtin\Textured + + + Source Files\Engine\builtin\Textured + + + Source Files\Example\Application @@ -236,15 +263,9 @@ Header Files\Engine\builtin - - Header Files\Engine\builtin - Header Files\Engine\Rendering - - Header Files\Engine\builtin - Header Files\Example\Application @@ -260,9 +281,6 @@ Header Files\Engine\builtin - - Header Files\Engine\builtin - Header Files\Engine\builtin @@ -275,17 +293,38 @@ Header Files\Engine\builtin - - Header Files\Engine\builtin - Header Files\Example\Application - Header Files\Engine\builtin + Header Files\Engine\builtin\Basic + + + Header Files\Engine\builtin\Basic + + + Header Files\Engine\builtin\Basic - Header Files\Engine\builtin + Header Files\Engine\builtin\Lit + + + Header Files\Engine\builtin\Lit + + + Header Files\Engine\builtin\Lit + + + Header Files\Engine\builtin\Textured + + + Header Files\Engine\builtin\Textured + + + Header Files\Engine\builtin\Textured + + + Header Files\Example\Application @@ -313,5 +352,11 @@ Source Files\Engine\builtin\Shaders + + Source Files\Engine\builtin\Shaders + + + Source Files\Engine\builtin\Shaders + \ No newline at end of file diff --git a/OpenGLEngine/Sampler.cpp b/OpenGLEngine/Sampler.cpp new file mode 100644 index 0000000..a9c7c15 --- /dev/null +++ b/OpenGLEngine/Sampler.cpp @@ -0,0 +1,21 @@ +#include "Sampler.h" + +namespace charcoal +{ + Sampler::Sampler(Wrap wrap_s, Wrap wrap_t, MagFilter magnification_filter, MinFilter minification_filter) + { + glGenSamplers(1, &m_sampler); + + glSamplerParameteri(m_sampler, GL_WRAP_S, wrap_s); + } + + Sampler::~Sampler() + { + glDeleteSamplers(1, &m_sampler); + } + + void Sampler::bind(GLuint texture_unit) + { + glBindSampler(texture_unit, m_sampler); + } +} \ No newline at end of file diff --git a/OpenGLEngine/Sampler.h b/OpenGLEngine/Sampler.h new file mode 100644 index 0000000..852ee44 --- /dev/null +++ b/OpenGLEngine/Sampler.h @@ -0,0 +1,48 @@ +#pragma once + +#include "stdafx.h" + +namespace charcoal +{ + class Sampler + { + public: + enum MinFilter : GLenum + { + NEAREST = GL_NEAREST, + LINEAR = GL_LINEAR, + NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST, + LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST, + NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR, + LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR, + }; + + enum MagFilter : GLenum + { + NEAREST = GL_NEAREST, + LINEAR = GL_LINEAR, + }; + + enum Wrap : GLenum + { + CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE, + MIRRORED_REPEAT = GL_MIRRORED_REPEAT, + REPEAT = GL_REPEAT, + }; + + /* + TODO: + GL_TEXTURE_BORDER_COLOR, GL_TEXTURE_MIN_LOD, GL_TEXTURE_MAX_LOD, + GL_TEXTURE_LOD_BIAS GL_TEXTURE_COMPARE_MODE, GL_TEXTURE_COMPARE_FUNC, + GL_TEXTURE_WRAP_R + */ + + Sampler(Wrap wrap_s, Wrap wrap_t, MagFilter magnification_filter, MinFilter minification_filter); + ~Sampler(); + + void bind(GLuint texture_unit); + + private: + GLuint m_sampler = 0; + }; +} \ No newline at end of file diff --git a/OpenGLEngine/Texture.h b/OpenGLEngine/Texture.h new file mode 100644 index 0000000..d36f252 --- /dev/null +++ b/OpenGLEngine/Texture.h @@ -0,0 +1,199 @@ +#pragma once + +#include "stdafx.h" + +#include "Exception.h" + +#include + +namespace charcoal +{ + class Texture + { + public: + enum Target : GLenum + { + TEXTURE_2D = GL_TEXTURE_2D, + PROXY_TEXTURE_2D = GL_PROXY_TEXTURE_2D, + TEXTURE_1D_ARRAY = GL_TEXTURE_1D_ARRAY, + PROXY_TEXTURE_1D_ARRAY = GL_PROXY_TEXTURE_1D_ARRAY, + TEXTURE_RECTANGLE = GL_TEXTURE_RECTANGLE, + PROXY_TEXTURE_RECTANGLE = GL_PROXY_TEXTURE_RECTANGLE, + TEXTURE_CUBE_MAP_POSITIVE_X = GL_TEXTURE_CUBE_MAP_POSITIVE_X, + TEXTURE_CUBE_MAP_NEGATIVE_X = GL_TEXTURE_CUBE_MAP_NEGATIVE_X, + TEXTURE_CUBE_MAP_POSITIVE_Y = GL_TEXTURE_CUBE_MAP_POSITIVE_Y, + TEXTURE_CUBE_MAP_NEGATIVE_Y = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, + TEXTURE_CUBE_MAP_POSITIVE_Z = GL_TEXTURE_CUBE_MAP_POSITIVE_Z, + TEXTURE_CUBE_MAP_NEGATIVE_Z = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, + PROXY_TEXTURE_CUBE_MAP = GL_PROXY_TEXTURE_CUBE_MAP, + }; + + enum InternalFormat : GLint + { + // Base Internal Formats + DEPTH_COMPONENT = GL_DEPTH_COMPONENT, + DEPTH_STENCIL = GL_DEPTH_STENCIL, + RED = GL_RED, + RG = GL_RG, + RGB = GL_RGB, + RGBA = GL_RGBA, + // Sized Internal Formats + R8 = GL_R8, + R8_SNORM = GL_R8_SNORM, + R16 = GL_R16, + R16_SNORM = GL_R16_SNORM, + RG8 = GL_RG8, + RG8_SNORM = GL_RG8_SNORM, + RG16 = GL_RG16, + RG16_SNORM = GL_RG16_SNORM, + R3_G3_B2 = GL_R3_G3_B2, + RGB4 = GL_RGB4, + RGB5 = GL_RGB5, + RGB8 = GL_RGB8, + RGB8_SNORM = GL_RGB8_SNORM, + RGB10 = GL_RGB10, + RGB12 = GL_RGB12, + RGB16_SNORM = GL_RGB16_SNORM, + RGBA2 = GL_RGBA2, + RGBA4 = GL_RGBA4, + RGB5_A1 = GL_RGB5_A1, + RGBA8 = GL_RGBA8, + RGBA8_SNORM = GL_RGBA8_SNORM, + RGB10_A2 = GL_RGB10_A2, + RGB10_A2UI = GL_RGB10_A2UI, + RGBA12 = GL_RGBA12, + RGBA16 = GL_RGBA16, + SRGB8 = GL_SRGB8, + SRGB8_ALPHA8 = GL_SRGB8_ALPHA8, + R16F = GL_R16F, + RG16F = GL_RG16F, + RGB16F = GL_RGB16F, + RGBA16F = GL_RGBA16F, + R32F = GL_R32F, + RG32F = GL_RG32F, + RGB32F = GL_RGB32F, + RGBA32F = GL_RGBA32F, + R11F_G11F_B10F = GL_R11F_G11F_B10F, + RGB9_E5 = GL_RGB9_E5, + R8I = GL_R8I, + R8UI = GL_R8UI, + R16I = GL_R16I, + R16UI = GL_R16UI, + R32I = GL_R32I, + R32UI = GL_R32UI, + RG8I = GL_RG8I, + RG8UI = GL_RG8UI, + RG16I = GL_RG16I, + RG16UI = GL_RG16UI, + RG32I = GL_RG32I, + RG32UI = GL_RG32UI, + RGB8I = GL_RGB8I, + RGB8UI = GL_RGB8UI, + RGB16I = GL_RGB16I, + RGB16UI = GL_RGB16UI, + RGB32I = GL_RGB32I, + RGB32UI = GL_RGB32UI, + RGBA8I = GL_RGBA8I, + RGBA8UI = GL_RGBA8UI, + RGBA16I = GL_RGBA16I, + RGBA16UI = GL_RGBA16UI, + RGBA32I = GL_RGBA32I, + RGBA32UI = GL_RGBA32UI, + // Compressed Internal Formats + COMPRESSED_RED = GL_COMPRESSED_RED, + COMPRESSED_RG = GL_COMPRESSED_RG, + COMPRESSED_RGB = GL_COMPRESSED_RGB, + COMPRESSED_RGBA = GL_COMPRESSED_RGBA, + COMPRESSED_SRGB = GL_COMPRESSED_SRGB, + COMPRESSED_SRGB_ALPHA = GL_COMPRESSED_SRGB_ALPHA, + COMPRESSED_RED_RGTC1 = GL_COMPRESSED_RED_RGTC1, + COMPRESSED_SIGNED_RED_RGTC1 = GL_COMPRESSED_SIGNED_RED_RGTC1, + COMPRESSED_RG_RGTC2 = GL_COMPRESSED_RG_RGTC2, + COMPRESSED_SIGNED_RG_RGTC2 = GL_COMPRESSED_SIGNED_RG_RGTC2, + COMPRESSED_RGBA_BPTC_UNORM = GL_COMPRESSED_RGBA_BPTC_UNORM, + COMPRESSED_SRGB_ALPHA_BPTC_UNORM = GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, + COMPRESSED_RGB_BPTC_SIGNED_FLOAT = GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, + COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, + }; + + enum Format : GLenum + { + RED = GL_RED, + RG = GL_RG, + RGB = GL_RGB, + BGR = GL_BGR, + RGBA = GL_RGBA, + BGRA = GL_BGRA, + RED_INTEGER = GL_RED_INTEGER, + RG_INTEGER = GL_RG_INTEGER, + RGB_INTEGER = GL_RGB_INTEGER, + BGR_INTEGER = GL_BGR_INTEGER, + RGBA_INTEGER = GL_RGBA_INTEGER, + BGRA_INTEGER = GL_BGRA_INTEGER, + STENCIL_INDEX = GL_STENCIL_INDEX, + DEPTH_COMPONENT = GL_DEPTH_COMPONENT, + DEPTH_STENCIL = GL_DEPTH_STENCIL, + }; + + enum Type : GLenum + { + UNSIGNED_BYTE = GL_UNSIGNED_BYTE, + BYTE = GL_BYTE, + UNSIGNED_SHORT = GL_UNSIGNED_SHORT, + SHORT = GL_SHORT, + UNSIGNED_INT = GL_UNSIGNED_INT, + INT = GL_INT, + FLOAT = GL_FLOAT, + UNSIGNED_BYTE_3_3_2 = GL_UNSIGNED_BYTE_3_3_2, + UNSIGNED_BYTE_2_3_3_REV = GL_UNSIGNED_BYTE_2_3_3_REV, + UNSIGNED_SHORT_5_6_5 = GL_UNSIGNED_SHORT_5_6_5, + UNSIGNED_SHORT_5_6_5_REV = GL_UNSIGNED_SHORT_5_6_5_REV, + UNSIGNED_SHORT_4_4_4_4 = GL_UNSIGNED_SHORT_4_4_4_4, + UNSIGNED_SHORT_4_4_4_4_REV = GL_UNSIGNED_SHORT_4_4_4_4_REV, + UNSIGNED_SHORT_5_5_5_1 = GL_UNSIGNED_SHORT_5_5_5_1, + UNSIGNED_SHORT_1_5_5_5_REV = GL_UNSIGNED_SHORT_1_5_5_5_REV, + UNSIGNED_INT_8_8_8_8 = GL_UNSIGNED_INT_8_8_8_8, + UNSIGNED_INT_8_8_8_8_REV = GL_UNSIGNED_INT_8_8_8_8_REV, + UNSIGNED_INT_10_10_10_2 = GL_UNSIGNED_INT_10_10_10_2, + UNSIGNED_INT_2_10_10_10_REV = GL_UNSIGNED_INT_2_10_10_10_REV, + }; + + template + Texture( + Format data_format, + Type data_type, + unsigned long width, + unsigned long height, + const std::vector& data, + InternalFormat internal_format, + unsigned short mipmap_level = 0, + Target texture_target = TEXTURE_2D + ) + : width(width), height(height) + { + if (texture_target != TEXTURE_2D) + throw EXCEPTION("Non 2D textures are not supported yet."); + + glGenTextures(1, &m_texture); + + glBindTexture(texture_target, m_texture); + + glTexImage2D(texture_target, mipmap_level, internal_format, width, height, 0, data_format, data_type, data); + + glGenerateMipmap(texture_target); + + glBindTexture(texture_target, NULL); + } + + ~Texture() + { + glDeleteTextures(1, &m_texture); + } + + + private: + GLuint m_texture = 0; + unsigned long width; + unsigned long height; + }; +} \ No newline at end of file diff --git a/OpenGLEngine/TexturedBatch.cpp b/OpenGLEngine/TexturedBatch.cpp new file mode 100644 index 0000000..38b6178 --- /dev/null +++ b/OpenGLEngine/TexturedBatch.cpp @@ -0,0 +1,32 @@ +#include "TexturedBatch.h" + +namespace charcoal +{ + namespace builtin + { + void TexturedBatch::setup_vao() + { + glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void*)offsetof(TexturedVertex, position)); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void*)offsetof(TexturedVertex, uv)); + glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(4); + glEnableVertexAttribArray(5); + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4))); + glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4))); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4))); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4))); + + glVertexAttribDivisor(0, 0); // Send the mesh data once + glVertexAttribDivisor(1, 0); // Send the mesh data once + glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn + glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn + glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn + glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn + } + } +} diff --git a/OpenGLEngine/TexturedBatch.h b/OpenGLEngine/TexturedBatch.h new file mode 100644 index 0000000..efa2ae1 --- /dev/null +++ b/OpenGLEngine/TexturedBatch.h @@ -0,0 +1,30 @@ +#pragma once + +#include "PoseableBatch.h" +#include "BuiltinTypes.h" + +namespace charcoal +{ + namespace builtin + { + class TexturedBatch : public PoseableBatch + { + public: + TexturedBatch( + TexturedRenderable* renderable, + int element_count + ) : PoseableBatch(renderable, element_count) + {} + + TexturedBatch( + TexturedRenderable* renderable, + int element_count, + int element_render_count + ) : PoseableBatch(renderable, element_count, element_render_count) + {} + + protected: + void setup_vao() override; + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/TexturedFS.glsl b/OpenGLEngine/TexturedFS.glsl new file mode 100644 index 0000000..e3a49de --- /dev/null +++ b/OpenGLEngine/TexturedFS.glsl @@ -0,0 +1,6 @@ +#version 430 +out vec4 frag_color; +void main() +{ + frag_color = vec4(1.0, 1.0, 1.0, 1.0); +} \ No newline at end of file diff --git a/OpenGLEngine/TexturedScene.cpp b/OpenGLEngine/TexturedScene.cpp new file mode 100644 index 0000000..1d42020 --- /dev/null +++ b/OpenGLEngine/TexturedScene.cpp @@ -0,0 +1,45 @@ +#include "TexturedScene.h" + +#include "stdafx.h" + +#include "GLUtil.h" +#include "MeshFactory.h" + +namespace charcoal +{ + namespace builtin + { + void TexturedScene::init() + { + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + TexturedBatch& batch = *iter; + batch.init(); + add_prerenderable(&batch); + } + } + + void TexturedScene::use() + { + // TODO: move to glutil + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + } + + void TexturedScene::unuse() + { + + } + + void TexturedScene::render() + { + glutil::clear_screen(); + m_shader_program.use(); + glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix()); + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + iter->render(); + } + } + } +} \ No newline at end of file diff --git a/OpenGLEngine/TexturedScene.h b/OpenGLEngine/TexturedScene.h new file mode 100644 index 0000000..5582809 --- /dev/null +++ b/OpenGLEngine/TexturedScene.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +#include "AutoPrerenderingScene.h" + +#include "BuiltinTypes.h" +#include "Camera.h" +#include "Batched.h" +#include "TexturedBatch.h" +#include "TexturedShaderProgram.h" + +namespace charcoal +{ + namespace builtin + { + class TexturedScene : public AutoPrerenderingScene, public Batched + { + public: + TexturedScene(Application& application) : AutoPrerenderingScene(application) {} + virtual ~TexturedScene() {} + + void init() override; + + void use() override; + + void unuse() override; + + void render() override; + + protected: + void set_camera(const Camera* p_camera) { m_p_camera = p_camera; } + + private: + TexturedShaderProgram m_shader_program; + const Camera* m_p_camera = nullptr; + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/TexturedShaderProgram.h b/OpenGLEngine/TexturedShaderProgram.h new file mode 100644 index 0000000..2301f7b --- /dev/null +++ b/OpenGLEngine/TexturedShaderProgram.h @@ -0,0 +1,15 @@ +#pragma once + +#include "VertexFragmentShaderProgram.h" + +namespace charcoal +{ + namespace builtin + { + class TexturedShaderProgram : public VertexFragmentShaderProgram + { + public: + TexturedShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "TexturedVS.glsl", SHADER_PATH "TexturedFS.glsl") {} + }; + } +} diff --git a/OpenGLEngine/TexturedVS.glsl b/OpenGLEngine/TexturedVS.glsl new file mode 100644 index 0000000..5596907 --- /dev/null +++ b/OpenGLEngine/TexturedVS.glsl @@ -0,0 +1,11 @@ +#version 430 +layout(location = 0) in vec3 vertex_position; +layout(location = 1) in vec2 vertex_uv; +layout(location = 2) in mat4 model_to_world; + +layout(location = 0) uniform mat4 world_to_projection; + +void main() +{ + gl_Position = world_to_projection * model_to_world * vec4(vertex_position, 1.0); +} \ No newline at end of file