Positioning & Rotation bugfixes.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m51s

Make it such that a sprites "origin" doesn't effect the position. It is only the point at which transformations are done about.
This commit is contained in:
2024-09-14 21:28:32 -04:00
parent 881d031f3c
commit 0e22bc721a
4 changed files with 24 additions and 14 deletions

View File

@@ -11,6 +11,8 @@ namespace JGL {
class JGL::RenderTarget { class JGL::RenderTarget {
private: private:
Color4 clear_color{0,0,0,0}; 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};
bool using_depth = false; bool using_depth = false;
GLuint framebuffer_object = 0; GLuint framebuffer_object = 0;
GLuint depth_buffer = 0; GLuint depth_buffer = 0;
@@ -29,7 +31,7 @@ public:
/// Create a render target for a texture that already exists. For decals. /// Create a render target for a texture that already exists. For decals.
explicit RenderTarget(const Texture& texture, const Color4& clear_color = Colors::Black, bool use_depth = false); explicit RenderTarget(const Texture& texture, const Color4& clear_color = Colors::Black, bool use_depth = false);
/// Create a Render Target with a brand new texture. Want to render JGL elements onto a texture and display it as a sprite? /// 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(unsigned int size, const Color4& clear_color = Colors::Black, bool use_depth = false); explicit RenderTarget(const Vector2& size, const Color4& clear_color = Colors::Black, bool use_depth = false);
void Erase(); void Erase();
}; };

View File

@@ -112,7 +112,7 @@ public:
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR); image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR);
j2d_render_target = new RenderTarget(1280, {0,0,0,0}); j2d_render_target = new RenderTarget({500, 500}, {255,0,0,0});
} }
Vector3 textAngle = {0,0,0}; Vector3 textAngle = {0,0,0};
@@ -147,7 +147,8 @@ public:
glLoadIdentity(); glLoadIdentity();
camera->render(); camera->render();
///All 3D elements of the scene and JGL elements *must* be rendered before the 2d stuff. // All 3D elements of the scene and JGL elements *must* be rendered before the 2D stuff
/* if rendering to screen space directly. */
J3D::Begin(); J3D::Begin();
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1}); J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
@@ -194,7 +195,7 @@ public:
//Draw the Render Target that we just drew all that stuff onto. //Draw the Render Target that we just drew all that stuff onto.
J2D::Begin(); J2D::Begin();
J2D::DrawRenderTargetAsSprite(*j2d_render_target, {0, 0}); J2D::DrawRenderTargetAsSprite(*j2d_render_target, {0, 0}, sprite_radians * 0.25, {0.5, 0.5}, {1,1}, Colors::White);
J2D::End(); J2D::End();
if (framerate_measurement) { if (framerate_measurement) {

View File

@@ -303,7 +303,8 @@ namespace JGL {
//Change the blending mode such that the alpha doesn't get multiplied again. //Change the blending mode such that the alpha doesn't get multiplied again.
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
J2D::DrawSprite(*rt.GetJGLTexture(), position, rad_rotation, origin, scale, color, d); //J2D::DrawSprite(*rt.GetJGLTexture(), position, rad_rotation, origin, scale, color, d);
J2D::DrawPartialSprite(*rt.GetJGLTexture(), position, {0, 0}, rt.GetDimensions(), rad_rotation, origin, scale, color, d);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} }
void J2D::DrawSprite(const Texture& texture, const Vector2& pos, float rad_rotation, const Vector2& origin, void J2D::DrawSprite(const Texture& texture, const Vector2& pos, float rad_rotation, const Vector2& origin,
@@ -320,7 +321,7 @@ namespace JGL {
// Factors in scaling and origin correctly. // Factors in scaling and origin correctly.
// i.e. to render at 2x size, from the center, at coords XY, use {2, 2} scale, and {0.5, 0.5} offset. // i.e. to render at 2x size, from the center, at coords XY, use {2, 2} scale, and {0.5, 0.5} offset.
const Vector2 offset = origin * size; const Vector2 offset = origin * size;
Vector2 pos2 = pos - offset * scale; Vector2 pos2 = pos;
Vector2 scaled_size = scale * size; Vector2 scaled_size = scale * size;
Vector2 size2 = scaled_size; Vector2 size2 = scaled_size;
float cos_theta = std::cos(rad_rotation); float cos_theta = std::cos(rad_rotation);
@@ -400,7 +401,7 @@ namespace JGL {
std::swap(textureCoordinates[2], textureCoordinates[4]); std::swap(textureCoordinates[2], textureCoordinates[4]);
const Vector2 offset = origin * sub_texture_size; const Vector2 offset = origin * sub_texture_size;
Vector2 pos2 = position - offset * scale; Vector2 pos2 = position;
Vector2 scaled_size = scale * sub_texture_size; Vector2 scaled_size = scale * sub_texture_size;
Vector2 size2 = scaled_size; Vector2 size2 = scaled_size;
float cos_theta = std::cos(rad_rotation); float cos_theta = std::cos(rad_rotation);
@@ -467,7 +468,7 @@ namespace JGL {
textureCoordinates = {Vector2(0, 0), Vector2(0, 2), Vector2(2, 2), Vector2(2, 0)}; textureCoordinates = {Vector2(0, 0), Vector2(0, 2), Vector2(2, 2), Vector2(2, 0)};
const Vector2 offset = origin * size; const Vector2 offset = origin * size;
Vector2 pos2 = position - offset * scale; Vector2 pos2 = position;
Vector2 size2 = scale * size; Vector2 size2 = scale * size;
float cos_theta = std::cos(rad_rotation); float cos_theta = std::cos(rad_rotation);
float sin_theta = std::sin(rad_rotation); float sin_theta = std::sin(rad_rotation);

View File

@@ -27,11 +27,11 @@ GLuint JGL::RenderTarget::GetActiveGLFramebufferHandle() {
void JGL::RenderTarget::SetActiveGLRenderTarget(const RenderTarget& render_target) { void JGL::RenderTarget::SetActiveGLRenderTarget(const RenderTarget& render_target) {
RenderTarget rt = render_target; RenderTarget rt = render_target;
glBindFramebuffer(GL_FRAMEBUFFER, rt.GetGLFramebufferObjectHandle()); glBindFramebuffer(GL_FRAMEBUFFER, rt.GetGLFramebufferObjectHandle());
glViewport(0,0, render_target.GetJGLTexture()->GetDimensions().x, render_target.GetJGLTexture()->GetDimensions().x); glViewport(0,0, rt.GetDimensions().x, rt.GetDimensions().y);
} }
Vector2 JGL::RenderTarget::GetDimensions() const { Vector2 JGL::RenderTarget::GetDimensions() const {
return texture->GetDimensions(); return size;
} }
void JGL::RenderTarget::Erase() { void JGL::RenderTarget::Erase() {
@@ -50,22 +50,27 @@ Color4 JGL::RenderTarget::GetClearColor() const {
return clear_color; return clear_color;
} }
JGL::RenderTarget::RenderTarget(const unsigned int size, const Color4& clear_color, bool use_depth) { JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color, bool use_depth) {
GLuint current_fbo = GetActiveGLFramebufferHandle(); GLuint current_fbo = GetActiveGLFramebufferHandle();
GLint viewport[4] = {0, 0, 0, 0}; GLint viewport[4] = {0, 0, 0, 0};
glGetIntegerv(GL_VIEWPORT, viewport); glGetIntegerv(GL_VIEWPORT, viewport);
texture = new Texture(Vector2(size, size)); unsigned int biggest;
if (size.x >= size.y)
biggest = size.x;
else biggest = size.y;
texture = new Texture(Vector2(biggest, biggest));
glGenFramebuffers(1, &framebuffer_object); glGenFramebuffers(1, &framebuffer_object);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_object); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_object);
glViewport(0,0, size, size); glViewport(0,0, size.x, size.y);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetGLTextureHandle(), 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetGLTextureHandle(), 0);
if (use_depth) { if (use_depth) {
GLuint depthBuffer; GLuint depthBuffer;
glGenRenderbuffers(1, &depthBuffer); glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer); glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size, size); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, biggest, biggest);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
using_depth = true; using_depth = true;
@@ -84,4 +89,5 @@ JGL::RenderTarget::RenderTarget(const unsigned int size, const Color4& clear_col
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo); glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
this->clear_color = clear_color; this->clear_color = clear_color;
this->size = size;
} }