Compare commits

...

13 Commits

Author SHA1 Message Date
608e9e29f5 Support for multiple fonts 2024-07-05 05:31:25 -04:00
c53fa52f6f FreeType2 2024-06-29 15:07:02 -04:00
7fa75cac54 initial 2024-06-29 12:37:16 -04:00
Redacted
d5f9eaa5a8 Update CMakeLists.txt 2024-06-26 23:30:40 -04:00
dead018993 Update CMakeLists.txt 2024-06-18 14:43:56 -04:00
Redacted
e9a339182b Update CMakeLists.txt 2024-06-18 14:22:10 -04:00
Redacted
076398b290 Update CMakeLists.txt 2024-06-18 14:19:45 -04:00
cf72d92c28 Merge branch 'master' of https://git.redacted.cc/josh/JGL 2024-05-29 22:18:41 -04:00
5d99f8ec1f update 2024-05-29 22:18:40 -04:00
a388ee8021 Implement proper GL state management in DrawString2D, DrawString3D 2024-05-29 15:27:24 -04:00
41916a4089 Update JGL.cpp 2024-05-29 15:05:06 -04:00
222dd346fb Fix memleak 2024-05-27 20:48:22 -04:00
e6e567725b Color 2024-05-23 12:05:00 -04:00
5 changed files with 182 additions and 83 deletions

View File

@@ -9,50 +9,88 @@ if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
endif()
set(CMAKE_CXX_STANDARD 20)
if (WIN32)
set(CMAKE_CXX_FLAGS "-municode")
endif(WIN32)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Enable Package Managers
include(cmake/CPM.cmake)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Release-6.zip
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.2.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.30.zip
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-3.zip
)
CPMAddPackage(
NAME GLAD
URL https://git.redacted.cc/Redacted/glad/archive/v2.1.zip
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_mt.zip
)
CPMAddPackage(
NAME jlog
URL https://git.redacted.cc/josh/jlog/Prerelease-9.zip
)
CPMAddPackage(
NAME Event
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
)
if (WIN32)
#CPMAddPackage(
#NAME harfbuzz
#URL https://github.com/harfbuzz/harfbuzz/archive/refs/tags/9.0.0.zip
#)
CPMAddPackage(
NAME freetype
URL https://github.com/freetype/freetype/archive/refs/tags/VER-2-13-2.zip
)
endif()
file(COPY "assets" DESTINATION "${PROJECT_BINARY_DIR}")
file(GLOB_RECURSE ASSETS "assets/*")
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp" )
file(GLOB_RECURSE ASSETS "assets/*")
add_library(JGL SHARED ${SOURCES})
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
find_package(OpenGL REQUIRED)
find_package(Freetype REQUIRED)
if (UNIX AND NOT APPLE)
find_package(Freetype REQUIRED)
add_library(JGL SHARED ${SOURCES})
endif()
if (WIN32)
add_library(JGL STATIC ${SOURCES})
endif()
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(JGL PUBLIC
${PROJECT_SOURCE_DIR}/include
${OPENGL_INCLUDE_DIRS}
${J3ML_SOURCE_DIR}/include
${Event_SOURCE_DIR}/include
${ReWindow_SOURCE_DIR}/include
${glad_SOURCE_DIR}/include
${jlog_SOURCE_DIR}/include
)
add_executable(JGL_Demo main.cpp)
set_target_properties(JGL_Demo PROPERTIES LINK_FLAGS "-Wl,-rpath,./lib")
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${OPENGL_INCLUDE_DIRS})
include_directories(${J3ML_SOURCE_DIR}/include)
include_directories(${ReWindow_SOURCE_DIR}/include)
include_directories(${glad_SOURCE_DIR}/include)
#set_target_properties(JGL_Demo PROPERTIES LINK_FLAGS "-Wl,-rpath,./lib")
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL PRIVATE ${FREETYPE_LIBRARIES})
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} J3ML ReWindowLibrary glad)
if (UNIX AND NOT APPLE)
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL PRIVATE ${FREETYPE_LIBRARIES})
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} J3ML ReWindowLibrary glad jlog Event)
endif()
if (WIN32)
target_include_directories(JGL PRIVATE ${freetype_SOURCE_DIR}/include)
target_link_libraries(JGL PRIVATE freetype)
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} J3ML ReWindowLibrary glad jlog Event)
endif()
target_link_libraries(JGL_Demo PUBLIC JGL)

BIN
assets/fonts/Jupiteroid.ttf Normal file

Binary file not shown.

View File

@@ -3,18 +3,15 @@
//
#pragma once
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include <string>
#include <iostream>
#include <JGL/Color3.h>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/Geometry/Sphere.h>
#include <J3ML/Geometry/OBB.h>
#include <J3ML/Geometry/Capsule.h>
#include <J3ML/Geometry/Plane.h>
#include <J3ML/Geometry/TriangleMesh.h>
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
namespace JGL {
@@ -52,7 +49,8 @@ namespace JGL {
};
bool InitTextEngine();
int LoadFont(const std::string& font_path);
void UnloadFont(int font_index);
// TODO: implement correct coloring
namespace J2D {
@@ -70,7 +68,7 @@ namespace JGL {
void FillTexturedPolygon2D();
void DrawSprite2D();
void DrawPartialSprite2D();
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size = 16);
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index);
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size);
void OutlineRect2D ( const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void FillRoundedRect2D (const Color3& color, const Vector2& pos, const Vector2& size, float radius);
@@ -89,7 +87,7 @@ namespace JGL {
void WireframeCapsule3D(const Color3& color, const Capsule& cap, float thickness = 1);
void FillTriangleMesh3D(const Color3& color, const TriangleMesh& mesh);
void WireframeTriangleMesh3D(const Color3& color, const TriangleMesh& mesh, float thickness = 1);
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size = 12);
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size, unsigned int font_index);
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
void DrawMatrixGizmo (const Matrix4x4&);

View File

@@ -6,10 +6,6 @@
using J3ML::LinearAlgebra::Vector2;
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
struct Character
{
unsigned int TextureID; // ID handle of the glyph texture
@@ -30,6 +26,9 @@ struct point {
GLfloat t;
};
int FreeSans;
int Jupiteroid;
class JGLDemoWindow : public ReWindow::RWindow
{
public:
@@ -57,12 +56,13 @@ public:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
JGL::InitTextEngine();
JGL::InitTextEngine();
FreeSans = JGL::LoadFont("assets/fonts/FreeSans.ttf");
Jupiteroid = JGL::LoadFont("assets/fonts/Jupiteroid.ttf");
}
void display()
{
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
@@ -80,15 +80,13 @@ public:
{105, 100}
};
JGL::J2D::FillTriangle2D(JGL::Colors::Yellow, tri);
JGL::J3D::DrawString3D(JGL::Colors::White, "JGL Sample Text", {1, -120, 0.5f}, 2.f);
JGL::J2D::DrawString2D(JGL::Colors::Green, "William J. Tomasine II ", 0.f, -120.f, 1.f);
JGL::J2D::DrawLine2D(JGL::Colors::Greens::DarkGreen, {10, 10}, {200, 300});
JGL::J3D::DrawLine3D(JGL::Colors::Red, {0,0,0}, {1,0,0});
JGL::J3D::DrawLine3D(JGL::Colors::Red, {0,0,0}, {1,0,0});
JGL::J3D::DrawString3D(JGL::Colors::Red, "JGL Sample Text", {1, -32, 0.5f}, 1.f, 32, FreeSans);
JGL::J2D::DrawString2D(JGL::Colors::Green, "Jupiteroid Font", 0.f, -48.f, 1.f, 16, Jupiteroid);
//glFlush();
}
@@ -109,7 +107,7 @@ int main(int argc, char** argv)
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
window->initGL();
window->setResizable(true);
window->setResizable(false);
while (window->isAlive())
{
window->pollEvents();

View File

@@ -2,39 +2,63 @@
// Created by dawsh on 1/17/24.
//
#include <vector>
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include <JGL/Color3.h>
#include <iostream>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
GLuint program;
GLuint texture;
namespace JGL
{
FT_Face face;
FT_Library ft;
struct Font {
int index = 0;
FT_Face face;
};
std::vector<Font> faces;
using namespace J3ML;
bool InitTextEngine() {
constexpr u32 default_font_size = 16;
if (FT_Init_FreeType(&ft))
{
std::cout << "Error::FREETYPE: " << std::endl;
return false;
}
return true;
return false;
}
if (FT_New_Face(ft, "assets/fonts/FreeSans.ttf", 0, &face))
{
int LoadFont(const std::string &font_path) {
if (ft == nullptr)
return -1;
Font font;
if (FT_New_Face(ft, font_path.c_str(), 0, &font.face)) {
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return false;
return -1;
}
return true;
unsigned int newIndex = 0;
for (const auto& f : faces)
if (f.index >= newIndex)
newIndex = f.index + 1;
font.index = newIndex;
faces.push_back(font);
std::cout << "Loaded font from " << font_path << " with index " << newIndex << std::endl;
return newIndex;
}
void UnloadFont(int font_index) {
for (int i = 0; i < faces.size(); i++)
if (faces[i].index == font_index)
FT_Done_Face(faces[i].face),
faces.erase(faces.begin() + i);
}
namespace J2D
@@ -146,23 +170,38 @@ namespace JGL
glEnd();
}
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size) {
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index) {
glUseProgram(0); // Fixed-function pipeline.
Font font{};
for (const auto& f : faces)
if (f.index == font_index)
font = f;
if (font.face == NULL) {
std::cout << "null font" << std::endl;
return;
}
GLfloat currentColor[4];
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
FT_Set_Pixel_Sizes(face, 0, size);
FT_Set_Pixel_Sizes(font.face, 0, size);
const char* c;
for (c = text.c_str(); *c; c++)
std::vector<GLuint> textures(text.length());
for (int i = 0; i < text.length(); i++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
if (FT_Load_Char(font.face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
FT_GlyphSlot g = font.face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textures.at(i));
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -200,9 +239,14 @@ namespace JGL
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
for (unsigned int& texture : textures)
glDeleteTextures(1, &texture);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
}
}
@@ -211,7 +255,6 @@ namespace JGL
{
void DrawLine3D(const Color3& color, const Vector3& A, const Vector3& B, float thickness)
{
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
@@ -220,27 +263,42 @@ namespace JGL
glEnd();
}
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size)
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size, unsigned int font_index)
{
float x = pos.x;
float y = pos.y;
float z = pos.z;
GLfloat currentColor[4];
std::vector<GLuint> textures(text.length());;
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glUseProgram(0); // Fixed-function pipeline.
glColor3f(1.0f, 1.0f, 1.0f);
glColor4f(color.r, color.g, color.b, 1.0f);
FT_Set_Pixel_Sizes(face, 0, size);
Font font;
for (auto& f : faces)
if (f.index == font_index)
font = f;
if (font.face == NULL) {
std::cout << "null font" << std::endl;
return;
}
const char* c;
for (c = text.c_str(); *c; c++)
FT_Set_Pixel_Sizes(font.face, 0, size);
//for (c = text.c_str(); *c; c++)
for (int i = 0; i < text.length(); i++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
if (FT_Load_Char(font.face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
FT_GlyphSlot g = font.face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textures.at(i));
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -281,7 +339,14 @@ namespace JGL
y += (g->advance.y >> 6) * scale;
}
for (unsigned int& texture : textures)
glDeleteTextures(1, &texture);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
}
}