Compare commits

...

67 Commits

Author SHA1 Message Date
4ff8d8ff07 Update Font.cpp
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m33s
fix memleak
2024-07-17 16:52:31 -04:00
162732e4b7 Fix Dereference
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m24s
2024-07-17 16:11:36 -04:00
289157ab8a Fiddle with README some
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 2m37s
2024-07-17 15:54:39 -04:00
3a658b1096 Testing MeasureString with underlaid box. But it appears to be offset by half-height?
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 4m39s
2024-07-17 15:19:38 -04:00
480502f89e Implement Font::MeasureString (TODO: double check!!)
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m53s
2024-07-17 14:59:34 -04:00
d28f680cd0 Half-baked Font class implementation
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m29s
2024-07-16 14:54:47 -04:00
2d1e42c23b Half-ass Font class implementation
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m30s
2024-07-16 14:39:44 -04:00
abd691b648 uhhhhhhhhhh yeeeeeeea
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m28s
2024-07-16 14:19:39 -04:00
e261b610c2 Update TextRendering.cpp
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 5m24s
Reset the Texture after drawing text.
2024-07-15 11:50:27 -04:00
8625c52ee9 Implement optimizations for 2D text rendering (#23)
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m51s
The following patches are included:

## J2D: Rewrite text rendering

This patch rewrites text rendering for `J2D::DrawString` to now construct
a texture atlas for all ASCII-range glyphs in the FT font face, instead
of constructing a texture for every glyph.

This improves text rendering performance for several reasons:

1. Binding textures is relatively expensive as the GPU is required to do
   a context switch for internal data like texture parameters, and also
   cannot optimize for accesses to the same texture across draw calls.
   This patch removes the need to call `glBindTexture` more than once per
   call to `J2D::DrawString`.
2. As a consequence of the above, all glyphs for a given string can now
   be rendered in a single call to `glDrawArrays`. This is done by storing
   the cached texture coordinates on `CachedGlyph` and constructing a full
   array of vertices and texture coordinates for the entire string at
   once, resulting in only /one/ set of client-to-device attribute
   uploads and only one draw call, instead of being required to upload
   attribute data for each glyph separately.

## FontCache: Use map for efficient glyph lookup

This patch updates `CachedFont` to now use an `std::map` for cached glyphs,
instead of an `std::vector`. `std::map` allows O(log n) lookup, whereas
`std::vector` only allows O(n) lookup.

Note: `std::unordered_map` technically has better lookup complexity here,
with amortized O(1) lookup. However, hashmaps have a higher inherent
overhead than red-black trees so this would only be viable when going
above around 1000 entries, which should never happen here for ASCII
glyphs.

Co-authored-by: Ori Sky Farrell <ori.sky.farrell+git@gmail.com>
Reviewed-on: #23
Co-authored-by: ori_sky <redacted@ori.mx>
Co-committed-by: ori_sky <redacted@ori.mx>
2024-07-15 11:38:20 -04:00
74a4705e44 Fix windows being picky.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m28s
2024-07-12 13:14:24 -04:00
ae327b96a5 Cleanup
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 2m23s
2024-07-12 01:47:49 -04:00
eb3e037c96 Gradient FillRect + GradientLine
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m31s
2024-07-10 20:00:49 -04:00
672a363c53 Fluff the README a little.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m30s
2024-07-10 15:17:37 -04:00
6a16c3f87e Update main.cpp
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m31s
2024-07-10 15:05:21 -04:00
23d3d1f378 Implement stub documentation & small refactors on J2D namespace
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
2024-07-10 15:05:46 -04:00
523806f9ef Rounded Fill Rect
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m49s
2024-07-10 14:49:37 -04:00
d118ae2f8e Accept Color3 & Color4
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m0s
2024-07-09 15:10:26 -04:00
5979ae41fc Performance optimization
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 2m41s
2024-07-08 23:13:45 -04:00
652626b2e4 text
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m20s
2024-07-08 19:12:16 -04:00
a8145cb926 Update JGL.cpp
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m22s
Setting glLineWidth inside of glBegin causes error 1282.
2024-07-07 19:59:10 -04:00
0914b496e7 Refactor & Fix inverted text & More
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m37s
2024-07-07 17:10:51 -04:00
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
Redacted
f5f590164a Update CMakeLists.txt
J3ML v20
2024-03-21 15:19:53 -04:00
c45485cabd Merge remote-tracking branch 'origin/master'
# Conflicts:
#	CMakeLists.txt
2024-02-29 02:18:05 -05:00
143703dea3 Migrating Sources to LearnOpenGL 2024-02-29 02:17:42 -05:00
Redacted
48493cb2fc Update CMakeLists.txt 2024-02-24 07:56:25 -05:00
5b7829b1f8 Implement LearnOpenGL::Mesh 2024-02-22 18:39:50 -05:00
868f52464e Fixed broken migration 2024-02-21 23:42:55 -05:00
d1d2493435 Migrate to latest ReWindow 2024-02-21 23:27:10 -05:00
da30a7eedd Merge pull request 'dawsh' (#12) from dawsh into master
Reviewed-on: #12
2024-02-20 17:23:59 -05:00
5d906d6f99 Move RenderText to J2D::DrawString2D 2024-02-20 17:20:54 -05:00
6107e9a762 Small Fix (Make Another Release) 2024-02-20 15:20:08 -05:00
a38a83692f Fix render to full viewport 2024-02-20 05:18:32 -05:00
c4d6426587 Merge pull request 'dawsh' (#11) from dawsh into master
Reviewed-on: #11
2024-02-20 04:47:38 -05:00
817b0166c0 Implement J3D::DrawString3D 2024-02-20 04:01:29 -05:00
2a98857bab Fix Implementation of J2D::DrawLine2D & J3D::DrawLine3D 2024-02-20 00:30:38 -05:00
29a64160e9 Semi-Working Text 2024-02-16 15:43:35 -05:00
f676ef5332 Currently Fucked 2024-02-16 13:58:51 -05:00
d343c6ca45 Merge pull request 'dawsh' (#3) from dawsh into master
Reviewed-on: #3
2024-02-16 13:29:17 -05:00
7a7e73a829 Start README 2024-02-16 13:28:23 -05:00
9406a9d429 Partial Implement RenderText 2024-02-16 13:11:18 -05:00
d0eb1f34ef SemiBrokend 2024-02-15 18:51:25 -05:00
406abbb5bd Attempting Text Input 2024-02-14 19:23:29 -05:00
328245f81a Implement LearnOpenGL::Shader 2024-02-14 19:23:18 -05:00
21 changed files with 1778 additions and 520 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,4 +1,4 @@
cmake_minimum_required(VERSION 3.25)
cmake_minimum_required(VERSION 3.18)
project(JGL
VERSION 1.0
LANGUAGES CXX
@@ -9,37 +9,95 @@ 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/Prerelease-7.zip
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-12.zip
)
CPMAddPackage(
NAME Event
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
)
CPMAddPackage(
NAME ReTexture
URL https://git.redacted.cc/Redacted/ReTexture/archive/Prerelease-2.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}
include/JGL/JGL.h
src/JGL/JGL.cpp)
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
find_package(OpenGL REQUIRED)
find_package(GLUT 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
${ReTexture_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
${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS}
${J3ML_SOURCE_DIR}/include)
#set_target_properties(JGL_Demo PROPERTIES LINK_FLAGS "-Wl,-rpath,./lib")
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} J3ML)
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 ReTexture)

View File

@@ -0,0 +1,59 @@
# Josh Graphics Library
Yet Another C++ Rendering Toolkit
![Static Badge](https://img.shields.io/badge/Lit-Based-%20)
## Features
* Modern C++ (20)
* Provides single-function-calls to render various graphics primitives in 2D and 3D.
* Integrates directly with our other toolkits (ReWindow, J3ML)
* Quick Rendering of Debug Text, Geometric Widgets, Textures, and so forth.
* Little-to-no overhead.
* Integrates right into an existing OpenGL rendering system.
* High-performance text rendering.
## API Overview
### J2D
* DrawPixel
* DrawLine / DrawGradientLine
* DrawSprite
* OutlineRect / FillRect / FillGradientRect / FillRoundedRect
* OutlineCircle / FillCircle
* OutlineTriangle / FillTriangle
* DrawString
### J3D
* DrawLine
* DrawString
* DrawMatrixGizmo (WIP)
* DrawAxisAngleGizmo (WIP)
* DrawQuaternionGizmo (WIP)
### Types
* Font
* Sprite
* Color4/Color3
* Gradient
## Usage
Install instructions and code samples coming soon :tm: !
## Documentation
Documentation is (sic: will be) automatically generated from latest commit and is hosted at https://doc.redacted.cc/jgl .
## Contributing
Contributions to JGL are welcome! If you find a bug, have a feature request, or would like to contribute code, please submit an issue or pull request to our repository!
## License
JGL is licensed under the Public Domain. See the LICENSE file for details.
## Acknowledgements
JGL is developed and maintained by Josh O'Leary @ Co from Redacted Software and contributors. Special thanks to Redacted.

BIN
assets/fonts/FreeSans.ttf Normal file

Binary file not shown.

BIN
assets/fonts/Jupiteroid.ttf Normal file

Binary file not shown.

24
include/JGL/Color3.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <J3ML/J3ML.h>
namespace JGL
{
using namespace J3ML;
struct Color3 {
u8 r;
u8 g;
u8 b;
Color3 Lerp(const Color3& rhs, float alpha) const;
Color3(u8 R, u8 G, u8 B) : r(R), g(G), b(B) {}
u8 RedChannel () const { return r; }
u8 GreenChannel() const { return g; }
u8 BlueChannel () const { return b; }
float RedChannelNormalized () const { return static_cast<float>(r) / 255.f;}
float BlueChannelNormalized() const { return static_cast<float>(b) / 255.f;}
float GreenChannelNormalized() const { return static_cast<float>(g) / 255.f;}
};
}

17
include/JGL/Color4.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <JGL/Color3.h>
namespace JGL
{
class Color4 {
public:
explicit Color4(const Color3& color3, unsigned int alpha) {r = color3.r; g = color3.g; b = color3.b; a = alpha;}
Color4(int red, int green, int blue, int alpha = 127) : r(red), g(green), b(blue), a(alpha) {}
static Color4 FromColor3(const Color3& color3, unsigned int alpha = 127) {return Color4(color3, alpha);}
int r;
int g;
int b;
int a;
};
}

178
include/JGL/Colors.h Normal file
View File

@@ -0,0 +1,178 @@
#pragma once
#include <JGL/Color3.h>
namespace JGL
{
namespace Colors {
namespace Primary {
static const Color3 Red{255, 0, 0};
static const Color3 Green{0, 255, 0};
static const Color3 Blue{0, 0, 255};
static const Color3 White{255, 255, 255};
static const Color3 Black{0, 0, 0};
static const Color3 Gray{128, 128, 128};
static const Color3 DarkGray{192, 192, 192};
static const Color3 LightGray{64, 64, 64};
static const Color3 Yellow{255, 255, 0};
}
using namespace Primary;
namespace Reds {
static const Color3 Fuchsia {255, 0, 255};
static const Color3 LightSalmon{255, 160, 122};
static const Color3 Salmon{250, 128, 114};
static const Color3 DarkSalmon{233, 150, 122};
static const Color3 LightCoral{240, 128, 128};
static const Color3 IndianRed{205, 92, 92};
static const Color3 Crimson{220, 20, 60};
static const Color3 Firebrick{178, 34, 34};
static const Color3 DarkRed{139, 0, 0};
}
namespace Oranges {
static const Color3 Coral{255, 127, 80};
static const Color3 Tomato{255, 99, 71};
static const Color3 OrangeRed{255, 69, 0};
static const Color3 Gold{255, 215, 0};
static const Color3 Orange{255, 165, 0};
static const Color3 DarkOrange{255, 140, 0};
}
namespace Yellows {
static const Color3 LightYellow{255, 255, 224};
static const Color3 LemonChiffon{255, 250, 205};
static const Color3 LightGoldenrodYellow{250, 250, 210};
static const Color3 PapayaWhip{255, 239, 213};
static const Color3 Moccasin{255, 228, 181};
static const Color3 PeachPuff{255, 218, 185};
static const Color3 PaleGoldenrod{238, 232, 170};
static const Color3 Khaki{240, 230, 140};
static const Color3 DarkKhaki{189, 183, 107};
}
namespace Greens {
static const Color3 LawnGreen{124, 252, 0};
static const Color3 Chartreuse{127, 255, 0};
static const Color3 LimeGreen{50, 205, 50};
static const Color3 ForestGreen{34, 139, 34};
static const Color3 DarkGreen{0, 100, 0};
static const Color3 GreenYellow{173, 255, 47};
static const Color3 YellowGreen{154, 205, 50};
static const Color3 SpringGreen{0, 255, 127};
static const Color3 MediumSpringGreen{0, 250, 154};
static const Color3 LightGreen{144, 238, 144};
static const Color3 PaleGreen{152, 251, 152};
static const Color3 DarkSeaGreen{143, 188, 143};
static const Color3 MediumSeaGreen{60, 179, 113};
static const Color3 SeaGreen{46, 139, 87};
static const Color3 DarkOliveGreen{85, 107, 47};
static const Color3 OliveDrab{107, 142, 35};
static const Color3 Lime{0, 255, 0};
static const Color3 Olive{128, 128, 0};
}
namespace Cyans {
static const Color3 LightCyan{224, 255, 255};
static const Color3 Cyan{0, 255, 255};
static const Color3 Aqua{0, 255, 255};
static const Color3 Aquamarine{127, 255, 212};
static const Color3 MediumAquamarine{102, 205, 170};
static const Color3 PaleTurquoise{175, 238, 238};
static const Color3 Turquoise{64, 224, 208};
static const Color3 MediumTurquoise{72, 209, 204};
static const Color3 DarkTurquoise{0, 206, 209};
static const Color3 LightSeaGreen{32, 178, 170};
static const Color3 CadetBlue{95, 158, 160};
static const Color3 DarkCyan{0, 139, 139};
static const Color3 Teal{0, 128, 128};
}
namespace Blues {
static const Color3 PowderBlue{176, 224, 230};
static const Color3 LightBlue{173, 216, 230};
static const Color3 LightSkyBlue{135, 206, 250};
static const Color3 SkyBlue{135, 206, 235};
static const Color3 DeepSkyBlue{0, 191, 255};
static const Color3 LightSteelBlue{176, 196, 222};
static const Color3 DodgerBlue{30, 144, 255};
static const Color3 CornflowerBlue{100, 149, 237};
static const Color3 SteelBlue{70, 130, 180};
static const Color3 RoyalBlue{65, 105, 225};
static const Color3 MediumBlue{0, 0, 205};
static const Color3 DarkBlue{0, 0, 139};
static const Color3 Navy{0, 0, 128};
static const Color3 MidnightBlue{25, 25, 112};
static const Color3 MediumSlateBlue{123, 104, 238};
static const Color3 SlateBlue{106, 90, 205};
static const Color3 DarkSlateBlue{72, 61, 139};
}
namespace Purples {
static const Color3 Lavender{230, 230, 250};
static const Color3 Thistle{216, 191, 216};
static const Color3 Plum{221, 160, 221};
static const Color3 Violet{238, 160, 221};
static const Color3 Orchid{218, 112, 214};
static const Color3 Fuchsia{255, 0, 255};
static const Color3 Magenta{255, 0, 255};
static const Color3 MediumOrchid{186, 85, 211};
static const Color3 MediumPurple{147, 112, 219};
static const Color3 BlueViolet{138, 43, 226};
static const Color3 DarkViolet{148, 0, 211};
static const Color3 DarkOrchid{153, 50, 204};
static const Color3 DarkMagenta{139, 0, 128};
static const Color3 Purple{128, 0, 128};
static const Color3 Indigo{75, 0, 130};
}
namespace Pinks {
static const Color3 Pink{255, 129, 203};
static const Color3 LightPink{255, 182, 193};
static const Color3 HotPink{255, 105, 180};
static const Color3 DeepPink{255, 20, 147};
static const Color3 PaleVioletRed{219, 112, 147};
static const Color3 MediumVioletRed{199, 21, 133};
}
namespace Whites {
static const Color3 Snow{255, 250, 250};
static const Color3 Honeydew{240, 255, 240};
static const Color3 MintCream{245, 255, 250};
static const Color3 Azure{240, 255, 255};
static const Color3 AliceBlue{240, 248, 255};
static const Color3 GhostWhite{248, 248, 255};
static const Color3 WhiteSmoke{245, 245, 245};
static const Color3 SeaShell{255, 245, 238};
static const Color3 Beige{245, 245, 220};
static const Color3 OldLace{253, 245, 230};
static const Color3 FloralWhite{255, 250, 240};
static const Color3 Ivory{255, 255, 240};
static const Color3 AntiqueWhite{250, 240, 215};
static const Color3 Linen{250, 240, 230};
static const Color3 LavenderBlush{255, 240, 245};
static const Color3 MistyRose{255, 228, 255};
}
namespace Grays {
static const Color3 Gainsboro{220, 220, 220};
static const Color3 LightGray{211, 211, 211};
static const Color3 Silver{192, 192, 192};
static const Color3 DimGray{105, 105, 105};
static const Color3 LightSlateGray{119, 136, 153};
static const Color3 SlateGray{112, 128, 144};
static const Color3 DarkSlateGray{47, 79, 79};
}
namespace Browns {
static const Color3 CornSilk{255, 248, 220};
static const Color3 BlanchedAlmond{255, 235, 205};
static const Color3 Bisque{255, 228, 196};
static const Color3 NavajoWhite{255, 222, 173};
static const Color3 Wheat{254, 222, 179};
static const Color3 BurlyWood{222, 184, 135};
static const Color3 Tan{210, 180, 140};
static const Color3 RosyBrown{188, 143, 143};
static const Color3 SandyBrown{244, 164, 96};
static const Color3 GoldenRod{218, 165, 32};
static const Color3 Peru{205, 133, 63};
static const Color3 Chocolate{210, 105, 30};
static const Color3 SaddleBrown{139, 69, 19};
static const Color3 Sienna{160, 82, 45};
static const Color3 Brown{164, 42, 42};
static const Color3 Maroon{128, 0, 0};
}
}
}

39
include/JGL/Font.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#include <vector>
#include <filesystem>
#include <J3ML/LinearAlgebra.h>
#include <filesystem>
#include <iostream>
// LMFAO
extern "C" typedef struct FT_FaceRec_* FT_Face;
extern "C" typedef struct FT_LibraryRec_* FT_Library;
namespace JGL
{
/// Initializes FreeType engine.
bool InitTextEngine();
/// A Font class implementation.
/// Wraps the font's FreeType asset handle and provides helper functions.
class Font {
public:
/// Default constructor does not initialize any members
Font() = default;
Font(const std::filesystem::path& path);
/// Destructor handles freeing of the underlying asset handle.
~Font();
static Font LoadTTF(const std::filesystem::path& filepath);
static std::vector<Font> GetLoadedFonts();
/// Returns the bounding-box the given string would occupy at the given point-size, assuming normal (1) scaling.
/// @param text The string to measure.
/// @param ptSize The font size at which to measure.
/// @return The size-in-pixels that would contain the entire text.
Vector2 MeasureString(const std::string& text, float ptSize);
public:
int index = 0;
FT_Face face;
};
}

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

@@ -0,0 +1,63 @@
#pragma once
#include <array>
#include <map>
#include <vector>
#include <glad/glad.h>
/// TODO: FontCache mechanism works amazing, but makes no fucking sense
/// Let's document and reorganize it to be a little nicer on the mental :)
namespace JGL {
class CachedGlyph;
class CachedFont;
class FontCache;
}
/// Represents a single font-character "glyph", that has been cached in-memory for fast retrieval.
class JGL::CachedGlyph {
private:
char character;
std::array<GLfloat, 12> texcoords;
public:
int x2offset = 0, y2offset = 0, w = 0, h = 0;
float advanceX = 0, advanceY = 0;
//CachedGlyph(GLuint texture_id, char c);
CachedGlyph(char c, std::array<GLfloat, 12> texcoords, float x2o, float y2o, float w, float h, float advX, float advY);
char getCharacter();
const std::array<GLfloat, 12> getTexCoords() const;
};
/// Represents a Font object as it exists in the font-cache.
class JGL::CachedFont {
private:
std::map<char, CachedGlyph*> glyphs;
GLuint texture = 0;
GLsizei texture_width = 0, texture_height = 0;
unsigned int font_size = 0;
unsigned int font_index = 0;
public:
void appendGlyph(CachedGlyph* glyph);
unsigned int getFontSize();
unsigned int getFontIndex();
CachedGlyph* getGlyph(char c);
std::map<char, CachedGlyph*> getGlyphs();
const GLuint* getTexture();
GLsizei getTextureWidth() const;
GLsizei getTextureHeight() const;
CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, 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(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index);
void eraseFont(CachedFont* font);
void purgeCache();
};

12
include/JGL/Gradient.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include <J3ML/J3ML.h>
namespace JGL {
enum class Gradient : u8 {
Vertical = 0,
Horizontal = 1,
DiagonalTopLeft = 2,
DiagonalBottomLeft = 3
};
}

View File

@@ -1,221 +1,37 @@
//
// Created by dawsh on 1/17/24.
//
/// Josh's Graphics Library
/// A C++20 Library for rendering 2D and 3D primitives in an OpenGL context.
/// Developed and Maintained by Josh O'Leary @ Redacted Software.
/// Special Thanks to William Tomasine II and Maxine Hayes.
/// (c) 2024 Redacted Software
/// This work is dedicated to the public domain.
/// @file JGL.h
/// @desc All JGL usable functions are defined here. This is the public API.
/// @edit 2024-07-16
#pragma once
#include <string>
#include <iostream>
#include <JGL/Color4.h>
#include <JGL/Gradient.h>
#include <JGL/FontCache.h>
#include <JGL/Font.h>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include "J3ML/LinearAlgebra/Vector3.h"
#include <J3ML/LinearAlgebra/Vector3.h>
#include <J3ML/Geometry/Sphere.h>
#include <J3ML/Geometry/Capsule.h>
#include <J3ML/Geometry/TriangleMesh.h>
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
namespace JGL {
// All functions accept coordinates in pixel-screen space [0, 600]
// and are internally transformed to OpenGL clip space [-1, +1]
using namespace LinearAlgebra;
struct RGBTuple {
int r; int g; int b;
};
struct RGBATuple {
int r; int g; int b; int a;
};
struct HSVTuple {
int h; int s; int v;
};
struct HSLTuple {
int h; int s; int l;
};
struct Color3 {
int r;
int g;
int b;
Color3 Lerp(const Color3& rhs, float alpha) const;
};
struct Color4{
int r;
int g;
int b;
int a;
};
namespace Colors {
namespace Primary {
static const Color3 Red {255, 0, 0};
static const Color3 Green { 0, 255, 0};
static const Color3 Blue { 0, 0, 255};
static const Color3 White {255, 255, 255};
static const Color3 Black { 0, 0, 0};
static const Color3 Gray {128, 128, 128};
static const Color3 DarkGray {192, 192, 192};
static const Color3 LightGray { 64, 64, 64};
static const Color3 Yellow {255, 255, 0};
}
using namespace Primary;
namespace Reds {
static const Color3 Fuchsia {};
static const Color3 LightSalmon {255, 160, 122};
static const Color3 Salmon {250, 128, 114};
static const Color3 DarkSalmon {233, 150, 122};
static const Color3 LightCoral {240, 128, 128};
static const Color3 IndianRed {205, 92, 92};
static const Color3 Crimson {220, 20, 60};
static const Color3 Firebrick {178, 34, 34};
static const Color3 DarkRed {139, 0, 0};
}
namespace Oranges {
static const Color3 Coral {255, 127, 80};
static const Color3 Tomato {255, 99, 71};
static const Color3 OrangeRed {255, 69, 0};
static const Color3 Gold {255, 215, 0};
static const Color3 Orange {255, 165, 0};
static const Color3 DarkOrange {255, 140, 0};
}
namespace Yellows {
static const Color3 LightYellow {255, 255, 224};
static const Color3 LemonChiffon {255, 250 ,205};
static const Color3 LightGoldenrodYellow {250, 250, 210};
static const Color3 PapayaWhip {255, 239, 213};
static const Color3 Moccasin {255, 228, 181};
static const Color3 PeachPuff {255, 218, 185};
static const Color3 PaleGoldenrod {238, 232, 170};
static const Color3 Khaki {240, 230, 140};
static const Color3 DarkKhaki {189, 183, 107};
}
namespace Greens {
static const Color3 LawnGreen {124, 252, 0};
static const Color3 Chartreuse {127, 255, 0};
static const Color3 LimeGreen {50, 205, 50};
static const Color3 ForestGreen {34, 139, 34};
static const Color3 DarkGreen {0, 100, 0};
static const Color3 GreenYellow{173, 255, 47};
static const Color3 YellowGreen{154, 205, 50};
static const Color3 SpringGreen{0, 255, 127};
static const Color3 MediumSpringGreen{0, 250, 154};
static const Color3 LightGreen{144, 238, 144};
static const Color3 PaleGreen{152, 251, 152};
static const Color3 DarkSeaGreen{143, 188, 143};
static const Color3 MediumSeaGreen{60, 179, 113};
static const Color3 SeaGreen{46, 139, 87};
static const Color3 DarkOliveGreen{85, 107, 47};
static const Color3 OliveDrab{107, 142, 35};
static const Color3 Lime{0, 255, 0};
static const Color3 Olive{128, 128, 0};
}
namespace Cyans {
static const Color3 LightCyan{224, 255, 255};
static const Color3 Cyan{0, 255, 255};
static const Color3 Aqua{0, 255, 255};
static const Color3 Aquamarine{127, 255, 212};
static const Color3 MediumAquamarine{102, 205, 170};
static const Color3 PaleTurquoise{175, 238, 238};
static const Color3 Turquoise{64, 224, 208};
static const Color3 MediumTurquoise{72, 209, 204};
static const Color3 DarkTurquoise{0, 206, 209};
static const Color3 LightSeaGreen{32, 178, 170};
static const Color3 CadetBlue{95, 158, 160};
static const Color3 DarkCyan{0, 139, 139};
static const Color3 Teal{0, 128, 128};
}
namespace Blues {
static const Color3 PowderBlue {176, 224, 230};
static const Color3 LightBlue {173, 216, 230};
static const Color3 LightSkyBlue {135, 206, 250};
static const Color3 SkyBlue {135, 206, 235};
static const Color3 DeepSkyBlue {0, 191, 255};
static const Color3 LightSteelBlue {176, 196, 222};
static const Color3 DodgerBlue {30, 144, 255};
static const Color3 CornflowerBlue {100, 149, 237};
static const Color3 SteelBlue {70, 130, 180};
static const Color3 RoyalBlue {65, 105, 225};
static const Color3 MediumBlue {0, 0, 205};
static const Color3 DarkBlue {0, 0, 139};
static const Color3 Navy {0, 0, 128};
static const Color3 MidnightBlue {25, 25, 112};
static const Color3 MediumSlateBlue {123, 104, 238};
static const Color3 SlateBlue {106, 90, 205};
static const Color3 DarkSlateBlue {72, 61, 139};
}
namespace Purples {
static const Color3 Lavender {230, 230, 250};
static const Color3 Thistle {216, 191, 216};
static const Color3 Plum {221, 160, 221};
static const Color3 Violet {238, 160, 221};
static const Color3 Orchid {218, 112, 214};
static const Color3 Fuchsia {255, 0, 255};
static const Color3 Magenta {255, 0, 255};
static const Color3 MediumOrchid {186, 85, 211};
static const Color3 MediumPurple {147, 112, 219};
static const Color3 BlueViolet {138, 43, 226};
static const Color3 DarkViolet {148, 0, 211};
static const Color3 DarkOrchid {153, 50, 204};
static const Color3 DarkMagenta {139, 0, 128};
static const Color3 Purple {128, 0, 128};
static const Color3 Indigo {75, 0, 130};
}
namespace Pinks {
static const Color3 Pink {255, 129, 203};
static const Color3 LightPink {255, 182, 193};
static const Color3 HotPink {255, 105, 180};
static const Color3 DeepPink {255, 20, 147};
static const Color3 PaleVioletRed {219, 112, 147};
static const Color3 MediumVioletRed {199, 21, 133};
}
namespace Whites {
static const Color3 Snow {255, 250, 250};
static const Color3 Honeydew {240, 255, 240};
static const Color3 MintCream {245, 255, 250};
static const Color3 Azure {240, 255, 255};
static const Color3 AliceBlue {240, 248, 255};
static const Color3 GhostWhite {248, 248, 255};
static const Color3 WhiteSmoke {245, 245, 245};
static const Color3 SeaShell {255, 245, 238};
static const Color3 Beige {245, 245, 220};
static const Color3 OldLace {253, 245, 230};
static const Color3 FloralWhite {255, 250, 240};
static const Color3 Ivory {255, 255, 240};
static const Color3 AntiqueWhite {250, 240, 215};
static const Color3 Linen {250, 240, 230};
static const Color3 LavenderBlush {255, 240, 245};
static const Color3 MistyRose {255, 228, 255};
}
namespace Grays {
static const Color3 Gainsboro {220, 220, 220};
static const Color3 LightGray {211, 211, 211};
static const Color3 Silver {192, 192, 192};
static const Color3 DimGray {105, 105, 105};
static const Color3 LightSlateGray {119, 136, 153};
static const Color3 SlateGray {112, 128, 144};
static const Color3 DarkSlateGray {47, 79, 79};
}
namespace Browns {
static const Color3 CornSilk {255, 248, 220};
static const Color3 BlanchedAlmond {255, 235, 205};
static const Color3 Bisque {255, 228, 196};
static const Color3 NavajoWhite {255, 222, 173};
static const Color3 Wheat {254, 222, 179};
static const Color3 BurlyWood {222, 184, 135};
static const Color3 Tan {210, 180, 140};
static const Color3 RosyBrown {188, 143, 143};
static const Color3 SandyBrown {244, 164, 96};
static const Color3 GoldenRod {218, 165, 32};
static const Color3 Peru {205, 133, 63};
static const Color3 Chocolate {210, 105, 30};
static const Color3 SaddleBrown {139, 69, 19};
static const Color3 Sienna {160, 82, 45};
static const Color3 Brown {164, 42, 42};
static const Color3 Maroon {128, 0, 0};
}
}
using namespace J3ML::LinearAlgebra;
using namespace J3ML::Geometry;
/// TODO:
struct HSV {
float hue;
float saturation;
@@ -226,7 +42,7 @@ namespace JGL {
{
Vector2 A;
Vector2 B;
Vector3 C;
Vector2 C;
};
struct Triangle3D
@@ -236,40 +52,119 @@ namespace JGL {
Vector3 C;
};
Vector2 ScreenToViewport(const Vector2& v);
Vector2 ViewportToScreen(const Vector2& v)
{
// TODO: Implement (CORRECT!!!) matrix transformation
void Update(const Vector2& window_size);
//bool InitTextEngine();
Font LoadFont(const std::string& font_path); // TODO: Fully deprecate
void PurgeFontCache();
void SetActiveFont(const Font& font); // TODO: Implement
// TODO: implement correct coloring
/// Drawing functions for primitive 2D Shapes.
/// Each function is overloaded with Color3 and Color4 for optional transparency.
namespace J2D {
/// Open a 2-D rendering context with the underlying graphics system (In this case & by default OpenGL).
/// @note This call may not strictly be necessary on some setups, but is provided to keep the API constant.
/// It is recommended to always open a JGL 2D context to render your content, then close when completed.
/// This keeps our code from, say, clobbering the OpenGL rendering context driving 3D content in between our calls.
void Begin();
/// Closes a 2-D rendering context with the underlying graphics system (In this case & by default OpenGL).
/// @see Begin().
void End();
/// Plots a single pixel on the screen.
/// @param color A 3-or-4 channel color value. @see classes Color3, Color4
/// @param coordinates The pixel-point on-screen at which to plot the pixel.
void DrawPixel(const Color3& color, const Vector2& coordinates);
void DrawPixel(const Color3& color, float x, float y);
void DrawPixel(const Color4& color, const Vector2& coordinates);
void DrawPixel(const Color4& color, float x, float y);
/// Plots a line (segment) on the screen.
/// @param color A 3-or-4 channel color value. @see classes Color3, Color4.
/// @param A The starting point of the line segment.
/// @param B The end point of the line segment.
/// @param thickness The width at which to render the line.
void DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color3& color, float x, float y, float w, float h, float thickness = 1);
void DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color4& color, float x1, float y1, float x2, float y2, float thickness = 1);
///Draws a line with a gradient that transitions across it.
void DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness = 1);
void DrawGradientLine(const Color3& color1, const Color3& color2, float x, float y, float w, float h, float thickness = 1);
/// Draws an outline of a rectangle on the screen.
void OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
///Draws a sprite to the screen.
void DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size);
void DrawSprite(GLuint texture, float x, float y, float w, float h);
/// Draws a filled rectangle on the screen.
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
void FillRect(const Color3& color, const Vector2& pos, const Vector2& size);
/// Draws a filled rectangle where the color transitions across it.
void FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
/// Draws a filled rectangle with rounded corners on the screen.
void FillRoundedRect(const Color4& color, const Vector2 &pos, const Vector2 &size, float radius = 5, unsigned int subdivisions = 8);
void FillRoundedRect(const Color3& color, const Vector2& pos, const Vector2& size, float radius = 5, unsigned int subdivisions = 8);
/// Draws an outline of a circle on the screen.
void OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 16, float thickness = 1);
void OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 16, float thickness = 1);
/// Draws a filled circle on the screen.
void FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
void FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
/// Draws an outline of a triangle on the screen.
void OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness = 1);
void OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness = 1);
// TODO: Implement an overload that simply takes 3 Vector3's
/// Draws a filled triangle on the screen.
void FillTriangle(const Color4& color, const Triangle2D &tri);
void FillTriangle(const Color3& color, const Triangle2D& tri);
// TODO: Implement an overload that simply takes 3 Vector3's
/// Draws a text string on the screen with a given point-size and font.
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
void DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
// TODO: Implement the following:
void FillTexturedTriangle();
void FillTexturedPolygon();
void DrawPartialSprite();
void DrawCubicBezierCurve();
void OutlinePolygon (const Color4& color, std::vector<Vector2> points);
void FillPolygon (const Color4& color, std::vector<Vector2> points, float thickness = 1);
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1);
}
namespace J2D {
void DrawPixel2D(const Color3& color, const Vector2 &coordinates);
void DrawPixel2D(const Color3& color, float x, float y);
void DrawLine2D(const Color3& color, const Vector2 &A, const Vector2 &B, float thickness = 1);
void DrawLine2D(const Color3& color, float x, float y, float w, float h, float thickness = 1);
void DrawCubicBezierCurve2D();
void OutlineCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions, float thickness = 1);
void FillCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions);
void OutlineTriangle2D();
void FillTriangle2D();
void FillTexturedTriangle2D();
void FillTexturedPolygon2D();
void DrawSprite2D();
void DrawPartialSprite2D();
void DrawString2D();
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);
void OutlineRoundedRect2D(const Color3& color, const Vector2& pos, const Vector2& size, float radius, float thickness = 1);
void OutlinePolygon2D (const Color3& color, std::vector<Vector2> points);
void FillPolygon2D (const Color3& color, std::vector<Vector2> points, float thickness = 1);
void GradientFillRect2D ();
}
namespace J3D {
void DrawLine3D(const Color3& color, const Vector3 &A, const Vector3 &B, float thickness = 1);
void FillSphere3D();
void WireframeSphere3D();
void DrawString3D();
void Begin();
void End();
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
void DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness = 1);
void DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness = 1);
void FillSphere(const Color3& color, const Sphere& sphere);
void WireframeSphere(const Color3& color, const Sphere& sphere, float thickness = 1);
void FillOBB(const Color3& color, const OBB& obb);
void WireframeOBB(const Color3& color, const OBB& obb, float thickness = 1);
void FillCapsule(const Color3& color, const Capsule& capsule);
void WireframeCapsule(const Color3& color, const Capsule& cap, float thickness = 1);
void FillTriangleMesh(const Color3& color, const TriangleMesh& mesh);
void WireframeTriangleMesh(const Color3& color, const TriangleMesh& mesh, float thickness = 1);
void DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font);
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
void DrawMatrixGizmo (const Matrix4x4&);

275
main.cpp
View File

@@ -1,142 +1,161 @@
#include <GL/glut.h>
#include "JGL/JGL.h"
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <rewindow/types/window.h>
#include <JGL/Colors.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <JGL/Font.h>
#include "jlog/jlog.hpp"
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
using J3ML::LinearAlgebra::Vector2;
using namespace JGL;
void initGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glOrtho(-100.f, 100.f, -100.f, 100.f, -100.f, 100.f);
//The Re3D style base projection.
std::vector<GLfloat> perspective(float fov, float aspect, float nearPlane, float farPlane) {
std::vector<float> result(16);
float f = 1.0f / tan(fov * 0.5f * M_PI / 180.0f);
result[0] = f / aspect;
result[5] = f;
result[10] = (farPlane + nearPlane) / (nearPlane - farPlane);
result[11] = -1.0f;
result[14] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
return result;
}
class Camera {
public:
Vector3 position = {0,0,0};
Vector3 angle = {0,0,0};
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_MODELVIEW); // To operate on the Model-View matrix
glLoadIdentity(); // Reset the model-view matrix.
std::vector<GLfloat> lookAt(const Vector3& eye, const Vector3& center, const Vector3& up) {
Vector3 f = Vector3::Normalized((center - eye));
Vector3 upN = Vector3::Normalized(up);
Vector3 s = Vector3::Normalized(f.Cross(upN));
Vector3 u = Vector3::Normalized(s.Cross(f));
// Define shapes enclosed within a pair of glBegin and glEnd
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.f, 0.f, 0.f);
glVertex2f(-0.8f, 0.1f);
glVertex2f(-0.2f, 0.1f);
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.9f, -0.7f);
glColor3f(1.f, 1.f, 1.f); // White
glVertex2f(-0.5f, -0.7f);
glColor3f(.2f, .2f, .2f); // Dark Gray
glVertex2f(-0.5f, -0.3f);
glColor3f(1.f, 1.f, 1.f); // White
glVertex2f(-0.9f, -0.3f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.f); // Blue
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
glColor3f(1.f, 0.f, 0.f); // Red
glVertex2f(0.3f, -0.4f);
glColor3f(0.0f, 1.0f, 0.f); // Green
glVertex2f(0.9f, -0.4f);
glColor3f(0.f, 0.f, 1.f); // Blue
glVertex2f(0.6f, -0.9f);
glEnd();
glBegin(GL_POLYGON); // These verts form a closed polygon
glColor3f(1.f, 1.f, 0.f); // Yellow
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
JGL::J2D::FillRect2D(JGL::Colors::White, {0, 0}, {128, 128});
JGL::J2D::OutlineRect2D(JGL::Colors::Red, {0, 0}, {128, 128}, 4);
JGL::J2D::DrawPixel2D(JGL::Colors::Green, {0, 0});
JGL::J2D::FillCircle2D(JGL::Colors::Purples::DarkViolet, {0, 0}, 0.75f, 64);
glFlush(); // Render now
}
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
bool fullscreenMode = true;
void specialKeys(int key, int x, int y) {
switch(key) {
case GLUT_KEY_F1:
fullscreenMode = !fullscreenMode;
if (fullscreenMode) {
windowPosX = glutGet(GLUT_WINDOW_X);
windowPosY = glutGet(GLUT_WINDOW_Y);
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
} else {
glutReshapeWindow(windowWidth, windowHeight);
glutPositionWindow(windowPosX, windowPosY);
}
break;
std::vector<GLfloat> result = {
s.x, u.x, -f.x, 0.0f,
s.y, u.y, -f.y, 0.0f,
s.z, u.z, -f.z, 0.0f,
-s.Dot(eye), -u.Dot(eye), f.Dot(eye), 1.0f
};
return result;
}
}
// Handler for window re-size event. Called back when the window first appears
// and whenever the window is re-sized with its new width and height
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat) width / (GLfloat) height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // Operate on the Projection Matrix;
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
// Aspect >= 1, set the height from -1 to 1
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
} else {
// Aspect < 1, set the width from -1 to 1, with larger height
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
void render() {
glRotatef(angle.x,1.0f, 0.0f, 0.0f);
glRotatef(angle.y,0.0f, 1.0f, 0.0f);
glRotatef(angle.z,0.0f, 0.0f, 1.0f);
glMultMatrixf(lookAt({position.x, position.y, position.z}, {position.x, position.y, 100.f + position.z}, {0,1,0}).data());
}
}
};
Camera* camera;
using J3ML::LinearAlgebra::Matrix4x4;
int main(int argc, char** argv)
struct point {
GLfloat x;
GLfloat y;
GLfloat s;
GLfloat t;
};
JGL::Font FreeSans;
JGL::Font Jupiteroid;
class JGLDemoWindow : public ReWindow::RWindow
{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Vertex, Primitive, & Color"); // Create window with given title
glutInitWindowSize(320, 320); // Set the window's initial width & height - non-square
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
glutSpecialFunc(specialKeys); // Register callback handler for special-key event
initGL(); // Our own OpenGL initialization
glutMainLoop();
public:
void initGL() {
camera = new Camera;
auto window_size = getSize();
auto aspect = (float) window_size[0] / (float) window_size[1];
gladLoadGL();
JGL::InitTextEngine();
JGL::Update(getSize());
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.ttf");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMultMatrixf(perspective(75, aspect, 0.001, 100).data());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.f, 0.f, 0.f, 0.f);
glViewport(0,0,window_size.x,window_size.y);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
}
Vector3 textAngle = {0,0,0};
void display() {
textAngle.y += 2.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
camera->render();
///All 3D elements of the scene and JGL elements *must* be rendered before the 2d stuff.
J3D::Begin();
J3D::DrawLine(JGL::Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
J3D::DrawLine(JGL::Colors::Red, {-0.33,-0.125,1}, {-0.33,0.25,1});
J3D::DrawString(JGL::Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f},textAngle, 1.f, 32, FreeSans);
J3D::End();
J2D::Begin();
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
J2D::FillRoundedRect(JGL::Colors::Red, {200, 52}, {100, 100}, 8, 8);
J2D::FillRoundedRect(JGL::Colors::Purples::BlueViolet, {300, 52}, {100, 100}, 8, 4);
J2D::FillCircle(JGL::Colors::White, {52, 204}, 50, 24);
J2D::OutlineCircle(JGL::Colors::White, {153, 204}, 50, 24);
J2D::FillTriangle(Colors::Red, {{0, 275}, {0, 375}, {100, 375}});
J2D::OutlineTriangle(Colors::Blue, {{100, 275}, {0, 275}, {100, 375}});
J2D::DrawGradientLine(JGL::Colors::Red, JGL::Colors::Blue, {105, 375}, {200, 275}, 2);
auto result = Jupiteroid.MeasureString("Jupiteroid Font", 16);
//DEBUG(std::format("Result: {},{}", result.x, result.y ));
// TODO: Note the discrepancy in Y between the FillRect and DrawString call here.
J2D::FillRect(JGL::Colors::Gray, {0, 0}, result);
// TODO: Does text render from the middle-point of the glyphs?
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 16, 1.f, 16, Jupiteroid);
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 33, 1,16, Jupiteroid);
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 50, 1,16, Jupiteroid);
J2D::End();
}
void OnRefresh(float elapsed) override {
display();
int glError = glGetError();
if (glError != GL_NO_ERROR)
std::cout << glError << std::endl;
glSwapBuffers();
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {return true;}
JGLDemoWindow() : ReWindow::RWindow() {}
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height){}
};
int main(int argc, char** argv) {
auto* window = new JGLDemoWindow("JGL Demo Window", 1280, 720);
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
window->initGL();
window->setResizable(false);
while (window->isAlive()) {
window->pollEvents();
window->refresh();
}
return 0;
}

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
'

107
src/FontCache.cpp Normal file
View File

@@ -0,0 +1,107 @@
#include <JGL/FontCache.h>
using namespace JGL;
char CachedGlyph::getCharacter() {
return character;
}
const std::array<GLfloat, 12> CachedGlyph::getTexCoords() const {
return texcoords;
}
CachedGlyph::CachedGlyph(char c, std::array<GLfloat, 12> texcoords, float x2offset, float y2offset, float w, float h, float advanceX, float advanceY) {
character = c;
this->x2offset = x2offset;
this->y2offset = y2offset;
this->w = w;
this->h = h;
this->advanceX = advanceX;
this->advanceY = advanceY;
this->texcoords = texcoords;
}
//TODO
//Because most things shown would be english characters. We can cut down on the iteration time significantly
//by putting each english character at the beginning of the list in order of how often they usually occur in text.
void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) {
glyphs.emplace(glyph->getCharacter(), glyph);
}
unsigned int JGL::CachedFont::getFontSize() {
return font_size;
}
unsigned int JGL::CachedFont::getFontIndex() {
return font_index;
}
//TODO make this code go faster.
CachedGlyph* JGL::CachedFont::getGlyph(char c) {
auto it = glyphs.find(c);
if (it != glyphs.end())
return it->second;
return nullptr;
}
CachedFont::CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index) {
this->texture = texture_id;
this->texture_width = texture_width;
this->texture_height = texture_height;
this->font_size = font_size;
this->font_index = font_index;
}
std::map<char, CachedGlyph*> CachedFont::getGlyphs() {
return glyphs;
}
const GLuint* CachedFont::getTexture() {
return &texture;
}
GLsizei CachedFont::getTextureWidth() const {
return texture_width;
}
GLsizei CachedFont::getTextureHeight() const {
return texture_height;
}
void FontCache::appendFont(CachedFont* font) {
cachedFonts.push_back(font);
}
void FontCache::newFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index) {
auto* font = new CachedFont(texture_id, texture_width, texture_height, 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) {
delete cachedFonts[i];
cachedFonts.erase(cachedFonts.begin() + i);
}
}
}
void FontCache::purgeCache() {
//Remove every font from the cache.
for (const auto& font : cachedFonts)
eraseFont(font);
}
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;
}

463
src/JGL.cpp Normal file
View File

@@ -0,0 +1,463 @@
//
// Created by dawsh on 1/17/24.
//
#include <JGL/JGL.h>
#include <glad/glad.h>
#include <JGL/Color3.h>
#include <jlog/jlog.hpp>
GLfloat oldColor[4] = {0, 0, 0, 255};
bool inJ2D = false;
bool inJ3D = false;
bool wasTexture2DEnabled = false;
bool wasTextureCoordArrayEnabled = false;
bool wasDepthTestEnabled = false;
bool wasVertexArraysEnabled = false;
bool wasCullFaceEnabled = false;
bool wasBlendEnabled = false;
bool wasColorArrayEnabled = false;
GLint activeTextureUnit = 0;
namespace JGL {
using namespace J3ML;
Vector2 wS;
void Update(const Vector2& window_size) {
wS = window_size;
}
void J2D::Begin() {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, wS.x, wS.y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
if (activeTextureUnit != 0)
glActiveTexture(GL_TEXTURE0);
wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST))
wasDepthTestEnabled = true,
glDisable(GL_DEPTH_TEST);
wasVertexArraysEnabled = true;
if (!glIsEnabled(GL_VERTEX_ARRAY))
wasVertexArraysEnabled = false,
glEnableClientState(GL_VERTEX_ARRAY);
wasCullFaceEnabled = true;
if (!glIsEnabled(GL_CULL_FACE))
wasCullFaceEnabled = false,
glEnable(GL_CULL_FACE),
glCullFace(GL_BACK);
wasBlendEnabled = true;
if (!glIsEnabled(GL_BLEND))
wasBlendEnabled = false,
glEnable(GL_BLEND),
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
wasTexture2DEnabled = true;
if (!glIsEnabled(GL_TEXTURE_2D))
wasTexture2DEnabled = false,
glEnable(GL_TEXTURE_2D);
wasTextureCoordArrayEnabled = true;
if (!glIsEnabled(GL_TEXTURE_COORD_ARRAY))
wasTextureCoordArrayEnabled = false,
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
wasColorArrayEnabled = false;
if (glIsEnabled(GL_COLOR_ARRAY))
wasColorArrayEnabled = true,
glDisableClientState(GL_COLOR_ARRAY);
if (!inJ3D)
inJ2D = true;
else { ERROR("Attempt to Begin J2D inside of J3D context.") }
}
void J2D::End() {
//Change back to the previous projection (The 3D one in Re3D's case.)
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
if (wasDepthTestEnabled)
glEnable(GL_DEPTH_TEST);
if (!wasVertexArraysEnabled)
glDisableClientState(GL_VERTEX_ARRAY);
if (!wasCullFaceEnabled)
glDisable(GL_CULL_FACE);
if (!wasBlendEnabled)
glDisable(GL_BLEND);
if (!wasTexture2DEnabled)
glDisable(GL_TEXTURE_2D);
if (!wasTextureCoordArrayEnabled)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if (wasColorArrayEnabled)
glEnableClientState(GL_COLOR_ARRAY);
//Select whatever texture unit was selected before.
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
//Put the draw color back how it was before.
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
inJ2D = false;
}
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
const std::vector<Vector2> textureCoordinates = {{0,0}, {1, 0}, {1,1}, {0,1}};
const std::vector<Vector2> vertices = {pos, {pos.x + size.x, pos.y}, {pos.x + size.x, pos.y + size.y}, {pos.x, pos.y + size.y}};
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);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
glDrawArrays(GL_QUADS, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);
}
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h) {
J2D::DrawSprite(texture, {x, y}, {w, h});
}
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_QUADS, 0, 4);
}
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
J2D::FillRect({color.r, color.g, color.b, 255}, pos, size);
}
void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
std::vector<GLfloat> colors = {};
if (gradient == Gradient::Horizontal)
colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
else if (gradient == Gradient::Vertical)
colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f,
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f};
else if (gradient == Gradient::DiagonalBottomLeft)
colors = {(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,
color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f,
(color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
else if (gradient == Gradient::DiagonalTopLeft)
colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f,
(color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f,
(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f,(color1.a + color2.a) / 2.f / 255.f};
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glColorPointer(4, GL_FLOAT, 0, colors.data());
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_COLOR_ARRAY);
}
void J2D::FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
J2D::FillGradientRect({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, gradient, pos, size);
}
void J2D::FillRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius, unsigned int subdivisions) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
J2D::FillRect(color, {pos.x + radius, pos.y}, {size.x - 2 * radius, size.y});
J2D::FillRect(color, {pos.x, pos.y + radius}, {size.x, size.y - 2 * radius});
J2D::FillCircle(color, {pos.x + radius, pos.y + radius}, radius, subdivisions);
J2D::FillCircle(color, {pos.x + size.x - radius, pos.y + radius}, radius, subdivisions);
J2D::FillCircle(color, {pos.x + radius, pos.y + size.y - radius}, radius, subdivisions);
J2D::FillCircle(color, {pos.x + size.x - radius, pos.y + size.y - radius}, radius, subdivisions);
}
void J2D::FillRoundedRect(const JGL::Color3& color, const J3ML::LinearAlgebra::Vector2& pos, const J3ML::LinearAlgebra::Vector2& size, float radius, unsigned int subdivisions) {
J2D::FillRoundedRect({color.r, color.g, color.b, 255}, pos, size, radius, subdivisions);
}
void J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
void J2D::OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness) {
J2D::OutlineRect({color.r, color.g, color.b, 255}, pos, size, thickness);
}
void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.");
Vector2 vertices[] = {A, B};
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
}
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
J2D::DrawLine({color.r, color.g, color.b, 255}, A, B, thickness);
}
void J2D::DrawLine(const Color4& color, float x, float y, float w, float h, float thickness) {
J2D::DrawLine(color, {x, y}, {w, h}, thickness);
}
void J2D::DrawLine(const Color3& color, float x, float y, float w, float h, float thickness) {
J2D::DrawLine({color.r, color.g, color.b, 255}, x, y, w, h, thickness);
}
void J2D::DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.");
Vector2 vertices[] = {A, B};
GLfloat colors[8] = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
glEnableClientState(GL_COLOR_ARRAY);
glLineWidth(thickness);
glColorPointer(4,GL_FLOAT,sizeof(GL_FLOAT) * 4, colors);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_COLOR_ARRAY);
}
void J2D::DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness) {
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, A, B, thickness);
}
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness) {
J2D::DrawGradientLine(color1, color2, {x, y}, {w, h}, thickness);
}
void DrawGradientLine(const Color3& color1, const Color3& color2, float x, float y, float w, float h, float thickness) {
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, {x, y}, {w, h}, thickness);
}
void J2D::DrawPixel(const Color4& color, const Vector2& coordinates) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.");
Vector2 vertices[] = {coordinates};
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 1);
}
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
J2D::DrawPixel({color.r, color.g, color.b, 255}, coordinates);
}
void J2D::DrawPixel(const Color4& color, float x, float y) {
DrawPixel(color, {x, y});
}
void J2D::DrawPixel(const Color3& color, float x, float y) {
DrawPixel({color.r, color.g, color.b, 255}, {x, y});
}
void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
float step = (2.f * M_PI) / (float) subdivisions;
std::vector<Vector2> vertices{};
GLfloat angle, x, y;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step) {
x = radius * std::sin(angle) + center.x;
y = radius * std::cos(angle) + center.y;
vertices.emplace_back(x,y);
}
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
}
void J2D::OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
J2D::OutlineCircle({color.r, color.g, color.b, 255}, center, radius, subdivisions, thickness);
}
void J2D::FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
float step = (2.f * M_PI) / (float) subdivisions;;
std::vector<Vector2> vertices{};
GLfloat angle, x, y;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step)
x = radius * sin(angle) + center.x,
y = radius * cos(angle) + center.y,
vertices.push_back({x, y});
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
}
void J2D::FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions) {
J2D::FillCircle({color.r, color.g, color.b, 255}, center, radius, subdivisions);
}
void J2D::OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 3);
}
void J2D::OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness) {
J2D::OutlineTriangle({color.r, color.g, color.b, 255}, tri, thickness);
}
void J2D::FillTriangle(const Color4& color, const Triangle2D &tri) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D &tri) {
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
}
void J3D::Begin() {
//Get what the draw color was before we did anything.
glGetFloatv(GL_CURRENT_COLOR, oldColor);
wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST))
wasDepthTestEnabled = true,
glDisable(GL_DEPTH_TEST);
wasVertexArraysEnabled = false;
if (!glIsEnabled(GL_VERTEX_ARRAY))
wasVertexArraysEnabled = false,
glEnableClientState(GL_VERTEX_ARRAY);
wasTexture2DEnabled = true;
if (!glIsEnabled(GL_TEXTURE_2D))
wasTexture2DEnabled = false,
glEnable(GL_TEXTURE_2D);
//TODO We won't always want this but for now we do.
wasCullFaceEnabled = false;
if (glIsEnabled(GL_CULL_FACE))
wasCullFaceEnabled = true,
glDisable(GL_CULL_FACE);
wasBlendEnabled = true;
if (!glIsEnabled(GL_BLEND))
wasBlendEnabled = false,
glEnable(GL_BLEND),
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (!inJ2D)
inJ3D = true;
else { ERROR("Attempt to Begin J3D inside of J2D context.") }
}
void J3D::End() {
glDisable(GL_TEXTURE_2D);
if (wasDepthTestEnabled)
glEnable(GL_DEPTH_TEST);
if (!wasVertexArraysEnabled)
glDisableClientState(GL_VERTEX_ARRAY);
if (wasTexture2DEnabled)
glDisable(GL_TEXTURE_2D);
if (wasBlendEnabled)
glDisable(GL_BLEND);
if (wasCullFaceEnabled)
glEnable(GL_CULL_FACE);
//Put the draw color back how it was before.
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
inJ3D = false;
}
void J3D::DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness) {
if (!inJ3D)
ERROR("Attempt to Render J3D element before J3D begin.")
Vector3 vertices[] = {A, B};
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
glDrawArrays(GL_LINES, 0, 2);
}
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {
J3D::DrawLine({color.r, color.g, color.b, 255}, A, B, thickness);
}
}

6
src/JGL/Color3.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <JGL/Color3.h>
namespace JGL
{
}

6
src/JGL/Color4.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <JGL/Color4.h>
namespace JGL
{
}

165
src/JGL/Font.cpp Normal file
View File

@@ -0,0 +1,165 @@
#include <vector>
#include <string>
#include <iostream>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
#include <JGL/Font.h>
namespace JGL::Detail
{
FT_Library ft;
std::vector<Font> fonts;
// TODO: Deprecating this in favor of class model of resource management.
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 : fonts)
if (f.index >= newIndex)
newIndex = f.index + 1;
font.index = newIndex;
fonts.push_back(font);
std::cout << "Loaded font from " << font_path << " with index " << newIndex << std::endl;
return newIndex;
}
bool InitTextEngine() {
if (ft != nullptr)
throw std::runtime_error("Error::FREETYPE: FT_Library was initialized but is already initialized.");
if (FT_Init_FreeType(&ft))
return true;
return false;
}
void UnloadFont(int font_index) {
auto iter = fonts.begin();
while (iter != fonts.end())
{
if (iter->index == font_index){
FT_Done_Face(iter->face);
iter = fonts.erase(iter);
} else ++iter;
}
}
}
namespace JGL
{
bool InitTextEngine()
{
return Detail::InitTextEngine();
}
Font::Font(const std::filesystem::path& path)
{
if (Detail::ft == nullptr)
throw new 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.c_str(), 0, &face)) {
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
throw new 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;
}
Font::~Font()
{
//Detail::UnloadFont(this->index);
}
std::vector<Font> Font::GetLoadedFonts() {
return Detail::fonts;
}
Font Font::LoadTTF(const std::filesystem::path& path)
{
return Font(path);
}
Vector2 Font::MeasureString(const std::string &text, float ptSize) {
// TODO: Work in-progress implementation unfortunately.
// This is likely returning slightly incorrect results for likely several reasons.
// Half-ass solution for now ~ dawsh.
FT_BBox glyph_bbox;
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
Vector2 extents = Vector2(0,0);
for (const char& c : text) {
FT_GlyphSlot slot = face->glyph;
auto glyph_index = FT_Get_Char_Index(this->face, c);
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if (error)
continue;
Vector2 advance = {static_cast<float>(slot->advance.x >> 6),
static_cast<float>(slot->advance.y >> 6)};
extents += advance;
// Gives smaller results than we'd want.
if (extents.y < slot->metrics.height / 64) {
extents.y = slot->metrics.height / 64;
}
// Just fucking hardcode it, we know the glyph height is roughly always the ptSize anyway.
if (extents.y < ptSize)
{
extents.y = ptSize;
}
}
return extents;
}
}

View File

@@ -1,132 +0,0 @@
//
// Created by dawsh on 1/17/24.
//
#include <JGL/JGL.h>
#include <GL/glut.h>
#include "J3ML/LinearAlgebra/Transform2D.h"
namespace JGL
{
Vector2 ScreenToViewport(const Vector2 &v) {
// TODO: Implement (CORRECT!!!) matrix transformation
//Transform2D transform;
//transform = transform.Translate(1.f, 1.f); // Center
//transform = transform.Scale(0.5f, 0.5f); // Scale by half
//transform = transform.Scale(viewportWidth, viewportHeight); // Scale by screen size
//return transform.Transform(v);
float x = v.x / 600.f;
float y = v.y / 600.f;
return {
x - .5f, y - .5f
};
}
namespace J2D
{
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = ScreenToViewport(pos);
auto vp_size = ScreenToViewport(size);
glBegin(GL_QUADS);
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 OutlineRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size, float thickness) {
auto vp_pos = ScreenToViewport(pos);
auto vp_size = ScreenToViewport(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 = ScreenToViewport(A);
auto vp_b = ScreenToViewport(B);
glBegin(GL_LINE);
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 = ScreenToViewport(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();
}
}
namespace J3D
{
void DrawLine3D(const Color3& color, const Vector3& A, const Vector3& B, float thickness)
{
glBegin(GL_LINE);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex3f(A.x, A.y, A.z);
glVertex3f(B.x, B.y, B.z);
glEnd();
}
}
}

248
src/JGL/TextRendering.cpp Normal file
View File

@@ -0,0 +1,248 @@
#include <JGL/JGL.h>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include "jlog/jlog.hpp"
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
#include <JGL/Font.h>
#include <JGL/FontCache.h>
namespace JGL {
FontCache fontCache; // <-- Not implemented yet
void PurgeFontCache() {
fontCache.purgeCache();
}
void J2D::DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
glUseProgram(0); // Fixed-function pipeline.
CachedFont* cachedFont = fontCache.getFont(size, font.index);
//Set up the regular font.
//for (const auto &f : Font::GetLoadedFonts())
// if (f.index == font.index)
// font = f;
if (font.face == nullptr)
return;
FT_Set_Pixel_Sizes(font.face, 0, size);
//If the font doesn't exist in the cache yet.
if (!cachedFont) {
DEBUG("Caching font data...");
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
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);
GLsizei width = 0;
GLsizei max_height = 0;
FT_ULong charcode;
FT_UInt gindex;
//We have to loop over the available glyphs twice as we need the
//final width and height of the texture before we can construct it
//and subsequently upload the glyph data.
charcode = FT_Get_First_Char(font.face, &gindex);
//Strings are char-based so we only handle charcodes within the extended ASCII range.
while (gindex != 0 && charcode < 255) {
if (FT_Load_Char(font.face, charcode, FT_LOAD_RENDER))
std::cout << "Error::FREETYPE: Failed to load charcode: " << charcode << std::endl;
FT_GlyphSlot g = font.face->glyph;
width += g->bitmap.width;
max_height = std::max(max_height, (GLsizei)g->bitmap.rows);
charcode = FT_Get_Next_Char(font.face, charcode, &gindex);
}
fontCache.newFont(texture_id, width, max_height, size, font.index);
cachedFont = fontCache.getFont(size, font.index);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, max_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, nullptr);
GLsizei xoffset = 0;
charcode = FT_Get_First_Char(font.face, &gindex);
while (gindex != 0 && charcode < 255) {
if (FT_Load_Char(font.face, charcode, FT_LOAD_RENDER))
std::cout << "Error::FREETYPE: Failed to load charcode: " << charcode << std::endl;
FT_GlyphSlot g = font.face->glyph;
glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, 0, g->bitmap.width, g->bitmap.rows, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
GLfloat u0 = (GLfloat)xoffset / cachedFont->getTextureWidth();
GLfloat u1 = u0 + (GLfloat)g->bitmap.width / cachedFont->getTextureWidth();
GLfloat v0 = 0.0f;
GLfloat v1 = (GLfloat)g->bitmap.rows / cachedFont->getTextureHeight();
std::array<GLfloat, 12> texcoords = {
u0, v0,
u0, v1,
u1, v1,
u0, v0,
u1, v1,
u1, v0
};
cachedFont->appendGlyph(new CachedGlyph((char)charcode, texcoords, g->bitmap_left, g->bitmap_top, g->bitmap.width, g->bitmap.rows, (g->advance.x >> 6), (g->advance.y >> 6)));
xoffset += g->bitmap.width;
charcode = FT_Get_Next_Char(font.face, charcode, &gindex);
}
}
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
//Texture parameters are restored when the texture is bound
glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture());
std::vector<GLfloat> vertices;
std::vector<GLfloat> texcoords;
for (int i = 0; i < text.length(); i++) {
float x2, y2, w, h;
CachedGlyph *glyph = cachedFont->getGlyph(text.c_str()[i]);
if (glyph == nullptr) continue;
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;
std::array<GLfloat, 12> glyph_vertices = {
x2, y2,
x2, y2 + h,
x2 + w, y2 + h,
x2, y2,
x2 + w, y2 + h,
x2 + w, y2
};
auto glyph_texcoords = glyph->getTexCoords();
//TODO make it like go faster because now the profiler says this is the slowest part.
vertices.insert(vertices.end(), glyph_vertices.begin(), glyph_vertices.end());
texcoords.insert(texcoords.end(), glyph_texcoords.begin(), glyph_texcoords.end());
}
glVertexPointer(2, GL_FLOAT, sizeof(GLfloat) * 2, vertices.data());
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat) * 2, texcoords.data());
glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 2);
glBindTexture(GL_TEXTURE_2D, 0);
}
void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
J2D::DrawString(Color4::FromColor3(color, 255), text, x, y, scale, size, font);
}
void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font) {
//TODO figure out what the scale should actually be mathematically.
scale = scale * 0.002f;
scale = -scale;
float x = pos.x;
float y = pos.y;
float z = pos.z;
std::vector<GLuint> textures(text.length());;
glUseProgram(0); // Fixed-function pipeline.
glColor4f(color.r, color.g, color.b, 1.0f);
//Font font;
//for (auto& f : Font::GetLoadedFonts())
//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);
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
x = 0;
y = 0;
z = 0;
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;
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);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glPopMatrix();
}
}