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);