Compare commits

...

23 Commits

Author SHA1 Message Date
c62b58aa16 Font cache
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m34s
2024-07-06 22:51:25 -04:00
11ce9ac5fe ReCI test
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m18s
2024-07-05 11:44:34 -04:00
7310825ddd ReCI test
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m28s
2024-07-05 11:38:48 -04:00
513dc1e332 fixed syntax error in reci script
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m2s
2024-07-05 10:56:43 -04:00
63bbb8794e Wrote local ReCI script for JGL
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 39s
2024-07-05 10:54:59 -04:00
178f328728 ReCI test
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 58s
2024-07-05 09:35:51 -04:00
1c74ee9c71 Add ReCI build test 2024-07-05 09:30:20 -04:00
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
93612bb816 Update JGL 2024-05-23 10:57:48 -04:00
9ac22a8a87 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	CMakeLists.txt
2024-05-01 20:30:50 -04:00
1eaa9c6574 Migrating dependencies to latest releases 2024-05-01 20:30:21 -04:00
12 changed files with 702 additions and 416 deletions

View File

@@ -0,0 +1,24 @@
name: Run ReCI Build Test
run-name: Run ReCI Build Test For ${{ gitea.repository }}.
on: [push]
jobs:
Explore-Gitea-Actions:
runs-on: ubuntu-22.04
env:
RECI_GIT: https://git.redacted.cc/maxine/ReCI
RECI: /RECI
steps:
- run: echo "The job was automatically triggered by a ${{ gitea.event_name }} event."
- run: echo "This job is now running on a ${{ runner.os }} server hosted by Gitea!"
- run: echo "The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "The ${{ gitea.repository }} repository has been cloned to the runner."
- run: echo "The workflow is now ready to run your tests on the runner."
- run: echo "Install toolchain and run ReCI build test"
- run: apt-get update && apt-get install -y git && git clone $RECI_GIT $RECI
- run: bash $RECI/scripts/setup_build_tools.sh
- run: bash reci/scripts/install_build_dependencies.sh
- run: bash $RECI/scripts/run_buildtest.sh ${{ gitea.repository }}
- run: echo "This job's status is ${{ job.status }}."

View File

@@ -1,77 +1,96 @@
cmake_minimum_required(VERSION 3.25)
project(JGL
VERSION 1.0
LANGUAGES CXX
)
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not allowed")
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/Prerelease-20.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.19.zip
)
CPMAddPackage(
NAME GLAD
URL https://git.redacted.cc/Redacted/glad/archive/v2.1.zip
)
CPMAddPackage(
NAME LearnOpenGL
URL https://git.redacted.cc/josh/LearnOpenGL/archive/Prerelease-2.zip
)
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp" )
file(GLOB_RECURSE ASSETS "content/*")
add_library(JGL SHARED ${SOURCES}
include/JGL/JGL.h
src/JGL/JGL.cpp
include/LearnOpenGL/Shader.h
include/LearnOpenGL/Texture2D.h
include/JGL/Color3.h
include/JGL/Colors.h
include/JGL/Color4.h
)
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(Freetype REQUIRED)
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(${GLUT_INCLUDE_DIRS})
include_directories(${J3ML_SOURCE_DIR}/include)
include_directories(${ReWindow_SOURCE_DIR}/include)
include_directories(${glad_SOURCE_DIR}/include)
target_include_directories(JGL PUBLIC ${LearnOpenGL_SOURCE_DIR}/include)
target_link_libraries(JGL PUBLIC LearnOpenGL)
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} ${GLUT_LIBRARIES} J3ML ReWindowLibrary GLEW glad)
cmake_minimum_required(VERSION 3.25)
project(JGL
VERSION 1.0
LANGUAGES CXX
)
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not allowed")
endif()
set(CMAKE_CXX_STANDARD 20)
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-2.2.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-3.zip
)
CPMAddPackage(
NAME GLAD
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" )
find_package(OpenGL 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")
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)

View File

@@ -8,4 +8,5 @@
# Non-Goals
* Full Rendering Engine
* OpenGL/Vulkan Wrapper
* Asset Loading & Management
* Asset Loading & Management

BIN
assets/fonts/Jupiteroid.ttf Normal file

Binary file not shown.

53
include/JGL/FontCache.h Normal file
View File

@@ -0,0 +1,53 @@
#pragma once
#include <vector>
#include <glad/glad.h>
namespace JGL {
class CachedGlyph;
class CachedFont;
class FontCache;
}
class JGL::CachedGlyph {
private:
GLuint texture = 0;
char character;
public:
int x2offset = 0, y2offset = 0, w = 0, h = 0;
float advanceX = 0, advanceY = 0;
//CachedGlyph(GLuint texture_id, char c);
CachedGlyph(GLuint texture_id, char c, float x2o, float y2o, float w, float h, float advX, float advY);
char getCharacter();
const GLuint* getTexture();
};
class JGL::CachedFont {
private:
std::vector<CachedGlyph*> glyphs{};
unsigned int font_size = 0;
unsigned int font_index = 0;
public:
void appendGlyph(CachedGlyph* glyph);
void eraseGlyph(CachedGlyph* glyph);
void eraseGlyph(char c);
void eraseGlyph(GLuint texture_id);
unsigned int getFontSize();
unsigned int getFontIndex();
CachedGlyph* getGlyph(char c);
std::vector<CachedGlyph*>* getGlyphs();
CachedFont(unsigned int font_size, unsigned int font_index);
};
class JGL::FontCache {
private:
std::vector<CachedFont*> cachedFonts = {};
public:
std::vector<CachedFont*>* getFonts();
CachedFont* getFont(unsigned int font_size, unsigned int font_index);
void appendFont(CachedFont* font);
void newFont(unsigned int font_size, unsigned int font_index);
void eraseFont(CachedFont* font);
void purgeCache();
};

View File

@@ -3,18 +3,16 @@
//
#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 <JGL/FontCache.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,9 +50,11 @@ namespace JGL {
};
bool InitTextEngine();
inline FontCache fontCache;
int LoadFont(const std::string& font_path);
void UnloadFont(int font_index);
// TODO: implement correct coloring
using J3ML::u32;
namespace J2D {
void DrawPixel2D(const Color3 &color, const Vector2 &coordinates);
@@ -71,7 +71,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);
@@ -90,7 +90,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

@@ -1,18 +1,11 @@
//#include <GL/glew.h>
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <rewindow/types/window.h>
#include <iostream>
#include <LearnOpenGL/Shader.h>
#include <JGL/Colors.h>
#include <J3ML/LinearAlgebra/Vector2.h>
using J3ML::LinearAlgebra::Vector2;
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
struct Character
{
unsigned int TextureID; // ID handle of the glyph texture
@@ -23,21 +16,6 @@ struct Character
std::map<char, Character> Characters;
GLuint VAO, VBO;
const std::string vertexShader = "attribute vec4 coord;\n"
"varying vec2 texpos;\n"
"\n"
"void main(void) {\n"
" gl_Position = vec4(coord.xy, 0, 1);\n"
" texpos = coord.zw;\n"
"}\n"
"";
const std::string fragmentShader = "varying vec2 texpos;\n"
"uniform sampler2D tex;\n"
"uniform vec4 color;\n"
"\n"
"void main(void) {\n"
" gl_FragColor = vec4(1, 1, 1, texture2D(tex, texpos).a) * color;\n"
"}";
using J3ML::LinearAlgebra::Matrix4x4;
@@ -48,20 +26,17 @@ struct point {
GLfloat t;
};
int FreeSans;
int Jupiteroid;
class JGLDemoWindow : public ReWindow::RWindow
{
public:
LearnOpenGL::Shader shader;
JGLDemoWindow() : ReWindow::RWindow() {}
JGLDemoWindow(const std::string& title, int width, int height) :
ReWindow::RWindow(title, width, height)
{
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height){}
}
void initGL()
{
void initGL() {
gladLoadGL();
glMatrixMode(GL_PROJECTION);
@@ -76,14 +51,14 @@ 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);
glLoadIdentity();
glOrtho(0, getSize().x, getSize().y, 0, -1, 1);
@@ -99,15 +74,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();
}
@@ -128,7 +101,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

@@ -0,0 +1,7 @@
source $RECI/shlib/info.sh
INFO "Installing build dependencies for JGL"
GROUP="Install build dependencies" RUN '
apt-get install -yq libgl1-mesa-dev libfreetype-dev
'

119
src/FontCache.cpp Normal file
View File

@@ -0,0 +1,119 @@
#include <JGL/FontCache.h>
using namespace JGL;
char CachedGlyph::getCharacter() {
return character;
}
const GLuint* CachedGlyph::getTexture() {
return &texture;
}
CachedGlyph::CachedGlyph(GLuint texture_id, char c, float x2offset, float y2offset, float w, float h, float advanceX, float advanceY) {
texture = texture_id;
character = c;
this->x2offset = x2offset;
this->y2offset = y2offset;
this->w = w;
this->h = h;
this->advanceX = advanceX;
this->advanceY = advanceY;
}
void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) {
glyphs.push_back(glyph);
}
unsigned int JGL::CachedFont::getFontSize() {
return font_size;
}
unsigned int JGL::CachedFont::getFontIndex() {
return font_index;
}
CachedGlyph* JGL::CachedFont::getGlyph(char c) {
for (const auto& g : glyphs)
if (c == g->getCharacter())
return g;
return nullptr;
}
CachedFont::CachedFont(unsigned int font_size, unsigned int font_index) {
this->font_size = font_size;
this->font_index = font_index;
}
void CachedFont::eraseGlyph(CachedGlyph* glyph) {
if (glyph == nullptr)
return;
for (int i = 0; i < glyphs.size(); i++)
if (glyphs[i] == glyph)
glDeleteTextures(1, glyphs[i]->getTexture()),
delete glyphs[i],
glyphs.erase(glyphs.begin() + i);
}
void CachedFont::eraseGlyph(char c) {
for (int i = 0; i < glyphs.size(); i++)
if (glyphs[i]->getCharacter() == c)
glDeleteTextures(1, glyphs[i]->getTexture()),
delete glyphs[i],
glyphs.erase(glyphs.begin() + i);
}
void CachedFont::eraseGlyph(GLuint texture_id) {
for (int i = 0; i < glyphs.size(); i++)
if (glyphs[i]->getTexture() == &texture_id)
glDeleteTextures(1, glyphs[i]->getTexture()),
delete glyphs[i],
glyphs.erase(glyphs.begin() + i);
}
std::vector<CachedGlyph*>* CachedFont::getGlyphs() {
return &glyphs;
}
void FontCache::appendFont(CachedFont* font) {
cachedFonts.push_back(font);
}
void FontCache::newFont(unsigned int font_size, unsigned int font_index) {
auto* font = new CachedFont(font_size, font_index);
cachedFonts.push_back(font);
}
void FontCache::eraseFont(CachedFont* font) {
for (int i = 0; i < cachedFonts.size(); i++) {
if (cachedFonts[i] == font) {
for (auto& g: *cachedFonts[i]->getGlyphs())
cachedFonts[i]->eraseGlyph(g);
delete cachedFonts[i];
cachedFonts.erase(cachedFonts.begin() + i);
}
}
}
void FontCache::purgeCache() {
//Remove every font from the cache.
for (const auto& font : cachedFonts)
eraseFont(font);
cachedFonts = {};
}
std::vector<CachedFont*>* FontCache::getFonts() {
return &cachedFonts;
}
CachedFont* FontCache::getFont(unsigned int font_size, unsigned int font_index) {
if (cachedFonts.empty())
return nullptr;
for (auto* f : cachedFonts)
if (f->getFontIndex() == font_index && f->getFontSize() == font_size)
return f;
return nullptr;
}

379
src/JGL.cpp Normal file
View File

@@ -0,0 +1,379 @@
//
// Created by dawsh on 1/17/24.
//
#include <vector>
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <JGL/Color3.h>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
GLuint program;
namespace JGL
{
FT_Library ft;
struct Font {
int index = 0;
FT_Face face;
};
std::vector<Font> faces;
using namespace J3ML;
bool InitTextEngine() {
if (FT_Init_FreeType(&ft))
return true;
return false;
}
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 -1;
}
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 {
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_QUADS);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glVertex2f(vp_pos.x, vp_pos.y);
glVertex2f(vp_pos.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y);
glEnd();
}
void OutlineRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size, float thickness) {
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_LINE_LOOP);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_pos.x, vp_pos.y);
glVertex2f(vp_pos.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y);
glEnd();
}
void DrawLine2D(const Color3 &color, const Vector2 &A, const Vector2 &B, float thickness) {
auto vp_a = A;
auto vp_b = B;
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_a.x, vp_a.y);
glVertex2f(vp_b.x, vp_b.y);
glEnd();
}
void DrawLine2D(const Color3 &color, float x, float y, float w, float h, float thickness) {
DrawLine2D(color, {x, y}, {w, h}, thickness);
}
void DrawPixel2D(const Color3 &color, float x, float y) {
DrawPixel2D(color, {x, y});
}
void DrawPixel2D(const Color3 &color, const Vector2 &coordinates) {
auto vp_pos = coordinates;
glBegin(GL_POINT);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_pos.x, vp_pos.y);
glEnd();
}
void
OutlineCircle2D(const Color3 &color, const Vector2 &center, float radius, int subdivisions, float thickness) {
glBegin(GL_LINE_LOOP);
GLfloat angle;
glColor3f(color.r, color.g, color.b);
float step = (2.f * M_PI) / (float) subdivisions;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step) {
GLfloat x = radius * sin(angle);
GLfloat y = radius * cos(angle);
x += center.x;
y += center.y;
glVertex2f(x, y);
}
glEnd();
}
void FillCircle2D(const Color3 &color, const Vector2 &center, float radius, int subdivisions) {
glBegin(GL_POLYGON);
GLfloat angle;
glColor3f(color.r, color.g, color.b);
float step = (2.f * M_PI) / (float) subdivisions;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step) {
GLfloat x = radius * sin(angle);
GLfloat y = radius * cos(angle);
x += center.x;
y += center.y;
glVertex2f(x, y);
}
glEnd();
}
void OutlineTriangle2D(const Color3 &color, const Triangle2D &tri) {
glBegin(GL_LINE_LOOP);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
}
void FillTriangle2D(const Color3 &color, const Triangle2D &tri) {
glBegin(GL_LINE_LOOP);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
}
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{};
CachedFont* cachedFont = fontCache.getFont(size, font_index);
//If the font doesn't exist in the cache yet.
if (!cachedFont) {
fontCache.newFont(size, font_index);
cachedFont = fontCache.getFont(size, font_index);
}
//Set up the regular font.
for (const auto& f : faces)
if (f.index == font_index)
font = f;
if (font.face == nullptr)
return;
GLfloat currentColor[4];
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, 1.0f);
FT_Set_Pixel_Sizes(font.face, 0, size);
std::vector<GLuint> textures(text.length());
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
//For each character
for (int i = 0; i < text.length(); i++) {
float x2, y2, w, h;
//If the font is in the cache already.
if (cachedFont->getGlyph(text.c_str()[i])) {
CachedGlyph* glyph = cachedFont->getGlyph(text.c_str()[i]);
glBindTexture(GL_TEXTURE_2D, *glyph->getTexture());
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
x2 = x + glyph->x2offset * scale;
y2 = -y - glyph->y2offset * scale; // Adjust y-coordinate
w = glyph->w * scale;
h = glyph->h * scale;
x += glyph->advanceX * scale;
y += glyph->advanceY * scale;
} else {
if (FT_Load_Char(font.face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = font.face->glyph;
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
x2 = x + g->bitmap_left * scale;
y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
w = g->bitmap.width * scale;
h = g->bitmap.rows * scale;
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
cachedFont->appendGlyph(new CachedGlyph(textures.at(i), text.c_str()[i], g->bitmap_left, g->bitmap_top, g->bitmap.width, g->bitmap.rows, (g->advance.x >> 6), (g->advance.y >> 6)));
}
glBegin(GL_TRIANGLES);
glTexCoord2f(0, 0);
glVertex2f(x2, y2);
glTexCoord2f(0, 1);
glVertex2f(x2, y2 + h);
glTexCoord2f(1, 1);
glVertex2f(x2 + w, y2 + h);
glTexCoord2f(0, 0);
glVertex2f(x2, y2);
glTexCoord2f(1, 1);
glVertex2f(x2 + w, y2 + h);
glTexCoord2f(1, 0);
glVertex2f(x2 + w, y2);
glEnd();
}
//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.
}
}
namespace J3D
{
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);
glVertex3f(A.x, A.y, A.z);
glVertex3f(B.x, B.y, B.z);
glEnd();
}
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.
glColor4f(color.r, color.g, color.b, 1.0f);
Font font;
for (auto& f : faces)
if (f.index == font_index)
font = f;
if (font.face == NULL) {
std::cout << "null font" << std::endl;
return;
}
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(font.face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = font.face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
float x2 = x + g->bitmap_left * scale;
float y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
float z2 = z;
float w = g->bitmap.width * scale;
float h = g->bitmap.rows * scale;
glBegin(GL_TRIANGLES);
glTexCoord2f(0, 0);
glVertex3f(x2, y2, z2);
glTexCoord2f(0, 1);
glVertex3f(x2, y2 + h, z2);
glTexCoord2f(1, 1);
glVertex3f(x2 + w, y2 + h, z2);
glTexCoord2f(0, 0);
glVertex3f(x2, y2, z2);
glTexCoord2f(1, 1);
glVertex3f(x2 + w, y2 + h, z2);
glTexCoord2f(1, 0);
glVertex3f(x2 + w, y2, z2);
glEnd();
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.
}
}
}

View File

@@ -1,289 +0,0 @@
//
// Created by dawsh on 1/17/24.
//
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <J3ML/LinearAlgebra/Transform2D.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include <JGL/Color3.h>
#include <iostream>
GLuint program;
GLuint texture;
namespace JGL
{
FT_Face face;
FT_Library ft;
using namespace J3ML;
bool InitTextEngine() {
constexpr u32 default_font_size = 16;
if (FT_Init_FreeType(&ft))
{
std::cout << "Error::FREETYPE: " << std::endl;
return false;
}
if (FT_New_Face(ft, "content/FreeSans.ttf", 0, &face))
{
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return false;
}
return true;
}
namespace J2D
{
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_QUADS);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(vp_pos.x, vp_pos.y);
glVertex2f(vp_pos.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y);
glEnd();
}
void OutlineRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size, float thickness) {
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_LINE_LOOP);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_pos.x, vp_pos.y);
glVertex2f(vp_pos.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y);
glEnd();
}
void DrawLine2D(const Color3 &color, const Vector2 &A, const Vector2 &B, float thickness) {
auto vp_a = A;
auto vp_b = B;
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_a.x, vp_a.y);
glVertex2f(vp_b.x, vp_b.y);
glEnd();
}
void DrawLine2D(const Color3 &color, float x, float y, float w, float h, float thickness) {
DrawLine2D(color, {x, y}, {w, h}, thickness);
}
void DrawPixel2D(const Color3 &color, float x, float y) {
DrawPixel2D(color, {x, y});
}
void DrawPixel2D(const Color3 &color, const Vector2 &coordinates) {
auto vp_pos = coordinates;
glBegin(GL_POINT);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_pos.x, vp_pos.y);
glEnd();
}
void
OutlineCircle2D(const Color3 &color, const Vector2 &center, float radius, int subdivisions, float thickness) {
glBegin(GL_LINE_LOOP);
GLfloat angle;
glColor3f(color.r, color.g, color.b);
float step = (2.f * M_PI) / (float)subdivisions;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step)
{
GLfloat x = radius * sin(angle);
GLfloat y = radius * cos(angle);
x += center.x;
y += center.y;
glVertex2f(x, y);
}
glEnd();
}
void FillCircle2D(const Color3 &color, const Vector2 &center, float radius, int subdivisions) {
glBegin(GL_POLYGON);
GLfloat angle;
glColor3f(color.r, color.g, color.b);
float step = (2.f * M_PI) / (float)subdivisions;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step)
{
GLfloat x = radius * sin(angle);
GLfloat y = radius * cos(angle);
x += center.x;
y += center.y;
glVertex2f(x, y);
}
glEnd();
}
void OutlineTriangle2D(const Color3& color, const Triangle2D& tri)
{
glBegin(GL_LINE_LOOP);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
}
void FillTriangle2D(const Color3& color, const Triangle2D& tri)
{
glBegin(GL_LINE_LOOP);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
}
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size) {
glUseProgram(0); // Fixed-function pipeline.
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
FT_Set_Pixel_Sizes(face, 0, size);
const char* c;
for (c = text.c_str(); *c; c++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
float x2 = x + g->bitmap_left * scale;
float y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
float w = g->bitmap.width * scale;
float h = g->bitmap.rows * scale;
glBegin(GL_TRIANGLES);
glTexCoord2f(0, 0);
glVertex2f(x2, y2);
glTexCoord2f(0, 1);
glVertex2f(x2, y2 + h);
glTexCoord2f(1, 1);
glVertex2f(x2 + w, y2 + h);
glTexCoord2f(0, 0);
glVertex2f(x2, y2);
glTexCoord2f(1, 1);
glVertex2f(x2 + w, y2 + h);
glTexCoord2f(1, 0);
glVertex2f(x2 + w, y2);
glEnd();
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
}
}
namespace J3D
{
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);
glVertex3f(A.x, A.y, A.z);
glVertex3f(B.x, B.y, B.z);
glEnd();
}
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size)
{
float x = pos.x;
float y = pos.y;
float z = pos.z;
glUseProgram(0); // Fixed-function pipeline.
glColor3f(1.0f, 1.0f, 1.0f);
FT_Set_Pixel_Sizes(face, 0, size);
const char* c;
for (c = text.c_str(); *c; c++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
float x2 = x + g->bitmap_left * scale;
float y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
float z2 = z;
float w = g->bitmap.width * scale;
float h = g->bitmap.rows * scale;
glBegin(GL_TRIANGLES);
glTexCoord2f(0, 0);
glVertex3f(x2, y2, z2);
glTexCoord2f(0, 1);
glVertex3f(x2, y2 + h, z2);
glTexCoord2f(1, 1);
glVertex3f(x2 + w, y2 + h, z2);
glTexCoord2f(0, 0);
glVertex3f(x2, y2, z2);
glTexCoord2f(1, 1);
glVertex3f(x2 + w, y2 + h, z2);
glTexCoord2f(1, 0);
glVertex3f(x2 + w, y2, z2);
glEnd();
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
}
}
}