Skip to content

Commit

Permalink
Added code in to the frame buffer and texture the code should be almo…
Browse files Browse the repository at this point in the history
…st there.
  • Loading branch information
anirul committed Jan 22, 2025
1 parent 8aa6448 commit 26bcfb5
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 157 deletions.
5 changes: 3 additions & 2 deletions asset/json/shadow.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Empty file added asset/shader/opengl/shadow.frag
Empty file.
Empty file added asset/shader/opengl/shadow.vert
Empty file.
17 changes: 10 additions & 7 deletions include/frame/light_interface.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once

#include <cinttypes>
#include <cinttypes>
#include <string>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>

#include "frame/name_interface.h"

namespace frame
{

class CameraInterface;

/**
* @brief This has to correspond to proto::SceneLight::LightTypeEnum!
Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/frame/node_light.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/frame/opengl/bind_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/frame/opengl/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<BufferInterface> CreatePointBuffer(std::vector<float>&& vector);
std::unique_ptr<BufferInterface> CreatePointBuffer(
std::vector<float>&& vector);
/**
* @brief Create an index buffer from a vector of unsigned integer.
* @param device: A pointer to a device.
Expand Down
28 changes: 13 additions & 15 deletions src/frame/opengl/frame_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand Down Expand Up @@ -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(
Expand All @@ -72,7 +70,7 @@ void FrameBuffer::AttachTexture(
UnBind();
}

FrameColorAttachment FrameBuffer::GetFrameColorAttachment(const int i)
FrameColorAttachment FrameBuffer::GetFrameColorAttachment(int i)
{
switch (i)
{
Expand All @@ -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<int>(frame_texture_type);
if (value >= 0)
Expand All @@ -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<FrameTextureType>(i);
}
Expand Down Expand Up @@ -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);
Expand All @@ -151,7 +149,7 @@ void FrameBuffer::DrawBuffers(const std::uint32_t size /*= 1*/)
UnBind();
}

const std::pair<bool, std::string> FrameBuffer::GetError() const
std::pair<bool, std::string> FrameBuffer::GetError() const
{
Bind();
std::pair<bool, std::string> status_error;
Expand Down Expand Up @@ -191,7 +189,7 @@ const std::pair<bool, std::string> FrameBuffer::GetError() const
}
}

const std::string FrameBuffer::GetStatus() const
std::string FrameBuffer::GetStatus() const
{
Bind();
std::stringstream ss;
Expand Down
22 changes: 11 additions & 11 deletions src/frame/opengl/frame_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -78,36 +78,36 @@ 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:
/**
* @brief Convert from OpenGL to FrameColorAttachment.
* @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.
Expand Down Expand Up @@ -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<bool, std::string> GetError() const;
std::pair<bool, std::string> 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;
Expand Down
50 changes: 0 additions & 50 deletions src/frame/opengl/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
45 changes: 31 additions & 14 deletions src/frame/opengl/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string>

#include "frame/light_interface.h"
#include "frame/camera_interface.h"
#include "frame/opengl/program.h"

namespace frame::opengl
Expand Down Expand Up @@ -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_;
};

/**
Expand Down Expand Up @@ -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_;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/frame/opengl/pixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 : " +
Expand Down
Loading

0 comments on commit 26bcfb5

Please sign in to comment.