From 26bcfb51d9cb24981ed9b1e70bb0422a6d2894b5 Mon Sep 17 00:00:00 2001 From: Frederic Dubouchet Date: Wed, 22 Jan 2025 19:24:56 +0100 Subject: [PATCH] Added code in to the frame buffer and texture the code should be almost there. --- asset/json/shadow.json | 5 +- asset/shader/opengl/shadow.frag | 0 asset/shader/opengl/shadow.vert | 0 include/frame/light_interface.h | 17 ++++--- src/frame/node_light.h | 2 +- src/frame/opengl/bind_interface.h | 2 +- src/frame/opengl/buffer.h | 3 +- src/frame/opengl/frame_buffer.cpp | 28 +++++------ src/frame/opengl/frame_buffer.h | 22 ++++---- src/frame/opengl/light.cpp | 50 ------------------- src/frame/opengl/light.h | 45 +++++++++++------ src/frame/opengl/pixel.cpp | 2 +- src/frame/opengl/renderer.cpp | 72 ++++++++++++++++++++++----- src/frame/opengl/renderer.h | 4 +- src/frame/opengl/texture.cpp | 48 +++++++++++++++--- src/frame/opengl/texture.h | 30 +++++++---- src/frame/opengl/texture_cube_map.cpp | 16 +++--- src/frame/opengl/texture_cube_map.h | 26 +++++----- 18 files changed, 215 insertions(+), 157 deletions(-) create mode 100644 asset/shader/opengl/shadow.frag create mode 100644 asset/shader/opengl/shadow.vert diff --git a/asset/json/shadow.json b/asset/json/shadow.json index 0e282760..60df4efe 100644 --- a/asset/json/shadow.json +++ b/asset/json/shadow.json @@ -188,8 +188,9 @@ { "name": "sun", "parent": "root", - "light_type": "DIRECTIONAL", - "shadow_type": "NONE", + "light_type": "DIRECTIONAL_LIGHT", + "shadow_type": "SOFT_SHADOW", + "shadow_texture": "shadow", "position": { "x": -1.0, "y": 1.0, diff --git a/asset/shader/opengl/shadow.frag b/asset/shader/opengl/shadow.frag new file mode 100644 index 00000000..e69de29b diff --git a/asset/shader/opengl/shadow.vert b/asset/shader/opengl/shadow.vert new file mode 100644 index 00000000..e69de29b diff --git a/include/frame/light_interface.h b/include/frame/light_interface.h index 9b31fbae..386e3d00 100644 --- a/include/frame/light_interface.h +++ b/include/frame/light_interface.h @@ -1,6 +1,7 @@ #pragma once -#include +#include +#include #define GLM_ENABLE_EXPERIMENTAL #include @@ -8,8 +9,6 @@ namespace frame { - -class CameraInterface; /** * @brief This has to correspond to proto::SceneLight::LightTypeEnum! @@ -63,11 +62,15 @@ struct LightInterface : public NameInterface */ virtual glm::vec3 GetColorIntensity() const = 0; /** - * @brief Get the shadow view. - * @param camera: Camera use to render the scene. - * @return Return the shadow view. + * @brief Set the shadow map texture name. + * @param name: Name of the shadow map texture. + */ + virtual void SetShadowMapTextureName(const std::string& name) = 0; + /** + * @brief Get the shadow map texture name. + * @return Name of the shadow map texture. */ - virtual glm::mat4 ComputeView(const CameraInterface& camera) const = 0; + virtual std::string GetShadowMapTextureName() const = 0; }; } // End namespace frame. diff --git a/src/frame/node_light.h b/src/frame/node_light.h index 0d37e947..d9a5847b 100644 --- a/src/frame/node_light.h +++ b/src/frame/node_light.h @@ -83,7 +83,7 @@ class NodeLight : public NodeInterface /** * @brief Create a spot light. * @param func: This function return the ID from a string (it will need - * a level passed in the capture list). + * a level passed in the capture list). * @param shadow_type: Type of shadow used. * @param shadow_texture: Name of the texture to render for the shadows. * @param position: Position of the spot light. diff --git a/src/frame/opengl/bind_interface.h b/src/frame/opengl/bind_interface.h index 8c0ecaf8..2c0c3ac8 100644 --- a/src/frame/opengl/bind_interface.h +++ b/src/frame/opengl/bind_interface.h @@ -16,7 +16,7 @@ struct BindInterface * @brief Bind the underlying interface to the current context. * @param slot: in case needed (see textures) the slot can be specified. */ - virtual void Bind(const unsigned int slot = 0) const = 0; + virtual void Bind(unsigned int slot = 0) const = 0; //! @brief Unbind free the resource from the current context. virtual void UnBind() const = 0; //! @brief same as Bind but used by the auto lock system see scoped bind diff --git a/src/frame/opengl/buffer.h b/src/frame/opengl/buffer.h index 89baf317..8c900e3c 100644 --- a/src/frame/opengl/buffer.h +++ b/src/frame/opengl/buffer.h @@ -162,7 +162,8 @@ class Buffer : public BindInterface, public BufferInterface * @param device: A pointer to a device. * @param vector: A vector that is moved into the device and level. */ -std::unique_ptr CreatePointBuffer(std::vector&& vector); +std::unique_ptr CreatePointBuffer( + std::vector&& vector); /** * @brief Create an index buffer from a vector of unsigned integer. * @param device: A pointer to a device. diff --git a/src/frame/opengl/frame_buffer.cpp b/src/frame/opengl/frame_buffer.cpp index cae2eeca..f4daf630 100644 --- a/src/frame/opengl/frame_buffer.cpp +++ b/src/frame/opengl/frame_buffer.cpp @@ -21,7 +21,7 @@ FrameBuffer::~FrameBuffer() glDeleteFramebuffers(1, &frame_id_); } -void FrameBuffer::Bind(const unsigned int slot /*= 0*/) const +void FrameBuffer::Bind(unsigned int slot /*= 0*/) const { assert(slot == 0); if (locked_bind_) @@ -54,13 +54,11 @@ void FrameBuffer::AttachRender(const RenderBuffer& render) const void FrameBuffer::AttachTexture( unsigned int texture_id, - const FrameColorAttachment frame_color_attachment /*= - FrameColorAttachment::COLOR_ATTACHMENT0*/ - , - const FrameTextureType frame_texture_type /*= - FrameTextureType::TEXTURE_2D*/ - , - const int mipmap /*= 0*/) const + FrameColorAttachment frame_color_attachment /*= + FrameColorAttachment::COLOR_ATTACHMENT0*/, + FrameTextureType frame_texture_type /*= + FrameTextureType::TEXTURE_2D*/, + int mipmap /*= 0*/) const { Bind(); glFramebufferTexture2D( @@ -72,7 +70,7 @@ void FrameBuffer::AttachTexture( UnBind(); } -FrameColorAttachment FrameBuffer::GetFrameColorAttachment(const int i) +FrameColorAttachment FrameBuffer::GetFrameColorAttachment(int i) { switch (i) { @@ -97,8 +95,8 @@ FrameColorAttachment FrameBuffer::GetFrameColorAttachment(const int i) } } -const int FrameBuffer::GetFrameTextureType( - const FrameTextureType frame_texture_type) const +int FrameBuffer::GetFrameTextureType( + FrameTextureType frame_texture_type) const { int value = static_cast(frame_texture_type); if (value >= 0) @@ -108,7 +106,7 @@ const int FrameBuffer::GetFrameTextureType( return GL_TEXTURE_2D; } -FrameTextureType FrameBuffer::GetFrameTextureType(const int i) +FrameTextureType FrameBuffer::GetFrameTextureType(int i) { return static_cast(i); } @@ -137,7 +135,7 @@ frame::opengl::FrameTextureType FrameBuffer::GetFrameTextureType( } } -void FrameBuffer::DrawBuffers(const std::uint32_t size /*= 1*/) +void FrameBuffer::DrawBuffers(std::uint32_t size /*= 1*/) { Bind(); assert(size < 9); @@ -151,7 +149,7 @@ void FrameBuffer::DrawBuffers(const std::uint32_t size /*= 1*/) UnBind(); } -const std::pair FrameBuffer::GetError() const +std::pair FrameBuffer::GetError() const { Bind(); std::pair status_error; @@ -191,7 +189,7 @@ const std::pair FrameBuffer::GetError() const } } -const std::string FrameBuffer::GetStatus() const +std::string FrameBuffer::GetStatus() const { Bind(); std::stringstream ss; diff --git a/src/frame/opengl/frame_buffer.h b/src/frame/opengl/frame_buffer.h index 3e04af28..eaf87357 100644 --- a/src/frame/opengl/frame_buffer.h +++ b/src/frame/opengl/frame_buffer.h @@ -54,7 +54,7 @@ class FrameBuffer : public BindInterface * with frame buffers. * @param slot: Should be ignored. */ - void Bind(const unsigned int slot = 0) const override; + void Bind(unsigned int slot = 0) const override; //! @brief From the bind interface this will unbind the current frame //! buffer from the context. void UnBind() const override; @@ -78,22 +78,22 @@ class FrameBuffer : public BindInterface */ void AttachTexture( unsigned int texture_id, - const FrameColorAttachment frame_color_attachment = + FrameColorAttachment frame_color_attachment = FrameColorAttachment::COLOR_ATTACHMENT0, - const FrameTextureType frame_texture_type = + FrameTextureType frame_texture_type = FrameTextureType::TEXTURE_2D, - const int mipmap = 0) const; + int mipmap = 0) const; /** * @brief Define an array of buffers into which outputs from the * fragment shader data will be written. * @param size: Which draw buffer should be drawn upon [1, 8]. */ - void DrawBuffers(const std::uint32_t size = 1); + void DrawBuffers(std::uint32_t size = 1); /** * @brief For debug return a string of a potential error. * @return String that contain the error (if any). */ - const std::string GetStatus() const; + std::string GetStatus() const; public: /** @@ -101,13 +101,13 @@ class FrameBuffer : public BindInterface * @param i: OpenGL frame color attachment. * @return Frame color attachment in local system. */ - static FrameColorAttachment GetFrameColorAttachment(const int i); + static FrameColorAttachment GetFrameColorAttachment(int i); /** * @brief Convert from OpenGL to frame texture type. * @param i: OpenGL frame texture type. * @return Frame texture type in local system. */ - static FrameTextureType GetFrameTextureType(const int i); + static FrameTextureType GetFrameTextureType(int i); /** * @brief Convert from proto texture frame to frame texture type. * @param texture_frame: Proto texture frame. @@ -140,10 +140,10 @@ class FrameBuffer : public BindInterface protected: // This was here from the error interface (not used anymore) but it is // still used inside. - const std::pair GetError() const; + std::pair GetError() const; // Convert from the internal frame texture type to the OpenGL type. - const int GetFrameTextureType( - const FrameTextureType frame_texture_type) const; + int GetFrameTextureType( + FrameTextureType frame_texture_type) const; protected: friend class ScopedBind; diff --git a/src/frame/opengl/light.cpp b/src/frame/opengl/light.cpp index 61256245..8a913a7b 100644 --- a/src/frame/opengl/light.cpp +++ b/src/frame/opengl/light.cpp @@ -6,56 +6,6 @@ namespace frame::opengl { - -glm::mat4 LightPoint::ComputeView(const CameraInterface& camera) const -{ - // 1. Use the point light’s world position (assuming GetVector() is - // position). - glm::vec3 lightPos = GetVector(); - - // 2. Use the camera’s orientation - glm::vec3 front = glm::normalize(camera.GetFront()); - glm::vec3 up = glm::normalize(camera.GetUp()); - - // 3. Build a single view matrix - // We’ll “look” in the camera’s front direction from the light’s - // position. This only makes sense if you want a single direction of - // shadow from the point light. (For a true cubemap, you do 6 passes or - // something else.) - return glm::lookAt( - lightPos, // eye - lightPos + front, // target - up); -} - -glm::mat4 LightDirectional::ComputeView(const CameraInterface& camera) const -{ - // 1. The light’s direction from your proto (already stored in - // LightDirectional). - // e.g. "GetVector()" returns the directional vector. - glm::vec3 dir = glm::normalize(GetVector()); - - // 2. Use the camera’s up vector so shadows remain consistent with how the - // camera is oriented. - glm::vec3 up = glm::normalize(camera.GetUp()); - - // 3. Pick a “center” from the camera’s position (very naive): - // In many engines, we might actually compute a bounding box around what - // the camera sees, then figure out that box’s center. - glm::vec3 center = camera.GetPosition(); - - // 4. Position the light’s “eye” behind ‘center’ along ‘dir’. - // The distance is somewhat arbitrary or based on your scene size. - float distance = 100.0f; // adjust as needed - glm::vec3 eye = center - dir * distance; - - // 5. Build the view matrix - return glm::lookAt( - eye, // “eye” or camera position - center, // “center,” the point the light camera looks at - up // up vector from the real camera - ); -} void LightManager::RegisterToProgram(Program& program) const { diff --git a/src/frame/opengl/light.h b/src/frame/opengl/light.h index 9821c1e8..c85169de 100644 --- a/src/frame/opengl/light.h +++ b/src/frame/opengl/light.h @@ -5,7 +5,6 @@ #include #include "frame/light_interface.h" -#include "frame/camera_interface.h" #include "frame/opengl/program.h" namespace frame::opengl @@ -85,20 +84,29 @@ class LightPoint : public LightInterface { return color_intensity_; } - - public: /** - * @brief Get the shadow view. - * @param camera: Camera use to render the scene. - * @return Return the shadow view. + * @brief Set the shadow map texture name. + * @param name: Name of the shadow map texture. */ - glm::mat4 ComputeView(const CameraInterface& camera) const; + void SetShadowMapTextureName(const std::string& name) + { + shadow_map_texture_name_ = name; + } + /** + * @brief Get the shadow map texture name. + * @return Name of the shadow map texture. + */ + std::string GetShadowMapTextureName() const + { + return shadow_map_texture_name_; + } protected: glm::vec3 position_; glm::vec3 color_intensity_; ShadowTypeEnum shadow_type_enum_ = ShadowTypeEnum::NO_SHADOW; - std::string name_; + std::string name_; + std::string shadow_map_texture_name_; }; /** @@ -175,20 +183,29 @@ class LightDirectional : public LightInterface { return color_intensity_; } - - public: /** - * @brief Get the shadow view. - * @param camera: Camera use to render the scene. - * @return Return the shadow view. + * @brief Set the shadow map texture name. + * @param name: Name of the shadow map texture. + */ + void SetShadowMapTextureName(const std::string& name) + { + shadow_map_texture_name_ = name; + } + /** + * @brief Get the shadow map texture name. + * @return Name of the shadow map texture. */ - glm::mat4 ComputeView(const CameraInterface& camera) const; + std::string GetShadowMapTextureName() const + { + return shadow_map_texture_name_; + } protected: glm::vec3 direction_; glm::vec3 color_intensity_; ShadowTypeEnum shadow_type_enum_ = ShadowTypeEnum::NO_SHADOW; std::string name_; + std::string shadow_map_texture_name_; }; /** diff --git a/src/frame/opengl/pixel.cpp b/src/frame/opengl/pixel.cpp index 35ee6315..c7036b2e 100644 --- a/src/frame/opengl/pixel.cpp +++ b/src/frame/opengl/pixel.cpp @@ -19,7 +19,7 @@ GLenum ConvertToGLType(const frame::proto::PixelElementSize& pixel_element_size) case frame::proto::PixelElementSize::HALF: return GL_FLOAT; // Not half float. case frame::proto::PixelElementSize::FLOAT: - return GL_FLOAT; + return GL_FLOAT; default: throw std::runtime_error( "unknown element size : " + diff --git a/src/frame/opengl/renderer.cpp b/src/frame/opengl/renderer.cpp index 649cc865..13e5db0c 100644 --- a/src/frame/opengl/renderer.cpp +++ b/src/frame/opengl/renderer.cpp @@ -54,10 +54,12 @@ const glm::mat4 projection_cubemap = Renderer::Renderer(LevelInterface& level, glm::uvec4 viewport) : level_(level), viewport_(viewport) { + frame_buffer_ = std::make_unique(); + render_buffer_ = std::make_unique(); // TODO(anirul): Check viewport!!! - render_buffer_.CreateStorage( + render_buffer_->CreateStorage( {viewport_.z - viewport_.x, viewport_.w - viewport_.y}); - frame_buffer_.AttachRender(render_buffer_); + frame_buffer_->AttachRender(*render_buffer_); auto program = file::LoadProgram("display"); if (!program) throw std::runtime_error("No program!"); @@ -158,7 +160,7 @@ void Renderer::RenderMesh( glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w); - ScopedBind scoped_frame(frame_buffer_); + ScopedBind scoped_frame(*frame_buffer_); int i = 0; for (const auto& texture_id : program.GetOutputTextureIds()) { @@ -168,7 +170,7 @@ void Renderer::RenderMesh( dynamic_cast(level_.GetTextureFromId( texture_id)); // TODO(anirul): Check the mipmap level (last parameter)! - frame_buffer_.AttachTexture( + frame_buffer_->AttachTexture( opengl_texture.GetId(), FrameBuffer::GetFrameColorAttachment(i), FrameBuffer::GetFrameTextureType(texture_frame_), @@ -179,7 +181,7 @@ void Renderer::RenderMesh( auto& opengl_texture = dynamic_cast(level_.GetTextureFromId(texture_id)); // TODO(anirul): Check the mipmap level (last parameter)! - frame_buffer_.AttachTexture( + frame_buffer_->AttachTexture( opengl_texture.GetId(), FrameBuffer::GetFrameColorAttachment(i), FrameTextureType::TEXTURE_2D, @@ -187,7 +189,7 @@ void Renderer::RenderMesh( } i++; } - frame_buffer_.DrawBuffers( + frame_buffer_->DrawBuffers( static_cast(texture_out_ids.size())); std::map> uniform_include; @@ -403,17 +405,41 @@ void Renderer::RenderShadows(const CameraInterface& camera) { throw std::runtime_error("No shadow material id."); } - // Clear the depth buffer only once per light. - glClear(GL_DEPTH_BUFFER_BIT); auto& light = level_.GetLightFromId(light_id); // Check if light has shadow. if (light.GetShadowType() == ShadowTypeEnum::NO_SHADOW) { continue; } - + auto texture_name = light.GetShadowMapTextureName(); + auto texture_id = level_.GetIdFromName(texture_name); + if (texture_id == NullId) + { + throw std::runtime_error(fmt::format( + "Couldn't find texture {} for light {}", + texture_name, + light.GetName())); + } + // Save the current context. + std::unique_ptr temp_frame_buffer = + std::move(frame_buffer_); + auto temp_viewport = viewport_; + frame_buffer_ = std::make_unique(); + auto& texture_interface = level_.GetTextureFromId(texture_id); + auto size = texture_interface.GetSize(); + viewport_ = glm::ivec4(0, 0, size.x, size.y); if (light.GetType() == LightTypeEnum::DIRECTIONAL_LIGHT) { + Texture& depth_texture = + dynamic_cast(level_.GetTextureFromId(texture_id)); + frame_buffer_->AttachTexture( + depth_texture.GetId(), + static_cast(GL_DEPTH_ATTACHMENT), + FrameTextureType::TEXTURE_2D); + frame_buffer_->Bind(); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glClear(GL_DEPTH_BUFFER_BIT); // For every mesh / material pair in the scene. for (const auto& p : level_.GetStaticMeshMaterialIds( proto::SceneStaticMesh::SHADOW_RENDER_TIME)) @@ -428,13 +454,29 @@ void Renderer::RenderShadows(const CameraInterface& camera) // Render it acording to the light position. glm::mat4 proj = glm::ortho(-50.f, 50.f, -50.f, 50.f, 0.1f, 1000.f); - glm::mat4 view = light.ComputeView(camera); + glm::vec3 dir = glm::normalize(light.GetVector()); + glm::vec3 up = glm::normalize(camera.GetUp()); + glm::vec3 center = camera.GetPosition(); + float distance = 100.0f; + glm::vec3 eye = center - dir * distance; + glm::mat4 view = glm::lookAt(eye, center, up); RenderNode(p.first, shadow_material_id, proj, view); } - // TODO add the depth map to the light shadow map list. + frame_buffer_->UnBind(); } if (light.GetType() == LightTypeEnum::POINT_LIGHT) { + TextureCubeMap& depth_texture = + dynamic_cast( + level_.GetTextureFromId(texture_id)); + frame_buffer_->AttachTexture( + depth_texture.GetId(), + static_cast(GL_DEPTH_ATTACHMENT), + FrameTextureType::TEXTURE_2D); + frame_buffer_->Bind(); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glClear(GL_DEPTH_BUFFER_BIT); for (int i = 0; i < 6; ++i) { // For every mesh / material pair in the scene. @@ -458,10 +500,14 @@ void Renderer::RenderShadows(const CameraInterface& camera) glm::translate(glm::mat4(1.0f), -light.GetVector()); glm::mat4 view = rotation * translation; RenderNode(p.first, shadow_material_id, proj, view); - } - // TODO add the depth map to the light shadow map list. + } } + frame_buffer_->UnBind(); } + // Restore the context. + viewport_ = temp_viewport; + frame_buffer_.reset(); + frame_buffer_ = std::move(temp_frame_buffer); } } diff --git a/src/frame/opengl/renderer.h b/src/frame/opengl/renderer.h index dbb4130d..28f67177 100644 --- a/src/frame/opengl/renderer.h +++ b/src/frame/opengl/renderer.h @@ -137,8 +137,8 @@ class Renderer : public RendererInterface // Viewport top left and bottom right. glm::uvec4 viewport_; // Frame & Render buffers. - FrameBuffer frame_buffer_{}; - RenderBuffer render_buffer_{}; + std::unique_ptr frame_buffer_{nullptr}; + std::unique_ptr render_buffer_{nullptr}; // Display ids. EntityId display_program_id_ = 0; EntityId display_material_id_ = 0; diff --git a/src/frame/opengl/texture.cpp b/src/frame/opengl/texture.cpp index 9c359c5f..9b11bd8c 100644 --- a/src/frame/opengl/texture.cpp +++ b/src/frame/opengl/texture.cpp @@ -25,7 +25,17 @@ Texture::Texture(const TextureParameter& texture_parameter) pixel_structure_(texture_parameter.pixel_structure) { assert(texture_parameter.map_type == TextureTypeEnum::TEXTURE_2D); - CreateTexture(texture_parameter.data_ptr); + if (texture_parameter.pixel_structure.value() == + proto::PixelStructure::DEPTH) + { + CreateDepthTexture( + texture_parameter.size, + texture_parameter.pixel_element_size); + } + else + { + CreateTexture(texture_parameter.data_ptr); + } } void Texture::CreateTexture(const void* data /* = nullptr*/) @@ -48,6 +58,30 @@ void Texture::CreateTexture(const void* data /* = nullptr*/) format, type, data); +} + +void Texture::CreateDepthTexture( + glm::uvec2 size, + proto::PixelElementSize pixel_element_size /* = + proto::PixelElementSize_FLOAT()*/) +{ + size_ = size; + glGenTextures(1, &texture_id_); + ScopedBind scoped_bind(*this); + SetMinFilter(proto::TextureFilter::NEAREST); + SetMagFilter(proto::TextureFilter::NEAREST); + SetWrapS(proto::TextureFilter::CLAMP_TO_EDGE); + SetWrapT(proto::TextureFilter::CLAMP_TO_EDGE); + glTexImage2D( + GL_TEXTURE_2D, + 0, + GL_DEPTH_COMPONENT32F, + static_cast(size_.x), + static_cast(size_.y), + 0, + GL_DEPTH_COMPONENT, + GL_FLOAT, + nullptr); } Texture::~Texture() @@ -55,7 +89,7 @@ Texture::~Texture() glDeleteTextures(1, &texture_id_); } -void Texture::Bind(const unsigned int slot /*= 0*/) const +void Texture::Bind(unsigned int slot /*= 0*/) const { if (locked_bind_) return; @@ -76,7 +110,7 @@ void Texture::EnableMipmap() const glGenerateMipmap(GL_TEXTURE_2D); } -void Texture::SetMinFilter(const proto::TextureFilter::Enum texture_filter) +void Texture::SetMinFilter(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -93,7 +127,7 @@ proto::TextureFilter::Enum Texture::GetMinFilter() const return ConvertFromGLType(filter); } -void Texture::SetMagFilter(const proto::TextureFilter::Enum texture_filter) +void Texture::SetMagFilter(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -110,7 +144,7 @@ proto::TextureFilter::Enum Texture::GetMagFilter() const return ConvertFromGLType(filter); } -void Texture::SetWrapS(const proto::TextureFilter::Enum texture_filter) +void Texture::SetWrapS(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -127,7 +161,7 @@ proto::TextureFilter::Enum Texture::GetWrapS() const return ConvertFromGLType(filter); } -void Texture::SetWrapT(const proto::TextureFilter::Enum texture_filter) +void Texture::SetWrapT(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -154,7 +188,7 @@ void Texture::CreateFrameAndRenderBuffer() frame_->DrawBuffers(1); } -void Texture::Clear(const glm::vec4 color) +void Texture::Clear(glm::vec4 color) { // First time this is called this will create a frame and a render. Bind(); diff --git a/src/frame/opengl/texture.h b/src/frame/opengl/texture.h index 7442e5f0..d5e15332 100644 --- a/src/frame/opengl/texture.h +++ b/src/frame/opengl/texture.h @@ -55,7 +55,7 @@ class Texture : public TextureInterface, public BindInterface * the current context. * @param slot: Slot to be binded. */ - void Bind(const unsigned int slot = 0) const override; + void Bind(unsigned int slot = 0) const override; /** * @brief From the bind interface this will unbind the current texture * from the context. @@ -86,7 +86,7 @@ class Texture : public TextureInterface, public BindInterface * @brief Clear the texture (this is highly inefficient). * @param color: Color to paint the texture to. */ - void Clear(const glm::vec4 color) override; + void Clear(glm::vec4 color) override; /** * @brief Enable mipmap, this allow a recursive level of texture faster * for rendering. @@ -96,7 +96,7 @@ class Texture : public TextureInterface, public BindInterface * @brief Set the minification filter. * @param texture_filter: Usually and by default GL_LINEAR. */ - void SetMinFilter(const proto::TextureFilter::Enum texture_filter) override; + void SetMinFilter(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the minification filter. * @return The value of the minification filter. @@ -106,7 +106,7 @@ class Texture : public TextureInterface, public BindInterface * @brief Set the magnification filter. * @param texture_filter: Usually and by default GL_LINEAR. */ - void SetMagFilter(const proto::TextureFilter::Enum texture_filter) override; + void SetMagFilter(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the magnification filter. * @return The value of the magnification filter. @@ -119,7 +119,7 @@ class Texture : public TextureInterface, public BindInterface * @param texture_filter: Could be any of (REPEAT, CLAMP_TO_EDGE, * MIRRORED_REPEAT). */ - void SetWrapS(const proto::TextureFilter::Enum texture_filter) override; + void SetWrapS(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the wrapping on the s size of the texture (horizontal). * @return The way the texture is wrap could be any of (REPEAT, @@ -133,7 +133,7 @@ class Texture : public TextureInterface, public BindInterface * @param texture_filter: Could be any of (REPEAT, CLAMP_TO_EDGE, * MIRRORED_REPEAT). */ - void SetWrapT(const proto::TextureFilter::Enum texture_filter) override; + void SetWrapT(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the wrapping on the t size of the texture (vertical). * @return The way the texture is wrap could be any of (REPEAT, @@ -212,11 +212,11 @@ class Texture : public TextureInterface, public BindInterface protected: /** * @brief Protected constructor this is a way to create a simple - * structure from inside the class. + * structure from inside the class. * @param pixel_element_size: Size of one of the element in a pixel - * (BYTE, SHORT, HALF, FLOAT). + * (BYTE, SHORT, HALF, FLOAT). * @param pixel_element_structure: Structure of a pixel (R, RG, RGB, - * RGBA). + * RGBA). */ Texture( const proto::PixelElementSize pixel_element_size = @@ -229,10 +229,20 @@ class Texture : public TextureInterface, public BindInterface } /** * @brief Fill a texture with a data pointer, the size has to be set - * first! + * first! * @param data: pixel used to fill up (or null for don't care). */ void CreateTexture(const void* data = nullptr); + /** + * @brief Create a depth texture. + * @param size: Size of the texture. + * @param pixel_element_size: Size of the pixel element (this should be + * FLOAT). + */ + void CreateDepthTexture( + glm::uvec2 size, + proto::PixelElementSize pixel_element_size = + proto::PixelElementSize_FLOAT()); //! @brief Lock the bind for RAII interface to the bind interface. void LockedBind() const override { diff --git a/src/frame/opengl/texture_cube_map.cpp b/src/frame/opengl/texture_cube_map.cpp index df99c2c1..cfbb1096 100644 --- a/src/frame/opengl/texture_cube_map.cpp +++ b/src/frame/opengl/texture_cube_map.cpp @@ -63,7 +63,7 @@ TextureCubeMap::TextureCubeMap(const TextureParameter& texture_parameter) CreateTextureCubeMap(texture_parameter.array_data_ptr); } -void TextureCubeMap::Bind(const unsigned int slot /*= 0*/) const +void TextureCubeMap::Bind(unsigned int slot /*= 0*/) const { assert(slot < GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS); if (locked_bind_) @@ -84,8 +84,7 @@ void TextureCubeMap::EnableMipmap() const glGenerateMipmap(GL_TEXTURE_CUBE_MAP); } -void TextureCubeMap::SetMinFilter( - const proto::TextureFilter::Enum texture_filter) +void TextureCubeMap::SetMinFilter(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -104,8 +103,7 @@ frame::proto::TextureFilter::Enum TextureCubeMap::GetMinFilter() const return ConvertFromGLType(filter); } -void TextureCubeMap::SetMagFilter( - const proto::TextureFilter::Enum texture_filter) +void TextureCubeMap::SetMagFilter(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -124,7 +122,7 @@ frame::proto::TextureFilter::Enum TextureCubeMap::GetMagFilter() const return ConvertFromGLType(filter); } -void TextureCubeMap::SetWrapS(const proto::TextureFilter::Enum texture_filter) +void TextureCubeMap::SetWrapS(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -143,7 +141,7 @@ frame::proto::TextureFilter::Enum TextureCubeMap::GetWrapS() const return ConvertFromGLType(filter); } -void TextureCubeMap::SetWrapT(const proto::TextureFilter::Enum texture_filter) +void TextureCubeMap::SetWrapT(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -162,7 +160,7 @@ frame::proto::TextureFilter::Enum TextureCubeMap::GetWrapT() const return ConvertFromGLType(filter); } -void TextureCubeMap::SetWrapR(const proto::TextureFilter::Enum texture_filter) +void TextureCubeMap::SetWrapR(proto::TextureFilter::Enum texture_filter) { Bind(); glTexParameteri( @@ -275,7 +273,7 @@ void TextureCubeMap::CreateFrameAndRenderBuffer() frame_->DrawBuffers(1); } -void TextureCubeMap::Clear(const glm::vec4 color) +void TextureCubeMap::Clear(glm::vec4 color) { // First time this is called this will create a frame and a render. Bind(); diff --git a/src/frame/opengl/texture_cube_map.h b/src/frame/opengl/texture_cube_map.h index b8e9850c..aabed324 100644 --- a/src/frame/opengl/texture_cube_map.h +++ b/src/frame/opengl/texture_cube_map.h @@ -83,7 +83,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * @brief Clear the texture (this is highly inefficient). * @param color: Color to paint the texture to. */ - void Clear(const glm::vec4 color) override; + void Clear(glm::vec4 color) override; /** * @brief Enable mip map, this allow a recursive level of texture faster * for rendering. @@ -94,7 +94,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * the current context. * @param slot: Slot to be binded. */ - void Bind(const unsigned int slot = 0) const override; + void Bind(unsigned int slot = 0) const override; /** * @brief From the bind interface this will unbind the current texture * from the context. @@ -104,7 +104,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * @brief Set the minification filter. * @param texture_filter: Usually and by default GL_LINEAR. */ - void SetMinFilter(const proto::TextureFilter::Enum texture_filter) override; + void SetMinFilter(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the minification filter. * @return The value of the minification filter. @@ -114,7 +114,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * @brief Set the magnification filter. * @param texture_filter: Usually and by default GL_LINEAR. */ - void SetMagFilter(const proto::TextureFilter::Enum texture_filter) override; + void SetMagFilter(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the magnification filter. * @return The value of the magnification filter. @@ -127,7 +127,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * @param texture_filter: Could be any of (REPEAT, CLAMP_TO_EDGE, * MIRRORED_REPEAT). */ - void SetWrapS(const proto::TextureFilter::Enum texture_filter) override; + void SetWrapS(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the wrapping on the s size of the texture (horizontal). * @return The way the texture is wrap could be any of (REPEAT, @@ -141,7 +141,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * @param texture_filter: Could be any of (REPEAT, CLAMP_TO_EDGE, * MIRRORED_REPEAT). */ - void SetWrapT(const proto::TextureFilter::Enum texture_filter) override; + void SetWrapT(proto::TextureFilter::Enum texture_filter) override; /** * @brief Get the wrapping on the t size of the texture (vertical). * @return The way the texture is wrap could be any of (REPEAT, @@ -155,7 +155,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface * @param texture_filter: Could be any of (REPEAT, CLAMP_TO_EDGE, * MIRRORED_REPEAT). */ - void SetWrapR(const proto::TextureFilter::Enum texture_filter); + void SetWrapR(proto::TextureFilter::Enum texture_filter); /** * @brief Get the wrapping on the t size of the texture (vertical). * @return The way the texture is wrap could be any of (REPEAT, @@ -200,7 +200,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface } /** * @brief Return the texture cube map OpenGL id, from the bind - * interface. + * interface. * @return Get the OpenGL id of the texture. */ unsigned int GetId() const override @@ -217,7 +217,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface } /** * @brief Get the pixel element size individual element (BYTE, SHORT, - * LONG, FLOAT). + * LONG, FLOAT). * @return The pixel element size. */ proto::PixelElementSize::Enum GetPixelElementSize() const override @@ -236,11 +236,11 @@ class TextureCubeMap : public TextureInterface, public BindInterface protected: /** * @brief Protected constructor this is a way to create a simple - * structure from inside the class. + * structure from inside the class. * @param pixel_element_size: Size of one of the element in a pixel - * (BYTE, SHORT, HALF, FLOAT). + * (BYTE, SHORT, HALF, FLOAT). * @param pixel_element_structure: Structure of a pixel (R, RG, RGB, - * RGBA). + * RGBA). */ TextureCubeMap( const proto::PixelElementSize pixel_element_size = @@ -253,7 +253,7 @@ class TextureCubeMap : public TextureInterface, public BindInterface } /** * @brief Fill a texture with a data pointer, the size has to be set - * first! + * first! * @param data: pixel used to fill up (or null for don't care). */ void CreateTextureCubeMap(