Compare commits
4 Commits
Prerelease
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
a22a83d2f8 | |||
76cd48c9b7 | |||
38bb1b22ce | |||
122644a013 |
@@ -27,7 +27,7 @@ CPMAddPackage(
|
||||
|
||||
CPMAddPackage(
|
||||
NAME ReWindow
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-21.zip
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-26.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
|
Binary file not shown.
@@ -28,13 +28,29 @@
|
||||
#include <J3ML/Geometry/Triangle2D.hpp>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/types/VRamList.h>
|
||||
|
||||
// Fonts that are included by default.
|
||||
namespace JGL::Fonts {
|
||||
void Init();
|
||||
// Built in fonts.
|
||||
inline Font Jupiteroid;
|
||||
}
|
||||
|
||||
// Simple shapes that are pre-computed and used in some draw functions.
|
||||
namespace JGL::ShapeCache {
|
||||
inline VRamList* cube_vertex_data = nullptr;
|
||||
inline VRamList* cube_index_data = nullptr;
|
||||
void Init();
|
||||
}
|
||||
|
||||
/// OpenGL Wrapper for rendering 2D & 3D graphics in both a 2D and 3D context.
|
||||
namespace JGL {
|
||||
|
||||
|
||||
using namespace J3ML::LinearAlgebra;
|
||||
using namespace J3ML::Geometry;
|
||||
|
||||
|
||||
[[nodiscard]] bool Init(const Vector2& window_size, float fovY, float far_plane);
|
||||
|
||||
/// @param window_size
|
||||
@@ -336,7 +352,7 @@ namespace JGL::J2D {
|
||||
/// @param scale The value (in both axes) to scale the text by. Defaults to {1,1}.
|
||||
/// @param size The point-size at which to render the font out. Re-using the same point-size allows efficient glyph caching.
|
||||
/// @param font The font to use for rendering. @see Font.
|
||||
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
|
||||
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font = Fonts::Jupiteroid);
|
||||
|
||||
|
||||
/// Draws an Arc (section of a circle) to the screen.
|
||||
@@ -652,7 +668,7 @@ namespace JGL::J3D {
|
||||
/// @param font The font object to use when drawing.
|
||||
/// @param angle The orientation in 3D space.
|
||||
/// @param draw_back_face
|
||||
void DrawString(const Color4& color, const std::string& text, const Vector3& pos, float scale, u32 size, const Font& font, const EulerAngleXYZ& angle = {0, 0, 0}, bool draw_back_face = false);
|
||||
void DrawString(const Color4& color, const std::string& text, const Vector3& pos, float scale, u32 size, const Font& font = Fonts::Jupiteroid, const EulerAngleXYZ& angle = {0, 0, 0}, bool draw_back_face = false);
|
||||
|
||||
/// Draws a string of text in 3D space that is always facing the exact direction of the camera projection.
|
||||
void DrawBillboardString();
|
||||
|
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
#include <JGL/types/VRamList.h>
|
||||
#include <array>
|
||||
|
||||
namespace JGL::ShapeCache {
|
||||
inline VRamList* cube_vertex_data = nullptr;
|
||||
inline VRamList* cube_index_data = nullptr;
|
||||
void Init();
|
||||
}
|
@@ -13,8 +13,6 @@ extern "C" typedef struct FT_LibraryRec_* FT_Library;
|
||||
|
||||
namespace JGL
|
||||
{
|
||||
|
||||
//bool Init();
|
||||
bool InitTextEngine();
|
||||
|
||||
/// A Font class implementation.
|
||||
@@ -23,7 +21,8 @@ namespace JGL
|
||||
public:
|
||||
/// Default constructor does not initialize any members
|
||||
Font() = default;
|
||||
Font(const std::filesystem::path& path);
|
||||
explicit Font(const std::filesystem::path& path);
|
||||
Font(const unsigned char* data, const size_t& size);
|
||||
/// Destructor handles freeing of the underlying asset handle.
|
||||
~Font();
|
||||
static Font LoadTTF(const std::filesystem::path& filepath);
|
||||
@@ -35,6 +34,6 @@ namespace JGL
|
||||
Vector2 MeasureString(const std::string& text, unsigned int ptSize);
|
||||
public:
|
||||
int index = 0;
|
||||
FT_Face face;
|
||||
FT_Face face = nullptr;
|
||||
};
|
||||
}
|
@@ -21,12 +21,27 @@ protected:
|
||||
float constant_attenuation;
|
||||
float linear_attenuation;
|
||||
float quadratic_attenuation;
|
||||
public:
|
||||
[[nodiscard]] Vector3 GetPosition() const;
|
||||
[[nodiscard]] Color4 GetAmbient() const;
|
||||
[[nodiscard]] Color4 GetDiffuse() const;
|
||||
[[nodiscard]] Color4 GetSpecular() const;
|
||||
|
||||
[[nodiscard]] float GetConstantAttenuation() const;
|
||||
[[nodiscard]] float GetLinearAttenuation() const;
|
||||
[[nodiscard]] float GetQuadraticAttenuation() const;
|
||||
public:
|
||||
/// Runs a calculation to determine the lights influence on a given point in 3D space.
|
||||
/// @note 0 would be no impact, 1 would be the light is at the same position.
|
||||
[[nodiscard]] virtual float GetAttenuationAtPosition(const Vector3& pos) const { return 0; }
|
||||
public:
|
||||
virtual ~LightBase() = default;
|
||||
};
|
||||
|
||||
/// Omni-directional lights.
|
||||
class JGL::PointLight : public LightBase {
|
||||
public:
|
||||
[[nodiscard]] float GetAttenuationAtPosition(const Vector3& pos) const override;
|
||||
public:
|
||||
PointLight(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular, float constant_attenuation = 1, float linear_attenuation = 0, float quadratic_attenuation = 0);
|
||||
};
|
||||
|
60
main.cpp
60
main.cpp
@@ -7,10 +7,10 @@
|
||||
#include <J3ML/Geometry/AABB.hpp>
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL::Fonts;
|
||||
using namespace JGL;
|
||||
|
||||
JGL::Font FreeSans;
|
||||
JGL::Font Jupiteroid;
|
||||
float fps = 0.0f;
|
||||
|
||||
class Gizmo
|
||||
@@ -107,11 +107,10 @@ public:
|
||||
void initGL() {
|
||||
camera = new Camera;
|
||||
|
||||
if (!JGL::Init(getSize(), 75, 100))
|
||||
if (!JGL::Init(GetSize(), 75, 100))
|
||||
Logger::Fatal("Initialization failed.");
|
||||
|
||||
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
|
||||
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.ttf");
|
||||
|
||||
glClearColor(0.f, 0.f, 0.f, 0.f);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -132,8 +131,9 @@ public:
|
||||
|
||||
void display() {
|
||||
|
||||
float dt = 1.f / fps;
|
||||
JGL::Update(getSize());
|
||||
float dt = GetDeltaTime();
|
||||
|
||||
JGL::Update(GetSize());
|
||||
|
||||
if (fov_increasing)
|
||||
fov += 0.025;
|
||||
@@ -232,27 +232,30 @@ public:
|
||||
}
|
||||
|
||||
void OnRefresh(float elapsed) override {
|
||||
if (isKeyDown(Keys::RightArrow))
|
||||
|
||||
fps = GetRefreshRate();
|
||||
|
||||
if (IsKeyDown(Keys::RightArrow))
|
||||
camera->angle.y += 45.f * elapsed;
|
||||
if (isKeyDown(Keys::LeftArrow))
|
||||
if (IsKeyDown(Keys::LeftArrow))
|
||||
camera->angle.y -= 45.f * elapsed;
|
||||
if (isKeyDown(Keys::UpArrow))
|
||||
if (IsKeyDown(Keys::UpArrow))
|
||||
camera->angle.x -= 45.f * elapsed;
|
||||
if (isKeyDown(Keys::DownArrow))
|
||||
if (IsKeyDown(Keys::DownArrow))
|
||||
camera->angle.x += 45.f * elapsed;
|
||||
if (isKeyDown(Keys::Space))
|
||||
if (IsKeyDown(Keys::Space))
|
||||
camera->position.y += 1.f * elapsed;
|
||||
if (isKeyDown(Keys::LeftShift))
|
||||
if (IsKeyDown(Keys::LeftShift))
|
||||
camera->position.y -= 1.f * elapsed;
|
||||
|
||||
//This is wrong of course. Just for testing purposes.
|
||||
if (isKeyDown(Keys::W))
|
||||
if (IsKeyDown(Keys::W))
|
||||
camera->position.z += 1.f * elapsed;
|
||||
if (isKeyDown(Keys::S))
|
||||
if (IsKeyDown(Keys::S))
|
||||
camera->position.z -= 1.f * elapsed;
|
||||
if (isKeyDown(Keys::A))
|
||||
if (IsKeyDown(Keys::A))
|
||||
camera->position.x += 1.f * elapsed;
|
||||
if (isKeyDown(Keys::D))
|
||||
if (IsKeyDown(Keys::D))
|
||||
camera->position.x -= 1.f * elapsed;
|
||||
|
||||
|
||||
@@ -265,11 +268,11 @@ public:
|
||||
int glError = glGetError();
|
||||
if (glError != GL_NO_ERROR)
|
||||
std::cout << glError << std::endl;
|
||||
glSwapBuffers();
|
||||
GLSwapBuffers();
|
||||
}
|
||||
|
||||
|
||||
void OnMouseButtonDown(const ReWindow::WindowEvents::MouseButtonDownEvent & ev) override
|
||||
void OnMouseButtonDown(const ReWindow::MouseButtonDownEvent & ev) override
|
||||
{
|
||||
RWindow::OnMouseButtonDown(ev);
|
||||
a.Grab();
|
||||
@@ -278,7 +281,7 @@ public:
|
||||
d.Grab();
|
||||
}
|
||||
|
||||
void OnMouseButtonUp(const ReWindow::WindowEvents::MouseButtonUpEvent & ev) override
|
||||
void OnMouseButtonUp(const ReWindow::MouseButtonUpEvent & ev) override
|
||||
{
|
||||
RWindow::OnMouseButtonUp(ev);
|
||||
a.Release();
|
||||
@@ -294,19 +297,20 @@ public:
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
auto* window = new JGLDemoWindow("JGL Demo Window", 1280, 720);
|
||||
window->setRenderer(RenderingAPI::OPENGL);
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
window->Open();
|
||||
window->initGL();
|
||||
window->setResizable(true);
|
||||
window->setVsyncEnabled(false);
|
||||
window->SetResizable(true);
|
||||
window->SetVsyncEnabled(false);
|
||||
|
||||
while (window->isAlive()) {
|
||||
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
||||
window->pollEvents();
|
||||
window->refresh();
|
||||
std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<float> frame_time = stop - start;
|
||||
fps = 1.0f / frame_time.count();
|
||||
while (window->IsAlive()) {
|
||||
window->ManagedRefresh();
|
||||
//std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
||||
//window->PollEvents();
|
||||
//window->Refresh();
|
||||
//std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
|
||||
//std::chrono::duration<float> frame_time = stop - start;
|
||||
//fps = 1.0f / frame_time.count();
|
||||
}
|
||||
return 0;
|
||||
}
|
8671
src/Fonts.cpp
Normal file
8671
src/Fonts.cpp
Normal file
File diff suppressed because it is too large
Load Diff
14
src/JGL.cpp
14
src/JGL.cpp
@@ -11,7 +11,6 @@
|
||||
#include <J3ML/Geometry/OBB.hpp>
|
||||
#include <JGL/types/VRamList.h>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/ShapeCache.h>
|
||||
|
||||
JGL::RenderTarget* render_target = nullptr;
|
||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||
@@ -50,6 +49,7 @@ namespace JGL {
|
||||
}
|
||||
|
||||
InitTextEngine();
|
||||
Fonts::Init();
|
||||
ShapeCache::Init();
|
||||
wS = window_size;
|
||||
j3d_fov = fovY;
|
||||
@@ -1172,6 +1172,10 @@ namespace JGL {
|
||||
// For objects that are *extremely* big, Like base level geometry, I'll have to use its collision map for this to look right. - Redacted.
|
||||
|
||||
#pragma region J3D
|
||||
|
||||
#pragma region internal_drawables
|
||||
#pragma endregion
|
||||
|
||||
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);
|
||||
@@ -1183,6 +1187,14 @@ namespace JGL {
|
||||
return result;
|
||||
}
|
||||
|
||||
float EffectOfLightOnPointIn3DSpace(const PointLight* light, const Vector3& position) {
|
||||
Vector3 light_pos = light->GetPosition();
|
||||
Vector3 vector_to_position = position - light_pos;
|
||||
float distance = vector_to_position.Length();
|
||||
|
||||
return 1.0f / (light->GetConstantAttenuation() + light->GetLinearAttenuation() * distance + light->GetQuadraticAttenuation() * distance * distance);
|
||||
}
|
||||
|
||||
void J3D::ChangeFOV(float fov) {
|
||||
j3d_fov = fov;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include <JGL/ShapeCache.h>
|
||||
#include <JGL/JGL.h>
|
||||
|
||||
void JGL::ShapeCache::Init() {
|
||||
if (!cube_vertex_data) {
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
#include <glad/glad.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <JGL/JGL.h>
|
||||
|
||||
#if __linux__
|
||||
#include <freetype2/ft2build.h>
|
||||
@@ -58,25 +59,38 @@ namespace JGL {
|
||||
return Detail::InitTextEngine();
|
||||
}
|
||||
|
||||
Font::Font(const unsigned char* data, const size_t& size) {
|
||||
if (Detail::ft == nullptr)
|
||||
throw std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||
|
||||
if (FT_New_Memory_Face(Detail::ft, data, size, 0, &face))
|
||||
throw std::runtime_error("Error::FREETYPE: Failed to load font!");
|
||||
|
||||
unsigned int new_index = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= new_index)
|
||||
new_index = f.index + 1;
|
||||
index = new_index;
|
||||
|
||||
Detail::fonts.push_back(*this);
|
||||
std::cout << "Loaded font from memory at " << static_cast<const void*>(data) << " with index " << new_index << std::endl;
|
||||
}
|
||||
|
||||
Font::Font(const std::filesystem::path& path) {
|
||||
if (Detail::ft == nullptr)
|
||||
throw std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||
|
||||
Font font;
|
||||
if (FT_New_Face(Detail::ft, path.string().c_str(), 0, &face)) {
|
||||
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
|
||||
if (FT_New_Face(Detail::ft, path.string().c_str(), 0, &face))
|
||||
throw std::runtime_error("Error::FREETYPE: Failed to load font!");
|
||||
//return -1;
|
||||
}
|
||||
unsigned int newIndex = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= newIndex)
|
||||
newIndex = f.index + 1;
|
||||
|
||||
index = newIndex;
|
||||
Detail::fonts.push_back(font);
|
||||
std::cout << "Loaded font from " << path << " with index " << newIndex << std::endl;
|
||||
//return newIndex;
|
||||
unsigned int new_index = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= new_index)
|
||||
new_index = f.index + 1;
|
||||
|
||||
index = new_index;
|
||||
Detail::fonts.push_back(*this);
|
||||
std::cout << "Loaded font from " << path << " with index " << new_index << std::endl;
|
||||
}
|
||||
|
||||
Font::~Font() {
|
||||
@@ -119,14 +133,14 @@ namespace JGL {
|
||||
return extents;
|
||||
}
|
||||
|
||||
jlog::Warning("Measuring a font size that is not cached, This is *super* slow.");
|
||||
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
|
||||
jlog::Warning("Measuring a font size that is not cached, Defaulting to Jupiteroid.");
|
||||
FT_Set_Pixel_Sizes(Fonts::Jupiteroid.face, ptSize, ptSize);
|
||||
|
||||
for (const char& c : text) {
|
||||
FT_GlyphSlot slot = face->glyph;
|
||||
auto glyph_index = FT_Get_Char_Index(this->face, c);
|
||||
FT_GlyphSlot slot = Fonts::Jupiteroid.face->glyph;
|
||||
auto glyph_index = FT_Get_Char_Index(Fonts::Jupiteroid.face, c);
|
||||
|
||||
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
|
||||
auto error = FT_Load_Glyph(Fonts::Jupiteroid.face, glyph_index, FT_LOAD_DEFAULT);
|
||||
|
||||
if (error)
|
||||
continue;
|
||||
@@ -146,9 +160,6 @@ namespace JGL {
|
||||
if (extents.y < ptSize)
|
||||
extents.y = ptSize;
|
||||
}
|
||||
|
||||
return extents;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -10,6 +10,14 @@ JGL::PointLight::PointLight(const Vector3& position, const Color4& ambient, cons
|
||||
this->quadratic_attenuation = quadratic_attenuation;
|
||||
}
|
||||
|
||||
float JGL::PointLight::GetAttenuationAtPosition(const Vector3& pos) const {
|
||||
Vector3 light_pos = {position.x, position.y, position.z};
|
||||
Vector3 vector_to_position = pos - light_pos;
|
||||
float distance = vector_to_position.Length();
|
||||
|
||||
return 1.0f / (GetConstantAttenuation() + GetLinearAttenuation() * distance + GetQuadraticAttenuation() * distance * distance);
|
||||
}
|
||||
|
||||
JGL::SpotLight::SpotLight(const Vector3& position, const Matrix3x3& ro_mat, float cone_size_degrees, float exponent, const Color4& ambient, const Color4& diffuse, const Color4& specular,
|
||||
float constant_attenuation, float linear_attenuation, float quadratic_attenuation) {
|
||||
this->position = Vector4(position, 1);
|
||||
@@ -24,3 +32,33 @@ JGL::SpotLight::SpotLight(const Vector3& position, const Matrix3x3& ro_mat, floa
|
||||
this->linear_attenuation = linear_attenuation;
|
||||
this->quadratic_attenuation = quadratic_attenuation;
|
||||
}
|
||||
|
||||
Vector3 JGL::LightBase::GetPosition() const {
|
||||
return {position.x, position.y, position.z};
|
||||
}
|
||||
|
||||
Color4 JGL::LightBase::GetAmbient() const {
|
||||
return ambient;
|
||||
}
|
||||
|
||||
Color4 JGL::LightBase::GetDiffuse() const {
|
||||
return diffuse;
|
||||
}
|
||||
|
||||
Color4 JGL::LightBase::GetSpecular() const {
|
||||
return specular;
|
||||
}
|
||||
|
||||
float JGL::LightBase::GetConstantAttenuation() const {
|
||||
return constant_attenuation;
|
||||
}
|
||||
|
||||
float JGL::LightBase::GetLinearAttenuation() const {
|
||||
return linear_attenuation;
|
||||
}
|
||||
|
||||
float JGL::LightBase::GetQuadraticAttenuation() const {
|
||||
return quadratic_attenuation;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user