Fixed a memory error & unfinished lighting wrapper.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m50s

This commit is contained in:
2024-10-02 22:38:29 -04:00
parent 8fcfbddd44
commit 6e8185e2cd
9 changed files with 118 additions and 22 deletions

View File

@@ -19,6 +19,7 @@
#include <JGL/types/FontCache.h>
#include <JGL/types/Font.h>
#include <JGL/types/RenderTarget.h>
#include <JGL/types/Light.h>
#include <J3ML/LinearAlgebra.hpp>
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <J3ML/LinearAlgebra/Vector3.hpp>
@@ -33,10 +34,9 @@ namespace JGL {
void Update(const Vector2& window_size);
inline void PurgeFontCache() { fontCache.purgeCache(); }
std::vector<GLfloat> OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far);
std::array<GLfloat, 16> OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far);
/// Returns true if the graphics driver meets the requirements (GL Version & Extensions).
bool MeetsRequirements();
/// Drawing functions for primitive 2D Shapes.
namespace J2D {
/// Open a 2-D rendering context with the underlying graphics system (In this case& by default OpenGL).
@@ -47,6 +47,10 @@ namespace JGL {
/// Closes a 2-D rendering context with the underlying graphics system (In this case& by default OpenGL).
/// @see Begin().
void End();
/// 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(Light*, size_t size);
void LightArray(std::vector<Light> lights);
/// Plots a single pixel on the screen.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4

30
include/JGL/types/Light.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector4.hpp>
#include <J3ML/LinearAlgebra/Vector3.hpp>
#include <Color4.hpp>
namespace JGL {
class Light;
class OmnidirectionalLight2D;
class PointLight2D;
}
class JGL::Light {
private:
/// W in position seems to determine whether or not the light is omni-directional. 1 = omni 0 = point.
/// Position is un-normalized screen space. For ex 500, 500, 1 for a light coming from where you're sitting.
Vector4 position = {0, 0, 0, 1};
Color4 ambient = {0, 0, 0, 0};
Color4 diffuse = {0, 0, 0, 0};
Color4 specular = {0, 0, 0, 0};
public:
Light(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular);
Vector3 GetNormalizedSceenSpaceCoordinates() const;
};
class JGL::OmnidirectionalLight2D {
private:
public:
OmnidirectionalLight2D(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular);
};

View File

@@ -0,0 +1,4 @@
/// A simple wrapper for OpenGL materials. Lets you set things such as the "shininess" of your elements.
class Material {
};

View File

@@ -24,11 +24,11 @@ private:
void load(const GLuint* data, const long& size);
public:
VRamList() = default;
VRamList(const GLuint* data, const long& size);
VRamList(const GLfloat* data, const long& size);
VRamList(Vector2* data, const long& size);
VRamList(Vector3* data, const long& size);
VRamList(Vector4* data, const long& size);
VRamList(const GLuint* data, const long& size);
public:
[[nodiscard]] GLuint GetHandle() const;
/// Returns the number of GLfloat or GLuint in the list.
@@ -39,8 +39,12 @@ public:
[[nodiscard]] bool IsFloatArray() const;
void Erase();
/// Get list data back from the GPU. This is *very* slow.
/// It's not recommended you use this in your normal rendering routines.
/// Update the data of an existing VBO. Data would be the same type as the list already is.
/// "size" refers to the number of elements in data. Not the number of bytes.
void SetData(void* data, const long& size);
/* Get VBO data back from the GPU. This is *very* slow because the CPU is going to wait
for the transfer to finish. I don't know why you'd want to do this outside of testing reasons.*/
template <typename T>
[[nodiscard]] std::vector<T> GetData() const {
GLenum buffer_type;
@@ -51,14 +55,15 @@ public:
else if constexpr (std::is_same<T, GLuint>::value)
buffer_type = GL_ELEMENT_ARRAY_BUFFER,
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &current_buffer);
else {
Logger::Fatal("Typename T must be either GLfloat or GLuint.");
exit(-1);
}
else
jlog::Fatal("Typename T must be either GLfloat or GLuint.");
if ((element_array_buffer && buffer_type == GL_ARRAY_BUFFER) || (!element_array_buffer && buffer_type == GL_ELEMENT_ARRAY_BUFFER))
jlog::Fatal("Returning the contents of a VRamList using the incorrect Typename T?");
glBindBuffer(buffer_type, list_handle);
std::vector<T> data(size);
memcpy(data.data(), (T*) glMapBuffer(buffer_type, GL_READ_ONLY), size * sizeof(T));
memcpy(data.data(), glMapBuffer(buffer_type, GL_READ_ONLY), size * sizeof(T));
glUnmapBuffer(buffer_type);
glBindBuffer(buffer_type, current_buffer);

View File

@@ -649,8 +649,8 @@ namespace JGL {
#pragma endregion
#pragma region J3D
std::vector<GLfloat> OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far) {
std::vector<GLfloat> result(16);
std::array<GLfloat, 16> OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far) {
std::array<GLfloat, 16> result{};
GLfloat f = 1.0f / std::tan(fovY * 0.5f * Math::Pi / 180.0f);
result[0] = f / aspect;
result[5] = f;

19
src/types/Light.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <JGL/types/Light.h>
#include <glad/glad.h>
JGL::Light::Light(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular) {
this->position = Vector4(position, 1.0f);
this->ambient = ambient;
this->diffuse = diffuse;
this->specular = specular;
}
Vector3 JGL::Light::GetNormalizedSceenSpaceCoordinates() const {
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
float normalized_x = 2.0f * (position.x - viewport[0]) / viewport[2] - 1.0f;
float normalized_y = 2.0f * (position.y - viewport[1]) / viewport[3] - 1.0f;
return {normalized_x, normalized_y, position.z};
}

0
src/types/Material.cpp Normal file
View File

View File

@@ -54,7 +54,7 @@ JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color,
GLuint current_fbo = GetActiveGLFramebufferHandle();
GLint viewport[4] = {0, 0, 0, 0};
glGetIntegerv(GL_VIEWPORT, viewport);
//Textures behave strangely if they're not square aaaaaaaaaaaaa.
unsigned int biggest;
if (size.x >= size.y)
biggest = size.x;
@@ -82,9 +82,8 @@ JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color,
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(old_clear_color[0], old_clear_color[1], old_clear_color[2], old_clear_color[3]);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("Error " + std::to_string(status) + "while generating framebuffer");
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("A new framebuffer could not be allocated.");
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);

View File

@@ -7,9 +7,10 @@ void JGL::VRamList::load(const GLfloat* data, const long& s) {
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &current_array_buffer);
glGenBuffers(1, &list_handle);
glBindBuffer(GL_ARRAY_BUFFER, list_handle);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, s, data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, current_array_buffer);
size = s;
element_array_buffer = false;
size = s / sizeof(GLfloat);
}
void JGL::VRamList::load(const GLuint* data, const long& s) {
@@ -17,15 +18,14 @@ void JGL::VRamList::load(const GLuint* data, const long& s) {
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &current_element_array_buffer);
glGenBuffers(1, &list_handle);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, list_handle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, s, data, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, current_element_array_buffer);
element_array_buffer = true;
size = s;
size = s / sizeof(GLuint);
}
JGL::VRamList::VRamList(const GLfloat* data, const long& size) {
long data_size = (long) sizeof(GLfloat) * size;
load(data, data_size);
load(data, (long) sizeof(GLfloat) * size);
}
JGL::VRamList::VRamList(const GLuint* data, const long& size) {
@@ -83,3 +83,38 @@ long JGL::VRamList::GetDataSize() const {
return (long) sizeof(GLuint) * size;
return (long) sizeof(GLfloat) * size;
}
void JGL::VRamList::SetData(void* data, const long& size) {
bool should_resize = (this->size != size);
if (should_resize) {
glDeleteBuffers(1, &list_handle);
list_handle = 0;
if (!element_array_buffer)
load((GLfloat*) data, sizeof(GLfloat) * size);
else
load((GLuint*) data, sizeof(GLuint) * size);
return;
}
GLint current_buffer = 0;
GLenum buffer_type = GL_ARRAY_BUFFER;
GLenum buffer_binding = GL_ARRAY_BUFFER_BINDING;
if (element_array_buffer)
buffer_type = GL_ELEMENT_ARRAY_BUFFER,
buffer_binding = GL_ELEMENT_ARRAY_BUFFER_BINDING;
glGetIntegerv(buffer_binding, &current_buffer);
glBindBuffer(buffer_type, list_handle);
void* buffer_data = glMapBuffer(buffer_type, GL_WRITE_ONLY);
if (buffer_data == nullptr)
jlog::Fatal("Mapping in a VBO that doesn't exist?");
memcpy(buffer_data, data, (element_array_buffer ? sizeof(GLuint) : sizeof(GLfloat)) * size);
if (!glUnmapBuffer(buffer_type))
jlog::Fatal("We couldn't unmap the buffer?");
glBindBuffer(buffer_type, current_buffer);
}