Compare commits

...

35 Commits

Author SHA1 Message Date
32de87229e Update Texture.h
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 18m9s
2024-08-21 20:59:25 -04:00
3759affa5d Fix texture
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 6m10s
2024-08-21 20:36:31 -04:00
8ce9a08951 Resize fix
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 6m54s
2024-08-21 13:20:42 -04:00
ec964dbee1 Wondows :(
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 5m25s
2024-08-08 00:14:24 -04:00
91baa826fa Allow changing the fov and render distance
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m30s
2024-08-06 12:31:44 -04:00
c2446763ad Merge remote-tracking branch 'origin/master'
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m24s
2024-08-05 20:11:18 -04:00
6286b62998 Pull out debug iteration steps from DrawCubicBezierCurve 2024-08-05 20:11:12 -04:00
4de44ba1e2 Put this code back because it got removed again. Make sure you have the latest commit before doing anything :/
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m26s
2024-08-05 19:49:30 -04:00
166db43f2e Migrate to latest J3ML and ReWindow
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m36s
2024-08-05 18:31:13 -04:00
2a1085df2d Revert "Merge remote-tracking branch 'origin/master'"
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m20s
This reverts commit 240fa7ddbc, reversing
changes made to d89f79e70e.
2024-08-05 15:04:22 -04:00
240fa7ddbc Merge remote-tracking branch 'origin/master'
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m20s
# Conflicts:
#	main.cpp
2024-08-05 14:39:36 -04:00
d89f79e70e Cool BezierCurve demo (You can drag the endpoints around) 2024-08-05 14:38:42 -04:00
7bf30f6f39 Idk like make the viewport work maybe?
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m29s
2024-08-05 13:41:12 -04:00
2a70981bf0 Get software texture back.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m44s
2024-08-05 11:30:40 -04:00
0147245325 Refactored DrawSprite
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m16s
2024-08-05 00:52:08 -04:00
15dcb79479 Upd8
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m29s
2024-08-04 20:28:08 -04:00
50895153f5 Texture filtering
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m46s
2024-08-03 22:17:21 -04:00
9688854533 Texture class
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m25s
2024-08-02 20:03:32 -04:00
0005c036b4 Texture2D header
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 5m32s
2024-08-02 13:05:34 -04:00
af42d8fd25 Merge remote-tracking branch 'origin/master'
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m26s
2024-08-02 10:49:05 -04:00
ff4d014739 Implement Color3::FromHex() 2024-08-02 10:49:00 -04:00
9dda4bebc5 Make measure string go brrrrrrr
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m9s
2024-07-31 12:01:57 -04:00
a5bfb4972a Fill quad
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m32s
2024-07-30 23:37:47 -04:00
4348f2708a Gradient triangle
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m32s
2024-07-30 15:49:13 -04:00
32dd2b54ca incorporate builddeps.reci
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m40s
2024-07-29 19:30:23 -04:00
e8b601aa25 install build deps reci script
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 45s
2024-07-29 19:29:11 -04:00
d5fd3e1a49 reci lua edition
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 48s
2024-07-29 19:24:55 -04:00
f593a0beac Cleanup & Update ReTexture.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m29s
2024-07-19 17:16:38 -04:00
0b7af6fd31 Cleanup
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m46s
2024-07-19 01:34:43 -04:00
eca4309e85 DrawSprite
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m21s
2024-07-18 22:24:19 -04:00
4be97f52d9 Minor documentation additions.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m19s
2024-07-18 14:51:19 -04:00
613a13618c Merge branch 'master' of https://git.redacted.cc/Josh/JGL
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
2024-07-18 14:49:19 -04:00
4150c93c85 Make it like, go faster. 2024-07-18 14:49:15 -04:00
9dee59fd45 Merge remote-tracking branch 'origin/master'
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m32s
2024-07-18 14:04:16 -04:00
b6b2ca1bfe Remove old LoadFont function. 2024-07-18 14:04:11 -04:00
21 changed files with 789 additions and 492 deletions

View File

@@ -17,8 +17,6 @@ jobs:
- 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: apt-get update && apt-get install -y lua5.3 git && git clone $RECI_GIT $RECI
- run: lua $RECI/reci.lua -f $RECI/scripts/buildtools.reci -f reci/scripts/builddeps.reci -f $RECI/scripts/buildtest.reci
- run: echo "This job's status is ${{ job.status }}."

View File

@@ -14,14 +14,20 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Enable Package Managers
include(cmake/CPM.cmake)
CPMAddPackage(
NAME mcolor
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-3.zip
)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.2.zip
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.0.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-3.zip
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-12.zip
)
CPMAddPackage(
@@ -41,7 +47,7 @@ CPMAddPackage(
CPMAddPackage(
NAME ReTexture
URL https://git.redacted.cc/Redacted/ReTexture/archive/Prerelease-2.zip
URL https://git.redacted.cc/Redacted/ReTexture/archive/Release-1.2.zip
)
if (WIN32)
@@ -74,15 +80,18 @@ endif()
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
#Don't expose this one because it's only to be used in the demo program.
include_directories(${ReTexture_SOURCE_DIR}/include)
target_include_directories(JGL PUBLIC
${PROJECT_SOURCE_DIR}/include
${OPENGL_INCLUDE_DIRS}
${mcolor_SOURCE_DIR}/include
${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)
@@ -91,13 +100,13 @@ add_executable(JGL_Demo main.cpp)
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)
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} mcolor J3ML ReWindowLibrary glad jlog Event ReTexture)
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)
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} mcolor J3ML ReWindowLibrary glad jlog Event ReTexture)
endif()
target_link_libraries(JGL_Demo PUBLIC JGL ReTexture)
target_link_libraries(JGL_Demo PUBLIC JGL)

View File

@@ -18,7 +18,7 @@ Yet Another C++ Rendering Toolkit
## API Overview
### J2D
* DrawPixel
* DrawPoint
* DrawLine / DrawGradientLine
* DrawSprite
* OutlineRect / FillRect / FillGradientRect / FillRoundedRect

BIN
assets/sprites/Re3D.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@@ -1,28 +0,0 @@
#pragma once
#include <J3ML/J3ML.h>
namespace JGL
{
using namespace J3ML;
class Color3 {
public:
u8 r;
u8 g;
u8 b;
public:
Color3(u8 R, u8 G, u8 B);
static Color3 FromHex(const std::string& hexCode);
public:
Color3 Lerp(const Color3& rhs, float alpha) const;
u8 RedChannel () const;
u8 GreenChannel() const;
u8 BlueChannel () const;
float RedChannelNormalized () const;
float BlueChannelNormalized() const;
float GreenChannelNormalized() const;
};
}

View File

@@ -1,30 +0,0 @@
#pragma once
#include <JGL/Color3.h>
namespace JGL
{
class Color4 {
public:
u8 r;
u8 g;
u8 b;
u8 a;
public:
explicit Color4(const Color3& color3, u8 alpha = 255);
Color4(u8 red, u8 green, u8 blue, u8 alpha = 255);
static Color4 FromColor3(const Color3& color3, u8 alpha = 255);
static Color4 FromHex(const std::string& hexCode, u8 alpha = 255);
public:
u8 RedChannel() const;
u8 GreenChannel() const;
u8 BlueChannel() const;
u8 AlphaChannel() const;
float RedChannelNormalized() const;
float GreenChannelNormalized() const;
float BlueChannelNormalized() const;
float AlphaChannelNormalized() const;
};
}

View File

@@ -1,178 +0,0 @@
#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};
}
}
}

View File

@@ -2,18 +2,19 @@
#include <vector>
#include <filesystem>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra.hpp>
#include <filesystem>
#include <iostream>
// LMFAO
/// Defines external C references to FreeType Data Structures
extern "C" typedef struct FT_FaceRec_* FT_Face;
extern "C" typedef struct FT_LibraryRec_* FT_Library;
namespace JGL
{
/// Initializes FreeType engine.
/// Initializes FreeType engine. Remember to call this during program initialization.
bool InitTextEngine();
/// A Font class implementation.
@@ -31,7 +32,7 @@ namespace JGL
/// @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);
Vector2 MeasureString(const std::string& text, unsigned int ptSize);
public:
int index = 0;
FT_Face face;

View File

@@ -61,3 +61,7 @@ public:
void eraseFont(CachedFont* font);
void purgeCache();
};
namespace JGL {
inline FontCache fontCache;
}

View File

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

View File

@@ -14,30 +14,35 @@
#include <string>
#include <iostream>
#include <JGL/Color4.h>
#include <JGL/Gradient.h>
#include <Color3.hpp>
#include <Color4.hpp>
#include <Colors.hpp>
#include <JGL/Texture.h>
#include <JGL/enums.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/Geometry/Sphere.h>
#include <J3ML/Geometry/Capsule.h>
#include <J3ML/Geometry/TriangleMesh.h>
#include <J3ML/LinearAlgebra.hpp>
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <J3ML/LinearAlgebra/Vector3.hpp>
#include <J3ML/Geometry/Sphere.hpp>
#include <J3ML/Geometry/Capsule.hpp>
#include <J3ML/Geometry/TriangleMesh.hpp>
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
/// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
namespace JGL {
using namespace J3ML::LinearAlgebra;
using namespace J3ML::Geometry;
/// TODO:
/// TODO: Implement HSV and other color representation conversions
struct HSV {
float hue;
float saturation;
float value;
};
/// TODO: Migrate to using J3ML's definition once finished (hint hint)
struct Triangle2D
{
Vector2 A;
@@ -45,20 +50,14 @@ namespace JGL {
Vector2 C;
};
struct Triangle3D
{
Vector3 A;
Vector3 B;
Vector3 C;
};
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
// TODO: Implement
void SetActiveFont(const Font& font);
// TODO: Overrides specifically for Color3 are not **strictly** necessary, Color3 and Color4 should implicitly convert back and forth.
/// Drawing functions for primitive 2D Shapes.
/// Each function is overloaded with Color3 and Color4 for optional transparency.
@@ -74,12 +73,12 @@ namespace JGL {
void End();
/// Plots a single pixel on the screen.
/// @param color A 3-or-4 channel color value. @see classes Color3, Color4
/// @param color A 3-or-4 channel color value. @see class Color3, class 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);
void DrawPoint(const Color3& color, const Vector2& coordinates, float radius = 1.f);
void DrawPoint(const Color3& color, float x, float y, float radius = 1.f);
void DrawPoint(const Color4& color, const Vector2& coordinates, float radius = 1.f);
void DrawPoint(const Color4& color, float x, float y, float radius = 1.f);
/// Plots a line (segment) on the screen.
/// @param color A 3-or-4 channel color value. @see classes Color3, Color4.
@@ -101,9 +100,37 @@ namespace JGL {
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 sprite to the screen by passing a GLuint that represents a handle to a loaded texture.
/// @param texture
/// @param position
/// @param origin The center point around which the image should have all transformations applied to it.
/// @param scale The scale transformation for the image. X and Y axis are independently-scalable.
/// @param color A 32-bit RGBA value represented as four unsigned 8-bit integers.
/// @param inversion
/// @see class Texture
void DrawSprite(const Texture& texture,
const Vector2& position,
const Vector2& origin = Vector2(0,0),
const Vector2& scale = Vector2(1,1),
const Color4& color = Colors::White,
Inversion inversion = Inversion::None);
void DrawSprite(const Texture& texture,
float positionX, float positionY,
float originX = 0, float originY = 0,
float scaleX = 1, float scaleY = 1,
const Color4& color = Colors::White,
Inversion inversion = Inversion::None);
/// Draws a non axis-aligned fill rect to the screen.
/// The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
void FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
void FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
/// Draws a non axis-aligned outline rect to the screen.
/// The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
void OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
void OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
/// Draws a filled rectangle on the screen.
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
@@ -126,13 +153,27 @@ namespace JGL {
void FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
/// Draws an outline of a triangle on the screen.
/// @param color
/// @param tri
/// @param thickness
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
void OutlineTriangle(const Color4& color,
const Vector2& triA, const Vector2& triB, const Vector2& triC,
float thickness = 1);
void OutlineTriangle(const Color3& color,
const Vector2& triA, const Vector2& triB, const Vector2& triC,
float thickness = 1);
// TODO: Take more Focalin
/// Draws a filled triangle on the screen.
void FillTriangle(const Color4& color, const Triangle2D &tri);
void FillTriangle(const Color4& color, const Triangle2D& tri);
void FIllTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC);
void FillTriangle(const Color3& color, const Triangle2D& tri);
/// Draws a triangle where each corner is defined by a given color, Smoothly transitioning between them.
void FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri);
void FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri);
// TODO: Implement an overload that simply takes 3 Vector3's
@@ -144,13 +185,24 @@ namespace JGL {
void FillTexturedTriangle();
void FillTexturedPolygon();
void DrawPartialSprite();
void DrawCubicBezierCurve();
void DrawCubicBezierCurve(const Color4& color,
const Vector2& controlA, const Vector2& pointA,
const Vector2& pointB, const Vector2& controlB,
int subdivisions = 10,
float thickness = 1);
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);
}
/// Drawing functions for primitive 3D Shapes.
namespace J3D {
void Init(const Vector2& window_size, float fov, float far_plane);
void ChangeFOV(float fov);
void ChangeFarPlane(float far_plane);
void Begin();
void End();
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
@@ -164,8 +216,8 @@ namespace JGL {
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 DrawString(const Color4& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font);
void DrawSprite();
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
void DrawMatrixGizmo (const Matrix4x4&);
void DrawAxisAngleGizmo (const AxisAngle&, const Vector3&);

52
include/JGL/Texture.h Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include <ReTexture/Texture.h>
#include <J3ML/LinearAlgebra.hpp>
#include <Color3.hpp>
#include <Color4.hpp>
#include <glad/glad.h>
namespace JGL {
using namespace ReTexture;
enum class TextureFilteringMode : u8 {
NEAREST = 0, //Fastest for 2D, Sometimes causes graphical issues.
BILINEAR = 1, //Fast and pretty, The best for 2D.
MIPMAP_NEAREST = 2, //Nearest with mipmaps. The fastest for 3D, Sometimes causes graphical issues. Uses more vram.
MIPMAP_BILINEAR = 3, //Bilinear with mipmaps, Fast and pretty. Uses more vram.
MIPMAP_TRILINEAR = 4 //The prettiest. Still decent speed. Uses more vram.
};
enum class TextureWrappingMode : u8 {
REPEAT = 0,
MIRRORED_REPEAT = 1,
CLAMP_TO_EDGE = 2,
CLAMP_TO_BORDER = 3 //Effectively the same as clamp_to_edge
};
/// Represents texture data loaded on the GPU. Contains a handle that can be passed to OpenGL draw calls.
class Texture {
protected:
GLuint texture_handle = 0;
Vector2 texture_size = {0, 0};
ReTexture::TextureFlag texture_flags;
ReTexture::TextureFormat texture_format;
TextureFilteringMode texture_filtering_mode;
TextureWrappingMode texture_wrapping_mode;
virtual void load(SoftwareTexture* software_texture, const Vector2& size, const TextureFormat& format, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode);
public:
explicit Texture(const std::string& file, TextureFilteringMode filtering_mode = TextureFilteringMode::BILINEAR, TextureWrappingMode wrapping_mode = TextureWrappingMode::CLAMP_TO_EDGE);
Texture(const std::string& file, const TextureFlag& flags, TextureFilteringMode filtering_mode = TextureFilteringMode::BILINEAR, TextureWrappingMode wrapping_mode = TextureWrappingMode::CLAMP_TO_EDGE);
Texture() = default;
public:
GLuint GetGLTextureHandle() const;
Vector2 GetDimensions() const;
TextureFilteringMode GetFilteringMode() const;
TextureFlag GetFlags() const;
TextureFormat GetFormat() const;
std::vector<Color4> GetPixelData() const;
void SetTextureHandle(GLuint handle);
void Erase();
};
}

33
include/JGL/enums.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
namespace JGL {
enum class Inversion : u8 {
None = 0,
Vertical = 1,
Horizontal = 2,
};
inline Inversion operator|(Inversion a, Inversion b) {
return static_cast<Inversion>(static_cast<int>(a) | static_cast<int>(b));
}
inline bool operator&(Inversion a, Inversion b) {
return (u8)a & (u8)b;
}
}
namespace JGL {
enum class Gradient : u8 {
Vertical = 0,
Horizontal = 1,
DiagonalTopLeft = 2,
DiagonalBottomLeft = 4
};
inline Gradient operator|(Gradient a, Gradient b) {
return static_cast<Gradient>(static_cast<int>(a) | static_cast<int>(b));
}
inline bool operator&(Gradient a, Gradient b) {
return (u8)a & (u8)b;
}
}

185
main.cpp
View File

@@ -1,25 +1,76 @@
#include <JGL/JGL.h>
#include <rewindow/types/window.h>
#include <JGL/Colors.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <Colors.hpp>
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <JGL/Font.h>
#include "jlog/jlog.hpp"
#include <jlog/jlog.hpp>
#include <ReTexture/Texture.h>
using J3ML::LinearAlgebra::Vector2;
using namespace JGL;
//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;
}
JGL::Font FreeSans;
JGL::Font Jupiteroid;
class Gizmo
{
public:
Gizmo() {}
Gizmo(const Vector2& pos) : position(pos) {}
bool dragging = false;
bool hovered = false;
Vector2 position;
float range = 6.f;
void Grab() {
if (hovered)
{
dragging = true;
}
}
void Release() {
dragging = false;
}
void Update(const Vector2& mouse)
{
if (dragging)
{
position = position.Lerp(mouse, 0.25f);
}
if (mouse.Distance(position) < range)
{
hovered = true;
} else
{
hovered = false;
}
}
void Draw()
{
if (dragging)
{
J2D::DrawPoint(Colors::White, position, 4.f);
} else if (hovered)
{
J2D::DrawPoint(Colors::Reds::Crimson, position, 6.f);
} else
{
J2D::DrawPoint(Colors::Reds::Salmon, position, 3.f);
}
J2D::DrawString(Colors::White, std::format("{:.1f},{:.1f}", position.x, position.y), position.x, position.y, 1.f, 10, FreeSans);
}
protected:
private:
};
Texture* image;
class Camera {
public:
@@ -59,37 +110,50 @@ struct point {
GLfloat t;
};
JGL::Font FreeSans;
JGL::Font Jupiteroid;
Gizmo a({250, 150});
Gizmo b({200, 250});
Gizmo c({350, 300});
Gizmo d({450, 250});
class JGLDemoWindow : public ReWindow::RWindow
{
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());
J3D::Init(getSize(), 90, 100);
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.ttf");
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);
image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR);
}
Vector3 textAngle = {0,0,0};
float fov = 90;
bool fov_increasing = true;
void display() {
JGL::Update(getSize());
if (fov_increasing)
fov += 0.25;
else
fov -= 0.50;
if (fov >= 120)
fov_increasing = false;
else if (fov <= 75)
fov_increasing = true;
J3D::ChangeFOV(fov);
textAngle.y += 2.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
@@ -100,37 +164,54 @@ public:
///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::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-0.33,0.25,1});
J3D::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f},textAngle, 1.f, 32, FreeSans);
J3D::End();
J2D::Begin();
J2D::FillQuad(Color4(Colors::Red), {500, 52}, {500, 152}, {600, 152}, {600, 52});
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
J2D::DrawSprite(*image, {200, 252}, {0.5, 0.5}, {2, 1});
J2D::FillRect(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::FillRoundedRect(Colors::Red, {200, 52}, {100, 100}, 8, 8);
J2D::FillRoundedRect(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::FillCircle(Colors::White, {52, 204}, 50, 24);
J2D::OutlineCircle(Colors::White, {153, 204}, 50, 24);
J2D::FillTriangle(Colors::Red, {{0, 275}, {0, 375}, {100, 375}});
//J2D::FillTriangle(Colors::Red, {{0, 275}, {0, 375}, {100, 375}});
J2D::FillGradientTriangle(Color4(Colors::Red), Color4(Colors::Green), Color4(Colors::Blue), {{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);
J2D::DrawGradientLine(Colors::Red, Colors::Blue, {105, 375}, {200, 275}, 2);
auto result = Jupiteroid.MeasureString("Jupiteroid Font", 16);
//DEBUG(std::format("Result: {},{}", result.x, result.y ));
J2D::FillRect(JGL::Colors::Gray, {0, 0}, result);
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 0, 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, 16, 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, 33, 1,16, Jupiteroid);
J2D::FillRect(Colors::Gray, {0, 0}, result);
J2D::DrawString(Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid);
J2D::DrawString(Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 16, 1,16, Jupiteroid);
J2D::DrawString(Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 33, 1,16, Jupiteroid);
J2D::DrawCubicBezierCurve(Colors::Blues::CornflowerBlue,
a.position,
b.position,
c.position,
d.position
, 20, 1.5f);
a.Draw();
b.Draw();
c.Draw();
d.Draw();
J2D::End();
}
void OnRefresh(float elapsed) override {
auto mouse = GetMouseCoordinates();
a.Update(mouse);
b.Update(mouse);
c.Update(mouse);
d.Update(mouse);
display();
int glError = glGetError();
if (glError != GL_NO_ERROR)
@@ -138,6 +219,25 @@ public:
glSwapBuffers();
}
void OnMouseButtonDown(const ReWindow::WindowEvents::MouseButtonDownEvent & ev) override
{
RWindow::OnMouseButtonDown(ev);
a.Grab();
b.Grab();
c.Grab();
d.Grab();
}
void OnMouseButtonUp(const ReWindow::WindowEvents::MouseButtonUpEvent & ev) override
{
RWindow::OnMouseButtonUp(ev);
a.Release();
b.Release();
c.Release();
d.Release();
}
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){}
@@ -148,7 +248,8 @@ int main(int argc, char** argv) {
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
window->initGL();
window->setResizable(false);
window->setResizable(true);
window->setVsyncEnabled(true);
while (window->isAlive()) {
window->pollEvents();

View File

@@ -0,0 +1 @@
Main:new("Install build dependencies", "apt-get install -yq libgl1-mesa-dev libfreetype-dev")

View File

@@ -4,10 +4,12 @@
#include <JGL/JGL.h>
#include <glad/glad.h>
#include <JGL/Color3.h>
#include <Color3.hpp>
#include <jlog/jlog.hpp>
#include <J3ML/Algorithm/Bezier.hpp>
GLfloat oldColor[4] = {0, 0, 0, 255};
GLfloat oldColor[4] = {0, 0, 0, 1};
GLfloat baseColor[4] = {1, 1, 1, 1};
bool inJ2D = false;
bool inJ3D = false;
bool wasTexture2DEnabled = false;
@@ -25,6 +27,7 @@ namespace JGL {
void Update(const Vector2& window_size) {
wS = window_size;
glViewport(0, 0, wS.x, wS.y);
}
@@ -32,12 +35,17 @@ namespace JGL {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0, 0, wS.x, wS.y);
glOrtho(0, wS.x, wS.y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
//Get what the draw color was before we did anything.
glGetFloatv(GL_CURRENT_COLOR, oldColor);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
glGetIntegerv(GL_ACTIVE_TEXTURE,& activeTextureUnit);
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
if (activeTextureUnit != 0)
glActiveTexture(GL_TEXTURE0);
@@ -111,50 +119,118 @@ namespace JGL {
if (wasColorArrayEnabled)
glEnableClientState(GL_COLOR_ARRAY);
//Select whatever texture unit was selected before.
//Select whatever texture_handle 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) {
void J2D::DrawSprite(const Texture& texture, const Vector2& pos, const Vector2& origin, const Vector2& scale, const Color4& color, Inversion inversion) {
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}};
const Vector2 size = texture.GetDimensions();
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());
std::array<Vector2, 4> textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};
// TODO: Kind of a mess, refactor to be more sensible later.
// Factors in scaling and origin correctly.
// i.e. to render at 2x size, from the center, at coords XY, use {2, 2} scale, and {0.5, 0.5} offset.
const Vector2 offset = origin * size;
Vector2 pos2 = pos - offset*scale;
Vector2 scaled_size = scale * size;
Vector2 size2 = scaled_size;
const Vector2 vertices[] = {
pos2, // Top-left vertex
{pos2.x, pos2.y + size2.y}, // Bottom-left
{pos2.x + size2.x, pos2.y + size2.y}, // Bottom-right
{pos2.x + size2.x, pos2.y} // Top-right
};
if (inversion& Inversion::Vertical)
textureCoordinates = {Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), Vector2(1, 1)};
if (inversion& Inversion::Horizontal)
textureCoordinates = {Vector2(1, 0), Vector2(1, 1), Vector2(0, 1), Vector2(0, 0)};
if ((inversion& Inversion::Horizontal) && (inversion& Inversion::Vertical))
textureCoordinates = {Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(0, 1)};
glColor4ubv(color.ptr());
glBindTexture(GL_TEXTURE_2D, texture.GetGLTextureHandle());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
glDrawArrays(GL_QUADS, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h) {
J2D::DrawSprite(texture, {x, y}, {w, h});
void J2D::DrawSprite(const Texture& texture, float positionX, float positionY, float originX, float originY, float scaleX, float scaleY, const Color4& color, Inversion inversion)
{
DrawSprite(texture,
{positionX, positionX},
{originX, originY},
{scaleX, scaleY},
color,
inversion);
}
void J2D::FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {v1, v2, v3, v4};
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_QUADS, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
J2D::FillQuad(Color4(color), v1, v2, v3, v4);
}
void J2D::OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {v1, v2, v3, v4};
glLineWidth(thickness);
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) {
J2D::OutlineQuad(Color4(color), v1, v2, v3, v4);
}
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);
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_QUADS, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
@@ -191,13 +267,14 @@ namespace JGL {
glColorPointer(4, GL_FLOAT, 0, colors.data());
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_COLOR_ARRAY);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
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) {
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.")
@@ -210,7 +287,7 @@ namespace JGL {
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) {
void J2D::FillRoundedRect(const 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);
}
@@ -221,9 +298,13 @@ namespace JGL {
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);
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness) {
@@ -237,9 +318,13 @@ namespace JGL {
Vector2 vertices[] = {A, B};
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
@@ -268,6 +353,7 @@ namespace JGL {
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_COLOR_ARRAY);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness) {
@@ -281,47 +367,56 @@ namespace JGL {
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) {
void J2D::DrawPoint(const Color4& color, const Vector2& coordinates, float radius) {
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);
glPointSize(radius);
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 1);
glDrawArrays(GL_POINTS, 0, 1);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
J2D::DrawPixel({color.r, color.g, color.b, 255}, coordinates);
void J2D::DrawPoint(const Color3& color, const Vector2& coordinates, float radius) {
J2D::DrawPoint({color.r, color.g, color.b, 255}, coordinates);
}
void J2D::DrawPixel(const Color4& color, float x, float y) {
DrawPixel(color, {x, y});
void J2D::DrawPoint(const Color4& color, float x, float y, float radius) {
DrawPoint(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::DrawPoint(const Color3& color, float x, float y, float radius) {
DrawPoint({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;
float step = (2.f * Math::Pi) / (float) subdivisions;
std::vector<Vector2> vertices{};
GLfloat angle, x, y;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step) {
for (angle = 0.0f; angle < (2.f * Math::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);
glColor4f(color.RedChannelNormalized(),
color.GreenChannelNormalized(),
color.BlueChannelNormalized(),
color.AlphaChannelNormalized());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
@@ -332,18 +427,19 @@ namespace JGL {
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;
float step = (2.f * Math::Pi) / (float) subdivisions;
std::vector<Vector2> vertices{};
for (angle = 0.0f; angle < (2.f * M_PI); angle += step)
for (angle = 0.0f; angle < (2.f * Math::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);
glColor4ubv(color.ptr());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions) {
@@ -360,13 +456,14 @@ namespace JGL {
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);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[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) {
void J2D::FillTriangle(const Color4& color, const Triangle2D& tri) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
@@ -375,16 +472,108 @@ namespace JGL {
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);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D &tri) {
void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_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}};
GLfloat colors[] = {a_color.r / 255.f, a_color.g / 255.f, a_color.b / 255.f, a_color.a / 255.f,b_color.r / 255.f,
b_color.g / 255.f, b_color.b / 255.f, b_color.a / 255.f,c_color.r / 255.f, c_color.g / 255.f, c_color.b / 255.f,
c_color.a / 255.f };
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, sizeof(Color4), colors);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_COLOR_ARRAY);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri) {
J2D::FillGradientTriangle(Color4(a_color), Color4(b_color), Color4(c_color), tri);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D& tri) {
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
}
void J2D::DrawCubicBezierCurve(const Color4 &color, const J3ML::LinearAlgebra::Vector2 &controlA,
const J3ML::LinearAlgebra::Vector2 &pointA,
const J3ML::LinearAlgebra::Vector2 &pointB,
const J3ML::LinearAlgebra::Vector2 &controlB, int subdivisions, float thickness) {
Vector2 last = controlA;
Vector2 first = controlB;
for (int i = 0; i < subdivisions; ++i)
{
float alpha = (float)i / (float)subdivisions;
Vector2 step = J3ML::Algorithm::Bezier(alpha, controlA, pointA, pointB, controlB);
DrawLine(color, last, step, thickness);
last = step;
}
// Have to manually draw the last segment of the curve.
DrawLine(color, last, first, thickness);
// Display control points
DrawPoint(Colors::Red, controlA, 2.f);
DrawPoint(Colors::Red, controlB, 2.f);
DrawPoint(Colors::Reds::Salmon, pointA, 2.f);
DrawPoint(Colors::Reds::Salmon, pointB, 2.f);
}
//The 3D 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 * Math::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;
}
bool j3d_initialized = false;
float j3d_far_plane = 0;
float j3d_fov = 0;
void J3D::Init(const J3ML::LinearAlgebra::Vector2& window_size, float fov, float far_plane) {
wS = window_size;
j3d_far_plane = far_plane;
j3d_fov = fov;
j3d_initialized = true;
}
void J3D::ChangeFOV(float fov) {
j3d_fov = fov;
}
void J3D::ChangeFarPlane(float far_plane) {
j3d_far_plane = far_plane;
}
void J3D::Begin() {
if (!j3d_initialized)
throw std::runtime_error("You have to run J3D::Init before rendering 3D elements.");
auto aspect = (float) wS.x / (float) wS.y;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMultMatrixf(perspective(j3d_fov, aspect, 0.001, j3d_far_plane).data());
glMatrixMode(GL_MODELVIEW);
//Get what the draw color was before we did anything.
glGetFloatv(GL_CURRENT_COLOR, oldColor);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST))
@@ -401,7 +590,7 @@ namespace JGL {
wasTexture2DEnabled = false,
glEnable(GL_TEXTURE_2D);
//TODO We won't always want this but for now we do.
// TODO: implement bool drawBackface as DrawString parameter.
wasCullFaceEnabled = false;
if (glIsEnabled(GL_CULL_FACE))
wasCullFaceEnabled = true,
@@ -452,6 +641,7 @@ namespace JGL {
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);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {

View File

@@ -1,19 +0,0 @@
#include <JGL/Color3.h>
namespace JGL
{
u8 Color3::RedChannel() const { return r; }
u8 Color3::GreenChannel() const { return g; }
u8 Color3::BlueChannel() const { return b; }
float Color3::RedChannelNormalized() const { return static_cast<float>(r) / 255.f;}
float Color3::BlueChannelNormalized() const { return static_cast<float>(b) / 255.f;}
float Color3::GreenChannelNormalized() const { return static_cast<float>(g) / 255.f;}
Color3::Color3(u8 R, u8 G, u8 B) : r(R), g(G), b(B) {}
}

View File

@@ -1,27 +0,0 @@
#include <JGL/Color4.h>
namespace JGL
{
Color4::Color4(u8 red, u8 green, u8 blue, u8 alpha) : r(red), g(green), b(blue), a(alpha) {}
Color4::Color4(const Color3 &color3, u8 alpha) {r = color3.r; g = color3.g; b = color3.b; a = alpha;}
Color4 Color4::FromColor3(const Color3 &color3, u8 alpha) {return Color4(color3, alpha);}
u8 Color4::RedChannel() const { return r;}
u8 Color4::GreenChannel() const { return g;}
u8 Color4::BlueChannel() const {return b;}
u8 Color4::AlphaChannel() const {return a;}
float Color4::RedChannelNormalized() const {return static_cast<float>(r/255.f); }
float Color4::GreenChannelNormalized() const {return static_cast<float>(g/255.f); }
float Color4::BlueChannelNormalized() const {return static_cast<float>(b/255.f); }
float Color4::AlphaChannelNormalized() const {return static_cast<float>(a/255.f); }
}

View File

@@ -5,49 +5,26 @@
#include <glad/glad.h>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#endif
#include <JGL/Font.h>
#include <JGL/FontCache.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() {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // NOTE: This MUST be called for text rendering to work properly!!!
@@ -88,7 +65,7 @@ namespace JGL
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)) {
if (FT_New_Face(Detail::ft, path.string().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;
@@ -120,24 +97,39 @@ namespace JGL
return Font(path);
}
Vector2 Font::MeasureString(const std::string &text, float ptSize) {
Vector2 Font::MeasureString(const std::string &text, unsigned int ptSize) {
Vector2 extents = Vector2(0,0);
bool font_of_size_in_cache = false;
// TODO: Work in-progress implementation unfortunately.
// This is likely returning slightly incorrect results for likely several reasons.
// Half-ass solution for now ~ dawsh.
for(const auto& f : fontCache.getFonts()) {
if (f->getFontSize() == ptSize) {
font_of_size_in_cache = true;
break;
}
}
FT_BBox glyph_bbox;
if (font_of_size_in_cache) {
CachedFont* font;
for (auto* f: fontCache.getFonts())
if (f->getFontSize() == ptSize)
font = f;
for (const char& c : text)
extents.x += font->getGlyph(c)->advanceX;
extents.y = ptSize;
return extents;
}
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;
@@ -149,19 +141,14 @@ namespace JGL
extents += advance;
// Gives smaller results than we'd want.
if (extents.y < slot->metrics.height / 64) {
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

@@ -5,21 +5,20 @@
#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
#include FT_OUTLINE_H
#endif
#include <JGL/Font.h>
#include <JGL/FontCache.h>
#include <jlog/jlog.hpp>
namespace JGL {
FontCache fontCache; // <-- Not implemented yet
void PurgeFontCache() {
fontCache.purgeCache();
}
@@ -64,7 +63,7 @@ namespace JGL {
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
//final width and height of the texture_handle before we can construct it
//and subsequently upload the glyph data.
charcode = FT_Get_First_Char(font.face, &gindex);
@@ -118,11 +117,11 @@ namespace JGL {
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
//Texture parameters are restored when the texture_handle is bound
glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture());
std::vector<GLfloat> vertices;
std::vector<GLfloat> texcoords;
std::vector<std::array<GLfloat, 12>> vertices(text.size());
std::vector<std::array<GLfloat, 12>> texcoords(text.size());
for (int i = 0; i < text.length(); i++) {
float x2, y2, w, h;
@@ -145,23 +144,24 @@ namespace JGL {
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());
vertices[i] = glyph_vertices;
texcoords[i] = glyph_texcoords;
}
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);
glDrawArrays(GL_TRIANGLES, 0, vertices.size() * 6);
glBindTexture(GL_TEXTURE_2D, 0);
glColor4f(1, 1, 1, 1);
}
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) {
void J3D::DrawString(const Color4& 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;
@@ -169,9 +169,9 @@ namespace JGL {
float x = pos.x;
float y = pos.y;
float z = pos.z;
std::vector<GLuint> textures(text.length());;
std::vector<GLuint> textures(text.length());
glUseProgram(0); // Fixed-function pipeline.
glColor4f(color.r, color.g, color.b, 1.0f);
glColor4ubv(color.ptr());
//Font font;
//for (auto& f : Font::GetLoadedFonts())
@@ -244,8 +244,8 @@ namespace JGL {
for (unsigned int& texture : textures)
glDeleteTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture_handle
glColor4f(1, 1, 1, 1);
glPopMatrix();
}
}

163
src/JGL/Texture.cpp Normal file
View File

@@ -0,0 +1,163 @@
#include <JGL/Texture.h>
#include <iostream>
using namespace ReTexture;
namespace JGL
{
Texture::Texture(const std::string &file, const ReTexture::TextureFlag &flags, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode)
{
auto *t = new ReTexture::SoftwareTexture(file, flags);
load(t, {(float) t->getWidth(), (float) t->getHeight()}, t->getTextureFormat(), filtering_mode,
wrapping_mode);
texture_flags = flags;
delete t;
}
Texture::Texture(const std::string &file, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
auto *t = new SoftwareTexture(file);
load(t, {(float) t->getWidth(), (float) t->getHeight()}, t->getTextureFormat(), filtering_mode,
wrapping_mode);
texture_flags = TextureFlag::NONE;
delete t;
}
void Texture::load(SoftwareTexture *software_texture, const Vector2 &size, const TextureFormat &format,
TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
glGenTextures(1, &texture_handle);
glBindTexture(GL_TEXTURE_2D, texture_handle);
if (format == TextureFormat::RGBA)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int) size.x, (int) size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
software_texture->pixelData.data());
else if (format == TextureFormat::RGB)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (int) size.x, (int) size.y, 0, GL_RGB, GL_UNSIGNED_BYTE,
software_texture->pixelData.data());
if (wrapping_mode == TextureWrappingMode::CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
else if (wrapping_mode == TextureWrappingMode::REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
else if (wrapping_mode == TextureWrappingMode::MIRRORED_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
else if (wrapping_mode == TextureWrappingMode::CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
if (filtering_mode == TextureFilteringMode::NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
else if (filtering_mode == TextureFilteringMode::BILINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
else if (filtering_mode == TextureFilteringMode::MIPMAP_NEAREST ||
filtering_mode == TextureFilteringMode::MIPMAP_BILINEAR ||
filtering_mode == TextureFilteringMode::MIPMAP_TRILINEAR) {
//3 mipmap levels.
auto *m1 = new SoftwareTexture(software_texture->downscale(2));
auto *m2 = new SoftwareTexture(software_texture->downscale(4));
auto *m3 = new SoftwareTexture(software_texture->downscale(8));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
if (format == TextureFormat::RGBA) {
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, m1->getWidth(), m1->getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, m1->pixelData.data());
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA, m2->getWidth(), m2->getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, m2->pixelData.data());
glTexImage2D(GL_TEXTURE_2D, 3, GL_RGBA, m3->getWidth(), m3->getHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, m3->pixelData.data());
} else if (format == TextureFormat::RGB) {
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, m1->getWidth(), m1->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
m1->pixelData.data());
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, m2->getWidth(), m2->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
m2->pixelData.data());
glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, m3->getWidth(), m3->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
m3->pixelData.data());
}
if (filtering_mode == TextureFilteringMode::MIPMAP_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
else if (filtering_mode == TextureFilteringMode::MIPMAP_BILINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
else if (filtering_mode == TextureFilteringMode::MIPMAP_TRILINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR),
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete m1;
delete m2;
delete m3;
}
glBindTexture(GL_TEXTURE_2D, 0);
texture_size = size;
texture_format = format;
texture_filtering_mode = filtering_mode;
}
std::vector<Color4> JGL::Texture::GetPixelData() const {
std::vector<Color4> result((size_t) (texture_size.x * texture_size.y));
glBindTexture(GL_TEXTURE_2D, texture_handle);
if (texture_format == TextureFormat::RGBA) {
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, result.data());
return result;
}
//if RGB
std::vector<Color3> color3((size_t) (texture_size.x * texture_size.y));
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, color3.data());
for (const auto &c: color3)
result.emplace_back(c);
return result;
}
void Texture::Erase() {
if (texture_handle != 0)
glDeleteTextures(1, &texture_handle);
}
GLuint Texture::GetGLTextureHandle() const {
return texture_handle;
}
Vector2 Texture::GetDimensions() const {
return texture_size;
}
TextureFlag Texture::GetFlags() const {
return texture_flags;
}
TextureFormat Texture::GetFormat() const {
return texture_format;
}
TextureFilteringMode Texture::GetFilteringMode() const {
return texture_filtering_mode;
}
void Texture::SetTextureHandle(GLuint handle) {
texture_handle = handle;
}
}