small refactor
This commit is contained in:
@@ -17,7 +17,7 @@ include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME mcolor
|
||||
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-4.zip
|
||||
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-5.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
|
@@ -40,6 +40,8 @@ namespace JGL::Fonts {
|
||||
namespace JGL::ShapeCache {
|
||||
inline VRamList* cube_vertex_data = nullptr;
|
||||
inline VRamList* cube_index_data = nullptr;
|
||||
// Facing straight out.
|
||||
inline VRamList* j2d_default_normal_data = nullptr;
|
||||
void Init();
|
||||
}
|
||||
|
||||
@@ -75,7 +77,10 @@ namespace JGL::J2D {
|
||||
|
||||
/// Provide a list of lights to be used in 2D space. Typically directly after J2D::Begin();
|
||||
/// 8 lights maximum for now. Some kind of light sorting will eventually be needed per j2d element.
|
||||
void LightArray(LightBase*, size_t light_count);
|
||||
void LightArray(const LightBase** lights, const size_t& light_count);
|
||||
|
||||
/// Specifies a light which is required for every object in the scene.
|
||||
void RequiredLight(const LightBase* light);
|
||||
|
||||
/// Plots a single pixel on the screen.
|
||||
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
|
||||
|
@@ -1,20 +1,21 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <glad/glad.h>
|
||||
#include <Color4.hpp>
|
||||
#include <Colors.hpp>
|
||||
#include <JGL/types/Enums.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <J3ML/LinearAlgebra/Vector2i.hpp>
|
||||
|
||||
namespace JGL {
|
||||
class RenderTarget;
|
||||
class Texture; // Forward declare.
|
||||
class Texture;
|
||||
}
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2i;
|
||||
class JGL::RenderTarget {
|
||||
private:
|
||||
Color4 clear_color{0,0,0,0};
|
||||
/// "Size" in this sense is the "Renderable Area" because OpenGL textures behave strangely if they're not square.
|
||||
Vector2 size{0, 0};
|
||||
Vector2i size{0, 0};
|
||||
bool using_depth = false;
|
||||
bool texture_created_by_us = false;
|
||||
GLuint framebuffer_object = 0;
|
||||
@@ -26,47 +27,90 @@ private:
|
||||
GLuint msaa_render_buffer = 0;
|
||||
void Erase();
|
||||
public:
|
||||
/// @returns The Render Target currently in use by OpenGL.
|
||||
/// @note Zero is the screen.
|
||||
static GLuint GetActiveGLFramebufferHandle();
|
||||
|
||||
/// Changes the Render Target that OpenGL will draw on.
|
||||
/// @param render_target The new Render Target for OpenGL to draw on.
|
||||
static void SetActiveGLRenderTarget(const RenderTarget& render_target);
|
||||
|
||||
/** Change the size of the renderable area of the Render Target. **/
|
||||
/// @param new_size new size in px.
|
||||
void Resize(const Vector2& new_size);
|
||||
void SetMSAAEnabled(MSAA_SAMPLE_RATE sample_rate);
|
||||
/// If you're using raw OpenGL commands to draw to this outside of J2D or J3D don't forget to do this.
|
||||
/// Blits the MSAA FBO onto the regular FBO if MSAA is enabled and or If you're rendering to a texture which uses mipmaps,
|
||||
/// It regenerates them so what you drew doesn't disappear at a distance. Otherwise it does nothing.
|
||||
void Blit() const;
|
||||
/// Changes the size of the renderable area of this Render Target.
|
||||
/// @param new_size new width & height in pixels.
|
||||
/// @note The data stored in this Render Target will be lost.
|
||||
void Resize(const Vector2i& new_size);
|
||||
|
||||
/// Blit a render target onto another. Will break if they're not the same size.
|
||||
/// Sets the MSAA mode for this Render Target.
|
||||
/// @returns false if the mode isn't available, true for success.
|
||||
[[nodiscard]] bool SetMSAAEnabled(MSAA_SAMPLE_RATE sample_rate);
|
||||
|
||||
/// If you're using MSAA and not using J2D || J3D Begin & End you must do this.
|
||||
void MSAABlit() const;
|
||||
|
||||
/// Copy one Render Target onto another. Will break if they're not the same size.
|
||||
// TODO support different sizes. If the destination is too small fix it for them but log a warning.
|
||||
static void Blit(const RenderTarget& source, RenderTarget* destination);
|
||||
|
||||
/// Blit a single pixel onto a Render Target.
|
||||
/// Plots a single pixel onto a Render Target.
|
||||
/// @param color The color to render.
|
||||
/// @param position The position in the destination to draw the pixel.
|
||||
/// @param destination The destination RenderTarget.
|
||||
static void Blit(const Color4& color, const Vector2& position, RenderTarget* destination);
|
||||
[[nodiscard]] bool TextureCreatedByRenderTarget() const;
|
||||
static void Blit(const Color4& color, const Vector2i& position, RenderTarget* destination);
|
||||
|
||||
/// @returns Whether or not this Render Target created it's Texture.
|
||||
[[nodiscard]] bool OwnsTexture() const;
|
||||
public:
|
||||
[[nodiscard]] Vector2 GetDimensions() const;
|
||||
/// @returns the size of the renderable area.
|
||||
[[nodiscard]] Vector2i GetDimensions() const;
|
||||
|
||||
/// @returns The currently selected MSAA Sample Rate.
|
||||
[[nodiscard]] MSAA_SAMPLE_RATE GetMSAASampleRate() const;
|
||||
/// Returns whether or not MSAA is enabled, If it is and you're not using J2D || J3D Begin / End,
|
||||
/// You need to run "Blit()" after rendering to your FBO before you show it.
|
||||
/// @note Also, If the texture wasn't made by the RenderTarget you don't want this. It would destroy the texture.
|
||||
|
||||
/// @returns Whether or not this Render Target is using MSAA.
|
||||
[[nodiscard]] bool MSAAEnabled() const;
|
||||
|
||||
/// @returns The JGL texture this Render Target draws on.
|
||||
[[nodiscard]] const Texture* GetJGLTexture() const;
|
||||
|
||||
/// @returns The OpenGL handle for the texture this Render Target draws on.
|
||||
[[nodiscard]] GLuint GetGLTextureHandle() const;
|
||||
|
||||
/// @returns The OpenGL handle for this Render Target.
|
||||
[[nodiscard]] GLuint GetGLFramebufferObjectHandle() const;
|
||||
|
||||
/// @returns The handle to the OpenGL buffer containing depth information
|
||||
/// @note Only valid if this Render Target is being used for 3D.
|
||||
[[nodiscard]] GLuint GetGLDepthBufferHandle() const;
|
||||
|
||||
/// @returns The color that should be used to clear this Render Target.
|
||||
[[nodiscard]] Color4 GetClearColor() const;
|
||||
/// Get the data back from the FBO. This is *not* async friendly.
|
||||
[[nodiscard]] std::vector<GLfloat> GetData() const;
|
||||
|
||||
|
||||
/// @returns The color information for this Render Target.
|
||||
/// @note Both the CPU & GPU cannot do anything while this takes place. It's very slow.
|
||||
[[nodiscard]] std::vector<GLfloat> GetPixels() const;
|
||||
public:
|
||||
/// Copy constructor. Will always set "texture_created_by_us" to true and use our own texture to avoid memleaks.
|
||||
/// Create a Render Target from a Render Target that already exists.
|
||||
/** @note Render Targets that are copies of another will copy the Texture.
|
||||
* This is so that deleting the copy doesn't delete the Texture of the original.
|
||||
*/
|
||||
RenderTarget(const RenderTarget& rhs);
|
||||
/// Create a render target for a texture that already exists. For adding to an existing texture.
|
||||
explicit RenderTarget(const Texture* texture, const Color4& clear_color = Colors::Black);
|
||||
/// Create a Render Target with a brand new texture. Want to render JGL elements onto a texture and display it as a sprite?
|
||||
explicit RenderTarget(const Vector2& size, const Color4& clear_color = Colors::Black, bool use_depth = false, MSAA_SAMPLE_RATE sample_rate = MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
|
||||
/// Create a Render Target for a texture that already exists.
|
||||
/// @param texture The Texture that using this Render Target would draw on.
|
||||
/// @param clear_color The color to be used if you want to clear the Render Target.
|
||||
/// @note For Render Targets created this way, The destructor will not delete the texture.
|
||||
explicit RenderTarget(const Texture* texture, const Color4& clear_color = Colors::Transparent);
|
||||
|
||||
/// Create a Render Target with a new texture.
|
||||
/// @param size The width & height the Render Target should have.
|
||||
/// @param clear_color The color to be used if you want to clear the Render Target.
|
||||
/// @param use_depth Whether or not this Render Target will have depth information.
|
||||
/// @param sample_rate The MSAA sample rate this Render Target will use.
|
||||
explicit RenderTarget(const Vector2i& size, const Color4& clear_color = Colors::Transparent, bool use_depth = false, MSAA_SAMPLE_RATE sample_rate = MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
|
||||
/// Deletes this Render Target.
|
||||
/** @note If this Render Target was made with a Texture that already existed
|
||||
* the Texture will not be deleted. */
|
||||
~RenderTarget();
|
||||
};
|
@@ -8,19 +8,19 @@
|
||||
namespace JGL {
|
||||
using namespace ReImage;
|
||||
enum class TextureFilteringMode : u8 {
|
||||
NEAREST = 0, //Fastest for 2D, Sometimes causes graphical issues.
|
||||
BILINEAR = 1, //Fast and pretty, The best for 2D.
|
||||
NEAREST = 0, // Fastest for 2D, Sometimes causes graphical issues.
|
||||
BILINEAR = 1, // Fast and pretty, The best for 2D.
|
||||
|
||||
MIPMAP_NEAREST = 2, //Nearest with mipmaps. The fastest for 3D, Sometimes causes graphical issues. Uses more vram.
|
||||
MIPMAP_BILINEAR = 3, //Bilinear with mipmaps, Fast and pretty. Uses more vram.
|
||||
MIPMAP_TRILINEAR = 4 //The prettiest. Still decent speed. Uses more vram.
|
||||
MIPMAP_NEAREST = 2, // Nearest with mipmaps. The fastest for 3D, Sometimes causes graphical issues. Uses more vram.
|
||||
MIPMAP_BILINEAR = 3, // Bilinear with mipmaps, Fast and pretty. Uses more vram.
|
||||
MIPMAP_TRILINEAR = 4 // The prettiest. Still decent speed. Uses more vram.
|
||||
};
|
||||
|
||||
enum class TextureWrappingMode : u8 {
|
||||
REPEAT = 0,
|
||||
MIRRORED_REPEAT = 1,
|
||||
CLAMP_TO_EDGE = 2,
|
||||
CLAMP_TO_BORDER = 3 //Effectively the same as clamp_to_edge
|
||||
CLAMP_TO_BORDER = 3 // Effectively the same as clamp_to_edge
|
||||
};
|
||||
|
||||
/// Represents texture data loaded on the GPU. Contains a handle that can be passed to OpenGL draw calls.
|
||||
|
@@ -35,4 +35,9 @@ void JGL::ShapeCache::Init() {
|
||||
};
|
||||
cube_index_data = new VRamList(indices.data(), indices.size());
|
||||
}
|
||||
|
||||
if (!j2d_default_normal_data) {
|
||||
std::array<GLfloat, 3> normal {0, 0, 1};
|
||||
j2d_default_normal_data = new VRamList(normal.data(), normal.size());
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@
|
||||
#include <glad/glad.h>
|
||||
|
||||
namespace JGL {
|
||||
inline std::array<const LightBase*, 8> empty_light_array = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
inline bool inJ2D = false;
|
||||
inline bool inJ3D = false;
|
||||
}
|
||||
@@ -21,6 +22,7 @@ namespace JGL::OpenGLState {
|
||||
|
||||
inline bool wasTexture2DEnabled = false;
|
||||
inline bool wasTextureCoordArrayEnabled = false;
|
||||
inline bool wasNormalArrayEnabled = false;
|
||||
|
||||
inline bool wasDepthTestEnabled = false;
|
||||
inline bool wasVertexArraysEnabled = false;
|
||||
@@ -30,11 +32,18 @@ namespace JGL::OpenGLState {
|
||||
inline GLint activeTextureUnit = 0;
|
||||
}
|
||||
|
||||
namespace JGL::J3D {
|
||||
inline std::array<const JGL::LightBase*, 8> J3D_Empty_Light_Array{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
inline std::array<const JGL::LightBase*, 8> J3D_Required_Lights{};
|
||||
inline std::vector<const JGL::LightBase*> J3D_Light_Array{};
|
||||
namespace JGL::J2D {
|
||||
inline std::array<const LightBase*, 8> required_lights;
|
||||
inline std::vector<const LightBase*> light_array;
|
||||
}
|
||||
|
||||
namespace JGL::J3D {
|
||||
// List of lights required for each objects in the scene. up-to 8. For example, the sun. Or a flash-light.
|
||||
inline std::array<const LightBase*, 8> required_lights;
|
||||
// List of all lights in the scene.
|
||||
inline std::vector<const LightBase*> light_array;
|
||||
inline float far_plane = 0;
|
||||
inline float fov = 0;
|
||||
|
||||
|
||||
}
|
@@ -53,6 +53,12 @@ void JGL::J2D::Begin(RenderTarget* rt, bool clear_buffers) {
|
||||
else
|
||||
OpenGLState::wasVertexArraysEnabled = true;
|
||||
|
||||
if (glIsEnabled(GL_NORMAL_ARRAY))
|
||||
OpenGLState::wasNormalArrayEnabled = true,
|
||||
glDisableClientState(GL_NORMAL_ARRAY);
|
||||
else
|
||||
OpenGLState::wasNormalArrayEnabled = false;
|
||||
|
||||
if (!glIsEnabled(GL_CULL_FACE))
|
||||
OpenGLState::wasCullFaceEnabled = false,
|
||||
glEnable(GL_CULL_FACE),
|
||||
@@ -120,6 +126,9 @@ void JGL::J2D::End() {
|
||||
if (OpenGLState::wasTextureCoordArrayEnabled)
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
if (OpenGLState::wasNormalArrayEnabled)
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
|
||||
if (OpenGLState::wasColorArrayEnabled)
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
|
||||
@@ -127,7 +136,7 @@ void JGL::J2D::End() {
|
||||
glColor4fv(OpenGLState::oldColor);
|
||||
|
||||
if (OpenGLState::render_target != nullptr) {
|
||||
OpenGLState::render_target->Blit();
|
||||
OpenGLState::render_target->MSAABlit();
|
||||
OpenGLState::render_target = nullptr;
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, OpenGLState::current_fbo);
|
||||
glViewport(OpenGLState::viewport[0], OpenGLState::viewport[1], OpenGLState::viewport[2], OpenGLState::viewport[3]);
|
||||
@@ -135,6 +144,24 @@ void JGL::J2D::End() {
|
||||
inJ2D = false;
|
||||
}
|
||||
|
||||
void JGL::J2D::RequiredLight(const JGL::LightBase* light) {
|
||||
bool success = false;
|
||||
for (auto& item : required_lights)
|
||||
if (item == nullptr) {
|
||||
item = light;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!success)
|
||||
Logger::Error("You cannot specify more than 8 required lights.");
|
||||
}
|
||||
|
||||
void JGL::J2D::LightArray(const LightBase** lights, const size_t& light_count) {
|
||||
for (size_t i = 0; i < light_count; i++)
|
||||
light_array.push_back(lights[i]);
|
||||
}
|
||||
|
||||
void JGL::J2D::DrawPoint(const Color4& color, const Vector2& coordinates, float radius) {
|
||||
if (!inJ2D)
|
||||
Logger::Error("Drawing J2D element before J2D begin.");
|
||||
@@ -371,12 +398,12 @@ void JGL::J2D::DrawSprite(const JGL::RenderTarget& rt, const Vector2& position,
|
||||
}
|
||||
|
||||
//Change the blending mode such that the alpha doesn't get multiplied again.
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
if (rt.OwnsTexture())
|
||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
JGL::J2D::DrawPartialSprite(*rt.GetJGLTexture(), position, {0, 0}, rt.GetDimensions(), rad_rotation, origin, scale, color, d);
|
||||
JGL::J2D::DrawPartialSprite(*rt.GetJGLTexture(), position, {0, 0}, Vector2(rt.GetDimensions()), rad_rotation, origin, scale, color, d);
|
||||
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
if (rt.OwnsTexture())
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
@@ -388,7 +415,7 @@ void JGL::J2D::DrawSprite(const Texture& texture, const Texture &alpha_mask, con
|
||||
if (texture.GetDimensions() != alpha_mask.GetDimensions())
|
||||
Logger::Warning("The alpha mask and the texture are not the same size.");
|
||||
|
||||
const Vector2 size = texture.GetDimensions();
|
||||
const Vector2 size = Vector2(texture.GetDimensions());
|
||||
|
||||
std::array<Vector2, 4> textureCoordinates{};
|
||||
if (texture.GetFlags() & INVERT_Y)
|
||||
@@ -506,12 +533,12 @@ void JGL::J2D::DrawPartialRenderTarget(const JGL::RenderTarget& rt, const Vector
|
||||
}
|
||||
|
||||
//Change the blending mode such that the alpha doesn't get multiplied again.
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
if (rt.OwnsTexture())
|
||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
JGL::J2D::DrawPartialSprite(*rt.GetJGLTexture(), position, sub_texture_position, sub_texture_size, rad_rotation, origin, scale, color, d);
|
||||
|
||||
if (rt.TextureCreatedByRenderTarget())
|
||||
if (rt.OwnsTexture())
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
@@ -526,7 +553,7 @@ void JGL::J2D::DrawSprite(const Texture& texture, const Vector2& pos, float rad_
|
||||
Logger::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
|
||||
const Vector2 size = texture.GetDimensions();
|
||||
const Vector2 size = Vector2(texture.GetDimensions());
|
||||
|
||||
std::array<Vector2, 4> textureCoordinates{};
|
||||
if (texture.GetFlags() & INVERT_Y)
|
||||
@@ -598,7 +625,7 @@ void JGL::J2D::DrawPartialSprite(const Texture& texture, const Vector2& position
|
||||
if (!inJ2D)
|
||||
Logger::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
const Vector2 textureSize = texture.GetDimensions();
|
||||
const Vector2 textureSize = Vector2(texture.GetDimensions());
|
||||
std::array<GLfloat, 8> textureCoordinates{};
|
||||
// Calculate texture coordinates (relative to the whole texture)
|
||||
if (!(texture.GetFlags() & INVERT_Y))
|
||||
@@ -688,7 +715,7 @@ void JGL::J2D::DrawMirrorSprite(const Texture& texture, const Vector2& position,
|
||||
Logger::Warning("Drawing non-mirrored sprite with JGL::J2D::DrawMirrorSprite?");
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture.GetGLTextureHandle());
|
||||
Vector2 size = texture.GetDimensions();
|
||||
Vector2 size = Vector2(texture.GetDimensions());
|
||||
|
||||
std::array<Vector2, 4> textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};
|
||||
if (mirror_axis == Direction::Horizontal) {
|
||||
|
@@ -35,14 +35,21 @@ void JGL::J3D::ChangeFarPlane(float far_plane) {
|
||||
}
|
||||
|
||||
void JGL::J3D::RequiredLight(const JGL::LightBase* light) {
|
||||
for (auto* i : J3D_Required_Lights)
|
||||
if (i == nullptr)
|
||||
bool success = false;
|
||||
for (auto& i : required_lights)
|
||||
if (i == nullptr) {
|
||||
i = light;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!success)
|
||||
Logger::Error("You cannot specify more than 8 required lights.");
|
||||
}
|
||||
|
||||
void JGL::J3D::LightArray(const JGL::LightBase** lights, const size_t& light_count) {
|
||||
for (size_t i = 0; i < light_count; i++)
|
||||
J3D_Light_Array.push_back(lights[i]);
|
||||
light_array.push_back(lights[i]);
|
||||
}
|
||||
|
||||
void JGL::J3D::Begin(bool two_pass) {
|
||||
@@ -88,8 +95,8 @@ void JGL::J3D::Begin(bool two_pass) {
|
||||
OpenGLState::wasBlendEnabled = true;
|
||||
|
||||
// Reset the lights.
|
||||
J3D_Required_Lights = J3D_Empty_Light_Array;
|
||||
J3D_Light_Array = {};
|
||||
required_lights = empty_light_array;
|
||||
light_array = {};
|
||||
|
||||
glDisable(GL_LIGHTING);
|
||||
if (!inJ2D)
|
||||
@@ -129,8 +136,8 @@ void JGL::J3D::End() {
|
||||
glDisable(GL_LIGHT7);
|
||||
}
|
||||
|
||||
J3D_Required_Lights = J3D_Empty_Light_Array;
|
||||
J3D_Light_Array = {};
|
||||
required_lights = empty_light_array;
|
||||
light_array = {};
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4fv(OpenGLState::oldColor);
|
||||
|
@@ -30,7 +30,7 @@ void JGL::RenderTarget::SetActiveGLRenderTarget(const RenderTarget& render_targe
|
||||
glViewport(0,0, render_target.GetDimensions().x, render_target.GetDimensions().y);
|
||||
}
|
||||
|
||||
Vector2 JGL::RenderTarget::GetDimensions() const {
|
||||
Vector2i JGL::RenderTarget::GetDimensions() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -71,12 +71,12 @@ JGL::RenderTarget::RenderTarget(const JGL::Texture* texture, const Color4& clear
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
|
||||
this->clear_color = clear_color;
|
||||
this->size = texture->GetDimensions();
|
||||
this->size = {(int) size.x, (int) size.y};
|
||||
this->texture = texture;
|
||||
texture_created_by_us = false;
|
||||
}
|
||||
|
||||
JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color, bool use_depth, MSAA_SAMPLE_RATE sample_rate) {
|
||||
JGL::RenderTarget::RenderTarget(const Vector2i& size, const Color4& clear_color, bool use_depth, MSAA_SAMPLE_RATE sample_rate) {
|
||||
if (size.x < 1 || size.y < 1)
|
||||
Logger::Fatal("Creating a render target where the color attachment is empty?");
|
||||
|
||||
@@ -124,7 +124,7 @@ JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color,
|
||||
SetMSAAEnabled(sample_rate);
|
||||
}
|
||||
|
||||
std::vector<GLfloat> JGL::RenderTarget::GetData() const {
|
||||
std::vector<GLfloat> JGL::RenderTarget::GetPixels() const {
|
||||
std::vector<GLfloat> data(GetDimensions().x * GetDimensions().y * 4);
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
|
||||
@@ -132,12 +132,11 @@ std::vector<GLfloat> JGL::RenderTarget::GetData() const {
|
||||
glReadPixels(0, 0, GetDimensions().x, GetDimensions().y, GL_RGBA, GL_FLOAT, data.data());
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Resize(const Vector2& new_size) {
|
||||
void JGL::RenderTarget::Resize(const Vector2i& new_size) {
|
||||
if (!texture_created_by_us)
|
||||
Logger::Fatal("Resizing a texture that already existed?");
|
||||
Logger::Error("Resizing a Render Target that does not own it's texture?");
|
||||
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
GLfloat old_clear_color[4];
|
||||
@@ -145,8 +144,8 @@ void JGL::RenderTarget::Resize(const Vector2& new_size) {
|
||||
glGetIntegerv(GL_VIEWPORT, old_viewport);
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, old_clear_color);
|
||||
|
||||
/* If what was previously not part of the renderable area is big enough to
|
||||
* just set the new size without reading data back */
|
||||
/* If what was previously not part of the renderable area is big enough
|
||||
* to just set the new size. */
|
||||
if (new_size.x <= texture->GetDimensions().x && new_size.y <= texture->GetDimensions().y) {
|
||||
size = new_size;
|
||||
|
||||
@@ -182,7 +181,7 @@ void JGL::RenderTarget::Resize(const Vector2& new_size) {
|
||||
glClearColor(cc.RedChannelNormalized(), cc.GreenChannelNormalized(), cc.BlueChannelNormalized(), cc.AlphaChannelNormalized());
|
||||
glViewport(0,0, size.x, size.y);
|
||||
|
||||
texture = new Texture(Vector2((float) biggest, (float) biggest));
|
||||
texture = new Texture(Vector2(biggest, biggest));
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetGLTextureHandle(), 0);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
@@ -204,7 +203,7 @@ JGL::RenderTarget::~RenderTarget() {
|
||||
delete texture;
|
||||
}
|
||||
|
||||
bool JGL::RenderTarget::TextureCreatedByRenderTarget() const {
|
||||
bool JGL::RenderTarget::OwnsTexture() const {
|
||||
return texture_created_by_us;
|
||||
}
|
||||
|
||||
@@ -216,19 +215,20 @@ bool JGL::RenderTarget::MSAAEnabled() const {
|
||||
return msaa_sample_rate != MSAA_SAMPLE_RATE::MSAA_NONE;
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE sample_rate) {
|
||||
bool JGL::RenderTarget::SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE sample_rate) {
|
||||
// If we'd be setting the same sample_rate we already have.
|
||||
if (sample_rate == msaa_sample_rate)
|
||||
return;
|
||||
return false;
|
||||
|
||||
// If we'd be rendering onto a texture and not a plain render target we don't want this.
|
||||
if (!TextureCreatedByRenderTarget())
|
||||
return;
|
||||
if (!OwnsTexture())
|
||||
return false;
|
||||
|
||||
// Remove it if they request no msaa or if what they requested is different than what they already have.
|
||||
if (sample_rate == MSAA_SAMPLE_RATE::MSAA_NONE || msaa_sample_rate != MSAA_SAMPLE_RATE::MSAA_NONE) {
|
||||
if(using_depth)
|
||||
glDeleteRenderbuffers(1, &msaa_depth_buffer);
|
||||
|
||||
glDeleteRenderbuffers(1, &msaa_render_buffer);
|
||||
glDeleteFramebuffers(1, &msaa_framebuffer_object);
|
||||
|
||||
@@ -239,7 +239,7 @@ void JGL::RenderTarget::SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE sample_rate) {
|
||||
|
||||
// Only return here if they specifically requested no MSAA. else continue to change mode.
|
||||
if (sample_rate == MSAA_SAMPLE_RATE::MSAA_NONE)
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
@@ -271,11 +271,11 @@ void JGL::RenderTarget::SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE sample_rate) {
|
||||
|
||||
if (failure)
|
||||
SetMSAAEnabled(MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
return failure;
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Blit() const {
|
||||
if (MSAAEnabled() && TextureCreatedByRenderTarget()) {
|
||||
|
||||
void JGL::RenderTarget::MSAABlit() const {
|
||||
if (MSAAEnabled() && OwnsTexture()) {
|
||||
// Save the GL state.
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
GLint current_draw_fbo = 0;
|
||||
@@ -309,7 +309,6 @@ void JGL::RenderTarget::Blit() const {
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Blit(const JGL::RenderTarget& source, JGL::RenderTarget* destination) {
|
||||
//TODO allow blitting onto an FBO that is bigger but not smaller.
|
||||
if (source.size != destination->size)
|
||||
Logger::Warning("Blitting a render target but they're not the same size?");
|
||||
|
||||
@@ -333,7 +332,7 @@ void JGL::RenderTarget::Blit(const JGL::RenderTarget& source, JGL::RenderTarget*
|
||||
|
||||
// To avoid repeatedly allocating and deallocating.
|
||||
JGL::RenderTarget* pixel = nullptr;
|
||||
void JGL::RenderTarget::Blit(const Color4& color, const Vector2& position, JGL::RenderTarget* destination) {
|
||||
void JGL::RenderTarget::Blit(const Color4& color, const Vector2i& position, JGL::RenderTarget* destination) {
|
||||
if (position.x > destination->size.x || position.y > destination->size.y)
|
||||
Logger::Warning("Blitting outside of the renderable area of the destination.");
|
||||
|
||||
|
Reference in New Issue
Block a user