From c0d49da992e7e38e7a038787b6fe1f53f3d413e7 Mon Sep 17 00:00:00 2001 From: elipzer Date: Sat, 15 Sep 2018 20:43:29 -0400 Subject: [PATCH] Getting Closer to Textures --- OpenGLEngine/Batch.h | 4 +- OpenGLEngine/BuiltinTypes.h | 3 +- OpenGLEngine/MyBuiltinTexturedScene.cpp | 8 ++- OpenGLEngine/OpenGLEngine.vcxproj | 6 +++ OpenGLEngine/OpenGLEngine.vcxproj.filters | 24 +++++++-- OpenGLEngine/Renderable.h | 2 +- OpenGLEngine/Sampler.cpp | 5 +- OpenGLEngine/Sampler.h | 8 +-- OpenGLEngine/Texture.h | 21 ++++---- OpenGLEngine/TextureFactory.h | 47 +++++++++++++++++ OpenGLEngine/TextureGenerator.h | 64 +++++++++++++++++++++++ OpenGLEngine/TextureRenderable.h | 25 +++++++++ OpenGLEngine/TexturedBatch.cpp | 7 +++ OpenGLEngine/TexturedBatch.h | 2 + OpenGLEngine/TexturedFS.glsl | 3 ++ OpenGLEngine/TexturedScene.cpp | 1 + 16 files changed, 209 insertions(+), 21 deletions(-) create mode 100644 OpenGLEngine/TextureFactory.h create mode 100644 OpenGLEngine/TextureGenerator.h create mode 100644 OpenGLEngine/TextureRenderable.h diff --git a/OpenGLEngine/Batch.h b/OpenGLEngine/Batch.h index 4da3aef..deda0c5 100644 --- a/OpenGLEngine/Batch.h +++ b/OpenGLEngine/Batch.h @@ -76,8 +76,11 @@ namespace charcoal update_element_buffers(); } + virtual void preprender() const {} + void render() const { + preprender(); glBindVertexArray(m_vao); glDrawElementsInstanced( m_p_renderable->get_draw_mode(), @@ -111,7 +114,6 @@ namespace charcoal GLuint m_vertex_vbo; std::vector m_element_buffers; - private: GLuint m_vao; GLuint m_index_vbo; GLenum m_gl_index_type; diff --git a/OpenGLEngine/BuiltinTypes.h b/OpenGLEngine/BuiltinTypes.h index c961e21..a06738e 100644 --- a/OpenGLEngine/BuiltinTypes.h +++ b/OpenGLEngine/BuiltinTypes.h @@ -4,6 +4,7 @@ #include "Renderable.h" #include "PoseableBatch.h" #include "VertexFragmentShaderProgram.h" +#include "TextureRenderable.h" namespace charcoal { @@ -111,6 +112,6 @@ namespace charcoal typedef PTVertex TexturedVertex; typedef Index TexturedIndex; - typedef Renderable TexturedRenderable; + typedef TextureRenderable TexturedRenderable; } } \ No newline at end of file diff --git a/OpenGLEngine/MyBuiltinTexturedScene.cpp b/OpenGLEngine/MyBuiltinTexturedScene.cpp index 5de5f5a..053d249 100644 --- a/OpenGLEngine/MyBuiltinTexturedScene.cpp +++ b/OpenGLEngine/MyBuiltinTexturedScene.cpp @@ -1,12 +1,18 @@ #include "MyBuiltinTexturedScene.h" #include "MeshGenerator.h" +#include "TextureGenerator.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_shape( + meshgenerator::gen_cube_p(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), + texturegenerator::gen_quick_cube_texture(), + texturegenerator::gen_quick_sampler(), + 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)) { diff --git a/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine.vcxproj index 7aca24e..94642cb 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine.vcxproj @@ -176,6 +176,7 @@ + NotUsing NotUsing @@ -211,7 +212,12 @@ + + + + + diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index fa8a887..e6acbf9 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -171,6 +171,9 @@ Source Files\Example\Application + + Source Files\Engine + @@ -203,9 +206,6 @@ Header Files\Engine\Rendering - - Header Files\Engine\Rendering - Header Files\Engine\Rendering @@ -326,6 +326,24 @@ Header Files\Example\Application + + Header Files\Engine\Rendering + + + Header Files\Engine + + + Header Files\Engine + + + Header Files\Engine + + + Header Files\Engine + + + Header Files\Engine\builtin + diff --git a/OpenGLEngine/Renderable.h b/OpenGLEngine/Renderable.h index c2b3171..c0342dd 100644 --- a/OpenGLEngine/Renderable.h +++ b/OpenGLEngine/Renderable.h @@ -11,7 +11,7 @@ namespace charcoal { template - class Renderable final + class Renderable { public: typedef VertexType VertexType; diff --git a/OpenGLEngine/Sampler.cpp b/OpenGLEngine/Sampler.cpp index a9c7c15..c1121c7 100644 --- a/OpenGLEngine/Sampler.cpp +++ b/OpenGLEngine/Sampler.cpp @@ -6,7 +6,10 @@ namespace charcoal { glGenSamplers(1, &m_sampler); - glSamplerParameteri(m_sampler, GL_WRAP_S, wrap_s); + glSamplerParameteri(m_sampler, GL_TEXTURE_WRAP_S, (GLenum)wrap_s); + glSamplerParameteri(m_sampler, GL_TEXTURE_WRAP_T, (GLenum)wrap_t); + glSamplerParameteri(m_sampler, GL_TEXTURE_MAG_FILTER, (GLenum)magnification_filter); + glSamplerParameteri(m_sampler, GL_TEXTURE_MIN_FILTER, (GLenum)minification_filter); } Sampler::~Sampler() diff --git a/OpenGLEngine/Sampler.h b/OpenGLEngine/Sampler.h index 852ee44..d6a722c 100644 --- a/OpenGLEngine/Sampler.h +++ b/OpenGLEngine/Sampler.h @@ -7,7 +7,7 @@ namespace charcoal class Sampler { public: - enum MinFilter : GLenum + enum class MinFilter : GLenum { NEAREST = GL_NEAREST, LINEAR = GL_LINEAR, @@ -17,13 +17,13 @@ namespace charcoal LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR, }; - enum MagFilter : GLenum + enum class MagFilter : GLenum { NEAREST = GL_NEAREST, LINEAR = GL_LINEAR, }; - enum Wrap : GLenum + enum class Wrap : GLenum { CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE, MIRRORED_REPEAT = GL_MIRRORED_REPEAT, @@ -42,6 +42,8 @@ namespace charcoal void bind(GLuint texture_unit); + GLuint get_sampler() const { return m_sampler; } + private: GLuint m_sampler = 0; }; diff --git a/OpenGLEngine/Texture.h b/OpenGLEngine/Texture.h index d36f252..b8416cd 100644 --- a/OpenGLEngine/Texture.h +++ b/OpenGLEngine/Texture.h @@ -11,7 +11,7 @@ namespace charcoal class Texture { public: - enum Target : GLenum + enum class Target : GLenum { TEXTURE_2D = GL_TEXTURE_2D, PROXY_TEXTURE_2D = GL_PROXY_TEXTURE_2D, @@ -28,7 +28,7 @@ namespace charcoal PROXY_TEXTURE_CUBE_MAP = GL_PROXY_TEXTURE_CUBE_MAP, }; - enum InternalFormat : GLint + enum class InternalFormat : GLint { // Base Internal Formats DEPTH_COMPONENT = GL_DEPTH_COMPONENT, @@ -116,7 +116,7 @@ namespace charcoal COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, }; - enum Format : GLenum + enum class Format : GLenum { RED = GL_RED, RG = GL_RG, @@ -135,7 +135,7 @@ namespace charcoal DEPTH_STENCIL = GL_DEPTH_STENCIL, }; - enum Type : GLenum + enum class Type : GLenum { UNSIGNED_BYTE = GL_UNSIGNED_BYTE, BYTE = GL_BYTE, @@ -167,22 +167,22 @@ namespace charcoal const std::vector& data, InternalFormat internal_format, unsigned short mipmap_level = 0, - Target texture_target = TEXTURE_2D + Target texture_target = Target::TEXTURE_2D ) : width(width), height(height) { - if (texture_target != TEXTURE_2D) + if (texture_target != Target::TEXTURE_2D) throw EXCEPTION("Non 2D textures are not supported yet."); glGenTextures(1, &m_texture); - glBindTexture(texture_target, m_texture); + glBindTexture((GLenum)texture_target, m_texture); - glTexImage2D(texture_target, mipmap_level, internal_format, width, height, 0, data_format, data_type, data); + glTexImage2D((GLenum)texture_target, mipmap_level, (GLint)internal_format, width, height, 0, (GLenum)data_format, (GLenum)data_type, &data[0]); - glGenerateMipmap(texture_target); + glGenerateMipmap((GLenum)texture_target); - glBindTexture(texture_target, NULL); + glBindTexture((GLenum)texture_target, NULL); } ~Texture() @@ -190,6 +190,7 @@ namespace charcoal glDeleteTextures(1, &m_texture); } + GLuint get_texture() const { return m_texture; } private: GLuint m_texture = 0; diff --git a/OpenGLEngine/TextureFactory.h b/OpenGLEngine/TextureFactory.h new file mode 100644 index 0000000..477e829 --- /dev/null +++ b/OpenGLEngine/TextureFactory.h @@ -0,0 +1,47 @@ +#pragma once + +#include + +#include "Texture.h" +#include "Sampler.h" + +namespace charcoal +{ + class TextureFactory + { + private: + TextureFactory() {} + + public: + template + static Texture* gen_texture( + Texture::Format data_format, + Texture::Type data_type, + unsigned long width, + unsigned long height, + const std::vector& data, + Texture::InternalFormat internal_format, + unsigned short mipmap_level = 0, + Texture::Target texture_target = Texture::Target::TEXTURE_2D + ) + { + m_textures.emplace_back(data_format, data_type, width, height, data, internal_format, mipmap_level, texture_target); + return &m_textures.back(); + } + + static Sampler* gen_sampler( + Sampler::Wrap wrap_s, + Sampler::Wrap wrap_t, + Sampler::MagFilter magnification_filter, + Sampler::MinFilter minification_filter + ) + { + m_samplers.emplace_back(wrap_s, wrap_t, magnification_filter, minification_filter); + return &m_samplers.back(); + } + + private: + static std::vector m_textures; + static std::vector m_samplers; + }; +} diff --git a/OpenGLEngine/TextureGenerator.h b/OpenGLEngine/TextureGenerator.h new file mode 100644 index 0000000..d584c27 --- /dev/null +++ b/OpenGLEngine/TextureGenerator.h @@ -0,0 +1,64 @@ +#pragma once + +#include + +#include "Texture.h" +#include "Sampler.h" +#include "TextureFactory.h" + +namespace charcoal +{ + namespace builtin + { + namespace texturegenerator + { + Texture* gen_quick_cube_texture() + { + std::vector values; + + // Front + values.emplace_back(255); + values.emplace_back(255); + values.emplace_back(255); + values.emplace_back(255); + + // Right + values.emplace_back(255); + values.emplace_back(255); + values.emplace_back(0); + values.emplace_back(255); + + // Back + values.emplace_back(255); + values.emplace_back(0); + values.emplace_back(255); + values.emplace_back(255); + + // Left + values.emplace_back(0); + values.emplace_back(255); + values.emplace_back(255); + values.emplace_back(255); + + // Top + values.emplace_back(255); + values.emplace_back(0); + values.emplace_back(0); + values.emplace_back(255); + + // Bottom + values.emplace_back(0); + values.emplace_back(255); + values.emplace_back(0); + values.emplace_back(255); + + return TextureFactory::gen_texture(Texture::Format::RGBA, Texture::Type::UNSIGNED_BYTE, 6, 1, values, Texture::InternalFormat::RGBA); + } + + Sampler* gen_quick_sampler() + { + return TextureFactory::gen_sampler(Sampler::Wrap::REPEAT, Sampler::Wrap::REPEAT, Sampler::MagFilter::LINEAR, Sampler::MinFilter::LINEAR_MIPMAP_LINEAR); + } + } + } +} \ No newline at end of file diff --git a/OpenGLEngine/TextureRenderable.h b/OpenGLEngine/TextureRenderable.h new file mode 100644 index 0000000..7e72d06 --- /dev/null +++ b/OpenGLEngine/TextureRenderable.h @@ -0,0 +1,25 @@ +#pragma once + +#include "Renderable.h" +#include "Texture.h" +#include "Sampler.h" + +namespace charcoal +{ + template + class TextureRenderable : public Renderable + { + public: + TextureRenderable(const Renderable::MeshType* mesh, Texture* texture, Sampler* sampler, const DrawMode& draw_mode) + : Renderable(mesh, draw_mode), m_p_texture(texture), m_p_sampler(sampler) + {} + + const Texture* get_texture() const { return m_p_texture; } + + const Sampler* get_sampler() const { return m_p_sampler; } + + private: + Texture* m_p_texture; + Sampler* m_p_sampler; + }; +} \ No newline at end of file diff --git a/OpenGLEngine/TexturedBatch.cpp b/OpenGLEngine/TexturedBatch.cpp index 38b6178..e2fa913 100644 --- a/OpenGLEngine/TexturedBatch.cpp +++ b/OpenGLEngine/TexturedBatch.cpp @@ -4,6 +4,13 @@ namespace charcoal { namespace builtin { + void TexturedBatch::preprender() const + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, m_p_renderable->get_texture()->get_texture()); + glBindSampler(0, m_p_renderable->get_sampler()->get_sampler()); + } + void TexturedBatch::setup_vao() { glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); diff --git a/OpenGLEngine/TexturedBatch.h b/OpenGLEngine/TexturedBatch.h index efa2ae1..949eb8c 100644 --- a/OpenGLEngine/TexturedBatch.h +++ b/OpenGLEngine/TexturedBatch.h @@ -23,6 +23,8 @@ namespace charcoal ) : PoseableBatch(renderable, element_count, element_render_count) {} + void preprender() const override; + protected: void setup_vao() override; }; diff --git a/OpenGLEngine/TexturedFS.glsl b/OpenGLEngine/TexturedFS.glsl index e3a49de..2129390 100644 --- a/OpenGLEngine/TexturedFS.glsl +++ b/OpenGLEngine/TexturedFS.glsl @@ -1,5 +1,8 @@ #version 430 out vec4 frag_color; + +layout(location = 4) uniform sampler2D tex; + void main() { frag_color = vec4(1.0, 1.0, 1.0, 1.0); diff --git a/OpenGLEngine/TexturedScene.cpp b/OpenGLEngine/TexturedScene.cpp index 1d42020..fd76eec 100644 --- a/OpenGLEngine/TexturedScene.cpp +++ b/OpenGLEngine/TexturedScene.cpp @@ -36,6 +36,7 @@ namespace charcoal glutil::clear_screen(); m_shader_program.use(); glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix()); + glutil::uniform_int(4, 0); // The textured batch uses GL_TEXTURE0 for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) { iter->render();