Compare commits
44 Commits
Prerelease
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
b86377a092 | |||
5fc4914180 | |||
58e432b9c3 | |||
41fa634da1 | |||
9d6d256e80 | |||
0e22bc721a | |||
881d031f3c | |||
9e3e0c949f | |||
1526a101c3 | |||
f6e8875eb9 | |||
9903fc19c9 | |||
e809b1b665 | |||
d15b3f660d | |||
ff2a8ab787 | |||
55160044b6 | |||
04a4cbd54d | |||
f96a3851a1 | |||
4996288163 | |||
f3c2fd5e93 | |||
6650af4fc4 | |||
1d8823b046 | |||
c0b65818c8 | |||
32de87229e | |||
3759affa5d | |||
8ce9a08951 | |||
ec964dbee1 | |||
91baa826fa | |||
c2446763ad | |||
6286b62998 | |||
4de44ba1e2 | |||
166db43f2e | |||
2a1085df2d | |||
240fa7ddbc | |||
d89f79e70e | |||
7bf30f6f39 | |||
2a70981bf0 | |||
0147245325 | |||
15dcb79479 | |||
50895153f5 | |||
9688854533 | |||
0005c036b4 | |||
af42d8fd25 | |||
ff4d014739 | |||
9dda4bebc5 |
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.18)
|
cmake_minimum_required(VERSION 3.18..3.27)
|
||||||
project(JGL
|
project(JGL
|
||||||
VERSION 1.0
|
VERSION 1.0
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
@@ -14,19 +14,25 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
|
|||||||
# Enable Package Managers
|
# Enable Package Managers
|
||||||
include(cmake/CPM.cmake)
|
include(cmake/CPM.cmake)
|
||||||
|
|
||||||
|
|
||||||
|
CPMAddPackage(
|
||||||
|
NAME mcolor
|
||||||
|
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-4.zip
|
||||||
|
)
|
||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME J3ML
|
NAME J3ML
|
||||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-2.2.zip
|
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.1.zip
|
||||||
)
|
)
|
||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME ReWindow
|
NAME ReWindow
|
||||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-3.zip
|
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-15.zip
|
||||||
)
|
)
|
||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME GLAD
|
NAME GLAD
|
||||||
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_mt.zip
|
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_fbo.zip
|
||||||
)
|
)
|
||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
@@ -41,7 +47,7 @@ CPMAddPackage(
|
|||||||
|
|
||||||
CPMAddPackage(
|
CPMAddPackage(
|
||||||
NAME ReTexture
|
NAME ReTexture
|
||||||
URL https://git.redacted.cc/Redacted/ReTexture/archive/Release-1.zip
|
URL https://git.redacted.cc/Redacted/ReTexture/archive/Release-1.2.zip
|
||||||
)
|
)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
@@ -74,15 +80,16 @@ endif()
|
|||||||
|
|
||||||
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
|
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
|
||||||
|
|
||||||
#Don't expose this one because it's only to be used in the demo program.
|
#Don't expose these ones.
|
||||||
include_directories(${ReTexture_SOURCE_DIR}/include)
|
include_directories(${ReWindow_SOURCE_DIR}/include)
|
||||||
|
|
||||||
target_include_directories(JGL PUBLIC
|
target_include_directories(JGL PUBLIC
|
||||||
${PROJECT_SOURCE_DIR}/include
|
${PROJECT_SOURCE_DIR}/include
|
||||||
${OPENGL_INCLUDE_DIRS}
|
${OPENGL_INCLUDE_DIRS}
|
||||||
|
${ReTexture_SOURCE_DIR}/include
|
||||||
|
${mcolor_SOURCE_DIR}/include
|
||||||
${J3ML_SOURCE_DIR}/include
|
${J3ML_SOURCE_DIR}/include
|
||||||
${Event_SOURCE_DIR}/include
|
${Event_SOURCE_DIR}/include
|
||||||
${ReWindow_SOURCE_DIR}/include
|
|
||||||
${glad_SOURCE_DIR}/include
|
${glad_SOURCE_DIR}/include
|
||||||
${jlog_SOURCE_DIR}/include
|
${jlog_SOURCE_DIR}/include
|
||||||
)
|
)
|
||||||
@@ -93,13 +100,13 @@ add_executable(JGL_Demo main.cpp)
|
|||||||
if (UNIX AND NOT APPLE)
|
if (UNIX AND NOT APPLE)
|
||||||
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
|
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
|
||||||
target_link_libraries(JGL PRIVATE ${FREETYPE_LIBRARIES})
|
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 glad jlog Event ReTexture)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
target_include_directories(JGL PRIVATE ${freetype_SOURCE_DIR}/include)
|
target_include_directories(JGL PRIVATE ${freetype_SOURCE_DIR}/include)
|
||||||
target_link_libraries(JGL PRIVATE freetype)
|
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 glad jlog Event ReTexture)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(JGL_Demo PUBLIC JGL ReTexture)
|
target_link_libraries(JGL_Demo PUBLIC JGL ReWindowLibrary)
|
||||||
|
@@ -18,7 +18,7 @@ Yet Another C++ Rendering Toolkit
|
|||||||
## API Overview
|
## API Overview
|
||||||
|
|
||||||
### J2D
|
### J2D
|
||||||
* DrawPixel
|
* DrawPoint
|
||||||
* DrawLine / DrawGradientLine
|
* DrawLine / DrawGradientLine
|
||||||
* DrawSprite
|
* DrawSprite
|
||||||
* OutlineRect / FillRect / FillGradientRect / FillRoundedRect
|
* OutlineRect / FillRect / FillGradientRect / FillRoundedRect
|
||||||
|
5
assets/shader_programs/test_fragment.glsl
Normal file
5
assets/shader_programs/test_fragment.glsl
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#version 120
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_FragColor = vec4(1, 1, 1, 1);
|
||||||
|
}
|
7
assets/shader_programs/test_vertex.glsl
Normal file
7
assets/shader_programs/test_vertex.glsl
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#version 120
|
||||||
|
|
||||||
|
attribute vec2 position;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(position.x, position.y, 1.0, 1.0);
|
||||||
|
}
|
@@ -1,40 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <J3ML/J3ML.h>
|
|
||||||
|
|
||||||
namespace JGL
|
|
||||||
{
|
|
||||||
using namespace J3ML;
|
|
||||||
/// Represents a 3-channel color value, with Red, Green and Blue components.
|
|
||||||
class Color3 {
|
|
||||||
public:
|
|
||||||
u8 r;
|
|
||||||
u8 g;
|
|
||||||
u8 b;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/// Explicitly constructs a Color3 from the given Red, Green, and Blue values.
|
|
||||||
Color3(u8 R, u8 G, u8 B);
|
|
||||||
/// Returns a Color3 parsed from the given hexadecimal string.
|
|
||||||
static Color3 FromHex(const std::string& hexCode);
|
|
||||||
public:
|
|
||||||
/// Returns a Color3 that is somewhere in-between this and the given Color3, determined by the alpha [0-1].
|
|
||||||
Color3 Lerp(const Color3& rhs, float alpha) const;
|
|
||||||
|
|
||||||
/// Returns the red channel [0-255].
|
|
||||||
u8 RedChannel () const;
|
|
||||||
/// Returns the green channel [0-255].
|
|
||||||
u8 GreenChannel() const;
|
|
||||||
/// Returns the blue channel [0-255].
|
|
||||||
u8 BlueChannel () const;
|
|
||||||
/// Returns the red channel normalized from [0-255] to [0-1].
|
|
||||||
float RedChannelNormalized () const;
|
|
||||||
/// Returns the green channel normalized from [0-255] to [0-1].
|
|
||||||
float GreenChannelNormalized() const;
|
|
||||||
/// Returns the blue channel normalized from [0-255] to [0-1].
|
|
||||||
float BlueChannelNormalized() const;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@@ -1,32 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <JGL/Color3.h>
|
|
||||||
|
|
||||||
namespace JGL
|
|
||||||
{
|
|
||||||
|
|
||||||
/// Represents a 4-channel color value, with Red, Green, Blue, and Alpha components.
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
}
|
|
@@ -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};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -13,161 +13,160 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <Color4.hpp>
|
||||||
#include <JGL/Color4.h>
|
#include <JGL/types/Texture.h>
|
||||||
#include <JGL/enums.h>
|
#include <JGL/types/Enums.h>
|
||||||
#include <JGL/FontCache.h>
|
#include <JGL/types/FontCache.h>
|
||||||
#include <JGL/Font.h>
|
#include <JGL/types/Font.h>
|
||||||
#include <J3ML/LinearAlgebra.h>
|
#include <JGL/types/RenderTarget.h>
|
||||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
#include <J3ML/LinearAlgebra.hpp>
|
||||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||||
#include <J3ML/Geometry/Sphere.h>
|
#include <J3ML/LinearAlgebra/Vector3.hpp>
|
||||||
#include <J3ML/Geometry/Capsule.h>
|
#include <J3ML/Geometry/Sphere.hpp>
|
||||||
#include <J3ML/Geometry/TriangleMesh.h>
|
#include <J3ML/Geometry/Capsule.hpp>
|
||||||
|
#include <J3ML/Geometry/Triangle2D.hpp>
|
||||||
|
|
||||||
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
|
/// OpenGL Wrapper for rendering 2D & 3D graphics in both a 2D and 3D context.
|
||||||
namespace JGL {
|
namespace JGL {
|
||||||
|
|
||||||
using namespace J3ML::LinearAlgebra;
|
using namespace J3ML::LinearAlgebra;
|
||||||
using namespace J3ML::Geometry;
|
using namespace J3ML::Geometry;
|
||||||
|
|
||||||
/// TODO:
|
|
||||||
struct HSV {
|
|
||||||
float hue;
|
|
||||||
float saturation;
|
|
||||||
float value;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Triangle2D
|
|
||||||
{
|
|
||||||
Vector2 A;
|
|
||||||
Vector2 B;
|
|
||||||
Vector2 C;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Triangle3D
|
|
||||||
{
|
|
||||||
Vector3 A;
|
|
||||||
Vector3 B;
|
|
||||||
Vector3 C;
|
|
||||||
};
|
|
||||||
|
|
||||||
void Update(const Vector2& window_size);
|
void Update(const Vector2& window_size);
|
||||||
//bool InitTextEngine();
|
inline void PurgeFontCache() { fontCache.purgeCache(); }
|
||||||
Font LoadFont(const std::string& font_path); // TODO: Fully deprecate
|
std::vector<GLfloat> OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far);
|
||||||
void PurgeFontCache();
|
|
||||||
|
|
||||||
void SetActiveFont(const Font& font); // TODO: Implement
|
|
||||||
// TODO: implement correct coloring
|
|
||||||
|
|
||||||
/// Drawing functions for primitive 2D Shapes.
|
/// Drawing functions for primitive 2D Shapes.
|
||||||
/// Each function is overloaded with Color3 and Color4 for optional transparency.
|
|
||||||
namespace J2D {
|
namespace J2D {
|
||||||
|
/// Open a 2-D rendering context with the underlying graphics system (In this case& by default OpenGL).
|
||||||
/// 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.
|
/// @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.
|
/// 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.
|
/// This keeps our code from, say, clobbering the OpenGL rendering context driving 3D content in between our calls.
|
||||||
void Begin();
|
void Begin(RenderTarget* render_target = nullptr, bool clear_buffers = false);
|
||||||
/// Closes a 2-D rendering context with the underlying graphics system (In this case & by default OpenGL).
|
/// Closes a 2-D rendering context with the underlying graphics system (In this case& by default OpenGL).
|
||||||
/// @see Begin().
|
/// @see Begin().
|
||||||
void End();
|
void End();
|
||||||
|
|
||||||
/// Plots a single pixel on the screen.
|
/// 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.
|
/// @param coordinates The pixel-point on-screen at which to plot the pixel.
|
||||||
void DrawPixel(const Color3& color, const Vector2& coordinates);
|
void DrawPoint(const Color4& color, const Vector2& coordinates, float radius = 1.f);
|
||||||
void DrawPixel(const Color3& color, float x, float y);
|
void DrawPoint(const Color4& color, float x, float y, float radius = 1.f);
|
||||||
void DrawPixel(const Color4& color, const Vector2& coordinates);
|
|
||||||
void DrawPixel(const Color4& color, float x, float y);
|
|
||||||
|
|
||||||
/// Plots a line (segment) on the screen.
|
/// Plots a line (segment) 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 classes Color3, Color4.
|
||||||
/// @param A The starting point of the line segment.
|
/// @param A The starting point of the line segment.
|
||||||
/// @param B The end point of the line segment.
|
/// @param B The end point of the line segment.
|
||||||
/// @param thickness The width at which to render the line.
|
/// @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, 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);
|
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.
|
///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 Color4& color_a, const Color4& color_b, 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& color_a, const Color4& color_b, float x, float y, float w, float h, 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.
|
/// 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 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, u8 opacity = 255, Inversion::Inversion inversion = Inversion::None);
|
|
||||||
void DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity = 255, Inversion::Inversion inversion = Inversion::None);
|
|
||||||
|
|
||||||
///Draws a non axis-aligned fill rect to the screen.
|
|
||||||
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.
|
|
||||||
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.
|
/// Draws a filled rectangle on the screen.
|
||||||
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
|
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.
|
/// Draws a filled rectangle where the color transitions across it.
|
||||||
void FillGradientRect(const Color4& color1, const Color4& color2, const Gradient::Gradient& gradient, const Vector2& pos, const Vector2& size);
|
void FillGradientRect(const Color4& color1, const Color4& color2, const Direction& gradient, const Vector2& pos, const Vector2& size);
|
||||||
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient::Gradient& gradient, const Vector2& pos, const Vector2& size);
|
|
||||||
|
|
||||||
/// Draws a filled rectangle with rounded corners on the screen.
|
/// 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 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);
|
|
||||||
|
void DrawRenderTargetAsSprite(const RenderTarget& render_target, const Vector2& position, float rad_rotation = 0, const Vector2& origin = Vector2(0 , 0),
|
||||||
|
const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White, Direction inversion = Direction::None);
|
||||||
|
/// Draws a sprite to the screen by passing a G̶L̶u̶i̶n̶t̶ JGL Texture 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 rad_rotation A float representing the rotation of the sprite where 0 is no rotation and 1 is the maximum rotation (would look the same as 0).
|
||||||
|
/// @param color A 32-bit RGBA value represented as four unsigned 8-bit integers.
|
||||||
|
/// @param inversion inverts the texture only.
|
||||||
|
/// @see class Texture
|
||||||
|
void DrawSprite(const Texture& texture, const Vector2& position, float rad_rotation = 0, const Vector2& origin = Vector2(0,0),
|
||||||
|
const Vector2& scale = Vector2(1,1), const Color4& color = Colors::White, Direction inversion = Direction::None);
|
||||||
|
void DrawSprite(const Texture& texture,
|
||||||
|
float positionX, float positionY,
|
||||||
|
float rad_rotation = 0,
|
||||||
|
float originX = 0, float originY = 0,
|
||||||
|
float scaleX = 1, float scaleY = 1,
|
||||||
|
const Color4& color = Colors::White,
|
||||||
|
Direction inversion = Direction::None);
|
||||||
|
|
||||||
|
/// Draws a piece of a sprite to the screen, similar to DrawSprite.
|
||||||
|
/// @param texture
|
||||||
|
/// @param position
|
||||||
|
/// @param sub_texture_position The top left corner of the sub-texture to be drawn.
|
||||||
|
/// @param sub_texture_size The size of the sub-texture in px.
|
||||||
|
/// @param origin
|
||||||
|
/// @param scale
|
||||||
|
/// @param color
|
||||||
|
/// @param inversion
|
||||||
|
void DrawPartialSprite(const Texture& texture, const Vector2& position, const Vector2& sub_texture_position, const Vector2& sub_texture_size, float rad_rotation = 0,
|
||||||
|
const Vector2& origin = Vector2(0,0), const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White, Direction inversion = Direction::None);
|
||||||
|
void DrawPartialSprite(const Texture& texture, float positionX, float positionY, float sub_texture_positionX, float sub_texture_positionY, unsigned int sub_texture_sizeX, unsigned int sub_texture_sizeY,
|
||||||
|
float rad_rotation = 0, float originX = 0, float originY = 0, float scaleX = 1, float scaleY = 1, const Color4& color = Colors::White, Direction inversion = Direction::None);
|
||||||
|
|
||||||
|
/// To save v-ram, Use if a sprite would be identical if mirrored horizontally, vertically, or both. For example, a circle.
|
||||||
|
/// Assumes the input texture is the top left quadrant. You can use "SoftwareTexture" to invert it correctly so that's the case.
|
||||||
|
/// @param texture
|
||||||
|
/// @param position
|
||||||
|
/// @param mirror_axis The axes to mirror across, Vertical and Horizontal or both only.
|
||||||
|
/// @param rad_rotation The rotation of the final result.
|
||||||
|
/// @param origin The point at which transformations are done about.
|
||||||
|
/// @param scale
|
||||||
|
/// @param color
|
||||||
|
void DrawMirrorSprite(const Texture& texture, const Vector2& position, Direction mirror_axis = Direction::Horizontal | Direction::Vertical, float rad_rotation = 0, const Vector2& origin = Vector2(0,0), const Vector2& scale = Vector2(1,1), const Color4& color = Colors::White);
|
||||||
|
|
||||||
/// Draws an outline of a circle on the screen.
|
/// 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 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.
|
/// Draws a filled circle on the screen.
|
||||||
void FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
|
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.
|
/// 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 Color4& color, const Triangle2D& tri, float thickness = 1);
|
||||||
void OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness = 1);
|
void OutlineTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC, float thickness = 1);
|
||||||
// TODO: Implement an overload that simply takes 3 Vector3's
|
|
||||||
|
|
||||||
/// Draws a filled triangle on the screen.
|
/// 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 Color3& color, const Triangle2D& tri);
|
void FIllTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC);
|
||||||
|
|
||||||
/// Draws a triangle where each corner is defined by a given color, Smoothly transitioning between them.
|
/// 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 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
|
|
||||||
|
|
||||||
|
void DrawCubicBezierCurve(const Color4& color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
|
||||||
|
int subdivisions = 10, float thickness = 1);
|
||||||
|
|
||||||
|
/// Draws a series of points where the last point always connects to the first point.
|
||||||
|
void OutlinePolygon(const Color4& color, const std::vector<Vector2>& points, float thickness = 1);
|
||||||
|
|
||||||
/// Draws a text string on the screen with a given point-size and font.
|
/// 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 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:
|
/// TODO Implement the following. These ones are going to be extremely annoying.
|
||||||
void FillTexturedTriangle();
|
void FillPolygon(const Color4& color, const std::vector<Vector2>& points);
|
||||||
void FillTexturedPolygon();
|
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);
|
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1);
|
||||||
|
void FillTexturedTriangle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Drawing functions for primitive 3D Shapes.
|
||||||
namespace J3D {
|
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 Begin();
|
||||||
void End();
|
void End();
|
||||||
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
|
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 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 FillSphere(const Color3& color, const Sphere& sphere);
|
||||||
void WireframeSphere(const Color3& color, const Sphere& sphere, float thickness = 1);
|
void WireframeSphere(const Color3& color, const Sphere& sphere, float thickness = 1);
|
||||||
void FillOBB(const Color3& color, const OBB& obb);
|
void FillOBB(const Color3& color, const OBB& obb);
|
||||||
@@ -176,8 +175,8 @@ namespace JGL {
|
|||||||
void WireframeCapsule(const Color3& color, const Capsule& cap, float thickness = 1);
|
void WireframeCapsule(const Color3& color, const Capsule& cap, float thickness = 1);
|
||||||
void FillTriangleMesh(const Color3& color, const TriangleMesh& mesh);
|
void FillTriangleMesh(const Color3& color, const TriangleMesh& mesh);
|
||||||
void WireframeTriangleMesh(const Color3& color, const TriangleMesh& mesh, float thickness = 1);
|
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, float scale, u32 size, const Font& font, const EulerAngle& angle = {0, 0, 0}, bool draw_back_face = false);
|
||||||
|
void DrawSprite();
|
||||||
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
|
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
|
||||||
void DrawMatrixGizmo (const Matrix4x4&);
|
void DrawMatrixGizmo (const Matrix4x4&);
|
||||||
void DrawAxisAngleGizmo (const AxisAngle&, const Vector3&);
|
void DrawAxisAngleGizmo (const AxisAngle&, const Vector3&);
|
||||||
|
@@ -1,26 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
namespace JGL::Inversion {
|
|
||||||
enum Inversion {
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace JGL::Gradient {
|
|
||||||
enum Gradient {
|
|
||||||
Vertical = 0,
|
|
||||||
Horizontal = 1,
|
|
||||||
DiagonalTopLeft = 2,
|
|
||||||
DiagonalBottomLeft = 3
|
|
||||||
};
|
|
||||||
|
|
||||||
inline Gradient operator|(Gradient a, Gradient b) {
|
|
||||||
return static_cast<Gradient>(static_cast<int>(a) | static_cast<int>(b));
|
|
||||||
}
|
|
||||||
}
|
|
12
include/JGL/logger/logger.h
Normal file
12
include/JGL/logger/logger.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <jlog/Logger.hpp>
|
||||||
|
|
||||||
|
namespace JGL::Logger {
|
||||||
|
using namespace jlog;
|
||||||
|
|
||||||
|
extern GenericLogger Fatal;
|
||||||
|
extern GenericLogger Debug;
|
||||||
|
extern GenericLogger Error;
|
||||||
|
|
||||||
|
}
|
36
include/JGL/types/Enums.h
Normal file
36
include/JGL/types/Enums.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace JGL {
|
||||||
|
enum class Direction : u8 {
|
||||||
|
None = 0,
|
||||||
|
Vertical = 1,
|
||||||
|
Horizontal = 2,
|
||||||
|
Diagonal_NWSE = 3, // North West -> South East.
|
||||||
|
Diagonal_SWNE = 4 // South West -> North East.
|
||||||
|
};
|
||||||
|
|
||||||
|
inline Direction operator|(Direction a, Direction b) {
|
||||||
|
return static_cast<Direction>(static_cast<int>(a) | static_cast<int>(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator&(Direction a, Direction b) {
|
||||||
|
return (u8)a & (u8)b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string to_string(JGL::Direction direction) {
|
||||||
|
switch (direction) {
|
||||||
|
case JGL::Direction::None:
|
||||||
|
return "None";
|
||||||
|
case JGL::Direction::Vertical:
|
||||||
|
return "Vertical";
|
||||||
|
case JGL::Direction::Horizontal:
|
||||||
|
return "Horizontal";
|
||||||
|
case JGL::Direction::Diagonal_NWSE:
|
||||||
|
return "Diagonal_NWSE";
|
||||||
|
case JGL::Direction::Diagonal_SWNE:
|
||||||
|
return "Diagonal_SWNE";
|
||||||
|
default:
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <J3ML/LinearAlgebra.h>
|
#include <J3ML/LinearAlgebra.hpp>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ namespace JGL
|
|||||||
/// @param text The string to measure.
|
/// @param text The string to measure.
|
||||||
/// @param ptSize The font size at which to measure.
|
/// @param ptSize The font size at which to measure.
|
||||||
/// @return The size-in-pixels that would contain the entire text.
|
/// @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:
|
public:
|
||||||
int index = 0;
|
int index = 0;
|
||||||
FT_Face face;
|
FT_Face face;
|
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <array>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
|
||||||
/// TODO: FontCache mechanism works amazing, but makes no fucking sense
|
/// TODO: FontCache mechanism works amazing, but makes no fucking sense
|
||||||
@@ -27,13 +27,13 @@ public:
|
|||||||
//CachedGlyph(GLuint texture_id, char c);
|
//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);
|
CachedGlyph(char c, std::array<GLfloat, 12> texcoords, float x2o, float y2o, float w, float h, float advX, float advY);
|
||||||
char getCharacter();
|
char getCharacter();
|
||||||
const std::array<GLfloat, 12> getTexCoords() const;
|
[[nodiscard]] std::array<GLfloat, 12> getTexCoords() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents a Font object as it exists in the font-cache.
|
/// Represents a Font object as it exists in the font-cache.
|
||||||
class JGL::CachedFont {
|
class JGL::CachedFont {
|
||||||
private:
|
private:
|
||||||
std::map<char, CachedGlyph*> glyphs;
|
std::unordered_map<char, CachedGlyph*> glyphs;
|
||||||
GLuint texture = 0;
|
GLuint texture = 0;
|
||||||
GLsizei texture_width = 0, texture_height = 0;
|
GLsizei texture_width = 0, texture_height = 0;
|
||||||
unsigned int font_size = 0;
|
unsigned int font_size = 0;
|
||||||
@@ -43,16 +43,16 @@ public:
|
|||||||
unsigned int getFontSize();
|
unsigned int getFontSize();
|
||||||
unsigned int getFontIndex();
|
unsigned int getFontIndex();
|
||||||
CachedGlyph* getGlyph(char c);
|
CachedGlyph* getGlyph(char c);
|
||||||
std::map<char, CachedGlyph*> getGlyphs();
|
std::unordered_map<char, CachedGlyph*> getGlyphs();
|
||||||
const GLuint* getTexture();
|
const GLuint* getTexture();
|
||||||
GLsizei getTextureWidth() const;
|
[[nodiscard]] GLsizei getTextureWidth() const;
|
||||||
GLsizei getTextureHeight() const;
|
[[nodiscard]] GLsizei getTextureHeight() const;
|
||||||
CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index);
|
CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index);
|
||||||
};
|
};
|
||||||
|
|
||||||
class JGL::FontCache {
|
class JGL::FontCache {
|
||||||
private:
|
private:
|
||||||
std::vector<CachedFont*> cachedFonts = {};
|
std::vector<CachedFont*> cachedFonts{};
|
||||||
public:
|
public:
|
||||||
std::vector<CachedFont*> getFonts();
|
std::vector<CachedFont*> getFonts();
|
||||||
CachedFont* getFont(unsigned int font_size, unsigned int font_index);
|
CachedFont* getFont(unsigned int font_size, unsigned int font_index);
|
||||||
@@ -61,3 +61,7 @@ public:
|
|||||||
void eraseFont(CachedFont* font);
|
void eraseFont(CachedFont* font);
|
||||||
void purgeCache();
|
void purgeCache();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace JGL {
|
||||||
|
inline FontCache fontCache;
|
||||||
|
}
|
37
include/JGL/types/RenderTarget.h
Normal file
37
include/JGL/types/RenderTarget.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <JGL/types/Texture.h>
|
||||||
|
#include <Color4.hpp>
|
||||||
|
#include <Colors.hpp>
|
||||||
|
|
||||||
|
namespace JGL {
|
||||||
|
class RenderTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
class JGL::RenderTarget {
|
||||||
|
private:
|
||||||
|
Color4 clear_color{0,0,0,0};
|
||||||
|
/// "Size" in this sense is the "Renderable Area" because OpenGL textures behave strangely if they're not square.
|
||||||
|
Vector2 size{0, 0};
|
||||||
|
bool using_depth = false;
|
||||||
|
GLuint framebuffer_object = 0;
|
||||||
|
GLuint depth_buffer = 0;
|
||||||
|
Texture* texture = nullptr;
|
||||||
|
public:
|
||||||
|
static GLuint GetActiveGLFramebufferHandle();
|
||||||
|
static void SetActiveGLRenderTarget(const RenderTarget& render_target);
|
||||||
|
public:
|
||||||
|
[[nodiscard]] Vector2 GetDimensions() const;
|
||||||
|
[[nodiscard]] Texture* GetJGLTexture() const;
|
||||||
|
[[nodiscard]] GLuint GetGLTextureHandle() const;
|
||||||
|
[[nodiscard]] GLuint GetGLFramebufferObjectHandle() const;
|
||||||
|
[[nodiscard]] GLuint GetGLDepthBufferHandle() const;
|
||||||
|
[[nodiscard]] Color4 GetClearColor() const;
|
||||||
|
public:
|
||||||
|
/// Create a render target for a texture that already exists. For decals.
|
||||||
|
explicit RenderTarget(const Texture& texture, const Color4& clear_color = Colors::Black, bool use_depth = false);
|
||||||
|
/// Create a Render Target with a brand new texture. Want to render JGL elements onto a texture and display it as a sprite?
|
||||||
|
explicit RenderTarget(const Vector2& size, const Color4& clear_color = Colors::Black, bool use_depth = false);
|
||||||
|
|
||||||
|
void Erase();
|
||||||
|
};
|
57
include/JGL/types/Texture.h
Normal file
57
include/JGL/types/Texture.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#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;
|
||||||
|
void load(SoftwareTexture* software_texture, const Vector2& size, const TextureFormat& format, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode);
|
||||||
|
public:
|
||||||
|
///Load a texture from a file,
|
||||||
|
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);
|
||||||
|
/* Initialize a texture filled with trash data
|
||||||
|
this is primarily for the RenderTarget */
|
||||||
|
explicit Texture(const Vector2& size);
|
||||||
|
Texture() = default;
|
||||||
|
public:
|
||||||
|
[[nodiscard]] GLuint GetGLTextureHandle() const;
|
||||||
|
[[nodiscard]] Vector2 GetDimensions() const;
|
||||||
|
[[nodiscard]] TextureFilteringMode GetFilteringMode() const;
|
||||||
|
[[nodiscard]] TextureWrappingMode GetWrappingMode() const;
|
||||||
|
[[nodiscard]] TextureFlag GetFlags() const;
|
||||||
|
[[nodiscard]] TextureFormat GetFormat() const;
|
||||||
|
[[nodiscard]] std::vector<Color4> GetPixelData() const;
|
||||||
|
|
||||||
|
void SetTextureHandle(GLuint handle);
|
||||||
|
void Erase();
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
68
include/JGL/types/VRamList.h
Normal file
68
include/JGL/types/VRamList.h
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||||
|
#include <J3ML/LinearAlgebra/Vector3.hpp>
|
||||||
|
#include <J3ML/LinearAlgebra/Vector4.hpp>
|
||||||
|
#include <JGL/logger/logger.h>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace JGL {
|
||||||
|
class VRamList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper for VBO, Storing texture coordinates or vertices or indices in vram.
|
||||||
|
class JGL::VRamList {
|
||||||
|
private:
|
||||||
|
GLuint list_handle = 0;
|
||||||
|
long size = 0;
|
||||||
|
bool element_array_buffer = false;
|
||||||
|
|
||||||
|
void load(const GLfloat* data, const long& size);
|
||||||
|
void load(const GLuint* data, const long& size);
|
||||||
|
public:
|
||||||
|
VRamList() = default;
|
||||||
|
VRamList(const GLfloat* data, const long& size);
|
||||||
|
VRamList(Vector2* data, const long& size);
|
||||||
|
VRamList(Vector3* data, const long& size);
|
||||||
|
VRamList(Vector4* data, const long& size);
|
||||||
|
VRamList(const GLuint* data, const long& size);
|
||||||
|
public:
|
||||||
|
[[nodiscard]] GLuint GetHandle() const;
|
||||||
|
/// Returns the number of GLfloat or GLuint in the list.
|
||||||
|
[[nodiscard]] long GetSize() const;
|
||||||
|
/// Returns the size of the data in bytes.
|
||||||
|
[[nodiscard]] long GetDataSize() const;
|
||||||
|
[[nodiscard]] bool IsIntegerArray() const;
|
||||||
|
[[nodiscard]] bool IsFloatArray() const;
|
||||||
|
void Erase();
|
||||||
|
|
||||||
|
/// Get list data back from the GPU. This is *very* slow.
|
||||||
|
/// It's not recommended you use this in your normal rendering routines.
|
||||||
|
template <typename T>
|
||||||
|
[[nodiscard]] std::vector<T> GetListData() const {
|
||||||
|
GLenum buffer_type;
|
||||||
|
GLint current_buffer = 0;
|
||||||
|
|
||||||
|
if constexpr (std::is_same<T, GLfloat>::value)
|
||||||
|
buffer_type = GL_ARRAY_BUFFER,
|
||||||
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, ¤t_buffer);
|
||||||
|
else if constexpr (std::is_same<T, GLuint>::value)
|
||||||
|
buffer_type = GL_ELEMENT_ARRAY_BUFFER,
|
||||||
|
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤t_buffer);
|
||||||
|
else {
|
||||||
|
Logger::Fatal("Typename T must be either GLfloat or GLuint.");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glBindBuffer(buffer_type, list_handle);
|
||||||
|
std::vector<T> data(size);
|
||||||
|
memcpy(data.data(), (T*) glMapBuffer(buffer_type, GL_READ_ONLY), size * sizeof(T));
|
||||||
|
glUnmapBuffer(buffer_type);
|
||||||
|
glBindBuffer(buffer_type, current_buffer);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
};
|
209
main.cpp
209
main.cpp
@@ -1,29 +1,52 @@
|
|||||||
|
|
||||||
#include <JGL/JGL.h>
|
#include <JGL/JGL.h>
|
||||||
#include <rewindow/types/window.h>
|
#include <rewindow/types/window.h>
|
||||||
#include <JGL/Colors.h>
|
#include <Colors.hpp>
|
||||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
#include <chrono>
|
||||||
#include <JGL/Font.h>
|
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||||
#include <jlog/jlog.hpp>
|
|
||||||
#include <ReTexture/Texture.h>
|
|
||||||
|
|
||||||
using J3ML::LinearAlgebra::Vector2;
|
using J3ML::LinearAlgebra::Vector2;
|
||||||
using namespace JGL;
|
using namespace JGL;
|
||||||
using namespace ReTexture;
|
|
||||||
|
|
||||||
Texture* image;
|
JGL::Font FreeSans;
|
||||||
GLuint imageID;
|
JGL::Font Jupiteroid;
|
||||||
//The Re3D style base projection.
|
|
||||||
std::vector<GLfloat> perspective(float fov, float aspect, float nearPlane, float farPlane) {
|
class Gizmo
|
||||||
std::vector<float> result(16);
|
{
|
||||||
float f = 1.0f / tan(fov * 0.5f * M_PI / 180.0f);
|
public:
|
||||||
result[0] = f / aspect;
|
Gizmo() {}
|
||||||
result[5] = f;
|
Gizmo(const Vector2& pos) : position(pos) {}
|
||||||
result[10] = (farPlane + nearPlane) / (nearPlane - farPlane);
|
bool dragging = false;
|
||||||
result[11] = -1.0f;
|
bool hovered = false;
|
||||||
result[14] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
|
Vector2 position;
|
||||||
return result;
|
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);
|
||||||
|
|
||||||
|
hovered = mouse.Distance(position) < range;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
public:
|
public:
|
||||||
@@ -63,94 +86,130 @@ struct point {
|
|||||||
GLfloat t;
|
GLfloat t;
|
||||||
};
|
};
|
||||||
|
|
||||||
JGL::Font FreeSans;
|
Gizmo a({250, 150});
|
||||||
JGL::Font Jupiteroid;
|
Gizmo b({200, 250});
|
||||||
|
Gizmo c({350, 300});
|
||||||
|
Gizmo d({450, 250});
|
||||||
|
|
||||||
|
Texture* image;
|
||||||
|
RenderTarget* j2d_render_target;
|
||||||
|
|
||||||
class JGLDemoWindow : public ReWindow::RWindow
|
class JGLDemoWindow : public ReWindow::RWindow
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void initGL() {
|
void initGL() {
|
||||||
camera = new Camera;
|
camera = new Camera;
|
||||||
auto window_size = getSize();
|
|
||||||
auto aspect = (float) window_size[0] / (float) window_size[1];
|
|
||||||
|
|
||||||
gladLoadGL();
|
gladLoadGL();
|
||||||
JGL::InitTextEngine();
|
JGL::InitTextEngine();
|
||||||
JGL::Update(getSize());
|
JGL::Update(getSize());
|
||||||
|
J3D::Init(getSize(), 90, 100);
|
||||||
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
|
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
|
||||||
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.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);
|
glClearColor(0.f, 0.f, 0.f, 0.f);
|
||||||
glViewport(0,0,window_size.x,window_size.y);
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LESS);
|
||||||
glDepthMask(GL_TRUE);
|
glDepthMask(GL_TRUE);
|
||||||
|
image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR);
|
||||||
image = new Texture("assets/sprites/Re3D.png");
|
j2d_render_target = new RenderTarget({500, 500}, {255,0,0,0});
|
||||||
glGenTextures(1, &imageID);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, imageID);
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (image->getTextureFormat() == TextureFormat::RGBA)
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->getWidth(), image->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixelData.data());
|
|
||||||
if (image->getTextureFormat() == TextureFormat::RGB)
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixelData.data());
|
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 textAngle = {0,0,0};
|
Vector3 textAngle = {0,0,0};
|
||||||
|
float fov = 90;
|
||||||
|
float sprite_radians = 0;
|
||||||
|
bool fov_increasing = true;
|
||||||
|
std::chrono::high_resolution_clock::time_point start;
|
||||||
|
float fps = 0.0f;
|
||||||
|
|
||||||
void display() {
|
void display() {
|
||||||
|
start = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
sprite_radians += 0.05;
|
||||||
textAngle.y += 2.0f;
|
textAngle.y += 2.0f;
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
camera->render();
|
camera->render();
|
||||||
|
// All 3D elements of the scene and JGL elements *must* be rendered before the 2D stuff
|
||||||
///All 3D elements of the scene and JGL elements *must* be rendered before the 2d stuff.
|
/* if rendering to screen space directly. */
|
||||||
|
|
||||||
J3D::Begin();
|
J3D::Begin();
|
||||||
J3D::DrawLine(JGL::Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
|
J3D::DrawLine(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::DrawLine(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::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f}, 1.f, 32, FreeSans, textAngle, true);
|
||||||
J3D::End();
|
J3D::End();
|
||||||
|
|
||||||
|
|
||||||
J2D::Begin();
|
J2D::Begin();
|
||||||
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
|
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
|
||||||
J2D::FillQuad(Colors::Red, {0, 52}, {100, 52}, {0, 152}, {100, 152});
|
J2D::DrawSprite(*image, {300, 300}, 0, {0,0}, {1, 1}, Colors::White, Direction::Vertical | Direction::Horizontal);
|
||||||
J2D::DrawSprite(imageID, {0, 52}, {(float) image->getWidth(), (float) image->getHeight()}, 128);
|
J2D::DrawMirrorSprite(*image, {400, 300}, Direction::Horizontal | Direction::Vertical, sprite_radians, {0.5,0.5}, {1, 1}, Colors::White);
|
||||||
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
|
J2D::DrawPartialSprite(*image, {225, 300}, image->GetDimensions() * 0.25, image->GetDimensions() * 0.75, sprite_radians, {0.5, 0.5});
|
||||||
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
|
J2D::FillRect(Colors::Pinks::HotPink, {68, 120}, {32, 32});
|
||||||
J2D::FillRoundedRect(JGL::Colors::Red, {200, 52}, {100, 100}, 8, 8);
|
J2D::FillGradientRect(Colors::Red, Colors::Blue, Direction::Diagonal_SWNE, {100,52}, {100,100});
|
||||||
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::FillCircle(Colors::White, {52, 204}, 50, 24);
|
||||||
J2D::OutlineCircle(JGL::Colors::White, {153, 204}, 50, 24);
|
J2D::OutlineCircle(Colors::White, {153, 204}, 50, 24);
|
||||||
|
|
||||||
//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::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::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);
|
auto result = Jupiteroid.MeasureString("Jupiteroid Font", 16);
|
||||||
|
|
||||||
J2D::FillRect(JGL::Colors::Gray, {0, 0}, result);
|
J2D::FillRect(Colors::Gray, {0, 0}, result);
|
||||||
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid);
|
J2D::DrawString(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(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::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::DrawString(Colors::White, "Framerate: " + std::to_string((int) fps), 0, 48, 1, 16, Jupiteroid);
|
||||||
|
J2D::OutlinePolygon(Colors::White, {{200, 400}, {220, 420}, {220, 430}, {230, 410}, {200, 400}});
|
||||||
|
//J2D::FillPolygon(Colors::White, {{200, 400}, {220, 420}, {220, 430}, {230, 410}, {200, 400}});
|
||||||
|
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();
|
J2D::End();
|
||||||
|
|
||||||
|
//Draw the Render Target that we just drew all that stuff onto.
|
||||||
|
/*
|
||||||
|
J2D::Begin();
|
||||||
|
J2D::DrawRenderTargetAsSprite(*j2d_render_target, {0, 0}, 0, {0.5, 0.5}, {1,1}, Colors::White);
|
||||||
|
J2D::End();
|
||||||
|
*/
|
||||||
|
|
||||||
|
std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<float> frame_time = stop - start;
|
||||||
|
fps = 1.0f / frame_time.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnRefresh(float elapsed) override {
|
void OnRefresh(float elapsed) override {
|
||||||
|
auto mouse = GetMouseCoordinates();
|
||||||
|
a.Update(mouse);
|
||||||
|
b.Update(mouse);
|
||||||
|
c.Update(mouse);
|
||||||
|
d.Update(mouse);
|
||||||
display();
|
display();
|
||||||
int glError = glGetError();
|
int glError = glGetError();
|
||||||
if (glError != GL_NO_ERROR)
|
if (glError != GL_NO_ERROR)
|
||||||
@@ -158,6 +217,25 @@ public:
|
|||||||
glSwapBuffers();
|
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;}
|
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {return true;}
|
||||||
JGLDemoWindow() : ReWindow::RWindow() {}
|
JGLDemoWindow() : ReWindow::RWindow() {}
|
||||||
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height){}
|
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height){}
|
||||||
@@ -168,7 +246,8 @@ int main(int argc, char** argv) {
|
|||||||
window->setRenderer(RenderingAPI::OPENGL);
|
window->setRenderer(RenderingAPI::OPENGL);
|
||||||
window->Open();
|
window->Open();
|
||||||
window->initGL();
|
window->initGL();
|
||||||
window->setResizable(false);
|
window->setResizable(true);
|
||||||
|
window->setVsyncEnabled(true);
|
||||||
|
|
||||||
while (window->isAlive()) {
|
while (window->isAlive()) {
|
||||||
window->pollEvents();
|
window->pollEvents();
|
||||||
|
668
src/JGL.cpp
668
src/JGL.cpp
@@ -4,11 +4,16 @@
|
|||||||
|
|
||||||
#include <JGL/JGL.h>
|
#include <JGL/JGL.h>
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <JGL/Color3.h>
|
#include <J3ML/Algorithm/Bezier.hpp>
|
||||||
#include <jlog/jlog.hpp>
|
#include <JGL/logger/logger.h>
|
||||||
|
|
||||||
|
JGL::RenderTarget* render_target = nullptr;
|
||||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||||
GLfloat baseColor[4] = {1, 1, 1, 1};
|
GLfloat baseColor[4] = {1, 1, 1, 1};
|
||||||
|
|
||||||
|
GLuint current_fbo = 0;
|
||||||
|
GLint viewport[4] = {0, 0, 0, 0};
|
||||||
|
|
||||||
bool inJ2D = false;
|
bool inJ2D = false;
|
||||||
bool inJ3D = false;
|
bool inJ3D = false;
|
||||||
bool wasTexture2DEnabled = false;
|
bool wasTexture2DEnabled = false;
|
||||||
@@ -26,67 +31,96 @@ namespace JGL {
|
|||||||
|
|
||||||
void Update(const Vector2& window_size) {
|
void Update(const Vector2& window_size) {
|
||||||
wS = window_size;
|
wS = window_size;
|
||||||
|
glViewport(0, 0, (int) wS.x, (int) wS.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma region J2D
|
||||||
void J2D::Begin() {
|
void J2D::Begin(RenderTarget* rt, bool clear_buffers) {
|
||||||
|
GLfloat old_clear_color[4];
|
||||||
|
if (rt != nullptr) {
|
||||||
|
render_target = rt;
|
||||||
|
glGetFloatv(GL_COLOR_CLEAR_VALUE, old_clear_color);
|
||||||
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||||
|
current_fbo = JGL::RenderTarget::GetActiveGLFramebufferHandle();
|
||||||
|
JGL::RenderTarget::SetActiveGLRenderTarget(*rt);
|
||||||
|
}
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glOrtho(0, wS.x, wS.y, 0, -1, 1);
|
|
||||||
|
if (rt == nullptr)
|
||||||
|
glViewport(0, 0, (int) wS.x, (int) wS.y),
|
||||||
|
glOrtho(0, wS.x, wS.y, 0, -1, 1);
|
||||||
|
else
|
||||||
|
glOrtho(0, rt->GetDimensions().x, rt->GetDimensions().y, 0, -1, 1);
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
//Get what the draw color was before we did anything.
|
//Get what the draw color was before we did anything.
|
||||||
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
|
|
||||||
glGetIntegerv(GL_ACTIVE_TEXTURE,& activeTextureUnit);
|
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
|
||||||
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
|
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
|
||||||
|
|
||||||
if (activeTextureUnit != 0)
|
if (activeTextureUnit != 0)
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
|
||||||
wasDepthTestEnabled = false;
|
|
||||||
if (glIsEnabled(GL_DEPTH_TEST))
|
if (glIsEnabled(GL_DEPTH_TEST))
|
||||||
wasDepthTestEnabled = true,
|
wasDepthTestEnabled = true,
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
else
|
||||||
|
wasDepthTestEnabled = false;
|
||||||
|
|
||||||
wasVertexArraysEnabled = true;
|
|
||||||
if (!glIsEnabled(GL_VERTEX_ARRAY))
|
if (!glIsEnabled(GL_VERTEX_ARRAY))
|
||||||
wasVertexArraysEnabled = false,
|
wasVertexArraysEnabled = false,
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
else
|
||||||
|
wasVertexArraysEnabled = true;
|
||||||
|
|
||||||
wasCullFaceEnabled = true;
|
|
||||||
if (!glIsEnabled(GL_CULL_FACE))
|
if (!glIsEnabled(GL_CULL_FACE))
|
||||||
wasCullFaceEnabled = false,
|
wasCullFaceEnabled = false,
|
||||||
glEnable(GL_CULL_FACE),
|
glEnable(GL_CULL_FACE),
|
||||||
glCullFace(GL_BACK);
|
glCullFace(GL_BACK);
|
||||||
|
else
|
||||||
|
wasCullFaceEnabled = true;
|
||||||
|
|
||||||
wasBlendEnabled = true;
|
|
||||||
if (!glIsEnabled(GL_BLEND))
|
if (!glIsEnabled(GL_BLEND))
|
||||||
wasBlendEnabled = false,
|
wasBlendEnabled = false,
|
||||||
glEnable(GL_BLEND),
|
glEnable(GL_BLEND),
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
else
|
||||||
|
wasBlendEnabled = true;
|
||||||
|
|
||||||
wasTexture2DEnabled = true;
|
|
||||||
if (!glIsEnabled(GL_TEXTURE_2D))
|
if (!glIsEnabled(GL_TEXTURE_2D))
|
||||||
wasTexture2DEnabled = false,
|
wasTexture2DEnabled = false,
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
else
|
||||||
|
wasTexture2DEnabled = true;
|
||||||
|
|
||||||
wasTextureCoordArrayEnabled = true;
|
|
||||||
if (!glIsEnabled(GL_TEXTURE_COORD_ARRAY))
|
if (!glIsEnabled(GL_TEXTURE_COORD_ARRAY))
|
||||||
wasTextureCoordArrayEnabled = false,
|
wasTextureCoordArrayEnabled = false,
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
else
|
||||||
|
wasTextureCoordArrayEnabled = true;
|
||||||
|
|
||||||
wasColorArrayEnabled = false;
|
|
||||||
if (glIsEnabled(GL_COLOR_ARRAY))
|
if (glIsEnabled(GL_COLOR_ARRAY))
|
||||||
wasColorArrayEnabled = true,
|
wasColorArrayEnabled = true,
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
else
|
||||||
|
wasColorArrayEnabled = false;
|
||||||
|
|
||||||
if (!inJ3D)
|
if (!inJ3D)
|
||||||
inJ2D = true;
|
inJ2D = true;
|
||||||
else { ERROR("Attempt to Begin J2D inside of J3D context.") }
|
else { Logger::Error("Attempt to Begin J2D inside of J3D context."); }
|
||||||
|
|
||||||
|
if (rt != nullptr && clear_buffers) {
|
||||||
|
glClearColor(rt->GetClearColor().R(), rt->GetClearColor().G(), rt->GetClearColor().B(), rt->GetClearColor().A());
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
glClearColor(old_clear_color[0], old_clear_color[1], old_clear_color[2], old_clear_color[3]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::End() {
|
void J2D::End() {
|
||||||
@@ -116,129 +150,134 @@ namespace JGL {
|
|||||||
if (wasColorArrayEnabled)
|
if (wasColorArrayEnabled)
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
|
||||||
//Select whatever texture unit was selected before.
|
//Select whatever texture_handle unit was selected before.
|
||||||
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
|
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
|
||||||
|
|
||||||
//Put the draw color back how it was before.
|
//Put the draw color back how it was before.
|
||||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
glColor4fv(oldColor);
|
||||||
|
|
||||||
|
if (render_target != nullptr) {
|
||||||
|
render_target = nullptr;
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||||
|
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
|
||||||
|
}
|
||||||
inJ2D = false;
|
inJ2D = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO rotation, I'm unsure if @josh wants degrees or radians.
|
void J2D::DrawPoint(const Color4& color, const Vector2& coordinates, float radius) {
|
||||||
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size, u8 opacity, Inversion::Inversion inversion) {
|
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
std::array<Vector2, 4> textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};;
|
Vector2 vertices[] = {coordinates};
|
||||||
const 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}};
|
|
||||||
|
|
||||||
if (inversion& Inversion::Vertical)
|
glPointSize(radius);
|
||||||
textureCoordinates = {Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), Vector2(1, 1)};
|
glColor4ubv(color.ptr());
|
||||||
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)};
|
|
||||||
|
|
||||||
glColor4f(baseColor[0],baseColor[1],baseColor[2],opacity / 255.f);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
|
glDrawArrays(GL_POINTS, 0, 1);
|
||||||
glDrawArrays(GL_QUADS, 0, 4);
|
glColor4fv(baseColor);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
|
void J2D::DrawPoint(const Color4& color, float x, float y, float radius) {
|
||||||
|
DrawPoint(color, {x, y}, radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
Vector2 vertices[] = {A, B};
|
||||||
|
|
||||||
Vector2 vertices[] = {v1, v2, v3, v4};
|
|
||||||
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);
|
|
||||||
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);
|
glLineWidth(thickness);
|
||||||
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);
|
||||||
|
glDrawArrays(GL_LINES, 0, 2);
|
||||||
|
glColor4fv(baseColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
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::DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness) {
|
||||||
|
if (!inJ2D)
|
||||||
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
|
Vector2 vertices[] = {A, B};
|
||||||
|
GLfloat colors[8] = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),
|
||||||
|
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized() };
|
||||||
|
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
glLineWidth(thickness);
|
||||||
|
glColorPointer(4,GL_FLOAT,sizeof(Color4), colors);
|
||||||
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
|
glDrawArrays(GL_LINES, 0, 2);
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
|
glColor4fv(baseColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) {
|
||||||
|
if (!inJ2D)
|
||||||
|
Logger::Error("Drawing 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);
|
||||||
|
glColor4ubv(color.ptr());
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glDrawArrays(GL_LINE_LOOP, 0, 4);
|
glDrawArrays(GL_LINE_LOOP, 0, 4);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
|
||||||
|
|
||||||
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::DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity, Inversion::Inversion inversion) {
|
|
||||||
J2D::DrawSprite(texture, {x, y}, {w, h}, opacity, inversion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
|
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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}};
|
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);
|
glColor4ubv(color.ptr());
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glDrawArrays(GL_QUADS, 0, 4);
|
glDrawArrays(GL_QUADS, 0, 4);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
|
void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Direction& gradient, 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& gradient, const Vector2& pos, const Vector2& size) {
|
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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}};
|
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 = {};
|
std::vector<GLfloat> colors{};
|
||||||
|
|
||||||
if (gradient == Gradient::Horizontal)
|
if (gradient == Direction::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,
|
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),
|
||||||
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};
|
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||||
|
|
||||||
else if (gradient == Gradient::Vertical)
|
else if (gradient == Direction::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,
|
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(),
|
||||||
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};
|
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized()};
|
||||||
|
|
||||||
else if (gradient == Gradient::DiagonalBottomLeft)
|
else if (gradient == Direction::Diagonal_SWNE)
|
||||||
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,
|
colors = {(color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.f, (color1.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f, (color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.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.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), (color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.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.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f, (color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f, color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||||
|
|
||||||
else if (gradient == Gradient::DiagonalTopLeft)
|
else if (gradient == Direction::Diagonal_NWSE)
|
||||||
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,
|
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),(color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.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.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f, (color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f, color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(),
|
||||||
(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.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.f, (color1.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f,(color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f};
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glColorPointer(4, GL_FLOAT, 0, colors.data());
|
glColorPointer(4, GL_FLOAT, 0, colors.data());
|
||||||
glDrawArrays(GL_QUADS, 0, 4);
|
glDrawArrays(GL_QUADS, 0, 4);
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
|
||||||
|
|
||||||
void J2D::FillGradientRect(const Color3& color1, const Color3& color2, const Gradient::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)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
J2D::FillRect(color, {pos.x + radius, pos.y}, {size.x - 2 * radius, size.y});
|
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::FillRect(color, {pos.x, pos.y + radius}, {size.x, size.y - 2 * radius});
|
||||||
@@ -249,212 +288,384 @@ namespace JGL {
|
|||||||
J2D::FillCircle(color, {pos.x + size.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) {
|
void
|
||||||
J2D::FillRoundedRect({color.r, color.g, color.b, 255}, pos, size, radius, subdivisions);
|
J2D::DrawRenderTargetAsSprite(const JGL::RenderTarget& rt, const Vector2& position, float rad_rotation, const Vector2& origin,
|
||||||
}
|
const Vector2& scale, const Color4& color, Direction inversion) {
|
||||||
|
|
||||||
void J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) {
|
//Correct for the render-target being upside-down.
|
||||||
|
Direction d{};
|
||||||
|
if (inversion == Direction::None)
|
||||||
|
d = Direction::Vertical;
|
||||||
|
else if (inversion == Direction::Horizontal)
|
||||||
|
d = Direction::Horizontal | Direction::Vertical;
|
||||||
|
else if (inversion& Direction::Horizontal && inversion& Direction::Vertical)
|
||||||
|
d = Direction::Horizontal;
|
||||||
|
|
||||||
|
//Change the blending mode such that the alpha doesn't get multiplied again.
|
||||||
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
//J2D::DrawSprite(*rt.GetJGLTexture(), position, rad_rotation, origin, scale, color, d);
|
||||||
|
J2D::DrawPartialSprite(*rt.GetJGLTexture(), position, {0, 0}, rt.GetDimensions(), rad_rotation, origin, scale, color, d);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
}
|
||||||
|
void J2D::DrawSprite(const Texture& texture, const Vector2& pos, float rad_rotation, const Vector2& origin,
|
||||||
|
const Vector2& scale, const Color4& color, Direction inversion) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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);
|
const Vector2 size = texture.GetDimensions();
|
||||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
std::array<Vector2, 4> textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};
|
||||||
glDrawArrays(GL_LINE_LOOP, 0, 4);
|
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
// 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;
|
||||||
|
Vector2 scaled_size = scale * size;
|
||||||
|
Vector2 size2 = scaled_size;
|
||||||
|
float cos_theta = std::cos(rad_rotation);
|
||||||
|
float sin_theta = std::sin(rad_rotation);
|
||||||
|
|
||||||
|
std::array<Vector2, 4> 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
|
||||||
|
};
|
||||||
|
|
||||||
|
//Rotate the vertices about the origin by float rad_rotation.
|
||||||
|
if (rad_rotation != 0)
|
||||||
|
for (auto& v: vertices)
|
||||||
|
v = {(v.x - pos2.x - offset.x * scale.x) * cos_theta - (v.y - pos2.y - offset.y * scale.y) * sin_theta +
|
||||||
|
pos2.x + offset.x * scale.x,
|
||||||
|
(v.x - pos2.x - offset.x * scale.x) * sin_theta + (v.y - pos2.y - offset.y * scale.y) * cos_theta +
|
||||||
|
pos2.y + offset.y * scale.y
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if (inversion& Direction::Vertical)
|
||||||
|
textureCoordinates = {Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), Vector2(1, 1)};
|
||||||
|
if (inversion& Direction::Horizontal)
|
||||||
|
textureCoordinates = {Vector2(1, 0), Vector2(1, 1), Vector2(0, 1), Vector2(0, 0)};
|
||||||
|
if ((inversion& Direction::Horizontal) && (inversion& Direction::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.data());
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
|
||||||
|
glDrawArrays(GL_QUADS, 0, 4);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
glColor4fv(baseColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
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::DrawSprite(const Texture& texture, float positionX, float positionY, float rad_rotation, float originX,
|
||||||
|
float originY, float scaleX, float scaleY, const Color4& color, Direction inversion) {
|
||||||
|
DrawSprite(texture,
|
||||||
|
{positionX, positionY},
|
||||||
|
rad_rotation,
|
||||||
|
{originX, originY},
|
||||||
|
{scaleX, scaleY},
|
||||||
|
color, inversion);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) {
|
void J2D::DrawPartialSprite(const Texture& texture, const Vector2 &position, const Vector2& sub_texture_position,
|
||||||
|
const Vector2& sub_texture_size, float rad_rotation, const Vector2& origin,
|
||||||
|
const Vector2& scale, const Color4& color, Direction inversion) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
Vector2 vertices[] = {A, B};
|
const Vector2 textureSize = texture.GetDimensions();
|
||||||
|
|
||||||
glLineWidth(thickness);
|
// Calculate texture coordinates (relative to the whole texture)
|
||||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
std::array<GLfloat, 8> textureCoordinates = {
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
sub_texture_position.x / textureSize.x,
|
||||||
glDrawArrays(GL_LINES, 0, 2);
|
sub_texture_position.y / textureSize.y,
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
sub_texture_position.x / textureSize.x,
|
||||||
|
(sub_texture_position.y + sub_texture_size.y) / textureSize.y,
|
||||||
|
(sub_texture_position.x + sub_texture_size.x) / textureSize.x,
|
||||||
|
(sub_texture_position.y + sub_texture_size.y) / textureSize.y,
|
||||||
|
(sub_texture_position.x + sub_texture_size.x) / textureSize.x,
|
||||||
|
sub_texture_position.y / textureSize.y
|
||||||
|
};
|
||||||
|
|
||||||
|
if (inversion& Direction::Vertical)
|
||||||
|
std::swap(textureCoordinates[1], textureCoordinates[3]),
|
||||||
|
std::swap(textureCoordinates[5], textureCoordinates[7]);
|
||||||
|
|
||||||
|
if (inversion& Direction::Horizontal)
|
||||||
|
std::swap(textureCoordinates[0], textureCoordinates[6]),
|
||||||
|
std::swap(textureCoordinates[2], textureCoordinates[4]);
|
||||||
|
|
||||||
|
const Vector2 offset = origin * sub_texture_size;
|
||||||
|
Vector2 pos2 = position;
|
||||||
|
Vector2 scaled_size = scale * sub_texture_size;
|
||||||
|
Vector2 size2 = scaled_size;
|
||||||
|
float cos_theta = std::cos(rad_rotation);
|
||||||
|
float sin_theta = std::sin(rad_rotation);
|
||||||
|
|
||||||
|
std::array<Vector2, 4> vertices =
|
||||||
|
{
|
||||||
|
pos2, // Top-left
|
||||||
|
{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
|
||||||
|
};
|
||||||
|
|
||||||
|
//Rotate the vertices about the origin by float rad_rotation.
|
||||||
|
if (rad_rotation != 0)
|
||||||
|
for (auto& v: vertices)
|
||||||
|
v = {(v.x - pos2.x - offset.x * scale.x) * cos_theta - (v.y - pos2.y - offset.y * scale.y) * sin_theta +
|
||||||
|
pos2.x + offset.x * scale.x,
|
||||||
|
(v.x - pos2.x - offset.x * scale.x) * sin_theta + (v.y - pos2.y - offset.y * scale.y) * cos_theta +
|
||||||
|
pos2.y + offset.y * scale.y};
|
||||||
|
|
||||||
|
glColor4ubv(color.ptr());
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture.GetGLTextureHandle());
|
||||||
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates.data());
|
||||||
|
glDrawArrays(GL_QUADS, 0, 4);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
glColor4fv(baseColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
|
void
|
||||||
J2D::DrawLine({color.r, color.g, color.b, 255}, A, B, thickness);
|
J2D::DrawPartialSprite(const JGL::Texture& texture, float positionX, float positionY, float sub_texture_positionX,
|
||||||
|
float sub_texture_positionY, unsigned int sub_texture_sizeX,
|
||||||
|
unsigned int sub_texture_sizeY, float originX, float originY, float rad_rotation,
|
||||||
|
float scaleX, float scaleY, const Color4& color, JGL::Direction inversion) {
|
||||||
|
|
||||||
|
J2D::DrawPartialSprite(texture, {positionX, positionY}, {sub_texture_positionX, sub_texture_positionY},
|
||||||
|
{(float) sub_texture_sizeX, (float) sub_texture_sizeY}, rad_rotation, {originX, originY},
|
||||||
|
{scaleX, scaleY}, color, inversion);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::DrawLine(const Color4& color, float x, float y, float w, float h, float thickness) {
|
void J2D::DrawMirrorSprite(const Texture& texture, const Vector2& position, Direction mirror_axis, float rad_rotation, const Vector2& origin, const Vector2& scale, const Color4& color) {
|
||||||
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)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
Vector2 vertices[] = {A, B};
|
if (mirror_axis == Direction::None)
|
||||||
GLfloat colors[8] = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,
|
Logger::Warning("Drawing non-mirrored sprite with J2D::DrawMirrorSprite?");
|
||||||
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
|
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glBindTexture(GL_TEXTURE_2D, texture.GetGLTextureHandle());
|
||||||
glLineWidth(thickness);
|
Vector2 size = texture.GetDimensions();
|
||||||
glColorPointer(4,GL_FLOAT,sizeof(GL_FLOAT) * 4, colors);
|
std::array<Vector2, 4> textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};
|
||||||
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) {
|
if (mirror_axis& Direction::Horizontal)
|
||||||
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, A, B, thickness);
|
size.x *= 2,
|
||||||
}
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT),
|
||||||
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness) {
|
textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(2, 1), Vector2(2, 0)};
|
||||||
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) {
|
if (mirror_axis& Direction::Vertical)
|
||||||
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, {x, y}, {w, h}, thickness);
|
size.y *= 2,
|
||||||
}
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT),
|
||||||
|
textureCoordinates = {Vector2(0, 0), Vector2(0, 2), Vector2(1, 2), Vector2(1, 0)};
|
||||||
|
|
||||||
void J2D::DrawPixel(const Color4& color, const Vector2& coordinates) {
|
if ((mirror_axis& Direction::Horizontal) && (mirror_axis& Direction::Vertical))
|
||||||
if (!inJ2D)
|
textureCoordinates = {Vector2(0, 0), Vector2(0, 2), Vector2(2, 2), Vector2(2, 0)};
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
|
||||||
|
|
||||||
Vector2 vertices[] = {coordinates};
|
const Vector2 offset = origin * size;
|
||||||
|
Vector2 pos2 = position;
|
||||||
|
Vector2 size2 = scale * size;
|
||||||
|
float cos_theta = std::cos(rad_rotation);
|
||||||
|
float sin_theta = std::sin(rad_rotation);
|
||||||
|
|
||||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
std::array<Vector2, 4> vertices =
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
{
|
||||||
glDrawArrays(GL_LINES, 0, 1);
|
pos2,
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
{pos2.x, pos2.y + size2.y},
|
||||||
}
|
{pos2.x + size2.x, pos2.y + size2.y},
|
||||||
|
{pos2.x + size2.x, pos2.y}
|
||||||
|
};
|
||||||
|
|
||||||
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
|
if (rad_rotation != 0)
|
||||||
J2D::DrawPixel({color.r, color.g, color.b, 255}, coordinates);
|
for (auto& v : vertices)
|
||||||
}
|
v = {
|
||||||
|
(v.x - pos2.x - offset.x * scale.x) * cos_theta - (v.y - pos2.y - offset.y * scale.y) * sin_theta + pos2.x + offset.x * scale.x,
|
||||||
|
(v.x - pos2.x - offset.x * scale.x) * sin_theta + (v.y - pos2.y - offset.y * scale.y) * cos_theta + pos2.y + offset.y * scale.y
|
||||||
|
};
|
||||||
|
|
||||||
void J2D::DrawPixel(const Color4& color, float x, float y) {
|
glColor4ubv(color.ptr());
|
||||||
DrawPixel(color, {x, y});
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||||
}
|
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
|
||||||
|
glDrawArrays(GL_QUADS, 0, 4);
|
||||||
|
|
||||||
void J2D::DrawPixel(const Color3& color, float x, float y) {
|
//Reset the wrapping mode.
|
||||||
DrawPixel({color.r, color.g, color.b, 255}, {x, y});
|
if (texture.GetWrappingMode() == 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 (texture.GetWrappingMode() == TextureWrappingMode::REPEAT)
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT),
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
else if (texture.GetWrappingMode() == 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);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
glColor4fv(baseColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
|
void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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{};
|
std::vector<Vector2> vertices{};
|
||||||
GLfloat angle, x, y;
|
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;
|
x = radius * std::sin(angle) + center.x;
|
||||||
y = radius * std::cos(angle) + center.y;
|
y = radius * std::cos(angle) + center.y;
|
||||||
vertices.emplace_back(x,y);
|
vertices.emplace_back(x,y);
|
||||||
}
|
}
|
||||||
|
|
||||||
glLineWidth(thickness);
|
glLineWidth(thickness);
|
||||||
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());
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||||
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
|
glDrawArrays(GL_LINE_LOOP, 0, (int) vertices.size());
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
void J2D::FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
float step = (2.f * M_PI) / (float) subdivisions;;
|
|
||||||
std::vector<Vector2> vertices{};
|
|
||||||
GLfloat angle, x, y;
|
GLfloat angle, x, y;
|
||||||
|
float step = (2.f * Math::Pi) / (float) subdivisions;
|
||||||
for (angle = 0.0f; angle < (2.f * M_PI); angle += step)
|
std::vector<Vector2> vertices{};
|
||||||
x = radius * sin(angle) + center.x,
|
for (angle = 0.0f; angle < (2.f * Math::Pi); angle += step)
|
||||||
y = radius * cos(angle) + center.y,
|
x = radius * std::sin(angle) + center.x,
|
||||||
vertices.push_back({x, y});
|
y = radius * std::cos(angle) + center.y,
|
||||||
|
vertices.emplace_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());
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||||
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
|
glDrawArrays(GL_TRIANGLE_FAN, 0, (int) vertices.size());
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
void J2D::OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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}};
|
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
|
||||||
|
|
||||||
glLineWidth(thickness);
|
glLineWidth(thickness);
|
||||||
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);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glDrawArrays(GL_LINE_LOOP, 0, 3);
|
glDrawArrays(GL_LINE_LOOP, 0, 3);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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}};
|
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);
|
glColor4ubv(color.ptr());
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri) {
|
void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri) {
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
Logger::Error("Drawing 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}};
|
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,
|
GLfloat colors[] = {a_color.RedChannelNormalized(), a_color.GreenChannelNormalized(), a_color.BlueChannelNormalized(), a_color.AlphaChannelNormalized(),
|
||||||
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,
|
b_color.RedChannelNormalized(),b_color.GreenChannelNormalized(), b_color.BlueChannelNormalized(), b_color.AlphaChannelNormalized(),
|
||||||
c_color.a / 255.f };
|
c_color.RedChannelNormalized(), c_color.GreenChannelNormalized(), c_color.BlueChannelNormalized(),c_color.AlphaChannelNormalized()};
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
glColorPointer(4, GL_FLOAT, sizeof(Color4), colors);
|
glColorPointer(4, GL_FLOAT, sizeof(Color4), colors);
|
||||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void J2D::FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri) {
|
void J2D::DrawCubicBezierCurve(const Color4 &color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
|
||||||
J2D::FillGradientTriangle(Color4(a_color), Color4(b_color), Color4(c_color), tri);
|
int subdivisions, float thickness) {
|
||||||
|
|
||||||
|
|
||||||
|
Vector2 last = controlA;
|
||||||
|
const 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void J2D::OutlinePolygon(const Color4 &color, const std::vector<Vector2>& points, float thickness) {
|
||||||
|
if (!inJ2D)
|
||||||
|
Logger::Error("Drawing J2D element before J2D begin.");
|
||||||
|
|
||||||
void J2D::FillTriangle(const Color3& color, const Triangle2D& tri) {
|
if (points.front() != points.back())
|
||||||
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
|
throw std::runtime_error("J2D::OutlinePolygon: The first point and the last point must connect.");
|
||||||
|
|
||||||
|
glLineWidth(thickness);
|
||||||
|
glColor4ubv(color.ptr());
|
||||||
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), points.data());
|
||||||
|
glDrawArrays(GL_LINE_LOOP, 0, (int) points.size());
|
||||||
|
glColor4fv(baseColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region J3D
|
||||||
|
std::vector<GLfloat> OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far) {
|
||||||
|
std::vector<GLfloat> result(16);
|
||||||
|
GLfloat f = 1.0f / std::tan(fovY * 0.5f * Math::Pi / 180.0f);
|
||||||
|
result[0] = f / aspect;
|
||||||
|
result[5] = f;
|
||||||
|
result[10] = (z_far + z_near) / (z_near - z_far);
|
||||||
|
result[11] = -1.0f;
|
||||||
|
result[14] = (2.0f * z_far * z_near) / (z_near - z_far);
|
||||||
|
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() {
|
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(OpenGLPerspectiveProjectionRH(j3d_fov, aspect, 0.001, j3d_far_plane).data());
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
|
||||||
//Get what the draw color was before we did anything.
|
//Get what the draw color was before we did anything.
|
||||||
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
|
|
||||||
wasDepthTestEnabled = false;
|
wasDepthTestEnabled = false;
|
||||||
if (glIsEnabled(GL_DEPTH_TEST))
|
if (glIsEnabled(GL_DEPTH_TEST))
|
||||||
@@ -466,28 +677,34 @@ namespace JGL {
|
|||||||
wasVertexArraysEnabled = false,
|
wasVertexArraysEnabled = false,
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
|
||||||
|
if (!glIsEnabled(GL_TEXTURE_COORD_ARRAY))
|
||||||
|
wasTextureCoordArrayEnabled = false,
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
else
|
||||||
|
wasTextureCoordArrayEnabled = true;
|
||||||
|
|
||||||
|
|
||||||
wasTexture2DEnabled = true;
|
wasTexture2DEnabled = true;
|
||||||
if (!glIsEnabled(GL_TEXTURE_2D))
|
if (!glIsEnabled(GL_TEXTURE_2D))
|
||||||
wasTexture2DEnabled = false,
|
wasTexture2DEnabled = false,
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
// TODO: implement bool drawBackface as DrawString parameter.
|
|
||||||
wasCullFaceEnabled = false;
|
wasCullFaceEnabled = false;
|
||||||
if (glIsEnabled(GL_CULL_FACE))
|
if (glIsEnabled(GL_CULL_FACE))
|
||||||
wasCullFaceEnabled = true,
|
wasCullFaceEnabled = true,
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
|
|
||||||
wasBlendEnabled = true;
|
|
||||||
if (!glIsEnabled(GL_BLEND))
|
if (!glIsEnabled(GL_BLEND))
|
||||||
wasBlendEnabled = false,
|
wasBlendEnabled = false,
|
||||||
glEnable(GL_BLEND),
|
glEnable(GL_BLEND),
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
else
|
||||||
|
wasBlendEnabled = true;
|
||||||
|
|
||||||
if (!inJ2D)
|
if (!inJ2D)
|
||||||
inJ3D = true;
|
inJ3D = true;
|
||||||
else { ERROR("Attempt to Begin J3D inside of J2D context.")}
|
else
|
||||||
|
Logger::Error("Can't begin J3D context inside J2D context.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void J3D::End() {
|
void J3D::End() {
|
||||||
@@ -506,26 +723,25 @@ namespace JGL {
|
|||||||
if (wasCullFaceEnabled)
|
if (wasCullFaceEnabled)
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
//Put the draw color back how it was before.
|
if (!wasTextureCoordArrayEnabled)
|
||||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
|
||||||
|
//Put the draw color back how it was before.
|
||||||
|
glColor4fv(oldColor);
|
||||||
inJ3D = false;
|
inJ3D = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void J3D::DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness) {
|
void J3D::DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness) {
|
||||||
if (!inJ3D)
|
if (!inJ3D)
|
||||||
ERROR("Attempt to Render J3D element before J3D begin.")
|
Logger::Error("Attempt to Render J3D element before J3D begin.");
|
||||||
|
|
||||||
Vector3 vertices[] = {A, B};
|
Vector3 vertices[] = {A, B};
|
||||||
|
|
||||||
glLineWidth(thickness);
|
glLineWidth(thickness);
|
||||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
glColor4ubv(color.ptr());
|
||||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
|
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
|
||||||
glDrawArrays(GL_LINES, 0, 2);
|
glDrawArrays(GL_LINES, 0, 2);
|
||||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
glColor4fv(baseColor);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
#pragma endregion
|
||||||
}
|
}
|
||||||
|
@@ -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) {}
|
|
||||||
}
|
|
@@ -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); }
|
|
||||||
}
|
|
@@ -1,250 +0,0 @@
|
|||||||
#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.
|
|
||||||
|
|
||||||
// Offset by height to render at "correct" location.
|
|
||||||
y += size;
|
|
||||||
|
|
||||||
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<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;
|
|
||||||
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();
|
|
||||||
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() * 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) {
|
|
||||||
//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
|
|
||||||
glColor4f(1, 1, 1, 1);
|
|
||||||
glPopMatrix();
|
|
||||||
}
|
|
||||||
}
|
|
216
src/TextRendering.cpp
Normal file
216
src/TextRendering.cpp
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
#include <JGL/JGL.h>
|
||||||
|
|
||||||
|
|
||||||
|
#if __linux__
|
||||||
|
#include <freetype2/ft2build.h>
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
#include FT_OUTLINE_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include <ft2build.h>
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
#include FT_OUTLINE_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <JGL/types/Font.h>
|
||||||
|
#include <JGL/types/FontCache.h>
|
||||||
|
#include "JGL/logger/logger.h"
|
||||||
|
|
||||||
|
namespace JGL {
|
||||||
|
CachedFont* CacheFont(const Font& font, u32 size) {
|
||||||
|
CachedFont* cachedFont;
|
||||||
|
FT_Set_Pixel_Sizes(font.face, 0, size);
|
||||||
|
jlog::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_handle 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);
|
||||||
|
}
|
||||||
|
return cachedFont;
|
||||||
|
}
|
||||||
|
|
||||||
|
void J2D::DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
|
||||||
|
// Offset by height to render at "correct" location.
|
||||||
|
y += size;
|
||||||
|
|
||||||
|
CachedFont* cachedFont = fontCache.getFont(size, font.index);
|
||||||
|
|
||||||
|
if (font.face == nullptr)
|
||||||
|
jlog::Fatal("Drawing a string with an uninitialized font?");
|
||||||
|
|
||||||
|
//If the font doesn't exist in the cache yet.
|
||||||
|
if (!cachedFont)
|
||||||
|
cachedFont = CacheFont(font, size);
|
||||||
|
|
||||||
|
glColor4ubv(color.ptr());
|
||||||
|
//Texture parameters are restored when the texture_handle is bound
|
||||||
|
glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture());
|
||||||
|
|
||||||
|
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;
|
||||||
|
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();
|
||||||
|
vertices[i] = glyph_vertices;
|
||||||
|
texcoords[i] = glyph_texcoords;
|
||||||
|
}
|
||||||
|
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), texcoords.data());
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, (int) vertices.size() * 6);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
glColor4f(1, 1, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void J3D::DrawString(const Color4& color, const std::string& text, const Vector3& pos, float scale, u32 size, const Font& font, const EulerAngle& angle, bool draw_back_face) {
|
||||||
|
// TODO: Determine the proper scale factor mathematically
|
||||||
|
scale = scale * 0.002f;
|
||||||
|
scale = -scale;
|
||||||
|
float x = pos.x;
|
||||||
|
float y = pos.y;
|
||||||
|
float z = pos.z;
|
||||||
|
|
||||||
|
CachedFont* cachedFont = fontCache.getFont(size, font.index);
|
||||||
|
if (font.face == nullptr)
|
||||||
|
jlog::Fatal("Drawing a string with an uninitialized font?");
|
||||||
|
|
||||||
|
if (!cachedFont)
|
||||||
|
cachedFont = CacheFont(font, size);
|
||||||
|
|
||||||
|
glColor4ubv(color.ptr());
|
||||||
|
glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture());
|
||||||
|
|
||||||
|
std::vector<std::array<GLfloat, 18>> vertices(text.size());
|
||||||
|
std::vector<std::array<GLfloat, 12>> texcoords(text.size());
|
||||||
|
|
||||||
|
glPushMatrix();
|
||||||
|
glTranslatef(x, y, z);
|
||||||
|
glRotatef(angle.pitch, 1.0f, 0.0f, 0.0f);
|
||||||
|
glRotatef(angle.yaw, 0.0f, 1.0f, 0.0f);
|
||||||
|
glRotatef(angle.roll, 0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
x = y = z = 0;
|
||||||
|
for (int i = 0; i < text.length(); i++) {
|
||||||
|
CachedGlyph* glyph = cachedFont->getGlyph(text[i]);
|
||||||
|
|
||||||
|
float x2 = x + glyph->x2offset * scale;
|
||||||
|
float y2 = y - glyph->y2offset * scale;
|
||||||
|
float w = glyph->w * scale;
|
||||||
|
float h = glyph->h * scale;
|
||||||
|
|
||||||
|
std::array<GLfloat, 18> glyph_vertices
|
||||||
|
{
|
||||||
|
x2, y2, z,
|
||||||
|
x2, y2 + h, z,
|
||||||
|
x2 + w, y2 + h, z,
|
||||||
|
x2, y2, z,
|
||||||
|
x2 + w, y2 + h, z,
|
||||||
|
x2 + w, y2, z
|
||||||
|
};
|
||||||
|
|
||||||
|
vertices[i] = glyph_vertices;
|
||||||
|
texcoords[i] = glyph->getTexCoords();
|
||||||
|
x += glyph->advanceX * scale;
|
||||||
|
y += glyph->advanceY * scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), texcoords.data());
|
||||||
|
|
||||||
|
if (!draw_back_face)
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
|
glCullFace(GL_BACK);
|
||||||
|
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, (int) vertices.size() * 6);
|
||||||
|
|
||||||
|
if (!draw_back_face)
|
||||||
|
glDisable(GL_CULL_FACE);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
|
glColor4f(1, 1, 1, 1);
|
||||||
|
glPopMatrix();
|
||||||
|
}
|
||||||
|
}
|
9
src/logger/logger.cpp
Normal file
9
src/logger/logger.cpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <JGL/logger/logger.h>
|
||||||
|
|
||||||
|
namespace JGL::Logger {
|
||||||
|
using namespace jlog;
|
||||||
|
|
||||||
|
GenericLogger Fatal {"JGL::fatal", GlobalLogFile, Colors::Reds::Crimson, Colors::Gray, Colors::Gray, Colors::Reds::Crimson, Colors::White};
|
||||||
|
GenericLogger Debug {"JGL::debug", GlobalLogFile, Colors::Purples::Purple, Colors::Gray, Colors::Gray, Colors::Purples::Purple, Colors::White};
|
||||||
|
GenericLogger Error {"JGL::error", GlobalLogFile, Colors::Red, Colors::Gray, Colors::Gray, Colors::Red, Colors::White};
|
||||||
|
}
|
@@ -3,18 +3,22 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
#include <jlog/Logger.hpp>
|
||||||
|
|
||||||
#if __linux__
|
#if __linux__
|
||||||
#include <freetype2/ft2build.h>
|
#include <freetype2/ft2build.h>
|
||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
#include FT_OUTLINE_H
|
#include FT_OUTLINE_H
|
||||||
#endif
|
|
||||||
#if _WIN32
|
|
||||||
#include <ft2build.h>
|
|
||||||
#include FT_FREETYPE_H
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <JGL/Font.h>
|
#if _WIN32
|
||||||
|
#include <ft2build.h>
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
#include FT_OUTLINE_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <JGL/types/Font.h>
|
||||||
|
#include <JGL/types/FontCache.h>
|
||||||
|
|
||||||
namespace JGL::Detail
|
namespace JGL::Detail
|
||||||
{
|
{
|
||||||
@@ -62,7 +66,7 @@ namespace JGL
|
|||||||
throw new std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
throw new std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||||
|
|
||||||
Font 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;
|
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
|
||||||
throw new std::runtime_error("Error::FREETYPE: Failed to load font!");
|
throw new std::runtime_error("Error::FREETYPE: Failed to load font!");
|
||||||
//return -1;
|
//return -1;
|
||||||
@@ -94,26 +98,40 @@ namespace JGL
|
|||||||
return Font(path);
|
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: Check if a font of that size is in the cache and if so use the info from that because this is sloooooooooooooow.
|
for(const auto& f : fontCache.getFonts()) {
|
||||||
// That'll make it go vroom vroom.
|
if (f->getFontSize() == ptSize) {
|
||||||
// TODO: Work in-progress implementation unfortunately.
|
font_of_size_in_cache = true;
|
||||||
// This is likely returning slightly incorrect results for likely several reasons.
|
break;
|
||||||
// Half-ass solution for now ~ dawsh.
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
jlog::Warning("Measuring a font size that is not cached, This is *super* slow.");
|
||||||
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
|
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
|
||||||
|
|
||||||
Vector2 extents = Vector2(0,0);
|
|
||||||
|
|
||||||
for (const char& c : text) {
|
for (const char& c : text) {
|
||||||
|
|
||||||
FT_GlyphSlot slot = face->glyph;
|
FT_GlyphSlot slot = face->glyph;
|
||||||
auto glyph_index = FT_Get_Char_Index(this->face, c);
|
auto glyph_index = FT_Get_Char_Index(this->face, c);
|
||||||
|
|
||||||
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
|
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
|
||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -125,19 +143,14 @@ namespace JGL
|
|||||||
extents += advance;
|
extents += advance;
|
||||||
|
|
||||||
// Gives smaller results than we'd want.
|
// 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;
|
extents.y = slot->metrics.height / 64;
|
||||||
}
|
|
||||||
|
|
||||||
// Just fucking hardcode it, we know the glyph height is roughly always the ptSize anyway.
|
// Just fucking hardcode it, we know the glyph height is roughly always the ptSize anyway.
|
||||||
if (extents.y < ptSize)
|
if (extents.y < ptSize)
|
||||||
{
|
|
||||||
extents.y = ptSize;
|
extents.y = ptSize;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return extents;
|
return extents;
|
||||||
}
|
}
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
#include <JGL/FontCache.h>
|
#include <JGL/types/FontCache.h>
|
||||||
|
|
||||||
using namespace JGL;
|
using namespace JGL;
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@ char CachedGlyph::getCharacter() {
|
|||||||
return character;
|
return character;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::array<GLfloat, 12> CachedGlyph::getTexCoords() const {
|
std::array<GLfloat, 12> CachedGlyph::getTexCoords() const {
|
||||||
return texcoords;
|
return texcoords;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,9 +21,6 @@ CachedGlyph::CachedGlyph(char c, std::array<GLfloat, 12> texcoords, float x2offs
|
|||||||
this->texcoords = texcoords;
|
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) {
|
void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) {
|
||||||
glyphs.emplace(glyph->getCharacter(), glyph);
|
glyphs.emplace(glyph->getCharacter(), glyph);
|
||||||
}
|
}
|
||||||
@@ -36,7 +33,6 @@ unsigned int JGL::CachedFont::getFontIndex() {
|
|||||||
return font_index;
|
return font_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO make this code go faster.
|
|
||||||
CachedGlyph* JGL::CachedFont::getGlyph(char c) {
|
CachedGlyph* JGL::CachedFont::getGlyph(char c) {
|
||||||
auto it = glyphs.find(c);
|
auto it = glyphs.find(c);
|
||||||
if (it != glyphs.end())
|
if (it != glyphs.end())
|
||||||
@@ -52,7 +48,7 @@ CachedFont::CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture
|
|||||||
this->font_index = font_index;
|
this->font_index = font_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<char, CachedGlyph*> CachedFont::getGlyphs() {
|
std::unordered_map<char, CachedGlyph*> CachedFont::getGlyphs() {
|
||||||
return glyphs;
|
return glyphs;
|
||||||
}
|
}
|
||||||
|
|
93
src/types/RenderTarget.cpp
Normal file
93
src/types/RenderTarget.cpp
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#include <JGL/types/RenderTarget.h>
|
||||||
|
#include <jlog/Logger.hpp>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
JGL::Texture* JGL::RenderTarget::GetJGLTexture() const {
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint JGL::RenderTarget::GetGLTextureHandle() const {
|
||||||
|
return texture->GetGLTextureHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint JGL::RenderTarget::GetGLFramebufferObjectHandle() const {
|
||||||
|
return framebuffer_object;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint JGL::RenderTarget::GetGLDepthBufferHandle() const {
|
||||||
|
return depth_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint JGL::RenderTarget::GetActiveGLFramebufferHandle() {
|
||||||
|
GLuint fbo;
|
||||||
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*) &fbo);
|
||||||
|
return fbo;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JGL::RenderTarget::SetActiveGLRenderTarget(const RenderTarget& render_target) {
|
||||||
|
RenderTarget rt = render_target;
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, rt.GetGLFramebufferObjectHandle());
|
||||||
|
glViewport(0,0, rt.GetDimensions().x, rt.GetDimensions().y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 JGL::RenderTarget::GetDimensions() const {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JGL::RenderTarget::Erase() {
|
||||||
|
if (GetActiveGLFramebufferHandle() == framebuffer_object)
|
||||||
|
jlog::Warning("Deleting the framebuffer that's currently in use?");
|
||||||
|
|
||||||
|
texture->Erase();
|
||||||
|
|
||||||
|
if (using_depth)
|
||||||
|
glDeleteRenderbuffers(1, &depth_buffer);
|
||||||
|
|
||||||
|
glDeleteFramebuffers(1, &framebuffer_object);
|
||||||
|
}
|
||||||
|
|
||||||
|
Color4 JGL::RenderTarget::GetClearColor() const {
|
||||||
|
return clear_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color, bool use_depth) {
|
||||||
|
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||||
|
GLint viewport[4] = {0, 0, 0, 0};
|
||||||
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||||
|
|
||||||
|
unsigned int biggest;
|
||||||
|
if (size.x >= size.y)
|
||||||
|
biggest = size.x;
|
||||||
|
else biggest = size.y;
|
||||||
|
|
||||||
|
texture = new Texture(Vector2(biggest, biggest));
|
||||||
|
glGenFramebuffers(1, &framebuffer_object);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_object);
|
||||||
|
glViewport(0,0, size.x, size.y);
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetGLTextureHandle(), 0);
|
||||||
|
|
||||||
|
if (use_depth) {
|
||||||
|
GLuint depthBuffer;
|
||||||
|
glGenRenderbuffers(1, &depthBuffer);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
|
||||||
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, biggest, biggest);
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
|
||||||
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
|
using_depth = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLfloat old_clear_color[4];
|
||||||
|
glGetFloatv(GL_COLOR_CLEAR_VALUE, old_clear_color);
|
||||||
|
glClearColor(clear_color.RedChannelNormalized(), clear_color.GreenChannelNormalized(), clear_color.BlueChannelNormalized(), clear_color.AlphaChannelNormalized());
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
glClearColor(old_clear_color[0], old_clear_color[1], old_clear_color[2], old_clear_color[3]);
|
||||||
|
|
||||||
|
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||||
|
if (status != GL_FRAMEBUFFER_COMPLETE)
|
||||||
|
throw std::runtime_error("Error " + std::to_string(status) + "while generating framebuffer");
|
||||||
|
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||||
|
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
|
||||||
|
this->clear_color = clear_color;
|
||||||
|
this->size = size;
|
||||||
|
}
|
195
src/types/Texture.cpp
Normal file
195
src/types/Texture.cpp
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
#include <JGL/types/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);
|
||||||
|
|
||||||
|
GLuint previous_texture;
|
||||||
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||||
|
|
||||||
|
load(t, {(float) t->getWidth(), (float) t->getHeight()}, t->getTextureFormat(), filtering_mode, wrapping_mode);
|
||||||
|
texture_flags = flags;
|
||||||
|
|
||||||
|
delete t;
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture::Texture(const std::string& file, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
|
||||||
|
GLuint previous_texture;
|
||||||
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||||
|
|
||||||
|
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;
|
||||||
|
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
Texture::Texture(const Vector2& size) {
|
||||||
|
GLuint previous_texture;
|
||||||
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||||
|
|
||||||
|
glGenTextures(1, &texture_handle);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, texture_handle);
|
||||||
|
//NEAREST
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
//Clamp
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int) size.x, (int) size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||||
|
|
||||||
|
texture_format = TextureFormat::RGBA;
|
||||||
|
texture_size = size;
|
||||||
|
texture_filtering_mode = TextureFilteringMode::NEAREST;
|
||||||
|
texture_wrapping_mode = TextureWrappingMode::CLAMP_TO_EDGE;
|
||||||
|
texture_flags = TextureFlag::NONE;
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureWrappingMode Texture::GetWrappingMode() const {
|
||||||
|
return texture_wrapping_mode;
|
||||||
|
}
|
||||||
|
}
|
85
src/types/VRamList.cpp
Normal file
85
src/types/VRamList.cpp
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include <JGL/types/VRamList.h>
|
||||||
|
#include <jlog/Logger.hpp>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
void JGL::VRamList::load(const GLfloat* data, const long& s) {
|
||||||
|
GLint current_array_buffer = 0;
|
||||||
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, ¤t_array_buffer);
|
||||||
|
glGenBuffers(1, &list_handle);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, list_handle);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, current_array_buffer);
|
||||||
|
size = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JGL::VRamList::load(const GLuint* data, const long& s) {
|
||||||
|
GLint current_element_array_buffer = 0;
|
||||||
|
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤t_element_array_buffer);
|
||||||
|
glGenBuffers(1, &list_handle);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, list_handle);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, current_element_array_buffer);
|
||||||
|
element_array_buffer = true;
|
||||||
|
size = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
JGL::VRamList::VRamList(const GLfloat* data, const long& size) {
|
||||||
|
long data_size = (long) sizeof(GLfloat) * size;
|
||||||
|
load(data, data_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
JGL::VRamList::VRamList(const GLuint* data, const long& size) {
|
||||||
|
load(data, (long) sizeof(GLuint) * size);
|
||||||
|
}
|
||||||
|
|
||||||
|
JGL::VRamList::VRamList(Vector2* data, const long& size) {
|
||||||
|
load(reinterpret_cast<GLfloat*>(data), (long) sizeof(Vector2) * size);
|
||||||
|
}
|
||||||
|
|
||||||
|
JGL::VRamList::VRamList(Vector3* data, const long& size) {
|
||||||
|
load(reinterpret_cast<GLfloat*>(data), (long) sizeof(Vector3) * size);
|
||||||
|
}
|
||||||
|
|
||||||
|
JGL::VRamList::VRamList(Vector4* data, const long& size) {
|
||||||
|
load(reinterpret_cast<GLfloat*>(data), (long) sizeof(Vector4) * size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JGL::VRamList::Erase() {
|
||||||
|
if (list_handle == 0)
|
||||||
|
jlog::Warning("Erasing an uninitialized array buffer?");
|
||||||
|
|
||||||
|
GLint current_element_array_buffer = 0;
|
||||||
|
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤t_element_array_buffer);
|
||||||
|
GLint current_array_buffer = 0;
|
||||||
|
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, ¤t_array_buffer);
|
||||||
|
|
||||||
|
if (element_array_buffer && current_element_array_buffer == list_handle)
|
||||||
|
jlog::Warning("Erasing an element array buffer while it's in use?");
|
||||||
|
|
||||||
|
else if (!element_array_buffer && current_array_buffer == list_handle)
|
||||||
|
jlog::Warning("Erasing an array buffer while it's in use?");
|
||||||
|
|
||||||
|
glDeleteBuffers(1, &list_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
GLuint JGL::VRamList::GetHandle() const {
|
||||||
|
return list_handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool JGL::VRamList::IsIntegerArray() const {
|
||||||
|
return element_array_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool JGL::VRamList::IsFloatArray() const {
|
||||||
|
return !element_array_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
long JGL::VRamList::GetSize() const {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
long JGL::VRamList::GetDataSize() const {
|
||||||
|
if (element_array_buffer)
|
||||||
|
return (long) sizeof(GLuint) * size;
|
||||||
|
return (long) sizeof(GLfloat) * size;
|
||||||
|
}
|
Reference in New Issue
Block a user