Compare commits
63 Commits
Prerelease
...
some_other
Author | SHA1 | Date | |
---|---|---|---|
1e350ee039 | |||
291b3f3778 | |||
4ee84d6442 | |||
59df950e11 | |||
03a687179c | |||
ae84a68e11 | |||
c5490cb321 | |||
04b7cc9544 | |||
7498390180 | |||
4ac28a2c10 | |||
61c1c3245c | |||
cb9fe4e5c9 | |||
c7e7aa6fb5 | |||
1261321992 | |||
6d1ddad428 | |||
eae3c794fa | |||
db7a37d647 | |||
26600915db | |||
7bc87d00ef | |||
5e65f17a90 | |||
426498d53c | |||
28bdc7f667 | |||
|
e8bfa7b6f0 | ||
d60620ef7c | |||
8cb470ad1c | |||
575a4a0f9b | |||
e9bdaf54b6 | |||
7b2f7de032 | |||
e8245c4442 | |||
bd918aa351 | |||
a0cc8524d9 | |||
1964aeae86 | |||
b84e2ee2c5 | |||
1597662e2e | |||
0823730e82 | |||
6cbd369d51 | |||
926ae06834 | |||
d5fd68eba8 | |||
08b2dfbecc | |||
e33a51b7d4 | |||
7f5ee5cf0c | |||
401e2c0883 | |||
e5bdc441d1 | |||
392375e56c | |||
b007c78cfe | |||
512dc3cb1a | |||
a22a83d2f8 | |||
76cd48c9b7 | |||
38bb1b22ce | |||
122644a013 | |||
|
641f2de8d0 | ||
6618aa5e6b | |||
3970aa2718 | |||
fb5ca55fda | |||
bcca6285af | |||
f8395726cd | |||
ca7abb3044 | |||
14c45ab0f1 | |||
1ca5e5a694 | |||
25fc3f8698 | |||
a836fc7b32 | |||
e6dcc9d61e | |||
6dff2f97c1 |
@@ -17,6 +17,6 @@ jobs:
|
||||
- run: echo "The ${{ gitea.repository }} repository has been cloned to the runner."
|
||||
- run: echo "The workflow is now ready to run your tests on the runner."
|
||||
- run: echo "Install toolchain and run ReCI build test"
|
||||
- run: apt-get update && apt-get install -y lua5.3 git libxrandr-dev && git clone $RECI_GIT $RECI
|
||||
- run: apt-get update && apt-get install -y lua5.3 git libxrandr-dev libvulkan-dev && git clone $RECI_GIT $RECI
|
||||
- run: lua $RECI/reci.lua -f $RECI/scripts/buildtools.reci -f reci/scripts/builddeps.reci -f $RECI/scripts/buildtest.reci
|
||||
- run: echo "This job's status is ${{ job.status }}."
|
||||
|
@@ -17,54 +17,42 @@ include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME mcolor
|
||||
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-4.zip
|
||||
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-5.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME J3ML
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/Release-3.4.zip
|
||||
URL https://git.redacted.cc/josh/j3ml/archive/3.4.5.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME ReWindow
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-21.zip
|
||||
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-32.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME GLAD
|
||||
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_fboV2.zip
|
||||
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_fbo_depthtexture_shadow_anisotropic.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME jlog
|
||||
URL https://git.redacted.cc/josh/jlog/Prerelease-16.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME ReImage
|
||||
URL https://git.redacted.cc/Redacted/ReImage/archive/Release-2.0.zip
|
||||
URL https://git.redacted.cc/josh/jlog/Prerelease-17.zip
|
||||
)
|
||||
|
||||
if (WIN32)
|
||||
#CPMAddPackage(
|
||||
#NAME harfbuzz
|
||||
#URL https://github.com/harfbuzz/harfbuzz/archive/refs/tags/9.0.0.zip
|
||||
#)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME freetype
|
||||
URL https://github.com/freetype/freetype/archive/refs/tags/VER-2-13-2.zip
|
||||
)
|
||||
endif()
|
||||
|
||||
#set(CMAKE_CXX_FLAGS "-O3 -Wall -Wextra")
|
||||
set(CMAKE_CXX_FLAGS "-O3 -Wall -Wextra")
|
||||
|
||||
file(COPY "assets" DESTINATION "${PROJECT_BINARY_DIR}")
|
||||
file(GLOB_RECURSE ASSETS "assets/*")
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
|
||||
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp" )
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp" )
|
||||
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp" "src/*.h")
|
||||
|
||||
if (UNIX AND NOT APPLE)
|
||||
find_package(Freetype REQUIRED)
|
||||
@@ -85,8 +73,6 @@ include_directories(
|
||||
|
||||
target_include_directories(JGL PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${OPENGL_INCLUDE_DIRS}
|
||||
${ReImage_SOURCE_DIR}/include
|
||||
${mcolor_SOURCE_DIR}/include
|
||||
${J3ML_SOURCE_DIR}/include
|
||||
${jlog_SOURCE_DIR}/include
|
||||
@@ -99,13 +85,13 @@ add_executable(JGL_Demo main.cpp)
|
||||
if (UNIX AND NOT APPLE)
|
||||
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS} )
|
||||
target_link_libraries(JGL PRIVATE ${FREETYPE_LIBRARIES})
|
||||
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} mcolor J3ML jlog ReImage glad)
|
||||
target_link_libraries(JGL PUBLIC mcolor J3ML jlog glad)
|
||||
endif()
|
||||
|
||||
if (WIN32)
|
||||
target_include_directories(JGL PRIVATE ${freetype_SOURCE_DIR}/include)
|
||||
target_link_libraries(JGL PRIVATE freetype)
|
||||
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} mcolor J3ML glad jlog ReImage glad)
|
||||
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} mcolor J3ML glad jlog glad)
|
||||
endif()
|
||||
|
||||
target_link_libraries(JGL_Demo PUBLIC JGL ReWindowLibrary Event glad)
|
||||
target_link_libraries(JGL_Demo PUBLIC JGL ReWindow Event glad)
|
||||
|
13
README.md
13
README.md
@@ -19,12 +19,17 @@ Yet Another C++ Rendering Toolkit
|
||||
|
||||
### J2D
|
||||
* DrawPoint
|
||||
* DrawLine / DrawGradientLine
|
||||
* DrawLine / DrawGradientLine / DrawDottedLine / DrawDashedLine / DrawLines
|
||||
* DrawSprite / DrawPartialSprite
|
||||
* OutlineRect / FillRect / FillGradientRect / FillRoundedRect
|
||||
* DrawRenderTarget / DrawPartialRenderTarget
|
||||
* OutlineRect / OutlineRoundedRect / OutlineChamferRect
|
||||
* FillRect / FillGradientRect / FillRoundedRect / FillChamferRect
|
||||
* OutlineCircle / FillCircle
|
||||
* OutlineTriangle / FillTriangle
|
||||
* OutlineTriangle / FillTriangle / FillGradientTriangle
|
||||
* DrawString
|
||||
* DrawCubicBezierCurve
|
||||
* DrawArc
|
||||
* OutlineEllipse / FillEllipse
|
||||
|
||||
### J3D
|
||||
* DrawLine
|
||||
@@ -53,7 +58,7 @@ JGL::J2D::End();
|
||||
```
|
||||
## Requirements
|
||||
|
||||
An OpenGL 2.1 or newer accelerator that supports the `GL_ARB_framebuffer_object` extension or
|
||||
An OpenGL 2.1 or newer accelerator with at-least two texture mappers that supports the `GL_ARB_framebuffer_object` extension or
|
||||
an implementation that can provide those features through alternative means (common on ArmSoC and Risc-V).
|
||||
|
||||
## Compatability
|
||||
|
Binary file not shown.
164
assets/models/cone.obj
Normal file
164
assets/models/cone.obj
Normal file
@@ -0,0 +1,164 @@
|
||||
# Blender v3.6.4 OBJ File: ''
|
||||
# www.blender.org
|
||||
o Cone
|
||||
v 0.000000 -1.000000 -1.000000
|
||||
v 0.195090 -1.000000 -0.980785
|
||||
v 0.382683 -1.000000 -0.923880
|
||||
v 0.555570 -1.000000 -0.831470
|
||||
v 0.707107 -1.000000 -0.707107
|
||||
v 0.831470 -1.000000 -0.555570
|
||||
v 0.923880 -1.000000 -0.382683
|
||||
v 0.980785 -1.000000 -0.195090
|
||||
v 1.000000 -1.000000 0.000000
|
||||
v 0.980785 -1.000000 0.195090
|
||||
v 0.923880 -1.000000 0.382683
|
||||
v 0.831470 -1.000000 0.555570
|
||||
v 0.707107 -1.000000 0.707107
|
||||
v 0.555570 -1.000000 0.831470
|
||||
v 0.382683 -1.000000 0.923880
|
||||
v 0.195090 -1.000000 0.980785
|
||||
v 0.000000 -1.000000 1.000000
|
||||
v -0.195090 -1.000000 0.980785
|
||||
v -0.382683 -1.000000 0.923880
|
||||
v -0.555570 -1.000000 0.831470
|
||||
v -0.707107 -1.000000 0.707107
|
||||
v -0.831470 -1.000000 0.555570
|
||||
v -0.923880 -1.000000 0.382683
|
||||
v -0.980785 -1.000000 0.195090
|
||||
v -1.000000 -1.000000 0.000000
|
||||
v -0.980785 -1.000000 -0.195090
|
||||
v -0.923880 -1.000000 -0.382683
|
||||
v -0.831470 -1.000000 -0.555570
|
||||
v -0.707107 -1.000000 -0.707107
|
||||
v -0.555570 -1.000000 -0.831470
|
||||
v -0.382683 -1.000000 -0.923880
|
||||
v -0.195090 -1.000000 -0.980785
|
||||
v 0.000000 1.000000 0.000000
|
||||
vt 0.250000 0.490000
|
||||
vt 0.250000 0.250000
|
||||
vt 0.296822 0.485388
|
||||
vt 0.341844 0.471731
|
||||
vt 0.383337 0.449553
|
||||
vt 0.419706 0.419706
|
||||
vt 0.449553 0.383337
|
||||
vt 0.471731 0.341844
|
||||
vt 0.485388 0.296822
|
||||
vt 0.490000 0.250000
|
||||
vt 0.485388 0.203178
|
||||
vt 0.471731 0.158156
|
||||
vt 0.449553 0.116663
|
||||
vt 0.419706 0.080294
|
||||
vt 0.383337 0.050447
|
||||
vt 0.341844 0.028269
|
||||
vt 0.296822 0.014612
|
||||
vt 0.250000 0.010000
|
||||
vt 0.203178 0.014612
|
||||
vt 0.158156 0.028269
|
||||
vt 0.116663 0.050447
|
||||
vt 0.080294 0.080294
|
||||
vt 0.050447 0.116663
|
||||
vt 0.028269 0.158156
|
||||
vt 0.014612 0.203178
|
||||
vt 0.010000 0.250000
|
||||
vt 0.014612 0.296822
|
||||
vt 0.028269 0.341844
|
||||
vt 0.050447 0.383337
|
||||
vt 0.080294 0.419706
|
||||
vt 0.116663 0.449553
|
||||
vt 0.158156 0.471731
|
||||
vt 0.796822 0.014612
|
||||
vt 0.514612 0.203178
|
||||
vt 0.703178 0.485388
|
||||
vt 0.203178 0.485388
|
||||
vt 0.750000 0.490000
|
||||
vt 0.796822 0.485388
|
||||
vt 0.841844 0.471731
|
||||
vt 0.883337 0.449553
|
||||
vt 0.919706 0.419706
|
||||
vt 0.949553 0.383337
|
||||
vt 0.971731 0.341844
|
||||
vt 0.985388 0.296822
|
||||
vt 0.990000 0.250000
|
||||
vt 0.985388 0.203178
|
||||
vt 0.971731 0.158156
|
||||
vt 0.949553 0.116663
|
||||
vt 0.919706 0.080294
|
||||
vt 0.883337 0.050447
|
||||
vt 0.841844 0.028269
|
||||
vt 0.750000 0.010000
|
||||
vt 0.703178 0.014612
|
||||
vt 0.658156 0.028269
|
||||
vt 0.616663 0.050447
|
||||
vt 0.580294 0.080294
|
||||
vt 0.550447 0.116663
|
||||
vt 0.528269 0.158156
|
||||
vt 0.510000 0.250000
|
||||
vt 0.514612 0.296822
|
||||
vt 0.528269 0.341844
|
||||
vt 0.550447 0.383337
|
||||
vt 0.580294 0.419706
|
||||
vt 0.616663 0.449553
|
||||
vt 0.658156 0.471731
|
||||
s off
|
||||
f 1/1 33/2 2/3
|
||||
f 2/3 33/2 3/4
|
||||
f 3/4 33/2 4/5
|
||||
f 4/5 33/2 5/6
|
||||
f 5/6 33/2 6/7
|
||||
f 6/7 33/2 7/8
|
||||
f 7/8 33/2 8/9
|
||||
f 8/9 33/2 9/10
|
||||
f 9/10 33/2 10/11
|
||||
f 10/11 33/2 11/12
|
||||
f 11/12 33/2 12/13
|
||||
f 12/13 33/2 13/14
|
||||
f 13/14 33/2 14/15
|
||||
f 14/15 33/2 15/16
|
||||
f 15/16 33/2 16/17
|
||||
f 16/17 33/2 17/18
|
||||
f 17/18 33/2 18/19
|
||||
f 18/19 33/2 19/20
|
||||
f 19/20 33/2 20/21
|
||||
f 20/21 33/2 21/22
|
||||
f 21/22 33/2 22/23
|
||||
f 22/23 33/2 23/24
|
||||
f 23/24 33/2 24/25
|
||||
f 24/25 33/2 25/26
|
||||
f 25/26 33/2 26/27
|
||||
f 26/27 33/2 27/28
|
||||
f 27/28 33/2 28/29
|
||||
f 28/29 33/2 29/30
|
||||
f 29/30 33/2 30/31
|
||||
f 30/31 33/2 31/32
|
||||
f 16/33 24/34 32/35
|
||||
f 31/32 33/2 32/36
|
||||
f 32/36 33/2 1/1
|
||||
f 32/35 1/37 2/38
|
||||
f 2/38 3/39 4/40
|
||||
f 4/40 5/41 6/42
|
||||
f 6/42 7/43 8/44
|
||||
f 8/44 9/45 10/46
|
||||
f 10/46 11/47 12/48
|
||||
f 12/48 13/49 14/50
|
||||
f 14/50 15/51 16/33
|
||||
f 16/33 17/52 18/53
|
||||
f 18/53 19/54 20/55
|
||||
f 20/55 21/56 22/57
|
||||
f 22/57 23/58 24/34
|
||||
f 24/34 25/59 26/60
|
||||
f 26/60 27/61 28/62
|
||||
f 28/62 29/63 30/64
|
||||
f 30/64 31/65 32/35
|
||||
f 32/35 2/38 8/44
|
||||
f 2/38 4/40 8/44
|
||||
f 4/40 6/42 8/44
|
||||
f 8/44 10/46 16/33
|
||||
f 10/46 12/48 16/33
|
||||
f 12/48 14/50 16/33
|
||||
f 16/33 18/53 24/34
|
||||
f 18/53 20/55 24/34
|
||||
f 20/55 22/57 24/34
|
||||
f 24/34 26/60 32/35
|
||||
f 26/60 28/62 32/35
|
||||
f 28/62 30/64 32/35
|
||||
f 32/35 8/44 16/33
|
46
assets/models/cube.amo
Normal file
46
assets/models/cube.amo
Normal file
@@ -0,0 +1,46 @@
|
||||
ao Cube 1
|
||||
v 8
|
||||
1.000000 1.000000 1.000000
|
||||
1.000000 1.000000 -1.000000
|
||||
1.000000 -1.000000 1.000000
|
||||
1.000000 -1.000000 -1.000000
|
||||
-1.000000 1.000000 1.000000
|
||||
-1.000000 1.000000 -1.000000
|
||||
-1.000000 -1.000000 1.000000
|
||||
-1.000000 -1.000000 -1.000000
|
||||
vt 14
|
||||
0.625000 0.500000
|
||||
0.875000 0.500000
|
||||
0.875000 0.750000
|
||||
0.625000 0.750000
|
||||
0.375000 0.750000
|
||||
0.625000 1.000000
|
||||
0.375000 1.000000
|
||||
0.375000 0.000000
|
||||
0.625000 0.000000
|
||||
0.625000 0.250000
|
||||
0.375000 0.250000
|
||||
0.125000 0.500000
|
||||
0.375000 0.500000
|
||||
0.125000 0.750000
|
||||
vn 6
|
||||
0.000000 0.000000 1.000000
|
||||
0.000000 -1.000000 0.000000
|
||||
-1.000000 0.000000 0.000000
|
||||
0.000000 0.000000 -1.000000
|
||||
1.000000 0.000000 0.000000
|
||||
0.000000 1.000000 0.000000
|
||||
f 12
|
||||
2 0 0 0 1 0 4 2 0
|
||||
7 3 1 3 4 1 2 3 1
|
||||
5 5 2 7 6 2 6 7 2
|
||||
7 8 3 5 9 3 1 10 3
|
||||
3 11 4 1 12 4 0 4 4
|
||||
1 13 5 5 12 5 4 0 5
|
||||
2 0 0 4 1 0 6 2 0
|
||||
7 3 1 2 4 1 6 3 1
|
||||
5 5 2 6 6 2 4 7 2
|
||||
7 8 3 1 9 3 3 10 3
|
||||
3 11 4 0 12 4 2 4 4
|
||||
1 13 5 4 12 5 0 0 5
|
||||
end
|
38
assets/models/cube.obj
Normal file
38
assets/models/cube.obj
Normal file
@@ -0,0 +1,38 @@
|
||||
# Blender v3.6.4 OBJ File: ''
|
||||
# www.blender.org
|
||||
o Cube
|
||||
v 1.000000 1.000000 -1.000000
|
||||
v 1.000000 -1.000000 -1.000000
|
||||
v 1.000000 1.000000 1.000000
|
||||
v 1.000000 -1.000000 1.000000
|
||||
v -1.000000 1.000000 -1.000000
|
||||
v -1.000000 -1.000000 -1.000000
|
||||
v -1.000000 1.000000 1.000000
|
||||
v -1.000000 -1.000000 1.000000
|
||||
vt 0.875000 0.500000
|
||||
vt 0.625000 0.750000
|
||||
vt 0.625000 0.500000
|
||||
vt 0.375000 1.000000
|
||||
vt 0.375000 0.750000
|
||||
vt 0.625000 0.000000
|
||||
vt 0.375000 0.250000
|
||||
vt 0.375000 0.000000
|
||||
vt 0.375000 0.500000
|
||||
vt 0.125000 0.750000
|
||||
vt 0.125000 0.500000
|
||||
vt 0.625000 0.250000
|
||||
vt 0.875000 0.750000
|
||||
vt 0.625000 1.000000
|
||||
s off
|
||||
f 5/1 3/2 1/3
|
||||
f 3/2 8/4 4/5
|
||||
f 7/6 6/7 8/8
|
||||
f 2/9 8/10 6/11
|
||||
f 1/3 4/5 2/9
|
||||
f 5/12 2/9 6/7
|
||||
f 5/1 7/13 3/2
|
||||
f 3/2 7/14 8/4
|
||||
f 7/6 5/12 6/7
|
||||
f 2/9 4/5 8/10
|
||||
f 1/3 3/2 4/5
|
||||
f 5/12 1/3 2/9
|
BIN
assets/sprites/alpha_mask.png
Normal file
BIN
assets/sprites/alpha_mask.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 942 B |
BIN
assets/sprites/alpha_mask_2.png
Normal file
BIN
assets/sprites/alpha_mask_2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
1136
include/JGL/JGL.h
1136
include/JGL/JGL.h
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace JGL {
|
||||
|
||||
#define stringify(name) # name
|
||||
|
||||
enum class Direction : u8 {
|
||||
None = 0,
|
||||
Vertical = 1,
|
||||
@@ -18,55 +21,61 @@ namespace JGL {
|
||||
}
|
||||
|
||||
static std::string to_string(const 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";
|
||||
}
|
||||
return stringify(direction);
|
||||
}
|
||||
|
||||
enum class MSAA_SAMPLE_RATE : u8 {
|
||||
MSAA_NONE = 0,
|
||||
// TODO come up with a cleaner way to type out sample-rates before release.
|
||||
// :shrug:, I'm not very creative. - Redacted.
|
||||
/*enum class MSAA_SAMPLE_RATE : u8 {
|
||||
NONE = 0,
|
||||
MSAA_2X = 1,
|
||||
MSAA_4X = 2,
|
||||
MSAA_8X = 3
|
||||
};
|
||||
|
||||
static std::string to_string(const JGL::MSAA_SAMPLE_RATE& sample_rate) {
|
||||
switch (sample_rate) {
|
||||
case MSAA_SAMPLE_RATE::MSAA_NONE:
|
||||
return "No MSAA";
|
||||
case MSAA_SAMPLE_RATE::MSAA_2X:
|
||||
return "MSAA 2x";
|
||||
case MSAA_SAMPLE_RATE::MSAA_4X:
|
||||
return "MSAA 4x";
|
||||
case MSAA_SAMPLE_RATE::MSAA_8X:
|
||||
return "MSAA 8x";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
enum class ANISOTROPY_SAMPLE_RATE : u8 {
|
||||
NONE = 0,
|
||||
ANISOTROPY_2X = 2,
|
||||
ANISOTROPY_4X = 4,
|
||||
ANISOTROPY_8X = 8,
|
||||
ANISOTROPY_16X = 16
|
||||
};*/
|
||||
|
||||
/// This enumeration is used by both MSAA and Anisotropic Filtering.
|
||||
/// However, higher rates may not be supported by one, or the other.
|
||||
/// This additionally depends on what your graphics supports.
|
||||
enum class SampleRate : u8 {
|
||||
NONE = 0, X0 = 0,
|
||||
X2 = 2,
|
||||
X4 = 4,
|
||||
X8 = 8,
|
||||
X16 = 16,
|
||||
};
|
||||
|
||||
enum class FilteringMode : u8 {
|
||||
NEAREST = 0, // Fastest for 2D, Sometimes causes graphical issues.
|
||||
BILINEAR = 1, // Fast and pretty, The best for 2D.
|
||||
|
||||
// Mipmapping uses approx 30% more v-ram.
|
||||
MIPMAP_NEAREST = 2, // Nearest with mipmaps. The fastest for 3D, Sometimes causes graphical issues.
|
||||
MIPMAP_BILINEAR = 3, // Bilinear with mipmaps, Fast and pretty. Uses more vram.
|
||||
MIPMAP_TRILINEAR = 4 // Prettier, But still looks weird at very steep angles.
|
||||
};
|
||||
|
||||
enum class WrappingMode : u8 {
|
||||
REPEAT = 0,
|
||||
MIRRORED_REPEAT = 1,
|
||||
CLAMP_TO_EDGE = 2,
|
||||
CLAMP_TO_BORDER = 3 // Effectively the same as clamp_to_edge
|
||||
};
|
||||
|
||||
|
||||
enum class ColorFormat : bool { RGB = false, RGBA = true };
|
||||
|
||||
static std::string to_string(const JGL::SampleRate& sample_rate) {
|
||||
return stringify(sample_rate);
|
||||
}
|
||||
static int to_int(const JGL::MSAA_SAMPLE_RATE& sample_rate) {
|
||||
switch (sample_rate) {
|
||||
case MSAA_SAMPLE_RATE::MSAA_NONE:
|
||||
return 0;
|
||||
case MSAA_SAMPLE_RATE::MSAA_2X:
|
||||
return 2;
|
||||
case MSAA_SAMPLE_RATE::MSAA_4X:
|
||||
return 4;
|
||||
case MSAA_SAMPLE_RATE::MSAA_8X:
|
||||
return 8;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
static int to_int(const JGL::SampleRate& sample_rate) {
|
||||
return (int)sample_rate;
|
||||
}
|
||||
}
|
||||
|
@@ -13,8 +13,6 @@ extern "C" typedef struct FT_LibraryRec_* FT_Library;
|
||||
|
||||
namespace JGL
|
||||
{
|
||||
|
||||
//bool Init();
|
||||
bool InitTextEngine();
|
||||
|
||||
/// A Font class implementation.
|
||||
@@ -23,7 +21,8 @@ namespace JGL
|
||||
public:
|
||||
/// Default constructor does not initialize any members
|
||||
Font() = default;
|
||||
Font(const std::filesystem::path& path);
|
||||
explicit Font(const std::filesystem::path& path);
|
||||
Font(const unsigned char* data, const size_t& size);
|
||||
/// Destructor handles freeing of the underlying asset handle.
|
||||
~Font();
|
||||
static Font LoadTTF(const std::filesystem::path& filepath);
|
||||
@@ -35,6 +34,6 @@ namespace JGL
|
||||
Vector2 MeasureString(const std::string& text, unsigned int ptSize);
|
||||
public:
|
||||
int index = 0;
|
||||
FT_Face face;
|
||||
FT_Face face = nullptr;
|
||||
};
|
||||
}
|
@@ -1,30 +1,70 @@
|
||||
#pragma once
|
||||
#include <J3ML/LinearAlgebra/Vector4.hpp>
|
||||
#include <J3ML/LinearAlgebra/Vector3.hpp>
|
||||
#include <J3ML/LinearAlgebra/DirectionVector.hpp>
|
||||
#include <J3ML/Geometry/Frustum.hpp>
|
||||
#include <Color4.hpp>
|
||||
|
||||
namespace JGL {
|
||||
class Light;
|
||||
class OmnidirectionalLight2D;
|
||||
class PointLight2D;
|
||||
class LightBase;
|
||||
class PointLight;
|
||||
class SpotLight;
|
||||
}
|
||||
|
||||
class JGL::Light {
|
||||
private:
|
||||
/// W in position seems to determine whether or not the light is omni-directional. 1 = omni 0 = point.
|
||||
/// Position is un-normalized screen space. For ex 500, 500, 1 for a light coming from where you're sitting.
|
||||
// TODO this can be consolidated.
|
||||
class JGL::LightBase {
|
||||
protected:
|
||||
/* The 4th number represents whether the light is a positional light.
|
||||
* In OpenGL, You can have positional lights, Like Point Lights or Spot Lights,
|
||||
* Or global lights, Like the sun. */
|
||||
Vector4 position = {0, 0, 0, 1};
|
||||
Color4 ambient = {0, 0, 0, 0};
|
||||
Color4 diffuse = {0, 0, 0, 0};
|
||||
Color4 specular = {0, 0, 0, 0};
|
||||
|
||||
float constant_attenuation = 1;
|
||||
float linear_attenuation = 0;
|
||||
float quadratic_attenuation = 0;
|
||||
public:
|
||||
Light(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular);
|
||||
Vector3 GetNormalizedSceenSpaceCoordinates() const;
|
||||
[[nodiscard]] Vector4 GetPosition() const;
|
||||
[[nodiscard]] Color4 GetAmbient() const;
|
||||
[[nodiscard]] Color4 GetDiffuse() const;
|
||||
[[nodiscard]] Color4 GetSpecular() const;
|
||||
|
||||
[[nodiscard]] float GetConstantAttenuation() const;
|
||||
[[nodiscard]] float GetLinearAttenuation() const;
|
||||
[[nodiscard]] float GetQuadraticAttenuation() const;
|
||||
public:
|
||||
/// Runs a calculation to determine the lights influence on a given point in 3D space.
|
||||
/// @note 0 would be no impact, 1 would be the light is at the same position.
|
||||
[[nodiscard]] virtual float GetAttenuationAtPosition(const Vector3& pos) const { return 0; }
|
||||
public:
|
||||
virtual ~LightBase() = default;
|
||||
};
|
||||
|
||||
class JGL::OmnidirectionalLight2D {
|
||||
private:
|
||||
/// Omni-directional lights.
|
||||
class JGL::PointLight : public LightBase {
|
||||
public:
|
||||
OmnidirectionalLight2D(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular);
|
||||
|
||||
[[nodiscard]] float GetAttenuationAtPosition(const Vector3& pos) const final;
|
||||
public:
|
||||
PointLight(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular, float constant_attenuation = 1, float linear_attenuation = 0, float quadratic_attenuation = 0);
|
||||
};
|
||||
|
||||
/// Lights which only effect things in a given cone.
|
||||
// TODO get attenuation at position for this.
|
||||
class JGL::SpotLight : public LightBase {
|
||||
protected:
|
||||
Matrix3x3 orientation;
|
||||
float exponent;
|
||||
float cut;
|
||||
public:
|
||||
/// Create a spotlight in 3D space.
|
||||
/// @param position The position of the light in 3D space.
|
||||
/// @param ro_mat Orientation of the light in 3D space.
|
||||
/// @param cone_size_degrees The size of the cone.
|
||||
/// @param exponent How focused the beam should be, Higher is more focused, Lower is less.
|
||||
/// @param ambient How much this light should effect the ambient light of the scene.
|
||||
/// @param diffuse
|
||||
/// @param specular How much this light should effect specular highlights of objects being influenced by it.
|
||||
SpotLight(const Vector3& position, const Matrix3x3& ro_mat, float cone_size_degrees, float exponent, const Color4& ambient, const Color4& diffuse, const Color4& specular, float constant_attenuation = 1, float linear_attenuation = 0, float quadratic_attenuation = 0);
|
||||
};
|
58
include/JGL/types/Light2D.h
Normal file
58
include/JGL/types/Light2D.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include <J3ML/LinearAlgebra/Vector4.hpp>
|
||||
#include <Colors.hpp>
|
||||
namespace JGL::J2D {
|
||||
class Light;
|
||||
class PointLight;
|
||||
}
|
||||
|
||||
// TODO Static member function to transform to RenderTarget space.
|
||||
// For ex, If the render target would be positioned at 10, 10 and the light is at 0, 0. Then fix it such that for the Render Target the light is at -10, -10.
|
||||
/// Only works if we know where the Render Target will be on screen beforehand. But that's the only scenario you'd need to be able to do this anyways - Redacted.
|
||||
class JGL::J2D::Light {
|
||||
protected:
|
||||
Vector4 light_pos = Vector4::Zero;
|
||||
Color4 ambient = Colors::White;
|
||||
Color4 diffuse = Colors::White;
|
||||
float constant_attenuation = 1;
|
||||
float linear_attenuation = 0;
|
||||
float quadratic_attenuation = 0;
|
||||
public:
|
||||
/// @returns The position of this light in screen-world 2D space.
|
||||
[[nodiscard]] Vector2 Position() const;
|
||||
/// @returns The ambient light color.
|
||||
[[nodiscard]] Color4 Ambient() const;
|
||||
/// @returns The diffuse light color.
|
||||
[[nodiscard]] Color4 Diffuse() const;
|
||||
/// @returns The constant attenuation value
|
||||
[[nodiscard]] float Constant() const;
|
||||
/// @returns The linear attenuation value.
|
||||
[[nodiscard]] float Linear() const;
|
||||
/// @returns The quadratic attenuation value.
|
||||
[[nodiscard]] float Quadratic() const;
|
||||
public:
|
||||
/// Set the position of this light in screen-world 2D space.
|
||||
void Position(const Vector2& new_position);
|
||||
/// Set the ambient light color.
|
||||
void Ambient(const Color4& new_ambient);
|
||||
/// Set the diffuse light color.
|
||||
void Diffuse(const Color4& new_diffuse);
|
||||
/// Set the constant attenuation value (0 - 1).
|
||||
void Constant(float new_constant_attenuation);
|
||||
/// Set the linear attenuation value (0 -1).
|
||||
void Linear(float new_linear_attenuation);
|
||||
/// Set the quadratic attenuation value (0 - 1).
|
||||
void Quadratic(float new_quadratic_attenuation);
|
||||
public:
|
||||
/// @returns float (0 - 1) representing how much this light impacts a given position in 2D space.
|
||||
[[nodiscard]] virtual float MeasureIntensityAtPosition(const Vector2& position) const = 0;
|
||||
};
|
||||
|
||||
class JGL::J2D::PointLight : public Light {
|
||||
public:
|
||||
[[nodiscard]] float MeasureIntensityAtPosition(const Vector2& position) const override;
|
||||
public:
|
||||
PointLight(const Vector2& position, const Color4& ambient, const Color4& diffuse,
|
||||
float constant_attenuation = 1, float linear_attenuation = 0, float quadratic_attenuation = 0);
|
||||
};
|
||||
|
@@ -1,4 +1,19 @@
|
||||
/// A simple wrapper for OpenGL materials. Lets you set things such as the "shininess" of your elements.
|
||||
class Material {
|
||||
|
||||
#include <Color4.hpp>
|
||||
#include <glad/glad.h>
|
||||
namespace JGL {
|
||||
class Material;
|
||||
}
|
||||
class JGL::Material {
|
||||
public:
|
||||
Color4 ambient_ref;
|
||||
Color4 diffuse_ref;
|
||||
Color4 specular_ref;
|
||||
Color4 emission;
|
||||
float shininess;
|
||||
public:
|
||||
Material(const Color4& ambient_reflection, const Color4& diffuse_reflection, const Color4& specular_reflection, const Color4& light_emission, float& shine);
|
||||
/// @param material Material to use.
|
||||
static void SetActiveMaterial(const Material& material);
|
||||
};
|
@@ -1,72 +1,128 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <glad/glad.h>
|
||||
#include <Color4.hpp>
|
||||
#include <Colors.hpp>
|
||||
#include <JGL/types/Enums.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <J3ML/LinearAlgebra/Vector2i.hpp>
|
||||
|
||||
namespace JGL {
|
||||
class RenderTarget;
|
||||
class Texture; // Forward declare.
|
||||
class Texture;
|
||||
}
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2i;
|
||||
|
||||
/// A
|
||||
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};
|
||||
Vector2i size{0, 0};
|
||||
bool using_depth = false;
|
||||
bool texture_created_by_us = false;
|
||||
GLuint framebuffer_object = 0;
|
||||
GLuint depth_buffer = 0;
|
||||
const Texture* texture = nullptr;
|
||||
MSAA_SAMPLE_RATE msaa_sample_rate = MSAA_SAMPLE_RATE::MSAA_NONE;
|
||||
SampleRate msaa_sample_rate = SampleRate::NONE;
|
||||
GLuint msaa_framebuffer_object = 0;
|
||||
GLuint msaa_depth_buffer = 0;
|
||||
GLuint msaa_render_buffer = 0;
|
||||
void Erase();
|
||||
public:
|
||||
/// @returns A handle to the RenderTarget currently in use by OpenGL.
|
||||
/// @note zero is returned when no RenderTarget is in use.
|
||||
static GLuint GetActiveGLFramebufferHandle();
|
||||
|
||||
/// Changes the RenderTarget that OpenGL will draw on.
|
||||
/// @param render_target The new RenderTarget for OpenGL to draw on.
|
||||
static void SetActiveGLRenderTarget(const RenderTarget& render_target);
|
||||
|
||||
/** Change the size of the renderable area of the Render Target. **/
|
||||
/// @param new_size new size in px.
|
||||
void Resize(const Vector2& new_size);
|
||||
void SetMSAAEnabled(MSAA_SAMPLE_RATE sample_rate);
|
||||
/// If you're using raw OpenGL commands to draw to this outside of J2D or J3D don't forget to do this.
|
||||
/// Blits the MSAA FBO onto the regular FBO if MSAA is enabled and or If you're rendering to a texture which uses mipmaps,
|
||||
/// It regenerates them so what you drew doesn't disappear at a distance. Otherwise it does nothing.
|
||||
void Blit() const;
|
||||
/// Changes the size of the render-able area of this RenderTarget.
|
||||
/// @param new_size new width & height in pixels.
|
||||
/// @note The data stored in this Render Target will be lost.
|
||||
void Resize(const Vector2i& new_size);
|
||||
|
||||
/// Blit a render target onto another. Will break if they're not the same size.
|
||||
static void Blit(const RenderTarget& source, RenderTarget* destination);
|
||||
/// Sets the MSAA mode for this Render Target.
|
||||
/// @returns false if the mode isn't available, true for success.
|
||||
[[nodiscard]] bool SetMSAAEnabled(SampleRate sample_rate);
|
||||
|
||||
/// Blit a single pixel onto a Render Target.
|
||||
/// If you're using MSAA and not using J2D || J3D Begin & End you must do this.
|
||||
void MSAABlit() const;
|
||||
|
||||
void RegenerateMipMaps();
|
||||
/// Copy one Render Target onto another. Will break if they're not the same size.
|
||||
// TODO support different sizes. If the destination is too small fix it for them but log a warning.
|
||||
static void Blit(const RenderTarget& source, RenderTarget* destination, const Vector2i& position = {0, 0});
|
||||
|
||||
/// Plots a single pixel onto a Render Target.
|
||||
/// @param color The color to render.
|
||||
/// @param position The position in the destination to draw the pixel.
|
||||
/// @param destination The destination RenderTarget.
|
||||
static void Blit(const Color4& color, const Vector2& position, RenderTarget* destination);
|
||||
[[nodiscard]] bool TextureCreatedByRenderTarget() const;
|
||||
static void Blit(const Color4& color, const Vector2i& position, RenderTarget* destination);
|
||||
|
||||
/// Blit an input texture onto this render target at the given position.
|
||||
/// @param source Source texture.
|
||||
/// @param destination Render Target to draw on.
|
||||
/// @param position Where in the destination to draw.
|
||||
static void Blit(const Texture* source, RenderTarget* destination, const Vector2i& position = {0, 0});
|
||||
|
||||
/// @returns Whether or not this Render Target created it's Texture.
|
||||
[[nodiscard]] bool OwnsTexture() const;
|
||||
public:
|
||||
[[nodiscard]] Vector2 GetDimensions() const;
|
||||
[[nodiscard]] MSAA_SAMPLE_RATE GetMSAASampleRate() const;
|
||||
/// Returns whether or not MSAA is enabled, If it is and you're not using J2D || J3D Begin / End,
|
||||
/// You need to run "Blit()" after rendering to your FBO before you show it.
|
||||
/// @note Also, If the texture wasn't made by the RenderTarget you don't want this. It would destroy the texture.
|
||||
/// @returns the size of the renderable area.
|
||||
[[nodiscard]] Vector2i GetDimensions() const;
|
||||
|
||||
/// @returns The currently selected MSAA Sample Rate.
|
||||
[[nodiscard]] SampleRate GetMSAASampleRate() const;
|
||||
|
||||
/// @returns Whether or not this Render Target is using MSAA.
|
||||
[[nodiscard]] bool MSAAEnabled() const;
|
||||
[[nodiscard]] const Texture* GetJGLTexture() const;
|
||||
[[nodiscard]] GLuint GetGLTextureHandle() const;
|
||||
|
||||
/// @returns The JGL texture this Render Target draws on.
|
||||
[[nodiscard]] const Texture* GetTexture() const;
|
||||
|
||||
/// @returns The OpenGL handle for the texture this Render Target draws on.
|
||||
[[nodiscard]] GLuint GetTextureHandle() const;
|
||||
|
||||
/// @returns The OpenGL handle for this Render Target.
|
||||
[[nodiscard]] GLuint GetGLFramebufferObjectHandle() const;
|
||||
|
||||
/// @returns The handle to the OpenGL buffer containing depth information
|
||||
/// @note Only valid if this Render Target is being used for 3D.
|
||||
[[nodiscard]] GLuint GetGLDepthBufferHandle() const;
|
||||
|
||||
/// @returns The color that should be used to clear this Render Target.
|
||||
[[nodiscard]] Color4 GetClearColor() const;
|
||||
/// Get the data back from the FBO. This is *not* async friendly.
|
||||
[[nodiscard]] std::vector<GLfloat> GetData() const;
|
||||
|
||||
/// @returns The color information for this Render Target.
|
||||
/// @note The CPU thread this is called from & the GPU cannot do anything while this takes place. It's very slow.
|
||||
[[nodiscard]] std::vector<GLfloat> GetPixels() const;
|
||||
|
||||
[[nodiscard]] static Vector2i MaxSize();
|
||||
public:
|
||||
/// Copy constructor. Will always set "texture_created_by_us" to true and use our own texture to avoid memleaks.
|
||||
/// Create a Render Target from a Render Target that already exists.
|
||||
/** @note Render Targets that are copies of another will copy the Texture.
|
||||
* This is so that deleting the copy doesn't delete the Texture of the original.
|
||||
*/
|
||||
RenderTarget(const RenderTarget& rhs);
|
||||
/// Create a render target for a texture that already exists. For adding to an existing texture.
|
||||
explicit RenderTarget(const Texture* texture, const Color4& clear_color = Colors::Black);
|
||||
/// 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, MSAA_SAMPLE_RATE sample_rate = MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
|
||||
/// Create a Render Target for a texture that already exists.
|
||||
/// @param texture The Texture that using this Render Target would draw on.
|
||||
/// @param clear_color The color to be used if you want to clear the Render Target.
|
||||
/// @note For Render Targets created this way, The destructor will not delete the texture.
|
||||
explicit RenderTarget(const Texture* texture, const Color4& clear_color = Colors::Transparent);
|
||||
|
||||
/// Create a Render Target with a new texture.
|
||||
/// @param size The width & height the Render Target should have.
|
||||
/// @param clear_color The color to be used if you want to clear the Render Target.
|
||||
/// @param use_depth Whether or not this Render Target will have depth information.
|
||||
/// @param sample_rate The MSAA sample rate this Render Target will use.
|
||||
explicit RenderTarget(const Vector2i& size, const Color4& clear_color = Colors::Transparent, bool use_depth = false,
|
||||
SampleRate sample_rate = SampleRate::NONE, FilteringMode filteirng_mode = FilteringMode::NEAREST);
|
||||
|
||||
/// Deletes this Render Target.
|
||||
/** @note If this Render Target was made with a Texture that already existed
|
||||
* the Texture will not be deleted. */
|
||||
~RenderTarget();
|
||||
|
||||
};
|
18
include/JGL/types/ShadowMap.h
Normal file
18
include/JGL/types/ShadowMap.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
#include <JGL/types/Light.h>
|
||||
|
||||
namespace JGL {
|
||||
class ShadowMap;
|
||||
}
|
||||
|
||||
/// You render your scene with all the static objects from the perspective of each static light to a ShadowMap.
|
||||
/// Then, for shadow casters which move. Or lights that move. You only redraw that object from the perspective of each light.
|
||||
/// Some of the approaches I saw for this were disgusting - Redacted.
|
||||
|
||||
class JGL::ShadowMap {
|
||||
private:
|
||||
RenderTarget shadow_map;
|
||||
private:
|
||||
void Create(const LightBase* Light);
|
||||
};
|
@@ -1,57 +1,69 @@
|
||||
#pragma once
|
||||
#include <ReImage/Image.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include<vector>
|
||||
#include <filesystem>
|
||||
#include <J3ML/LinearAlgebra/Vector2i.hpp>
|
||||
#include <Color3.hpp>
|
||||
#include <Color4.hpp>
|
||||
#include <glad/glad.h>
|
||||
#include <JGL/types/Enums.h>
|
||||
|
||||
namespace JGL {
|
||||
using namespace ReImage;
|
||||
enum class TextureFilteringMode : u8 {
|
||||
NEAREST = 0, //Fastest for 2D, Sometimes causes graphical issues.
|
||||
BILINEAR = 1, //Fast and pretty, The best for 2D.
|
||||
using J3ML::LinearAlgebra::Vector2i;
|
||||
class Texture;
|
||||
}
|
||||
|
||||
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 {
|
||||
private:
|
||||
void Erase();
|
||||
protected:
|
||||
GLuint texture_handle = 0;
|
||||
Vector2 texture_size = {0, 0};
|
||||
ReImage::TextureFlag texture_flags;
|
||||
ReImage::TextureFormat texture_format;
|
||||
TextureFilteringMode texture_filtering_mode;
|
||||
TextureWrappingMode texture_wrapping_mode;
|
||||
void load(Image* 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, const TextureFlag& flags = TextureFlag::INVERT_Y);
|
||||
Texture(Image* software_texture, const Vector2& size, const TextureFormat& format, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode);
|
||||
/* Initialize a texture filled with trash data
|
||||
this is primarily for the RenderTarget */
|
||||
explicit Texture(const Vector2& size);
|
||||
Texture(const Texture& rhs);
|
||||
~Texture();
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
/// Represents texture data loaded on the GPU. Contains a handle that can be passed to OpenGL draw calls.
|
||||
class JGL::Texture {
|
||||
protected:
|
||||
unsigned int texture_handle = 0;
|
||||
bool invert_y = false;
|
||||
Vector2i size = {0, 0};
|
||||
ColorFormat format = ColorFormat::RGBA;
|
||||
FilteringMode filtering_mode;
|
||||
WrappingMode wrapping_mode;
|
||||
SampleRate anisotropy;
|
||||
void load(const unsigned char* pixels);
|
||||
std::vector<unsigned char> png(const std::filesystem::path& file);
|
||||
std::vector<unsigned char> bmp(const std::filesystem::path& file);
|
||||
[[nodiscard]] bool SizeExceedsMaximum(const Vector2i& size);
|
||||
public:
|
||||
/// @returns A handle used to identify this texture.
|
||||
[[nodiscard]] unsigned int GetHandle() const;
|
||||
/// @returns The size of this texture.
|
||||
[[nodiscard]] Vector2i GetDimensions() const;
|
||||
/// @returns The filtering mode this texture is using.
|
||||
[[nodiscard]] FilteringMode GetFilteringMode() const;
|
||||
/// @returns The way this texture behaves when used on geometry of different sizes.
|
||||
[[nodiscard]] WrappingMode GetWrappingMode() const;
|
||||
/// @returns The current level of anisotropic filtering for this texture.
|
||||
[[nodiscard]] SampleRate GetAnisotropySampleRate() const;
|
||||
/// @returns The orientation of this texture in v-ram.
|
||||
/// @note true is right-side-up because OpenGL defaults to upside-down.
|
||||
[[nodiscard]] bool Inverted() const;
|
||||
[[nodiscard]] ColorFormat GetFormat() const;
|
||||
/// @returns The raw pixels this texture is made up of.
|
||||
/// @note This will read-back from the GPU. Slow.
|
||||
[[nodiscard]] std::vector<Color4> GetPixelData() const;
|
||||
public:
|
||||
/// Load a texture from a file,
|
||||
explicit Texture(const std::filesystem::path& file, FilteringMode filtering_mode = FilteringMode::BILINEAR,
|
||||
SampleRate anisotropy = SampleRate::NONE, WrappingMode wrapping_mode = WrappingMode::CLAMP_TO_EDGE, bool invert_y = true);
|
||||
/// Load a texture from raw pixels.
|
||||
Texture(const Color4* pixels, const Vector2i& size, FilteringMode filtering_mode = FilteringMode::BILINEAR,
|
||||
SampleRate anisotropy = SampleRate::NONE, WrappingMode wrapping_mode = WrappingMode::CLAMP_TO_EDGE);
|
||||
/// Load a texture from raw pixels.
|
||||
Texture(const Color3* pixels, const Vector2i& size, FilteringMode filtering_mode = FilteringMode::BILINEAR,
|
||||
SampleRate anisotropy = SampleRate::NONE, WrappingMode wrapping_mode = WrappingMode::CLAMP_TO_EDGE);
|
||||
/// Initialize a texture filled with trash data.
|
||||
/// @see RenderTarget
|
||||
explicit Texture(const Vector2i& size, FilteringMode filtering_mode = FilteringMode::NEAREST);
|
||||
Texture(const Texture& rhs);
|
||||
Texture() = default;
|
||||
~Texture();
|
||||
public:
|
||||
/// @returns True if this system supports anisotropy.
|
||||
// TODO add a similar mechanism for MSAA so the extension isn't required.
|
||||
static enum SampleRate MaxAnisotropySampleRate();
|
||||
/// @returns The biggest size for a texture on this system.
|
||||
/// @note on modern systems this is *usually* ridiculous.
|
||||
[[nodiscard]] static Vector2i MaxSize();
|
||||
};
|
35
include/JGL/types/TextureAtlas.h
Normal file
35
include/JGL/types/TextureAtlas.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <JGL/types/Texture.h>
|
||||
|
||||
namespace JGL {
|
||||
struct AtlasRegion {
|
||||
Vector2i position = { 0, 0 };
|
||||
Vector2i size = { 0, 0 };
|
||||
};
|
||||
class TextureAtlas;
|
||||
}
|
||||
|
||||
// A texture which actually contains multiple such that you can pass it in to draw sprite or draw partial sprite.
|
||||
// It also makes for more efficient sprite batch because you don't have to swap textures between.
|
||||
class JGL::TextureAtlas : public Texture {
|
||||
protected:
|
||||
std::vector<AtlasRegion> regions;
|
||||
public:
|
||||
unsigned int RegionCount() { return regions.size(); }
|
||||
[[nodiscard]] AtlasRegion GetRegion(unsigned int region) const;
|
||||
public:
|
||||
/// Create a texture atlas from multiple separate images.
|
||||
/// @param pixels The array(s) of pixels.
|
||||
/// @param sizes The size of each image.
|
||||
/// @param texture_count The number of textures this atlas will contain.
|
||||
TextureAtlas(const Color4** pixels, const Vector2i** sizes, unsigned int texture_count, FilteringMode filtering_mode = FilteringMode::NEAREST);
|
||||
|
||||
/// Create a texture atlas from a single image
|
||||
/// @param pixels The array of pixels.
|
||||
/// @param size The size of the image.
|
||||
/// @param regions The individual regions that will make up your atlas.
|
||||
/// @param region_count The number of regions there are.
|
||||
TextureAtlas(const Color4* pixels, const Vector2i& size, AtlasRegion** regions, unsigned int region_count, FilteringMode filtering_mode = FilteringMode::NEAREST, SampleRate anisotropy = SampleRate::NONE);
|
||||
~TextureAtlas() = default;
|
||||
};
|
@@ -20,21 +20,21 @@ private:
|
||||
bool spin_lock = false;
|
||||
void load(const GLfloat* data, const long& size);
|
||||
void load(const GLuint* data, const long& size);
|
||||
void SetData(void* data, const long& length);
|
||||
void UpdateData(void* data, const long& offset, const long& length);
|
||||
void SetData(void* data, const long& count);
|
||||
void UpdateData(void* data, const long& offset, const long& count);
|
||||
void Erase();
|
||||
public:
|
||||
VRamList(const GLuint* data, const long& length);
|
||||
VRamList(const GLfloat* data, const long& length);
|
||||
VRamList(const Vector2* data, const long& length);
|
||||
VRamList(const Vector3* data, const long& length);
|
||||
VRamList(const Vector4* data, const long& length);
|
||||
VRamList(const GLuint* data, const long& count);
|
||||
VRamList(const GLfloat* data, const long& count);
|
||||
VRamList(const Vector2* data, const long& count);
|
||||
VRamList(const Vector3* data, const long& count);
|
||||
VRamList(const Vector4* data, const long& count);
|
||||
|
||||
~VRamList();
|
||||
/** Copying around the VBO data to a new VBO like this is slow.
|
||||
* Pass to function by const reference or pointer always. */
|
||||
VRamList(const VRamList& rhs);
|
||||
VRamList() = default;
|
||||
VRamList() : list_handle(0), num_elements(0), element_array_buffer(false), spin_lock(false) {}
|
||||
public:
|
||||
[[nodiscard]] GLuint GetHandle() const;
|
||||
/// Returns the number of elements in the list.
|
||||
@@ -46,25 +46,23 @@ public:
|
||||
[[nodiscard]] std::vector<GLfloat> GetDataF() const;
|
||||
[[nodiscard]] std::vector<GLuint> GetDataUI() const;
|
||||
[[nodiscard]] bool IsFloatArray() const;
|
||||
/** Replace the data of an existing VBO in it's entirety. Must be same type.
|
||||
* "length" refers to the number of elements in data. Not the number of bytes. */
|
||||
void SetData(const GLfloat* data, const long& length);
|
||||
void SetData(const Vector2* data, const long& length);
|
||||
void SetData(const Vector3* data, const long& length);
|
||||
void SetData(const Vector4* data, const long& length);
|
||||
/** Replace the data of an existing VBO in it's entirety. Must be same type. */
|
||||
void SetData(const GLfloat* data, const long& count);
|
||||
void SetData(const Vector2* data, const long& count);
|
||||
void SetData(const Vector3* data, const long& count);
|
||||
void SetData(const Vector4* data, const long& count);
|
||||
|
||||
void SetData(const GLuint* data, const long& length);
|
||||
void SetData(const Vector2i* data, const long& length);
|
||||
void SetData(const GLuint* data, const long& count);
|
||||
void SetData(const Vector2i* data, const long& count);
|
||||
|
||||
/** Update only a portion of the data in a VBO. Must be same type.
|
||||
* "length" refers to the number of elements in data. Not the number of bytes.
|
||||
* "offset" refers the number of Typename T into the buffer the data you want to change is.
|
||||
* For ex, offset 0 and length of 1 overwrites the first value. Offset 1 the second etc */
|
||||
void UpdateData(const GLfloat* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector2* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector3* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector4* data, const long& offset, const long& length);
|
||||
void UpdateData(const GLfloat* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector2* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector3* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector4* data, const long& offset, const long& count);
|
||||
|
||||
void UpdateData(const GLuint* data, const long& offset, const long& length);
|
||||
void UpdateData(const Vector2i* data, const long& offset, const long& length);
|
||||
void UpdateData(const GLuint* data, const long& offset, const long& count);
|
||||
void UpdateData(const Vector2i* data, const long& offset, const long& count);
|
||||
};
|
@@ -29,10 +29,10 @@ protected:
|
||||
protected:
|
||||
std::vector<Animation> animations{};
|
||||
protected:
|
||||
VRamList vertices;
|
||||
VRamList indices;
|
||||
VRamList normals;
|
||||
VRamList texture_coordinates;
|
||||
VRamList* vertices;
|
||||
VRamList* indices;
|
||||
VRamList* normals;
|
||||
VRamList* texture_coordinates;
|
||||
protected:
|
||||
/** For models which are not animated, This is intended for a low quality version in
|
||||
* system memory for calculations to be done on the CPU. For models that are, the default pose of the model is here.
|
||||
@@ -43,10 +43,10 @@ protected:
|
||||
std::vector<Normal> local_normals{};
|
||||
public:
|
||||
/** Don't use these for anything other than drawing because the GPU is gonna spin during read-back */
|
||||
[[nodiscard]] VRamList GetVertices() const;
|
||||
[[nodiscard]] VRamList GetIndices() const;
|
||||
[[nodiscard]] VRamList GetNormals() const;
|
||||
[[nodiscard]] VRamList GetTextureCoordinates() const;
|
||||
[[nodiscard]] const VRamList* GetVertices() const;
|
||||
[[nodiscard]] const VRamList* GetIndices() const;
|
||||
[[nodiscard]] const VRamList* GetNormals() const;
|
||||
[[nodiscard]] const VRamList* GetTextureCoordinates() const;
|
||||
|
||||
/** These are for cpu side calculations */
|
||||
[[nodiscard]] std::vector<Vertex> GetLocalVertices() const;
|
||||
@@ -83,6 +83,7 @@ public:
|
||||
[[nodiscard]] AABB GetMEAABB(const Matrix3x3& rotation_matrix, const Vector3& scale = Vector3::One, const Vector3& translate_part = Vector3::Zero) const;
|
||||
[[nodiscard]] AABB GetMEAABB(const Matrix4x4& instance_matrix, bool translate = false) const;
|
||||
public:
|
||||
VertexArray() = default;
|
||||
/// Vertices are required, Everything else is optional.
|
||||
VertexArray(const Vertex* vertex_positions, const long& vp_length, const unsigned int* vertex_indices = nullptr, const long& vi_length = 0,
|
||||
const Normal* vertex_normals = nullptr, const long& vn_length = 0, const TextureCoordinate* texture_coordinates = nullptr, const long& vt_length = 0);
|
||||
@@ -90,9 +91,13 @@ public:
|
||||
/// Vertices are required, Everything else is optional.
|
||||
explicit VertexArray(const std::vector<Vertex>& vertex_positions, const std::vector<unsigned int>& vertex_indices = {},
|
||||
const std::vector<Normal>& vertex_normals = {}, const std::vector<TextureCoordinate>& texture_coordinates = {});
|
||||
|
||||
static VertexArray LoadWavefrontOBJ(const std::string& file_text);
|
||||
static VertexArray LoadAMO(const std::string& file_text);
|
||||
};
|
||||
|
||||
|
||||
using namespace JGL;
|
||||
|
||||
static VertexArray Animate(int animation_id, float animation_time);
|
||||
static VertexArray Animate(const AnimationState& anim_state);
|
||||
|
||||
|
179
main.cpp
179
main.cpp
@@ -1,17 +1,21 @@
|
||||
#include <JGL/JGL.h>
|
||||
#include <rewindow/types/window.h>
|
||||
#include <ReWindow/types/Window.h>
|
||||
#include <Colors.hpp>
|
||||
#include <chrono>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <J3ML/Geometry/AABB.hpp>
|
||||
#include <ReWindow/Logger.h>
|
||||
#include <JGL/types/VertexArray.h>
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL::Fonts;
|
||||
using namespace JGL;
|
||||
using JGL::Font;
|
||||
|
||||
JGL::Font FreeSans;
|
||||
JGL::Font Jupiteroid;
|
||||
float fps = 0.0f;
|
||||
|
||||
/// A draggable 2D point that highlights when moused over and when clicked.
|
||||
class Gizmo
|
||||
{
|
||||
public:
|
||||
@@ -21,6 +25,15 @@ public:
|
||||
bool hovered = false;
|
||||
Vector2 position;
|
||||
float range = 6.f;
|
||||
float base_radius = 3.f;
|
||||
float hover_radius = 6.f;
|
||||
float drag_radius = 4.f;
|
||||
Color4 base_color = Colors::Reds::Salmon;
|
||||
Color4 hover_color = Colors::Reds::Firebrick;
|
||||
Color4 drag_color = Colors::White;
|
||||
float lerp_rate = 0.25f;
|
||||
float text_scale = 1.f;
|
||||
int text_size = 12;
|
||||
|
||||
void Grab() {
|
||||
if (hovered)
|
||||
@@ -32,27 +45,25 @@ public:
|
||||
|
||||
void Update(const Vector2& mouse) {
|
||||
if (dragging)
|
||||
position = position.Lerp(mouse, 0.25f);
|
||||
position = position.Lerp(mouse, lerp_rate);
|
||||
|
||||
hovered = mouse.Distance(position) < range;
|
||||
}
|
||||
|
||||
void Draw() {
|
||||
|
||||
|
||||
if (dragging)
|
||||
J2D::DrawPoint(Colors::White, position, 4.f);
|
||||
J2D::DrawPoint(drag_color, position, drag_radius);
|
||||
else if (hovered)
|
||||
J2D::DrawPoint(Colors::Reds::Crimson, position, 6.f);
|
||||
J2D::DrawPoint(hover_color, position, hover_radius);
|
||||
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);
|
||||
J2D::DrawPoint(base_color, position, base_radius);
|
||||
|
||||
J2D::DrawString(Colors::White, std::format("{:.1f},{:.1f}", position.x, position.y), position.x, position.y, text_scale, text_size);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/// A 3D Camera Controller.
|
||||
class Camera {
|
||||
public:
|
||||
Vector3 position = {0,0,0};
|
||||
@@ -96,48 +107,47 @@ Gizmo b({200, 250});
|
||||
Gizmo c({350, 300});
|
||||
Gizmo d({450, 250});
|
||||
|
||||
JGL::Font FreeSans;
|
||||
Texture* image;
|
||||
Texture* image2;
|
||||
Texture* image_mask;
|
||||
RenderTarget* j2d_render_target;
|
||||
RenderTarget* image2_render_target;
|
||||
|
||||
class JGLDemoWindow : public ReWindow::RWindow
|
||||
class JGLDemoWindow : public ReWindow::OpenGLWindow
|
||||
{
|
||||
public:
|
||||
void initGL() {
|
||||
camera = new Camera;
|
||||
|
||||
if (!JGL::Init(getSize(), 75, 100))
|
||||
if (!JGL::Init({ GetSize().x, GetSize().y}, 75, 100))
|
||||
Logger::Fatal("Initialization failed.");
|
||||
|
||||
// Load a custom font.
|
||||
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
|
||||
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.ttf");
|
||||
|
||||
glClearColor(0.f, 0.f, 0.f, 0.f);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glDepthMask(GL_TRUE);
|
||||
image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR);
|
||||
j2d_render_target = new RenderTarget({540, 540}, {0,0,0,0}, false, MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
image2 = image;
|
||||
image2_render_target = new RenderTarget(image2);
|
||||
image = new Texture("assets/sprites/Re3D.png", FilteringMode::MIPMAP_NEAREST, JGL::SampleRate::X16);
|
||||
image_mask = new Texture("assets/sprites/alpha_mask_2.png");
|
||||
j2d_render_target = new RenderTarget({540, 500}, {0,0,0,0}, false,
|
||||
SampleRate::NONE, FilteringMode::MIPMAP_TRILINEAR);
|
||||
|
||||
J2D::Begin(image2_render_target);
|
||||
J2D::DrawString(Colors::Red, "TEST", 0, 16, 1, 16, FreeSans);
|
||||
J2D::FillRect(Colors::Blue, {0,0}, {4,4});
|
||||
J2D::End();
|
||||
//Texture::MultiplyByAlphaMask(*image, *image_mask);
|
||||
}
|
||||
|
||||
Vector3 textAngle = {0,0,0};
|
||||
EulerAngleXYZ textAngle = {0,0,0};
|
||||
float fov = 90;
|
||||
float pulse = 0;
|
||||
float sprite_radians = 0;
|
||||
bool fov_increasing = true;
|
||||
int blit_pos = 0;
|
||||
|
||||
void display() {
|
||||
pulse += 20 * delta_time;
|
||||
float dt = GetDeltaTime();
|
||||
|
||||
float dt = 1.f / fps;
|
||||
JGL::Update(getSize());
|
||||
JGL::Update({ GetSize().x, GetSize().y });
|
||||
|
||||
if (fov_increasing)
|
||||
fov += 0.025;
|
||||
@@ -151,7 +161,7 @@ public:
|
||||
//J3D::ChangeFOV(fov);
|
||||
|
||||
sprite_radians += 0.005;
|
||||
textAngle.y += (5.f * delta_time);
|
||||
textAngle.yaw += 1;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
@@ -159,28 +169,29 @@ public:
|
||||
camera->render();
|
||||
// All 3D elements of the scene and JGL elements *must* be rendered before the 2D stuff
|
||||
/* if rendering to screen space directly. */
|
||||
auto test_light = PointLight({2,1,2}, {0, 0, 0, 255}, {pulse, pulse, pulse, 255}, {0,0,0}, 0, 0.1, 0.01);
|
||||
// If a 3D object has transparency. The things you'd like to see through it must be drawn before.
|
||||
|
||||
J3D::Begin();
|
||||
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
|
||||
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-0.33,0.25,1});
|
||||
J3D::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f}, 1.f, 32, FreeSans, textAngle * 100, true);
|
||||
J3D::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f}, 1.f, 32, FreeSans, textAngle, true);
|
||||
//J3D::WireframeSphere(Colors::Green, {0,0,0.5f}, 0.25f, 1, 128, 128);
|
||||
Sphere sphere[5] = {{{0,0, 0.5f}, 0.2125}, {{0,0, 0.5f}, 0.025},{{0,0, 0.5f}, 0.05},{{0,0, 0.5f}, 0.075},{{0,0, 0.5f}, 0.1}};
|
||||
J3D::BatchWireframeRevoSphere(Colors::Green, sphere, 1, 1, 16, 16, true);
|
||||
//J3D::FillAABB(Colors::Whites::AliceBlue, {0,0,0.5f}, {0.1f, 0.05f, 0.1f});
|
||||
//J3D::WireframeAABB(Colors::Gray, {0,0,0.5f}, {0.11f, 0.06f, 0.11f});
|
||||
Sphere sphere = {{1,0, 0.5f}, 0.2125};
|
||||
J3D::BatchWireframeRevoSphere(Colors::Green, &sphere, 1, 1, 8, 8, true);
|
||||
J3D::RequiredLight(&test_light);glEnable(GL_NORMALIZE);
|
||||
|
||||
J3D::WireframeOBB(Colors::Red, {0, 0, 1.5f}, {0.40f, 0.10f, 0.10f}, {0,textAngle.y, 0});
|
||||
|
||||
//J3D::DrawCubicBezierCurve(Colors::Blue, {0,0,0.3}, {0,0,0.5}, {0.2,0,0.3}, {0.2, 0.3, 0.1}, 30);
|
||||
|
||||
//J3D::WireframeIcosahedron(Colors::Green, {0,0,0.5f}, 0.125f, 1.f);
|
||||
J3D::FillAABB(Colors::Whites::AliceBlue, {0,0,0.5f}, {0.05f, 0.05f, 0.05f});
|
||||
J3D::WireframeAABB(Colors::Yellow, {0.5, 0, 0.5}, {0.125, 0.125, 0.125}, 1);
|
||||
Matrix3x3 cube_rotation = Matrix3x3(Quaternion(EulerAngleXYZ(0, 20, 0)));
|
||||
J3D::WireframeOBB(Colors::Red, {0.25, 0 , 0.5}, {0.125, 0.125, 0.125}, cube_rotation, 1);
|
||||
J3D::End();
|
||||
|
||||
J2D::Begin(j2d_render_target, true);
|
||||
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
|
||||
J2D::DrawSprite(image2, {300, 400}, sprite_radians * 0.10f, {0.5,0.5}, {1, 1}, Colors::White);
|
||||
J2D::DrawSprite(image, {300, 400}, sprite_radians * 0.10f, {0.5,0.5}, {1, 1}, Colors::White);
|
||||
J2D::DrawMirrorSprite(image, {400, 300}, Direction::Horizontal | Direction::Vertical, sprite_radians, {0.5,0.5}, {1, 1}, Colors::White);
|
||||
J2D::DrawPartialSprite(image, {225, 300}, image->GetDimensions() * 0.25, image->GetDimensions() * 0.75, sprite_radians, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::DrawPartialSprite(image, Vector2(225, 300), Vector2(image->GetDimensions()) * 0.25, Vector2(image->GetDimensions()) * 0.75, sprite_radians, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::FillRect(Colors::Pinks::HotPink, {68, 120}, {32, 32});
|
||||
J2D::FillGradientRect(Colors::Red, Colors::Blue, Direction::Diagonal_SWNE, {100,52}, {100,100});
|
||||
J2D::FillRoundedRect(Colors::Red, {200, 52}, {100, 100}, 8, 8);
|
||||
@@ -196,14 +207,12 @@ public:
|
||||
J2D::DrawGradientLine(Colors::Red, Colors::Blue, {105, 375}, {200, 275}, 2);
|
||||
auto result = Jupiteroid.MeasureString("Jupiteroid Font", 16);
|
||||
|
||||
//J2D::FillRect(Colors::Gray, {0, 0}, result);
|
||||
J2D::DrawString(Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid);
|
||||
J2D::DrawString(Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 16, 1,16, Jupiteroid);
|
||||
J2D::DrawString(Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 33, 1,16, Jupiteroid);
|
||||
J2D::DrawString(Colors::White, "Framerate: " + std::to_string((int) fps), 0, 48, 1, 16, Jupiteroid);
|
||||
std::array<Vector2, 5> polygon = {Vector2(200, 400), {220, 420}, {220, 430}, {230, 410}, {200, 400}};
|
||||
J2D::OutlinePolygon(Colors::White, polygon.data(), polygon.size());
|
||||
//J2D::FillPolygon(Colors::White, {{200, 400}, {220, 420}, {220, 430}, {230, 410}, {200, 400}});
|
||||
J2D::DrawCubicBezierCurve(Colors::Blues::CornflowerBlue,
|
||||
a.position,
|
||||
b.position,
|
||||
@@ -216,45 +225,56 @@ public:
|
||||
c.Draw();
|
||||
d.Draw();
|
||||
J2D::End();
|
||||
RenderTarget::Blit(Colors::Red, {0, 0}, j2d_render_target);
|
||||
|
||||
//Draw the Render Target that we just drew all that stuff onto.
|
||||
|
||||
J2D::Begin();
|
||||
J2D::DrawSprite(j2d_render_target, {0, 0}, 0, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::DrawSprite(image2_render_target, {300, 500}, 0, {0.5, 0.5}, {1,1}, Colors::White);
|
||||
J2D::DrawRenderTarget(j2d_render_target, {0, 0});
|
||||
J2D::DrawSprite(image, image_mask, {0, 0}, 0.25, {0.5, 0.5}, {1,1});
|
||||
J2D::End();
|
||||
|
||||
}
|
||||
|
||||
void OnRefresh(float elapsed) override {
|
||||
if (isKeyDown(Keys::RightArrow))
|
||||
|
||||
fps = GetRefreshRate();
|
||||
|
||||
if (IsKeyDown(Keys::RightArrow))
|
||||
camera->angle.y += 45.f * elapsed;
|
||||
if (isKeyDown(Keys::LeftArrow))
|
||||
if (IsKeyDown(Keys::LeftArrow))
|
||||
camera->angle.y -= 45.f * elapsed;
|
||||
if (isKeyDown(Keys::UpArrow))
|
||||
if (IsKeyDown(Keys::UpArrow))
|
||||
camera->angle.x -= 45.f * elapsed;
|
||||
if (isKeyDown(Keys::DownArrow))
|
||||
if (IsKeyDown(Keys::DownArrow))
|
||||
camera->angle.x += 45.f * elapsed;
|
||||
if (isKeyDown(Keys::Space))
|
||||
if (IsKeyDown(Keys::Space))
|
||||
camera->position.y += 1.f * elapsed;
|
||||
if (isKeyDown(Keys::LeftShift))
|
||||
if (IsKeyDown(Keys::LeftShift))
|
||||
camera->position.y -= 1.f * elapsed;
|
||||
|
||||
//This is wrong of course. Just for testing purposes.
|
||||
if (IsKeyDown(Keys::W))
|
||||
camera->position.z += 1.f * elapsed;
|
||||
if (IsKeyDown(Keys::S))
|
||||
camera->position.z -= 1.f * elapsed;
|
||||
if (IsKeyDown(Keys::A))
|
||||
camera->position.x += 1.f * elapsed;
|
||||
if (IsKeyDown(Keys::D))
|
||||
camera->position.x -= 1.f * elapsed;
|
||||
|
||||
|
||||
auto mouse = GetMouseCoordinates();
|
||||
a.Update(mouse);
|
||||
b.Update(mouse);
|
||||
c.Update(mouse);
|
||||
d.Update(mouse);
|
||||
a.Update({(float) mouse.x, (float) mouse.y});
|
||||
b.Update({(float) mouse.x, (float) mouse.y});
|
||||
c.Update({(float) mouse.x, (float) mouse.y});
|
||||
d.Update({(float) mouse.x, (float) mouse.y});
|
||||
display();
|
||||
int glError = glGetError();
|
||||
if (glError != GL_NO_ERROR)
|
||||
std::cout << glError << std::endl;
|
||||
glSwapBuffers();
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
|
||||
void OnMouseButtonDown(const ReWindow::WindowEvents::MouseButtonDownEvent & ev) override
|
||||
void OnMouseButtonDown(const ReWindow::MouseButtonDownEvent & ev) override
|
||||
{
|
||||
RWindow::OnMouseButtonDown(ev);
|
||||
a.Grab();
|
||||
@@ -263,7 +283,7 @@ public:
|
||||
d.Grab();
|
||||
}
|
||||
|
||||
void OnMouseButtonUp(const ReWindow::WindowEvents::MouseButtonUpEvent & ev) override
|
||||
void OnMouseButtonUp(const ReWindow::MouseButtonUpEvent & ev) override
|
||||
{
|
||||
RWindow::OnMouseButtonUp(ev);
|
||||
a.Release();
|
||||
@@ -273,25 +293,36 @@ public:
|
||||
}
|
||||
|
||||
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {return true;}
|
||||
JGLDemoWindow() : ReWindow::RWindow() {}
|
||||
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height){}
|
||||
JGLDemoWindow(const std::string& title, int width, int height) : ReWindow::OpenGLWindow(title, width, height, 2, 1) {}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
auto* window = new JGLDemoWindow("JGL Demo Window", 1280, 720);
|
||||
window->setRenderer(RenderingAPI::OPENGL);
|
||||
window->Open();
|
||||
if (!window->Open())
|
||||
exit(-1);
|
||||
window->initGL();
|
||||
window->setResizable(true);
|
||||
window->setVsyncEnabled(false);
|
||||
window->SetResizable(true);
|
||||
window->SetVsyncEnabled(false);
|
||||
|
||||
while (window->isAlive()) {
|
||||
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
||||
window->pollEvents();
|
||||
window->refresh();
|
||||
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();
|
||||
}
|
||||
std::ifstream file("assets/models/cube.amo");
|
||||
if (!file.is_open())
|
||||
return -1;
|
||||
|
||||
/*
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
std::string file_text = buffer.str();
|
||||
file.close();
|
||||
std::cout << file_text << std::endl;
|
||||
|
||||
auto result = VertexArray::LoadAMO(file_text);
|
||||
*/
|
||||
|
||||
//ReWindow::Logger::Error.EnableConsole(false);
|
||||
//ReWindow::Logger::Warning.EnableConsole(false);
|
||||
//ReWindow::Logger::Debug.EnableConsole(false);
|
||||
|
||||
while (window->IsAlive())
|
||||
window->ManagedRefresh();
|
||||
return 0;
|
||||
}
|
8671
src/Fonts.cpp
Normal file
8671
src/Fonts.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1517
src/JGL.cpp
1517
src/JGL.cpp
File diff suppressed because it is too large
Load Diff
58
src/ShapeCache.cpp
Normal file
58
src/ShapeCache.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <JGL/JGL.h>
|
||||
|
||||
static std::array<Vector3, 8> cube_v {
|
||||
Vector3(1.0f, 1.0f, -1.0f),
|
||||
Vector3(1.0f, -1.0f, -1.0f),
|
||||
Vector3(1.0f, 1.0f, 1.0f),
|
||||
Vector3(1.0f, -1.0f, 1.0f),
|
||||
Vector3(-1.0f, 1.0f, -1.0f),
|
||||
Vector3(-1.0f, -1.0f, -1.0f),
|
||||
Vector3(-1.0f, 1.0f, 1.0f),
|
||||
Vector3(-1.0f, -1.0f, 1.0f)
|
||||
};
|
||||
|
||||
std::array<unsigned int, 36> cube_ind {
|
||||
4, 2, 0,
|
||||
2, 7, 3,
|
||||
6, 5, 7,
|
||||
1, 7, 5,
|
||||
0, 3, 1,
|
||||
4, 1, 5,
|
||||
4, 6, 2,
|
||||
2, 6, 7,
|
||||
6, 4, 5,
|
||||
1, 3, 7,
|
||||
0, 2, 3,
|
||||
4, 0, 1
|
||||
};
|
||||
|
||||
std::array<Vector3, 8> cube_vn {
|
||||
Vector3( 0.816f, 0.408f, -0.408f),
|
||||
Vector3( 0.333f, -0.667f, -0.667f),
|
||||
Vector3( 0.333f, 0.667f, 0.667f),
|
||||
Vector3( 0.816f, -0.408f, 0.408f),
|
||||
Vector3(-0.333f, 0.667f, -0.667f),
|
||||
Vector3(-0.816f, -0.408f, -0.408f),
|
||||
Vector3(-0.816f, 0.408f, 0.408f),
|
||||
Vector3(-0.333f, -0.667f, 0.667f)
|
||||
};
|
||||
|
||||
std::array<Vector2, 4> square_v { Vector2(0, 0), {1, 0}, {1, -1}, {0, -1} };
|
||||
std::array<GLfloat, 3> square_vn {0, 0, 1};
|
||||
|
||||
void JGL::ShapeCache::Init() {
|
||||
if (!cube_vertex_data)
|
||||
cube_vertex_data = new VRamList(cube_v.data(), cube_v.size());
|
||||
|
||||
if (!cube_index_data)
|
||||
cube_index_data = new VRamList(cube_ind.data(), cube_ind.size());
|
||||
|
||||
if (!cube_normal_data)
|
||||
cube_normal_data = new VRamList(cube_vn.data(), cube_vn.size());
|
||||
|
||||
if (!square_origin_topleft_vertex_data)
|
||||
square_origin_topleft_vertex_data = new VRamList(square_v.data(), square_v.size());
|
||||
|
||||
if (!j2d_default_normal_data)
|
||||
j2d_default_normal_data = new VRamList(square_vn.data(), square_vn.size());
|
||||
}
|
1085
src/renderer/OpenGL/J2D.cpp
Normal file
1085
src/renderer/OpenGL/J2D.cpp
Normal file
File diff suppressed because it is too large
Load Diff
570
src/renderer/OpenGL/J3D.cpp
Normal file
570
src/renderer/OpenGL/J3D.cpp
Normal file
@@ -0,0 +1,570 @@
|
||||
#include <JGL/JGL.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <J3ML/Geometry/AABB.hpp>
|
||||
#include <J3ML/Geometry/OBB.hpp>
|
||||
#include <J3ML/Algorithm/Bezier.hpp>
|
||||
#include <JGL/types/Light.h>
|
||||
#include "internals/internals.h"
|
||||
|
||||
|
||||
std::array<GLfloat, 16> JGL::OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far) {
|
||||
std::array<GLfloat, 16> result{};
|
||||
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;
|
||||
}
|
||||
|
||||
void J3D::ChangeFOV(float fov) {
|
||||
J3D::fov = fov;
|
||||
}
|
||||
|
||||
void J3D::ChangeFarPlane(float far_plane) {
|
||||
J3D::far_plane = far_plane;
|
||||
}
|
||||
|
||||
void J3D::RequiredLight(const LightBase* light) {
|
||||
bool success = false;
|
||||
for (auto& i : current_state.required_lights)
|
||||
if (i == nullptr) {
|
||||
i = light;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!success)
|
||||
Logger::Error("You cannot specify more than 8 required lights.");
|
||||
}
|
||||
|
||||
void J3D::OptionalLights(const LightBase** lights, const size_t& light_count) {
|
||||
for (size_t i = 0; i < light_count; i++)
|
||||
current_state.optional_lights.push_back(lights[i]);
|
||||
}
|
||||
|
||||
void J3D::Begin(bool two_pass) {
|
||||
auto aspect = (float) window_size.x / (float) window_size.y;
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glLoadMatrixf(OpenGLPerspectiveProjectionRH(fov, aspect, 0.001, far_plane).data());
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
|
||||
//Get what the draw color was before we did anything.
|
||||
glGetFloatv(GL_CURRENT_COLOR, current_state.draw_color);
|
||||
glColor4fv(current_state.draw_color);
|
||||
|
||||
if (!glIsEnabled(GL_DEPTH_TEST))
|
||||
current_state.depth_test = false,
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
else
|
||||
current_state.depth_test = true;
|
||||
|
||||
current_state.vertex_array = false;
|
||||
if (!glIsEnabled(GL_VERTEX_ARRAY))
|
||||
current_state.vertex_array = false,
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
|
||||
if (glIsEnabled(GL_NORMAL_ARRAY))
|
||||
current_state.normal_array = true,
|
||||
glDisableClientState(GL_NORMAL_ARRAY);
|
||||
|
||||
if (glIsEnabled(GL_TEXTURE_COORD_ARRAY))
|
||||
current_state.texture_coordinate_array = true,
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
if (glIsEnabled(GL_TEXTURE_2D))
|
||||
current_state.texture_2D = true,
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
current_state.cull_face = false;
|
||||
if (glIsEnabled(GL_CULL_FACE))
|
||||
current_state.cull_face = true,
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
if (!glIsEnabled(GL_BLEND))
|
||||
current_state.blend = false,
|
||||
glEnable(GL_BLEND),
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
else
|
||||
current_state.blend = true;
|
||||
|
||||
// Reset the lights.
|
||||
current_state.required_lights = empty_light_array;
|
||||
current_state.optional_lights = {};
|
||||
|
||||
glDisable(GL_LIGHTING);
|
||||
}
|
||||
|
||||
void J3D::End() {
|
||||
if (!current_state.depth_test)
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
if (!current_state.vertex_array)
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
|
||||
if (current_state.texture_2D)
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
if (!current_state.blend)
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
if (current_state.cull_face)
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
if (current_state.normal_array)
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
|
||||
if (current_state.texture_coordinate_array)
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
if (glIsEnabled(GL_LIGHTING)) {
|
||||
glDisable(GL_LIGHTING);
|
||||
glDisable(GL_LIGHT0);
|
||||
glDisable(GL_LIGHT1);
|
||||
glDisable(GL_LIGHT2);
|
||||
glDisable(GL_LIGHT3);
|
||||
glDisable(GL_LIGHT4);
|
||||
glDisable(GL_LIGHT5);
|
||||
glDisable(GL_LIGHT6);
|
||||
glDisable(GL_LIGHT7);
|
||||
}
|
||||
|
||||
current_state.required_lights = empty_light_array;
|
||||
current_state.optional_lights = {};
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness) {
|
||||
Vector3 vertices[] = {A, B};
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, unsigned int sectors, unsigned int stacks) {
|
||||
Sphere sphere = {position, radius};
|
||||
BatchWireframeSphere(color,& sphere, 1, thickness, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::WireframeSphere(const Color4& color, const Sphere& sphere, float thickness, unsigned int sectors, unsigned int stacks) {
|
||||
BatchWireframeSphere(color,& sphere, 1, thickness, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::BatchWireframeSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, unsigned int sectors, unsigned int stacks) {
|
||||
// Create one sphere with a radius of 1 about 0, 0.
|
||||
float r = 1;
|
||||
std::vector<Vector3> vertices((sectors + 1) * (stacks + 1));
|
||||
|
||||
int index = 0;
|
||||
for (int i = 0; i <= sectors; i++) {
|
||||
float lat = J3ML::Math::Pi * (-0.5 + (float) i / sectors);
|
||||
float z = J3ML::Math::Sin(lat);
|
||||
float zr = J3ML::Math::Cos(lat);
|
||||
|
||||
for (int j = 0; j <= stacks; j++) {
|
||||
float lng = 2 * J3ML::Math::Pi * (float) (j - 1) / stacks;
|
||||
float x = J3ML::Math::Cos(lng);
|
||||
float y = J3ML::Math::Sin(lng);
|
||||
|
||||
float pos_x = r * x * zr;
|
||||
float pos_y = r * y * zr;
|
||||
float pos_z = r * z;
|
||||
|
||||
vertices[index++] = Vector3(pos_x, pos_y, pos_z);
|
||||
}
|
||||
}
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
|
||||
// Render each sphere in the batch at their given position and radius.
|
||||
for(size_t i = 0; i < sphere_count; i++) {
|
||||
glPushMatrix();
|
||||
glTranslatef(spheres[i].Position.x, spheres[i].Position.y, spheres[i].Position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
|
||||
glPopMatrix();
|
||||
}
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::WireframeRevoSphere(const Color4& color, const Vector3& position, float radius, float thickness, unsigned int sectors, unsigned int revolutions, bool draw_stacks) {
|
||||
Sphere sphere = {position, radius};
|
||||
BatchWireframeRevoSphere(color,& sphere, 1, thickness, sectors, revolutions, draw_stacks);
|
||||
}
|
||||
|
||||
void J3D::WireframeRevoSphere(const Color4& color, const Sphere& sphere, float thickness, unsigned int sectors, unsigned int revolutions, bool draw_stacks) {
|
||||
BatchWireframeRevoSphere(color,& sphere, 1, thickness, sectors, revolutions, draw_stacks);
|
||||
}
|
||||
|
||||
void J3D::BatchWireframeRevoSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, unsigned int sectors, unsigned int revolutions, bool draw_stacks) {
|
||||
float r = 1;
|
||||
std::vector<Vector3> vertices;
|
||||
vertices.reserve((sectors + 1) * (revolutions + 1));
|
||||
|
||||
std::vector<Vector3> cross_section(sectors + 1);
|
||||
for (int i = 0; i <= sectors; i++) {
|
||||
float lat = J3ML::Math::Pi * (-0.5 + (float)i / sectors);
|
||||
float z = J3ML::Math::Sin(lat);
|
||||
float zr = J3ML::Math::Cos(lat);
|
||||
cross_section[i] = Vector3(0, zr * r, z * r);
|
||||
}
|
||||
|
||||
// Revolve
|
||||
for (int j = 0; j <= revolutions; j++) {
|
||||
float lng = 2 * J3ML::Math::Pi * (float)j / revolutions;
|
||||
float cosLng = J3ML::Math::Cos(lng);
|
||||
float sinLng = J3ML::Math::Sin(lng);
|
||||
|
||||
for (const auto& point : cross_section) {
|
||||
float pos_x = point.y * cosLng;
|
||||
float pos_y = point.y * sinLng;
|
||||
float pos_z = point.z;
|
||||
|
||||
vertices.emplace_back(pos_x, pos_y, pos_z);
|
||||
}
|
||||
}
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
VRamList vertex_data(vertices.data(), vertices.size());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
// Render each sphere in the batch at their given position and radius.
|
||||
for(size_t i = 0; i < sphere_count; i++) {
|
||||
glPushMatrix();
|
||||
glTranslatef(spheres[i].Position.x, spheres[i].Position.y, spheres[i].Position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.GetLength());
|
||||
if (draw_stacks)
|
||||
glRotatef(90, 0, 1, 0),
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertex_data.GetLength());
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::WireframeIcosphere(const Color4& color, const Vector3& position, float radius, float thickness, unsigned int subdivisions) {
|
||||
|
||||
// NOTE2SELF: Code i'm borrowing this from uses float-packed-arrays rather than discrete Vectors
|
||||
// working on translating that correctly...
|
||||
|
||||
// TODO: Revise this once J3ML::Geometry::Icosahedron is implemented.
|
||||
const float h_angle = J3ML::Math::Pi / 180.f * 72.f; // 72 degree = 360 / 5;
|
||||
const float v_angle = J3ML::Math::Atan(1.0f / 2.f); // elevation = 26.565;
|
||||
|
||||
std::vector<Vector3> vertices; // array of 12 vertices (x,y,z)
|
||||
unsigned int index = 0; // indices
|
||||
float z, xy; // coords
|
||||
float hAngle1 = -Math::Pi / 2.f - h_angle / 2.f; // start from -126 at 1st row
|
||||
float hAngle2 = -Math::Pi / 2.f; // start from -90 deg at 2nd row
|
||||
|
||||
// the first top vertex at (0,0,r)
|
||||
vertices[0] = position + Vector3{0,0,radius};
|
||||
|
||||
|
||||
|
||||
// compute 10 vertices at 1st and 2nd rows
|
||||
for (int i = 1; i <= 5; ++i)
|
||||
{
|
||||
|
||||
z = radius * Math::Sin(v_angle); // elevation
|
||||
xy = radius * Math::Cos(v_angle); // length on XY plane
|
||||
|
||||
vertices[index++] = position + Vector3{xy * Math::Cos(hAngle1), xy * Math::Sin(hAngle1), z} ;
|
||||
vertices[index++] = position + Vector3{xy * Math::Cos(hAngle2), xy * Math::Sin(hAngle2), -z};
|
||||
|
||||
// next horizontal angles
|
||||
hAngle1 += h_angle;
|
||||
hAngle2 += h_angle;
|
||||
}
|
||||
|
||||
// the last bottom vertex at (0, 0, -r)
|
||||
vertices[11] = position + Vector3{0,0, -radius};
|
||||
|
||||
//glLineWidth(thickness);
|
||||
//glColor4ubv(color.ptr());
|
||||
//glBindBuffer(GL_ARRAY_BUFFER, vertices.size());
|
||||
//glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
//glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
|
||||
//glColor4fv(baseColor);
|
||||
|
||||
std::vector<Vector3> tmpVertices;
|
||||
std::vector<Vector3> tmpIndices;
|
||||
std::vector<Vector2> newVs((subdivisions+1) * (subdivisions+2) / 2 * 3);
|
||||
const Vector3 v1, v2, v3;
|
||||
Vector3 newV1, newV2, newV3;
|
||||
|
||||
int i, j, k;
|
||||
index = 0; // unsigned int
|
||||
float a; // lerp alpha
|
||||
unsigned int i1, i2; // indices
|
||||
|
||||
// copy prev arrays
|
||||
tmpVertices = vertices;
|
||||
//tmpIndices = indices;
|
||||
|
||||
// iterate each triangle of icosahedron
|
||||
for (i = 0; i < tmpIndices.size(); i++)
|
||||
{
|
||||
//v1 = tmpVertices[tmpIndices[i]];
|
||||
//v2 = tmpVertices[tmpIndices[i+1]];
|
||||
//v3 = tmpVertices[tmpIndices[i+2]];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void J3D::WireframeIcosahedron(const Color4& color, const Vector3& position, float radius, float thickness) {
|
||||
// TODO: Revise this once J3ML::Geometry::Icosahedron is implemented.
|
||||
const float h_angle = J3ML::Math::Pi / 180.f * 72.f; // 72 degree = 360 / 5;
|
||||
const float v_angle = J3ML::Math::Atan(1.0f / 2.f); // elevation = 26.565;
|
||||
|
||||
std::array<Vector3, 12> vertices; // array of 12 vertices (x,y,z)
|
||||
int index = 0; // indices
|
||||
float z, xy; // coords
|
||||
float hAngle1 = -Math::Pi / 2.f - h_angle / 2.f; // start from -126 at 1st row
|
||||
float hAngle2 = -Math::Pi / 2.f; // start from -90 deg at 2nd row
|
||||
|
||||
// the first top vertex at (0,0,r)
|
||||
vertices[0] = position + Vector3{0,0,radius};
|
||||
|
||||
|
||||
|
||||
// compute 10 vertices at 1st and 2nd rows
|
||||
for (int i = 1; i <= 5; ++i)
|
||||
{
|
||||
|
||||
z = radius * Math::Sin(v_angle); // elevation
|
||||
xy = radius * Math::Cos(v_angle); // length on XY plane
|
||||
|
||||
vertices[index++] = position + Vector3{xy * Math::Cos(hAngle1), xy * Math::Sin(hAngle1), z} ;
|
||||
vertices[index++] = position + Vector3{xy * Math::Cos(hAngle2), xy * Math::Sin(hAngle2), -z};
|
||||
|
||||
// next horizontal angles
|
||||
hAngle1 += h_angle;
|
||||
hAngle2 += h_angle;
|
||||
}
|
||||
|
||||
// the last bottom vertex at (0, 0, -r)
|
||||
vertices[11] = position + Vector3{0,0, -radius};
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4ubv(color.ptr());
|
||||
//glBindBuffer(GL_ARRAY_BUFFER, vertices.size());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size());
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::BatchWireframeAABB(const Color4& color, const AABB* boxes, const size_t& box_count, float thickness) {
|
||||
glColor4ubv(color.ptr());
|
||||
glLineWidth(thickness);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->GetHandle());
|
||||
|
||||
for (size_t i = 0; i < box_count; i++) {
|
||||
Vector3 delta = (boxes[i].maxPoint - boxes[i].minPoint) / 2;
|
||||
Vector3 center = boxes[i].Centroid();
|
||||
glPushMatrix();
|
||||
glTranslatef(center.x, center.y, center.z);
|
||||
glScalef(delta.x, delta.y, delta.z);
|
||||
glDrawElements(GL_LINE_LOOP, ShapeCache::cube_index_data->GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::WireframeOBB(const Color4& color, const Vector3& center, const Vector3& radii, const Matrix3x3& rotation_matrix, float thickness) {
|
||||
glColor4ubv(color.ptr());
|
||||
glLineWidth(thickness);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->GetHandle());
|
||||
|
||||
glPushMatrix();
|
||||
glTranslatef(center.x, center.y, center.z);
|
||||
// TODO AxisAngle directly from rotation matrix.
|
||||
AxisAngle rotate_part = AxisAngle(Quaternion(rotation_matrix));
|
||||
glRotatef(rotate_part.angle, rotate_part.axis.x, rotate_part.axis.y, rotate_part.axis.z);
|
||||
glScalef(radii.x, radii.y, radii.z);
|
||||
glDrawElements(GL_LINE_LOOP, ShapeCache::cube_index_data->GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
glPopMatrix();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::WireframeAABB(const Color4& color, const Vector3& pos, const Vector3& radii, float thickness) {
|
||||
AABB aabb = {Vector3(pos.x - radii.x, pos.y - radii.y, pos.z - radii.z), Vector3(pos.x + radii.x, pos.y + radii.y, pos.z + radii.z)};
|
||||
BatchWireframeAABB(color, &aabb, 1, thickness);
|
||||
}
|
||||
|
||||
void J3D::BatchFillAABB(const Color4& color, const AABB* boxes, const size_t& box_count) {
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_vertex_data->GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ShapeCache::cube_index_data->GetHandle());
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK);
|
||||
|
||||
bool using_lights = false;
|
||||
if (UsingLighting()) {
|
||||
using_lights = true;
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ShapeCache::cube_normal_data->GetHandle());
|
||||
glNormalPointer(GL_FLOAT, sizeof(float), nullptr);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < box_count; i++) {
|
||||
Vector3 delta = (boxes[i].maxPoint - boxes[i].minPoint) / 2;
|
||||
Vector3 center = boxes[i].Centroid();
|
||||
glPushMatrix();
|
||||
glTranslatef(center.x, center.y, center.z);
|
||||
glScalef(delta.x, delta.y, delta.z);
|
||||
if (using_lights)
|
||||
SelectLights(center);
|
||||
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
|
||||
if (using_lights)
|
||||
ResetLights();
|
||||
|
||||
glPopMatrix();
|
||||
}
|
||||
glColor4fv(current_state.draw_color);
|
||||
if (using_lights)
|
||||
glDisableClientState(GL_NORMAL_ARRAY);
|
||||
glDisable(GL_CULL_FACE);
|
||||
}
|
||||
void J3D::FillAABB(const Color4& color, const Vector3& pos, const Vector3& radii) {
|
||||
AABB box = {pos - radii, pos + radii};
|
||||
BatchFillAABB(color, &box, 1);
|
||||
}
|
||||
|
||||
void J3D::FillAABB(const Color4& color, const AABB& aabb) {
|
||||
BatchFillAABB(color, &aabb, 1);
|
||||
}
|
||||
|
||||
void J3D::FillSphere(const Color4& color, const Sphere& sphere, unsigned int sectors, unsigned int stacks) {
|
||||
BatchFillSphere(color, &sphere, 1, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::BatchFillSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, unsigned int sectors, unsigned int stacks) {
|
||||
float r = 1;
|
||||
std::vector<Vector3> vertices((sectors + 1) * (stacks + 1));
|
||||
std::vector<unsigned int> indices; indices.reserve(sectors * stacks * 6);
|
||||
|
||||
float two_pi = 2 * J3ML::Math::Pi;
|
||||
int index = 0;
|
||||
for (int i = 0; i <= sectors; i++) {
|
||||
float lat = J3ML::Math::Pi * (-0.5 + (float) i / sectors);
|
||||
float z = J3ML::Math::Sin(lat);
|
||||
float zr = J3ML::Math::Cos(lat);
|
||||
|
||||
for (int j = 0; j <= stacks; j++) {
|
||||
float lng = two_pi * (float) (j - 1) / stacks;
|
||||
float x = J3ML::Math::Cos(lng);
|
||||
float y = J3ML::Math::Sin(lng);
|
||||
|
||||
float pos_x = r * x * zr;
|
||||
float pos_y = r * y * zr;
|
||||
float pos_z = r * z;
|
||||
vertices[index++] = Vector3(pos_x, pos_y, pos_z);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sectors; i++) {
|
||||
for (int j = 0; j < stacks; j++) {
|
||||
int first_index = i * (stacks + 1) + j;
|
||||
int second_index = first_index + stacks + 1;
|
||||
|
||||
indices.push_back(first_index);
|
||||
indices.push_back(second_index);
|
||||
indices.push_back(first_index + 1);
|
||||
|
||||
indices.push_back(second_index);
|
||||
indices.push_back(second_index + 1);
|
||||
indices.push_back(first_index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
VRamList vertex_data(vertices.data(), vertices.size());
|
||||
VRamList index_data(indices.data(), indices.size());
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_data.GetHandle());
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_data.GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), nullptr);
|
||||
|
||||
for (size_t i = 0; i < sphere_count; i++) {
|
||||
const Vector3 position = spheres[i].Position;
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
|
||||
glDrawElements(GL_TRIANGLES, index_data.GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
glPopMatrix();
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glColor4fv(current_state.draw_color);
|
||||
}
|
||||
|
||||
void J3D::FillSphere(const Color4& color, const Vector3& position, float radius, unsigned int sectors, unsigned int stacks) {
|
||||
Sphere sphere = {position, radius};
|
||||
BatchFillSphere(color, &sphere, 1, sectors, stacks);
|
||||
}
|
||||
|
||||
void J3D::WireframeAABB(const Color4& color, const AABB& aabb, float thickness) {
|
||||
BatchWireframeAABB(color, &aabb, 1, thickness);
|
||||
}
|
||||
|
||||
void J3D::DrawCubicBezierCurve(const Color4& color, const Vector3& controlA, const Vector3& pointA,
|
||||
const Vector3& pointB, const Vector3& controlB, int subdivisions, float thickness) {
|
||||
Vector3 last = controlA;
|
||||
const Vector3& first = controlB;
|
||||
for (int i = 0; i < subdivisions; ++i)
|
||||
{
|
||||
float alpha = (float) i / (float) subdivisions;
|
||||
Vector3 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 J3D::DrawVertexArray(const Color4& color, const VertexArray& vertex_array, const Vector3& position) {
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_array.GetVertices()->GetHandle());
|
||||
glVertexPointer(3, GL_FLOAT, 0, nullptr);
|
||||
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
//glScalef(1,1,1);
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertex_array.GetVertices()->GetLength());
|
||||
glPopMatrix();
|
||||
|
||||
//glDrawElements(GL_LINES, vertex_array.GetIndices()->GetLength(), GL_UNSIGNED_INT, nullptr);
|
||||
//std::cout << vertex_array.GetVertices()->GetLength() << std::endl;
|
||||
glColor4fv(current_state.draw_color);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
#include <JGL/JGL.h>
|
||||
#include "internals/internals.h"
|
||||
|
||||
|
||||
#if __linux__
|
||||
@@ -157,12 +158,12 @@ namespace JGL {
|
||||
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);
|
||||
glColor4fv(default_state.draw_color);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
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) {
|
||||
void J3D::DrawString(const Color4& color, const std::string& text, const Vector3& pos, float scale, u32 size, const Font& font, const EulerAngleXYZ& angle, bool draw_back_face) {
|
||||
// TODO: Determine the proper scale factor mathematically
|
||||
// This number was arrived at holistically.
|
||||
scale = scale * 0.002f;
|
||||
@@ -243,7 +244,7 @@ namespace JGL {
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glColor4f(1, 1, 1, 1);
|
||||
glColor4fv(current_state.draw_color);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glPopMatrix();
|
212
src/renderer/OpenGL/internals/internals.cpp
Normal file
212
src/renderer/OpenGL/internals/internals.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
#include "internals.h"
|
||||
#include <J3ML/LinearAlgebra/Vector4.hpp>
|
||||
#include <JGL/types/Light.h>
|
||||
|
||||
// TODO handle the case that a required light is in the list of optional lights.
|
||||
void JGL::J3D::SelectLights(const Vector3& position) {
|
||||
std::array<const LightBase*, 8> result = current_state.required_lights;
|
||||
|
||||
unsigned int required_light_count = 0;
|
||||
for (const auto& i : result)
|
||||
if (i) required_light_count++;
|
||||
|
||||
// If there is optional lights.
|
||||
if (!current_state.optional_lights.empty()) {
|
||||
// The number of lights we need to solve for
|
||||
unsigned int remaining_lights = 8 - required_light_count;
|
||||
|
||||
std::vector<std::pair<float, const LightBase*>> light_influence;
|
||||
for (const auto* light: current_state.optional_lights)
|
||||
light_influence.emplace_back(light->GetAttenuationAtPosition(position), light);
|
||||
|
||||
// Sort by biggest influence.
|
||||
std::sort(light_influence.begin(), light_influence.end(),
|
||||
[](const auto &a, const auto &b) { return a.first > b.first; });
|
||||
|
||||
// Add in optional lights.
|
||||
for (unsigned int i = 0; i < remaining_lights && i < light_influence.size(); i++)
|
||||
result[required_light_count++] = light_influence[i].second;
|
||||
}
|
||||
|
||||
glEnable(GL_LIGHTING);
|
||||
for (unsigned int i = 0; i < result.size(); i++) {
|
||||
// Terminate early if we have less than 8 lights.
|
||||
if (!result[i])
|
||||
break;
|
||||
|
||||
Vector4 ambient = { result[i]->GetAmbient().RN(), result[i]->GetAmbient().GN(), result[i]->GetAmbient().BN(), result[i]->GetAmbient().AN() };
|
||||
Vector4 diffuse = { result[i]->GetDiffuse().RN(), result[i]->GetDiffuse().GN(), result[i]->GetDiffuse().BN(), result[i]->GetDiffuse().AN() };
|
||||
Vector4 specular = { result[i]->GetSpecular().RN(), result[i]->GetSpecular().GN(), result[i]->GetSpecular().BN(), result[i]->GetSpecular().AN() };
|
||||
GLenum current_light = GL_LIGHT0 + i;
|
||||
|
||||
glEnable(GL_LIGHT0 + i);
|
||||
glLightfv(current_light, GL_POSITION, result[i]->GetPosition().ptr());
|
||||
|
||||
// How the light will affect different materials.
|
||||
glLightfv(current_light, GL_AMBIENT, ambient.ptr());
|
||||
glLightfv(current_light, GL_DIFFUSE, diffuse.ptr());
|
||||
glLightfv(current_light, GL_SPECULAR, specular.ptr());
|
||||
|
||||
// How far the light goes.
|
||||
glLightf(current_light, GL_CONSTANT_ATTENUATION, result[i]->GetConstantAttenuation());
|
||||
glLightf(current_light, GL_LINEAR_ATTENUATION, result[i]->GetLinearAttenuation());
|
||||
glLightf(current_light, GL_QUADRATIC_ATTENUATION, result[i]->GetQuadraticAttenuation());
|
||||
}
|
||||
}
|
||||
|
||||
void JGL::J3D::ResetLights() {
|
||||
Vector4 position = {0, 0, 1, 0};
|
||||
Vector4 ambient = {0, 0, 0, 1};
|
||||
Vector4 diffuse_light0 = {1,1,1,1};
|
||||
Vector4 diffuse = {0,0,0,1};
|
||||
Vector4 specular_light0 = {1,1,1,1};
|
||||
Vector4 specular = {0,0,0,1};
|
||||
Vector3 spot_direction = {0.0, 0.0, -1.0};
|
||||
|
||||
for (unsigned int i = 0; i < 8; i++) {
|
||||
GLenum current_light = GL_LIGHT0 + i;
|
||||
|
||||
glLightfv(current_light, GL_POSITION, position.ptr());
|
||||
glLightfv(current_light, GL_AMBIENT, ambient.ptr());
|
||||
glLightf(current_light, GL_CONSTANT_ATTENUATION, 1);
|
||||
glLightf(current_light, GL_LINEAR_ATTENUATION, 0);
|
||||
glLightf(current_light, GL_QUADRATIC_ATTENUATION, 0);
|
||||
glLightf(current_light, GL_SPOT_CUTOFF, 180.0);
|
||||
glLightfv(current_light, GL_SPOT_DIRECTION, spot_direction.ptr());
|
||||
glLightf(current_light, GL_SPOT_EXPONENT, 0.0);
|
||||
glLightfv(current_light, GL_DIFFUSE, (i == 0 ? diffuse_light0.ptr() : diffuse.ptr()));
|
||||
glLightfv(current_light, GL_SPECULAR, (i == 0 ? specular_light0.ptr() : specular.ptr()));
|
||||
glDisable(current_light);
|
||||
}
|
||||
glDisable(GL_LIGHTING);
|
||||
}
|
||||
|
||||
bool JGL::J3D::UsingLighting() {
|
||||
if (!current_state.optional_lights.empty())
|
||||
return true;
|
||||
|
||||
for (unsigned int i = 0; i < current_state.required_lights.size(); i++)
|
||||
if (current_state.required_lights[i])
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<Vector3> JGL::TriangleMeshVertexNormals(const Vector3* vertices, const size_t& vertex_count, const unsigned int* indices, const size_t& index_count) {
|
||||
std::vector<Vector3> normals(vertex_count, Vector3(0, 0, 0));
|
||||
|
||||
for (size_t i = 0; i < index_count; i += 3) {
|
||||
GLuint i1 = indices[i];
|
||||
GLuint i2 = indices[i + 1];
|
||||
GLuint i3 = indices[i + 2];
|
||||
|
||||
Vector3 v1 = vertices[i1];
|
||||
Vector3 v2 = vertices[i2];
|
||||
Vector3 v3 = vertices[i3];
|
||||
|
||||
Vector3 edge1 = v2 - v1;
|
||||
Vector3 edge2 = v3 - v1;
|
||||
|
||||
Vector3 faceNormal = Vector3::Cross(edge1, edge2);
|
||||
|
||||
normals[i1] += faceNormal;
|
||||
normals[i2] += faceNormal;
|
||||
normals[i3] += faceNormal;
|
||||
}
|
||||
for (auto& normal : normals)
|
||||
normal = normal.Normalized();
|
||||
|
||||
return normals;
|
||||
}
|
||||
|
||||
void JGL::StateStack::Push(const JGL::State& state) {
|
||||
states.push_back(state);
|
||||
}
|
||||
|
||||
void JGL::StateStack::Pop() {
|
||||
states.pop_back();
|
||||
}
|
||||
|
||||
JGL::State* JGL::StateStack::PreviousState() {
|
||||
if (states.empty())
|
||||
return nullptr;
|
||||
|
||||
return &states.back();
|
||||
}
|
||||
|
||||
JGL::State JGL::State::SaveState() {
|
||||
State result;
|
||||
|
||||
result.depth_test = glIsEnabled(GL_DEPTH_TEST);
|
||||
result.vertex_array = glIsEnabled(GL_VERTEX_ARRAY);
|
||||
result.normal_array = glIsEnabled(GL_NORMAL_ARRAY);
|
||||
result.cull_face = glIsEnabled(GL_CULL_FACE);
|
||||
result.blend = glIsEnabled(GL_BLEND);
|
||||
result.texture_2D = glIsEnabled(GL_TEXTURE_2D);
|
||||
result.texture_coordinate_array = glIsEnabled(GL_TEXTURE_COORD_ARRAY);
|
||||
result.color_array = glIsEnabled(GL_COLOR_ARRAY);
|
||||
|
||||
glGetIntegerv(GL_ACTIVE_TEXTURE, &result.selected_texture_unit);
|
||||
result.selected_texture_unit -= GL_TEXTURE0;
|
||||
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, result.clear_color);
|
||||
glGetFloatv(GL_CURRENT_COLOR, result.draw_color);
|
||||
glGetIntegerv(GL_VIEWPORT, result.viewport);
|
||||
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&result.current_fbo);
|
||||
glGetIntegerv(GL_BLEND_SRC, &result.blend_func[0]);
|
||||
glGetIntegerv(GL_BLEND_DST, &result.blend_func[1]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void JGL::State::RestoreState(const State& state) {
|
||||
if (state.depth_test)
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
else
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
if (state.vertex_array)
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
else
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
|
||||
if (state.normal_array)
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
else
|
||||
glDisableClientState(GL_NORMAL_ARRAY);
|
||||
|
||||
if (state.cull_face)
|
||||
glEnable(GL_CULL_FACE);
|
||||
else
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
if (state.blend)
|
||||
glEnable(GL_BLEND);
|
||||
else
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + state.selected_texture_unit);
|
||||
glClientActiveTexture(GL_TEXTURE0 + state.selected_texture_unit);
|
||||
|
||||
if (state.texture_2D)
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
else
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
if (state.texture_coordinate_array)
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
else
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
if (state.color_array)
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
else
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, state.current_fbo);
|
||||
glViewport(state.viewport[0], state.viewport[1], state.viewport[2], state.viewport[3]);
|
||||
glClearColor(state.clear_color[0], state.clear_color[1], state.clear_color[2], state.clear_color[3]);
|
||||
glColor4f(state.draw_color[0], state.draw_color[1], state.draw_color[2], state.draw_color[3]);
|
||||
glBlendFunc(state.blend_func[0], state.blend_func[1]);
|
||||
|
||||
}
|
89
src/renderer/OpenGL/internals/internals.h
Normal file
89
src/renderer/OpenGL/internals/internals.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
#include <JGL/types/Light.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
|
||||
namespace JGL {
|
||||
class State;
|
||||
class StateStack;
|
||||
}
|
||||
|
||||
class JGL::State {
|
||||
public:
|
||||
GLfloat clear_color[4] = {0, 0, 0, 1};
|
||||
GLfloat draw_color[4] = {1, 1, 1, 1};
|
||||
GLint viewport[4] = {0, 0, 0, 0};
|
||||
GLint blend_func[2];
|
||||
GLuint current_fbo = 0;
|
||||
RenderTarget* current_render_target = nullptr;
|
||||
|
||||
bool texture_2D = false;
|
||||
bool texture_coordinate_array = false;
|
||||
bool normal_array = false;
|
||||
bool depth_test = false;
|
||||
bool vertex_array = false;
|
||||
bool cull_face = false;
|
||||
bool blend = false;
|
||||
bool color_array = false;
|
||||
GLint selected_texture_unit = 0;
|
||||
// List of lights required for each object in the scene. up-to 8. For example, the sun. Or a flash-light.
|
||||
std::array<const LightBase*, 8> required_lights{};
|
||||
// List of all lights in the scene.
|
||||
std::vector<const LightBase*> optional_lights{};
|
||||
public:
|
||||
static State SaveState();
|
||||
static void RestoreState(const State& state);
|
||||
};
|
||||
|
||||
class JGL::StateStack {
|
||||
private:
|
||||
std::vector<State> states{};
|
||||
public:
|
||||
size_t Size() { return states.size(); }
|
||||
void Push(const State& state);
|
||||
void Pop();
|
||||
State* PreviousState();
|
||||
public:
|
||||
StateStack() = default;
|
||||
~StateStack() = default;
|
||||
};
|
||||
|
||||
namespace JGL {
|
||||
inline constexpr std::array<const LightBase*, 8> empty_light_array = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
|
||||
std::vector<Vector3> TriangleMeshVertexNormals(const Vector3* vertices, const size_t& vertex_count, const unsigned int* indices, const size_t& index_count);
|
||||
inline StateStack state_stack;
|
||||
inline Vector2i window_size;
|
||||
}
|
||||
|
||||
namespace JGL::J2D {
|
||||
// On windoze this can't be constexpr?
|
||||
inline State default_state
|
||||
{
|
||||
{0, 0, 0, 1},
|
||||
{1, 1, 1, 1},
|
||||
{0, 0, 0, 0},
|
||||
{GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
|
||||
0, nullptr, false, false,
|
||||
false, false, true, true,
|
||||
true, false, 0,
|
||||
{}, {}
|
||||
};
|
||||
|
||||
inline State current_state;
|
||||
}
|
||||
|
||||
namespace JGL::J3D {
|
||||
inline State current_state;
|
||||
inline float far_plane = 0;
|
||||
inline float fov = 0;
|
||||
// Enables lighting and selects the correct lights to use.
|
||||
void SelectLights(const Vector3& position);
|
||||
// Resets the GL lights to default and disables them. Then, disables lighting.
|
||||
void ResetLights();
|
||||
[[nodiscard]] bool UsingLighting();
|
||||
}
|
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
#include <glad/glad.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <JGL/JGL.h>
|
||||
|
||||
#if __linux__
|
||||
#include <freetype2/ft2build.h>
|
||||
@@ -58,25 +59,38 @@ namespace JGL {
|
||||
return Detail::InitTextEngine();
|
||||
}
|
||||
|
||||
Font::Font(const unsigned char* data, const size_t& size) {
|
||||
if (Detail::ft == nullptr)
|
||||
throw std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||
|
||||
if (FT_New_Memory_Face(Detail::ft, data, size, 0, &face))
|
||||
throw std::runtime_error("Error::FREETYPE: Failed to load font!");
|
||||
|
||||
unsigned int new_index = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= new_index)
|
||||
new_index = f.index + 1;
|
||||
index = new_index;
|
||||
|
||||
Detail::fonts.push_back(*this);
|
||||
std::cout << "Loaded font from memory at " << static_cast<const void*>(data) << " with index " << new_index << std::endl;
|
||||
}
|
||||
|
||||
Font::Font(const std::filesystem::path& path) {
|
||||
if (Detail::ft == nullptr)
|
||||
throw std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||
|
||||
Font font;
|
||||
if (FT_New_Face(Detail::ft, path.string().c_str(), 0, &face)) {
|
||||
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
|
||||
if (FT_New_Face(Detail::ft, path.string().c_str(), 0, &face))
|
||||
throw std::runtime_error("Error::FREETYPE: Failed to load font!");
|
||||
//return -1;
|
||||
}
|
||||
unsigned int newIndex = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= newIndex)
|
||||
newIndex = f.index + 1;
|
||||
|
||||
index = newIndex;
|
||||
Detail::fonts.push_back(font);
|
||||
std::cout << "Loaded font from " << path << " with index " << newIndex << std::endl;
|
||||
//return newIndex;
|
||||
unsigned int new_index = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= new_index)
|
||||
new_index = f.index + 1;
|
||||
|
||||
index = new_index;
|
||||
Detail::fonts.push_back(*this);
|
||||
std::cout << "Loaded font from " << path << " with index " << new_index << std::endl;
|
||||
}
|
||||
|
||||
Font::~Font() {
|
||||
@@ -119,14 +133,18 @@ namespace JGL {
|
||||
return extents;
|
||||
}
|
||||
|
||||
jlog::Warning("Measuring a font size that is not cached, This is *super* slow.");
|
||||
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
|
||||
jlog::Warning("Measuring a font size that is not cached, Defaulting to Jupiteroid.");
|
||||
FT_Set_Pixel_Sizes(Fonts::Jupiteroid.face, ptSize, ptSize);
|
||||
|
||||
for (const char& c : text) {
|
||||
// TODO: Fix segfault
|
||||
//FT_GlyphSlot slot = Fonts::Jupiteroid.face->glyph;
|
||||
//auto glyph_index = FT_Get_Char_Index(Fonts::Jupiteroid.face, c);
|
||||
FT_GlyphSlot slot = face->glyph;
|
||||
auto glyph_index = FT_Get_Char_Index(this->face, c);
|
||||
|
||||
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
|
||||
//auto error = FT_Load_Glyph(Fonts::Jupiteroid.face, glyph_index, FT_LOAD_DEFAULT);
|
||||
auto error = FT_Load_Glyph(this->face, glyph_index, FT_LOAD_DEFAULT);
|
||||
|
||||
if (error)
|
||||
continue;
|
||||
@@ -146,9 +164,6 @@ namespace JGL {
|
||||
if (extents.y < ptSize)
|
||||
extents.y = ptSize;
|
||||
}
|
||||
|
||||
return extents;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,19 +1,64 @@
|
||||
#include <JGL/types/Light.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
JGL::Light::Light(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular) {
|
||||
JGL::PointLight::PointLight(const Vector3& position, const Color4& ambient, const Color4& diffuse, const Color4& specular, float constant_attenuation, float linear_attenuation, float quadratic_attenuation) {
|
||||
this->position = Vector4(position, 1.0f);
|
||||
this->ambient = ambient;
|
||||
this->diffuse = diffuse;
|
||||
this->specular = specular;
|
||||
this->constant_attenuation = constant_attenuation;
|
||||
this->linear_attenuation = linear_attenuation;
|
||||
this->quadratic_attenuation = quadratic_attenuation;
|
||||
}
|
||||
|
||||
Vector3 JGL::Light::GetNormalizedSceenSpaceCoordinates() const {
|
||||
GLint viewport[4];
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
float JGL::PointLight::GetAttenuationAtPosition(const Vector3& pos) const {
|
||||
Vector3 light_pos = {position.x, position.y, position.z};
|
||||
Vector3 vector_to_position = pos - light_pos;
|
||||
float distance = vector_to_position.Length();
|
||||
|
||||
float normalized_x = 2.0f * (position.x - viewport[0]) / viewport[2] - 1.0f;
|
||||
float normalized_y = 2.0f * (position.y - viewport[1]) / viewport[3] - 1.0f;
|
||||
|
||||
return {normalized_x, normalized_y, position.z};
|
||||
return 1.0f / (GetConstantAttenuation() + GetLinearAttenuation() * distance + GetQuadraticAttenuation() * distance * distance);
|
||||
}
|
||||
|
||||
JGL::SpotLight::SpotLight(const Vector3& position, const Matrix3x3& ro_mat, float cone_size_degrees, float exponent, const Color4& ambient, const Color4& diffuse, const Color4& specular,
|
||||
float constant_attenuation, float linear_attenuation, float quadratic_attenuation) {
|
||||
this->position = Vector4(position, 1);
|
||||
//TODO RotationMatrix to "Normalized direction vector."
|
||||
orientation = ro_mat;
|
||||
this->cut = cone_size_degrees;
|
||||
this->exponent = exponent;
|
||||
this->ambient = ambient;
|
||||
this->diffuse = diffuse;
|
||||
this->specular = specular;
|
||||
this->constant_attenuation = constant_attenuation;
|
||||
this->linear_attenuation = linear_attenuation;
|
||||
this->quadratic_attenuation = quadratic_attenuation;
|
||||
}
|
||||
|
||||
Vector4 JGL::LightBase::GetPosition() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
Color4 JGL::LightBase::GetAmbient() const {
|
||||
return ambient;
|
||||
}
|
||||
|
||||
Color4 JGL::LightBase::GetDiffuse() const {
|
||||
return diffuse;
|
||||
}
|
||||
|
||||
Color4 JGL::LightBase::GetSpecular() const {
|
||||
return specular;
|
||||
}
|
||||
|
||||
float JGL::LightBase::GetConstantAttenuation() const {
|
||||
return constant_attenuation;
|
||||
}
|
||||
|
||||
float JGL::LightBase::GetLinearAttenuation() const {
|
||||
return linear_attenuation;
|
||||
}
|
||||
|
||||
float JGL::LightBase::GetQuadraticAttenuation() const {
|
||||
return quadratic_attenuation;
|
||||
}
|
||||
|
||||
|
||||
|
71
src/types/Light2D.cpp
Normal file
71
src/types/Light2D.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <JGL/types/Light2D.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
JGL::J2D::PointLight::PointLight(const Vector2& position, const Color4& ambient, const Color4& diffuse, float constant_attenuation, float linear_attenuation, float quadratic_attenuation) {
|
||||
light_pos = Vector4(position.x, position.y, 1, 1);
|
||||
this->ambient = ambient;
|
||||
this->diffuse = diffuse;
|
||||
this->constant_attenuation = constant_attenuation;
|
||||
this->linear_attenuation = linear_attenuation;
|
||||
this->quadratic_attenuation = quadratic_attenuation;
|
||||
}
|
||||
|
||||
float JGL::J2D::PointLight::MeasureIntensityAtPosition(const Vector2& position) const {
|
||||
float d = Vector2::Distance( Vector2(light_pos.x, light_pos.y), position);
|
||||
|
||||
// Fix possible divide by zero if the position we're measuring happens to be directly on-top of the light.
|
||||
if (d == 0)
|
||||
d = 0.00001;
|
||||
|
||||
return 1.f / (constant_attenuation + linear_attenuation * d + quadratic_attenuation * d * d);
|
||||
}
|
||||
|
||||
Vector2 JGL::J2D::Light::Position() const {
|
||||
return { light_pos.x, light_pos.y };
|
||||
}
|
||||
|
||||
Color4 JGL::J2D::Light::Ambient() const {
|
||||
return ambient;
|
||||
}
|
||||
|
||||
Color4 JGL::J2D::Light::Diffuse() const {
|
||||
return diffuse;
|
||||
}
|
||||
|
||||
float JGL::J2D::Light::Constant() const {
|
||||
return constant_attenuation;
|
||||
}
|
||||
|
||||
float JGL::J2D::Light::Linear() const {
|
||||
return linear_attenuation;
|
||||
}
|
||||
|
||||
float JGL::J2D::Light::Quadratic() const {
|
||||
return quadratic_attenuation;
|
||||
}
|
||||
|
||||
void JGL::J2D::Light::Position(const Vector2& new_position) {
|
||||
light_pos.x = new_position.x;
|
||||
light_pos.y = new_position.y;
|
||||
}
|
||||
|
||||
void JGL::J2D::Light::Ambient(const Color4& new_ambient) {
|
||||
ambient = new_ambient;
|
||||
}
|
||||
|
||||
void JGL::J2D::Light::Diffuse(const Color4& new_diffuse) {
|
||||
diffuse = new_diffuse;
|
||||
}
|
||||
|
||||
void JGL::J2D::Light::Constant(float new_constant_attenuation) {
|
||||
constant_attenuation = std::clamp(new_constant_attenuation, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
void JGL::J2D::Light::Linear(float new_linear_attenuation) {
|
||||
linear_attenuation = std::clamp(new_linear_attenuation, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
void JGL::J2D::Light::Quadratic(float new_quadratic_attenuation) {
|
||||
quadratic_attenuation = std::clamp(new_quadratic_attenuation, 0.0f, 1.0f);
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
#include <JGL/types/Material.h>
|
||||
#include <J3ML/LinearAlgebra/Vector4.hpp>
|
||||
|
||||
JGL::Material::Material(const Color4& ambient_reflection, const Color4& diffuse_reflection, const Color4& specular_reflection, const Color4& light_emission, float& shine) {
|
||||
ambient_ref = ambient_reflection;
|
||||
diffuse_ref = diffuse_reflection;
|
||||
specular_ref = specular_reflection;
|
||||
emission = light_emission;
|
||||
shininess = shine;
|
||||
}
|
||||
|
||||
void JGL::Material::SetActiveMaterial(const Material& material) {
|
||||
Vector4 ambient = { material.ambient_ref.RN(), material.ambient_ref.BN(), material.ambient_ref.GN(), material.ambient_ref.AN() };
|
||||
Vector4 diffuse = { material.diffuse_ref.RN(), material.diffuse_ref.BN(), material.diffuse_ref.GN(), material.diffuse_ref.AN() };
|
||||
Vector4 specular = { material.specular_ref.RN(), material.specular_ref.BN(), material.specular_ref.GN(), material.specular_ref.AN() };
|
||||
Vector4 emission = { material.emission.RN(), material.emission.BN(), material.emission.GN(), material.emission.AN() };
|
||||
|
||||
// Determine which faces we're going to apply this material to.
|
||||
GLenum face = GL_FRONT;
|
||||
if (glIsEnabled(GL_CULL_FACE)) {
|
||||
GLint current; glGetIntegerv(GL_CULL_FACE_MODE, ¤t);
|
||||
if (current == GL_FRONT)
|
||||
face = GL_BACK;
|
||||
}
|
||||
|
||||
glMaterialfv(face, GL_AMBIENT, ambient.ptr());
|
||||
glMaterialfv(face, GL_DIFFUSE, diffuse.ptr());
|
||||
glMaterialfv(face, GL_SPECULAR, specular.ptr());
|
||||
glMaterialfv(face, GL_EMISSION, emission.ptr());
|
||||
glMaterialf(face, GL_SHININESS, material.shininess);
|
||||
}
|
||||
|
@@ -3,12 +3,12 @@
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <stdexcept>
|
||||
|
||||
const JGL::Texture* JGL::RenderTarget::GetJGLTexture() const {
|
||||
const JGL::Texture* JGL::RenderTarget::GetTexture() const {
|
||||
return texture;
|
||||
}
|
||||
|
||||
GLuint JGL::RenderTarget::GetGLTextureHandle() const {
|
||||
return texture->GetGLTextureHandle();
|
||||
GLuint JGL::RenderTarget::GetTextureHandle() const {
|
||||
return texture->GetHandle();
|
||||
}
|
||||
|
||||
GLuint JGL::RenderTarget::GetGLFramebufferObjectHandle() const {
|
||||
@@ -30,7 +30,7 @@ void JGL::RenderTarget::SetActiveGLRenderTarget(const RenderTarget& render_targe
|
||||
glViewport(0,0, render_target.GetDimensions().x, render_target.GetDimensions().y);
|
||||
}
|
||||
|
||||
Vector2 JGL::RenderTarget::GetDimensions() const {
|
||||
Vector2i JGL::RenderTarget::GetDimensions() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ void JGL::RenderTarget::Erase() {
|
||||
glDeleteFramebuffers(1, &framebuffer_object);
|
||||
|
||||
if (MSAAEnabled())
|
||||
SetMSAAEnabled(MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
SetMSAAEnabled(SampleRate::NONE);
|
||||
}
|
||||
|
||||
Color4 JGL::RenderTarget::GetClearColor() const {
|
||||
@@ -56,6 +56,7 @@ JGL::RenderTarget::RenderTarget(const JGL::Texture* texture, const Color4& clear
|
||||
if (texture->GetDimensions().x < 1 || texture->GetDimensions().y < 1)
|
||||
Logger::Fatal("Creating a render target where the color attachment is empty?");
|
||||
|
||||
this->size = { static_cast<int>(texture->GetDimensions().x), static_cast<int>(texture->GetDimensions().y) };
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
GLint viewport[4] = {0, 0, 0, 0};
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
@@ -63,7 +64,7 @@ JGL::RenderTarget::RenderTarget(const JGL::Texture* texture, const Color4& clear
|
||||
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);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetHandle(), 0);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
throw std::runtime_error("A new framebuffer could not be allocated.");
|
||||
@@ -71,12 +72,11 @@ JGL::RenderTarget::RenderTarget(const JGL::Texture* texture, const Color4& clear
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
|
||||
this->clear_color = clear_color;
|
||||
this->size = texture->GetDimensions();
|
||||
this->texture = texture;
|
||||
texture_created_by_us = false;
|
||||
}
|
||||
|
||||
JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color, bool use_depth, MSAA_SAMPLE_RATE sample_rate) {
|
||||
JGL::RenderTarget::RenderTarget(const Vector2i& size, const Color4& clear_color, bool use_depth, SampleRate sample_rate, FilteringMode filtering_mode) {
|
||||
if (size.x < 1 || size.y < 1)
|
||||
Logger::Fatal("Creating a render target where the color attachment is empty?");
|
||||
|
||||
@@ -84,22 +84,18 @@ JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color,
|
||||
GLint viewport[4] = {0, 0, 0, 0};
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
//Textures behave strangely if they're not square aaaaaaaaaaaaa.
|
||||
unsigned int biggest;
|
||||
if (size.x >= size.y)
|
||||
biggest = size.x;
|
||||
else biggest = size.y;
|
||||
|
||||
texture = new Texture(Vector2(biggest, biggest));
|
||||
texture = new Texture(size, filtering_mode);
|
||||
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);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetHandle(), 0);
|
||||
|
||||
if (use_depth) {
|
||||
GLuint depthBuffer;
|
||||
glGenRenderbuffers(1, &depthBuffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, biggest, biggest);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size.x, size.y);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
using_depth = true;
|
||||
@@ -120,11 +116,11 @@ JGL::RenderTarget::RenderTarget(const Vector2& size, const Color4& clear_color,
|
||||
this->size = size;
|
||||
texture_created_by_us = true;
|
||||
|
||||
if (sample_rate != MSAA_SAMPLE_RATE::MSAA_NONE)
|
||||
if (sample_rate != SampleRate::NONE)
|
||||
SetMSAAEnabled(sample_rate);
|
||||
}
|
||||
|
||||
std::vector<GLfloat> JGL::RenderTarget::GetData() const {
|
||||
std::vector<GLfloat> JGL::RenderTarget::GetPixels() const {
|
||||
std::vector<GLfloat> data(GetDimensions().x * GetDimensions().y * 4);
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
|
||||
@@ -132,12 +128,11 @@ std::vector<GLfloat> JGL::RenderTarget::GetData() const {
|
||||
glReadPixels(0, 0, GetDimensions().x, GetDimensions().y, GL_RGBA, GL_FLOAT, data.data());
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Resize(const Vector2& new_size) {
|
||||
void JGL::RenderTarget::Resize(const Vector2i& new_size) {
|
||||
if (!texture_created_by_us)
|
||||
Logger::Fatal("Resizing a texture that already existed?");
|
||||
Logger::Error("Resizing a Render Target that does not own it's texture?");
|
||||
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
GLfloat old_clear_color[4];
|
||||
@@ -145,45 +140,19 @@ void JGL::RenderTarget::Resize(const Vector2& new_size) {
|
||||
glGetIntegerv(GL_VIEWPORT, old_viewport);
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, old_clear_color);
|
||||
|
||||
/* If what was previously not part of the renderable area is big enough to
|
||||
* just set the new size without reading data back */
|
||||
if (new_size.x <= texture->GetDimensions().x && new_size.y <= texture->GetDimensions().y) {
|
||||
size = new_size;
|
||||
|
||||
// Clear.
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_object);
|
||||
auto cc = GetClearColor();
|
||||
glClearColor(cc.RedChannelNormalized(), cc.GreenChannelNormalized(), cc.BlueChannelNormalized(), cc.AlphaChannelNormalized());
|
||||
glViewport(0,0, size.x, size.y);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
if (using_depth)
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
glClearColor(old_clear_color[0], old_clear_color[1], old_clear_color[2], old_clear_color[3]);
|
||||
glViewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
|
||||
return;
|
||||
}
|
||||
|
||||
//If we have to remake the texture.
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_object);
|
||||
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
||||
|
||||
// Erase it.
|
||||
delete texture;
|
||||
|
||||
unsigned int biggest;
|
||||
if (new_size.x >= new_size.y)
|
||||
biggest = new_size.x;
|
||||
else
|
||||
biggest = new_size.y;
|
||||
|
||||
auto cc = GetClearColor();
|
||||
glClearColor(cc.RedChannelNormalized(), cc.GreenChannelNormalized(), cc.BlueChannelNormalized(), cc.AlphaChannelNormalized());
|
||||
glViewport(0,0, size.x, size.y);
|
||||
|
||||
texture = new Texture(Vector2((float) biggest, (float) biggest));
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetGLTextureHandle(), 0);
|
||||
texture = new Texture(new_size);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetHandle(), 0);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
glClearColor(old_clear_color[0], old_clear_color[1], old_clear_color[2], old_clear_color[3]);
|
||||
@@ -192,8 +161,8 @@ void JGL::RenderTarget::Resize(const Vector2& new_size) {
|
||||
|
||||
//Disable & Re-enable MSAA so the msaa buffer is remade with the correct dimensions.
|
||||
if (MSAAEnabled()) {
|
||||
MSAA_SAMPLE_RATE current_sample_rate = msaa_sample_rate;
|
||||
SetMSAAEnabled(MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
SampleRate current_sample_rate = msaa_sample_rate;
|
||||
SetMSAAEnabled(SampleRate::NONE);
|
||||
SetMSAAEnabled(current_sample_rate);
|
||||
}
|
||||
}
|
||||
@@ -204,42 +173,43 @@ JGL::RenderTarget::~RenderTarget() {
|
||||
delete texture;
|
||||
}
|
||||
|
||||
bool JGL::RenderTarget::TextureCreatedByRenderTarget() const {
|
||||
bool JGL::RenderTarget::OwnsTexture() const {
|
||||
return texture_created_by_us;
|
||||
}
|
||||
|
||||
JGL::MSAA_SAMPLE_RATE JGL::RenderTarget::GetMSAASampleRate() const {
|
||||
JGL::SampleRate JGL::RenderTarget::GetMSAASampleRate() const {
|
||||
return msaa_sample_rate;
|
||||
}
|
||||
|
||||
bool JGL::RenderTarget::MSAAEnabled() const {
|
||||
return msaa_sample_rate != MSAA_SAMPLE_RATE::MSAA_NONE;
|
||||
return msaa_sample_rate != SampleRate::NONE;
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE sample_rate) {
|
||||
bool JGL::RenderTarget::SetMSAAEnabled(JGL::SampleRate sample_rate) {
|
||||
// If we'd be setting the same sample_rate we already have.
|
||||
if (sample_rate == msaa_sample_rate)
|
||||
return;
|
||||
return false;
|
||||
|
||||
// If we'd be rendering onto a texture and not a plain render target we don't want this.
|
||||
if (!TextureCreatedByRenderTarget())
|
||||
return;
|
||||
if (!OwnsTexture())
|
||||
return false;
|
||||
|
||||
// Remove it if they request no msaa or if what they requested is different than what they already have.
|
||||
if (sample_rate == MSAA_SAMPLE_RATE::MSAA_NONE || msaa_sample_rate != MSAA_SAMPLE_RATE::MSAA_NONE) {
|
||||
if (sample_rate == SampleRate::NONE || msaa_sample_rate != SampleRate::NONE) {
|
||||
if(using_depth)
|
||||
glDeleteRenderbuffers(1, &msaa_depth_buffer);
|
||||
|
||||
glDeleteRenderbuffers(1, &msaa_render_buffer);
|
||||
glDeleteFramebuffers(1, &msaa_framebuffer_object);
|
||||
|
||||
msaa_framebuffer_object = 0;
|
||||
msaa_depth_buffer = 0;
|
||||
msaa_render_buffer = 0;
|
||||
msaa_sample_rate = MSAA_SAMPLE_RATE::MSAA_NONE;
|
||||
msaa_sample_rate = SampleRate::NONE;
|
||||
|
||||
// Only return here if they specifically requested no MSAA. else continue to change mode.
|
||||
if (sample_rate == MSAA_SAMPLE_RATE::MSAA_NONE)
|
||||
return;
|
||||
if (sample_rate == SampleRate::NONE)
|
||||
return true;
|
||||
}
|
||||
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
@@ -250,32 +220,31 @@ void JGL::RenderTarget::SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE sample_rate) {
|
||||
glGetIntegerv(GL_RENDERBUFFER_BINDING, ¤t_renderbuffer);
|
||||
glGenRenderbuffers(1, &msaa_render_buffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, msaa_render_buffer);
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, JGL::to_int(sample_rate), GL_RGBA, size.x, size.y);
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, to_int(sample_rate), GL_RGBA, size.x, size.y);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaa_render_buffer);
|
||||
|
||||
if (using_depth) {
|
||||
glGenRenderbuffers(1, &msaa_depth_buffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, msaa_depth_buffer);
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, JGL::to_int(sample_rate), GL_DEPTH_COMPONENT, size.x, size.y);
|
||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, to_int(sample_rate), GL_DEPTH_COMPONENT, size.x, size.y);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, msaa_depth_buffer);
|
||||
}
|
||||
|
||||
bool failure = false;
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
failure = true,
|
||||
Logger::Fatal("A new MSAA " + std::to_string(to_int(sample_rate)) + "x framebuffer couldn't be allocated.");
|
||||
failure = true, Logger::Fatal("A new " + to_string(sample_rate) + " framebuffer couldn't be allocated.");
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, current_renderbuffer);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
msaa_sample_rate = sample_rate;
|
||||
|
||||
if (failure)
|
||||
SetMSAAEnabled(MSAA_SAMPLE_RATE::MSAA_NONE);
|
||||
SetMSAAEnabled(SampleRate::NONE);
|
||||
return failure;
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Blit() const {
|
||||
if (MSAAEnabled() && TextureCreatedByRenderTarget()) {
|
||||
|
||||
void JGL::RenderTarget::MSAABlit() const {
|
||||
if (MSAAEnabled() && OwnsTexture()) {
|
||||
// Save the GL state.
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
GLint current_draw_fbo = 0;
|
||||
@@ -293,25 +262,11 @@ void JGL::RenderTarget::Blit() const {
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, current_draw_fbo);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
}
|
||||
|
||||
// Fixes using render targets on a texture that has mipmaps.
|
||||
if (GetJGLTexture()->GetFilteringMode() == TextureFilteringMode::MIPMAP_NEAREST
|
||||
|| GetJGLTexture()->GetFilteringMode() == TextureFilteringMode::MIPMAP_BILINEAR ||
|
||||
GetJGLTexture()->GetFilteringMode() == TextureFilteringMode::MIPMAP_TRILINEAR) {
|
||||
GLint current_texture = 0;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, GetJGLTexture()->GetGLTextureHandle());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, current_texture);
|
||||
}
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Blit(const JGL::RenderTarget& source, JGL::RenderTarget* destination) {
|
||||
//TODO allow blitting onto an FBO that is bigger but not smaller.
|
||||
if (source.size != destination->size)
|
||||
Logger::Warning("Blitting a render target but they're not the same size?");
|
||||
void JGL::RenderTarget::Blit(const JGL::RenderTarget& source, JGL::RenderTarget* destination, const Vector2i& position) {
|
||||
if (source.GetDimensions().x > destination->GetDimensions().x || source.GetDimensions().y > destination->GetDimensions().y)
|
||||
Logger::Warning("Blitting a Render Target onto another Render Target but the destination Render Target is too small?");
|
||||
|
||||
// Save the GL state.
|
||||
GLuint current_fbo = GetActiveGLFramebufferHandle();
|
||||
@@ -323,7 +278,7 @@ void JGL::RenderTarget::Blit(const JGL::RenderTarget& source, JGL::RenderTarget*
|
||||
// Draw the contents of one into the other.
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, source.framebuffer_object);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, destination->framebuffer_object);
|
||||
glBlitFramebuffer(0, 0, source.size.x, source.size.y, 0, 0, source.size.x, source.size.y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
glBlitFramebuffer(0, 0, source.size.x, source.size.y, position.x, position.y, position.x + source.size.x, position.y + source.size.y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
|
||||
// Put the GL state back.
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, current_read_fbo);
|
||||
@@ -331,12 +286,20 @@ void JGL::RenderTarget::Blit(const JGL::RenderTarget& source, JGL::RenderTarget*
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::Blit(const Color4& color, const Vector2& position, JGL::RenderTarget* destination) {
|
||||
void JGL::RenderTarget::Blit(const Texture* source, RenderTarget* destination, const Vector2i& position) {
|
||||
auto temp = new RenderTarget(source);
|
||||
Blit(*temp, destination, position);
|
||||
delete temp;
|
||||
}
|
||||
|
||||
// To avoid repeatedly allocating and deallocating.
|
||||
JGL::RenderTarget* pixel = nullptr;
|
||||
void JGL::RenderTarget::Blit(const Color4& color, const Vector2i& position, JGL::RenderTarget* destination) {
|
||||
if (position.x > destination->size.x || position.y > destination->size.y)
|
||||
Logger::Warning("Blitting outside of the renderable area of the destination.");
|
||||
|
||||
Texture texture(Vector2(1,1));
|
||||
RenderTarget source(&texture,color);
|
||||
if (pixel == nullptr)
|
||||
pixel = new RenderTarget({1,1});
|
||||
|
||||
GLint current_draw_fbo = 0;
|
||||
GLint current_read_fbo = 0;
|
||||
@@ -348,13 +311,14 @@ void JGL::RenderTarget::Blit(const Color4& color, const Vector2& position, JGL::
|
||||
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, ¤t_read_fbo);
|
||||
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_draw_fbo);
|
||||
|
||||
SetActiveGLRenderTarget(source);
|
||||
SetActiveGLRenderTarget(*pixel);
|
||||
glClearColor(color.RedChannelNormalized(), color.GreenChannelNormalized(), color.BlueChannelNormalized(), color.AlphaChannelNormalized());
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Invert so it's relative to the top left corner.
|
||||
int target_y = destination->size.y - position.y - 1;
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, source.framebuffer_object);
|
||||
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, pixel->framebuffer_object);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, destination->framebuffer_object);
|
||||
glBlitFramebuffer(0, 0, 1, 1, position.x, target_y, position.x + 1, target_y + 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
||||
|
||||
@@ -383,3 +347,22 @@ JGL::RenderTarget::RenderTarget(const JGL::RenderTarget& rhs) {
|
||||
|
||||
operator delete(this_render_target);
|
||||
}
|
||||
|
||||
Vector2i JGL::RenderTarget::MaxSize() {
|
||||
return Texture::MaxSize();
|
||||
}
|
||||
|
||||
void JGL::RenderTarget::RegenerateMipMaps() {
|
||||
// Fixes using render targets on a texture that has mipmaps.
|
||||
if (GetTexture()->GetFilteringMode() == FilteringMode::MIPMAP_NEAREST
|
||||
|| GetTexture()->GetFilteringMode() == FilteringMode::MIPMAP_BILINEAR ||
|
||||
GetTexture()->GetFilteringMode() == FilteringMode::MIPMAP_TRILINEAR) {
|
||||
GLint current_texture = 0;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, GetTexture()->GetHandle());
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, current_texture);
|
||||
}
|
||||
}
|
||||
|
@@ -1,192 +1,328 @@
|
||||
#include <JGL/types/Texture.h>
|
||||
#include <iostream>
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <glad/glad.h>
|
||||
#include <fstream>
|
||||
|
||||
using namespace ReImage;
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "internals/stb_image.h"
|
||||
|
||||
namespace JGL
|
||||
{
|
||||
Texture::Texture(const std::string& file, TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode, const TextureFlag& flags)
|
||||
{
|
||||
auto* t = new ReImage::Image(file, flags);
|
||||
GLuint previous_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||
using namespace JGL;
|
||||
|
||||
load(t, {(float) t->getWidth(), (float) t->getHeight()}, t->getTextureFormat(), filtering_mode, wrapping_mode);
|
||||
texture_flags = flags;
|
||||
Texture::Texture(const std::filesystem::path& file, FilteringMode filtering_mode, SampleRate anisotropy, WrappingMode wrapping_mode, bool invert_y) :
|
||||
invert_y(invert_y), filtering_mode(filtering_mode), anisotropy(anisotropy), wrapping_mode(wrapping_mode) {
|
||||
std::vector<unsigned char> pixels{};
|
||||
|
||||
delete t;
|
||||
std::ifstream ifStream(file, std::ios::in | std::ios::binary);
|
||||
if (!ifStream.is_open())
|
||||
Logger::Fatal("Trying to load a texture from: " + file.string() + "but we couldn't read the file.");
|
||||
|
||||
unsigned char bmpFileHeader[14];
|
||||
|
||||
ifStream.read(reinterpret_cast<char*>(bmpFileHeader), 14);
|
||||
if (bmpFileHeader[0] == 'B' && bmpFileHeader[1] == 'M')
|
||||
pixels = bmp(file);
|
||||
|
||||
//TODO determine if it's a PNG using the file header instead.
|
||||
else if (file.string().ends_with(".png"))
|
||||
pixels = png(file);
|
||||
ifStream.close();
|
||||
|
||||
|
||||
if (invert_y) {
|
||||
unsigned int row_size = size.x * 4;
|
||||
if (format == ColorFormat::RGB)
|
||||
row_size = size.x * 3;
|
||||
|
||||
std::vector<unsigned char> temp(row_size);
|
||||
for (unsigned int y = 0; y < size.y / 2; ++y) {
|
||||
unsigned char *topRow = &pixels[y * row_size];
|
||||
unsigned char *bottomRow = &pixels[(size.y - y - 1) * row_size];
|
||||
std::copy(bottomRow, bottomRow + row_size, temp.begin());
|
||||
std::copy(topRow, topRow + row_size, bottomRow);
|
||||
std::copy(temp.begin(), temp.end(), topRow);
|
||||
}
|
||||
}
|
||||
|
||||
Texture::Texture(const Vector2& size) {
|
||||
GLuint previous_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||
load(pixels.data());
|
||||
}
|
||||
|
||||
glGenTextures(1, &texture_handle);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_handle);
|
||||
//NEAREST
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
std::vector<unsigned char> Texture::png(const std::filesystem::path& file) {
|
||||
std::vector<unsigned char> result{};
|
||||
int channels;
|
||||
|
||||
unsigned char* image_data = stbi_load(file.string().c_str(), &size.x, &size.y, &channels, 0);
|
||||
if (!image_data)
|
||||
Logger::Fatal("Trying to load a texture from: " + file.string() + "but we couldn't read the file.");
|
||||
|
||||
if (channels == 4)
|
||||
format = ColorFormat::RGBA;
|
||||
else if (channels == 3)
|
||||
format = ColorFormat::RGB;
|
||||
|
||||
if (image_data)
|
||||
result.assign(image_data, image_data + size.x * size.y * channels);
|
||||
|
||||
stbi_image_free(image_data);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> Texture::bmp(const std::filesystem::path& file) {
|
||||
std::vector<unsigned char> result{};
|
||||
std::ifstream bmp_file(file, std::ios::in | std::ios::binary);
|
||||
|
||||
unsigned char bmpFileHeader[14];
|
||||
unsigned char bmpInfoHeader[40];
|
||||
|
||||
bmp_file.read(reinterpret_cast<char *>(bmpFileHeader), 14);
|
||||
bmp_file.read(reinterpret_cast<char *>(bmpInfoHeader), 40);
|
||||
|
||||
size.x = bmpInfoHeader[4] + (bmpInfoHeader[5] << 8) + (bmpInfoHeader[6] << 16) + (bmpInfoHeader[7] << 24);
|
||||
size.y = bmpInfoHeader[8] + (bmpInfoHeader[9] << 8) + (bmpInfoHeader[10] << 16) + (bmpInfoHeader[11] << 24);
|
||||
|
||||
int row_padded = (size.x * 3 + 3) & (~3);
|
||||
result.resize(size.x * size.y * 3);
|
||||
|
||||
std::vector<unsigned char> rowData(row_padded);
|
||||
for (int y = size.y - 1; y >= 0; --y) {
|
||||
bmp_file.read(reinterpret_cast<char *>(rowData.data()), row_padded);
|
||||
for (int x = 0; x < size.x; ++x) {
|
||||
result[(y * size.x + x) * 3 + 2] = rowData[x * 3 + 0];
|
||||
result[(y * size.x + x) * 3 + 1] = rowData[x * 3 + 1];
|
||||
result[(y * size.x + x) * 3 + 0] = rowData[x * 3 + 2];
|
||||
}
|
||||
}
|
||||
|
||||
bmp_file.close();
|
||||
format = ColorFormat::RGB;
|
||||
return result;
|
||||
}
|
||||
|
||||
Texture::Texture(const Vector2i& size, FilteringMode filtering_mode) : invert_y(true), format(ColorFormat::RGBA), size(size), filtering_mode(filtering_mode), wrapping_mode(WrappingMode::CLAMP_TO_EDGE) {
|
||||
if (SizeExceedsMaximum(size))
|
||||
Logger::Error("Creating a texture where the size is bigger than the maximum for this system, use Texture::MaximumSize() to check for this beforehand.");
|
||||
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);
|
||||
|
||||
if (filtering_mode == FilteringMode::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);
|
||||
|
||||
else if (filtering_mode == FilteringMode::BILINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
if (filtering_mode == FilteringMode::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 == FilteringMode::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 == FilteringMode::MIPMAP_TRILINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
//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, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||
}
|
||||
void Texture::load(const unsigned char* pixels) {
|
||||
// TODO a static member function to query for this.
|
||||
if (SizeExceedsMaximum(size))
|
||||
Logger::Error("Creating a texture where the size is bigger than the maximum for this system.");
|
||||
|
||||
GLuint previous_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||
|
||||
glGenTextures(1, &texture_handle);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_handle);
|
||||
|
||||
if (format == ColorFormat::RGBA)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
else if (format == ColorFormat::RGB)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
||||
|
||||
if (wrapping_mode == WrappingMode::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);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int) size.x, (int) size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
else if (wrapping_mode == WrappingMode::REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
texture_format = TextureFormat::RGBA;
|
||||
texture_size = size;
|
||||
texture_filtering_mode = TextureFilteringMode::NEAREST;
|
||||
texture_wrapping_mode = TextureWrappingMode::CLAMP_TO_EDGE;
|
||||
// Because in vram it'll be right side up.
|
||||
texture_flags = TextureFlag::INVERT_Y;
|
||||
else if (wrapping_mode == WrappingMode::MIRRORED_REPEAT)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||
else if (wrapping_mode == WrappingMode::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 == FilteringMode::MIPMAP_NEAREST ||
|
||||
filtering_mode == FilteringMode::MIPMAP_BILINEAR ||
|
||||
filtering_mode == FilteringMode::MIPMAP_TRILINEAR) {
|
||||
|
||||
void Texture::load(Image* software_texture, const Vector2& size, const TextureFormat& format,
|
||||
TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
|
||||
|
||||
GLuint previous_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*) &previous_texture);
|
||||
if (filtering_mode == FilteringMode::MIPMAP_NEAREST)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glGenTextures(1, &texture_handle);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_handle);
|
||||
else if (filtering_mode == FilteringMode::MIPMAP_BILINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
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->pixel_data.data());
|
||||
else if (filtering_mode == FilteringMode::MIPMAP_TRILINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
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->pixel_data.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) {
|
||||
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
|
||||
|
||||
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);
|
||||
}
|
||||
texture_size = size;
|
||||
texture_format = format;
|
||||
texture_filtering_mode = filtering_mode;
|
||||
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||
}
|
||||
|
||||
std::vector<Color4> JGL::Texture::GetPixelData() const {
|
||||
GLint current_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
|
||||
|
||||
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());
|
||||
glBindTexture(GL_TEXTURE_2D, current_texture);
|
||||
return result;
|
||||
if (anisotropy != SampleRate::NONE) {
|
||||
if (anisotropy > MaxAnisotropySampleRate())
|
||||
Logger::Error("Anisotropy set higher than the maximum value for this system, disabled, use Texture::MaxAnisotropy()."),
|
||||
anisotropy = SampleRate::NONE;
|
||||
else
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, (int) anisotropy);
|
||||
}
|
||||
}
|
||||
|
||||
//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);
|
||||
else if (filtering_mode == FilteringMode::NEAREST || filtering_mode == FilteringMode::BILINEAR) {
|
||||
if (filtering_mode == FilteringMode::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 == FilteringMode::BILINEAR)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR),
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
if (anisotropy != SampleRate::NONE)
|
||||
Logger::Error("Anisotropy only applies when using mipmaps with a 3D perspective, disabled."),
|
||||
anisotropy = SampleRate::NONE;
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, previous_texture);
|
||||
}
|
||||
|
||||
std::vector<Color4> JGL::Texture::GetPixelData() const {
|
||||
GLint current_texture;
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
|
||||
|
||||
std::vector<Color4> result((size_t) (size.x * size.y));
|
||||
glBindTexture(GL_TEXTURE_2D, texture_handle);
|
||||
|
||||
if (format == ColorFormat::RGBA) {
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, result.data());
|
||||
glBindTexture(GL_TEXTURE_2D, current_texture);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Texture::Erase() {
|
||||
if (texture_handle != 0)
|
||||
glDeleteTextures(1, &texture_handle);
|
||||
texture_handle = 0;
|
||||
}
|
||||
//if RGB
|
||||
std::vector<Color3> color3((size_t) (size.x * size.y));
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, color3.data());
|
||||
for (const auto &c: color3)
|
||||
result.emplace_back(c);
|
||||
|
||||
GLuint Texture::GetGLTextureHandle() const {
|
||||
return texture_handle;
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, current_texture);
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector2 Texture::GetDimensions() const {
|
||||
return texture_size;
|
||||
}
|
||||
GLuint Texture::GetHandle() const {
|
||||
return texture_handle;
|
||||
}
|
||||
|
||||
TextureFlag Texture::GetFlags() const {
|
||||
return texture_flags;
|
||||
}
|
||||
Vector2i Texture::GetDimensions() const {
|
||||
return size;
|
||||
}
|
||||
|
||||
TextureFormat Texture::GetFormat() const {
|
||||
return texture_format;
|
||||
}
|
||||
bool Texture::Inverted() const {
|
||||
return invert_y;
|
||||
}
|
||||
|
||||
TextureFilteringMode Texture::GetFilteringMode() const {
|
||||
return texture_filtering_mode;
|
||||
}
|
||||
ColorFormat Texture::GetFormat() const {
|
||||
return format;
|
||||
}
|
||||
|
||||
TextureWrappingMode Texture::GetWrappingMode() const {
|
||||
return texture_wrapping_mode;
|
||||
}
|
||||
FilteringMode Texture::GetFilteringMode() const {
|
||||
return filtering_mode;
|
||||
}
|
||||
|
||||
Texture::Texture(Image* software_texture, const Vector2& size, const TextureFormat& format,
|
||||
TextureFilteringMode filtering_mode, TextureWrappingMode wrapping_mode) {
|
||||
load(software_texture, size, format, filtering_mode, wrapping_mode);
|
||||
}
|
||||
WrappingMode Texture::GetWrappingMode() const {
|
||||
return wrapping_mode;
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
Erase();
|
||||
}
|
||||
Texture::Texture(const Color4* pixels, const Vector2i& size, FilteringMode filtering_mode, SampleRate anisotropy, WrappingMode wrapping_mode) :
|
||||
size(size), format(ColorFormat::RGBA), filtering_mode(filtering_mode), anisotropy(anisotropy), wrapping_mode(wrapping_mode) {
|
||||
load((unsigned char*) pixels);
|
||||
}
|
||||
|
||||
Texture::Texture(const Texture& rhs) {
|
||||
auto* this_texture = new Texture(rhs.GetDimensions());
|
||||
auto this_render_target = RenderTarget(this);
|
||||
auto rhs_render_target = RenderTarget(&rhs);
|
||||
Texture::Texture(const Color3* pixels, const Vector2i& size, FilteringMode filtering_mode, SampleRate anisotropy, WrappingMode wrapping_mode) :
|
||||
size(size), format(ColorFormat::RGB), filtering_mode(filtering_mode), anisotropy(anisotropy), wrapping_mode(wrapping_mode) {
|
||||
load((unsigned char*) pixels);
|
||||
}
|
||||
|
||||
RenderTarget::Blit(rhs_render_target, &this_render_target);
|
||||
Texture::~Texture() {
|
||||
if (texture_handle != 0)
|
||||
glDeleteTextures(1, &texture_handle);
|
||||
texture_handle = 0;
|
||||
}
|
||||
|
||||
this->texture_handle = this_texture->texture_handle;
|
||||
this->texture_size = this_texture->texture_size;
|
||||
this->texture_flags = this_texture->texture_flags;
|
||||
this->texture_format = this_texture->texture_format;
|
||||
this->texture_filtering_mode = this_texture->texture_filtering_mode;
|
||||
this->texture_wrapping_mode = this_texture->texture_wrapping_mode;
|
||||
Texture::Texture(const Texture& rhs) {
|
||||
auto* this_texture = new Texture(rhs.GetDimensions());
|
||||
auto this_render_target = RenderTarget(this_texture);
|
||||
auto rhs_render_target = RenderTarget(&rhs);
|
||||
|
||||
// Free the memory of "this_texture" without calling the destructor.
|
||||
// In 99% of cases you wouldn't want this. But in this scenario we do.
|
||||
operator delete(this_texture);
|
||||
}
|
||||
}
|
||||
RenderTarget::Blit(rhs_render_target, &this_render_target);
|
||||
|
||||
this->texture_handle = this_texture->texture_handle;
|
||||
this->size = this_texture->size;
|
||||
this->invert_y = this_texture->invert_y;
|
||||
this->format = this_texture->format;
|
||||
this->filtering_mode = this_texture->filtering_mode;
|
||||
this->wrapping_mode = this_texture->wrapping_mode;
|
||||
operator delete(this_texture);
|
||||
}
|
||||
|
||||
bool Texture::SizeExceedsMaximum(const Vector2i& s) {
|
||||
|
||||
auto max_size = Texture::MaxSize();
|
||||
return s.x > max_size.x || s.y > max_size.y;
|
||||
}
|
||||
|
||||
Vector2i Texture::MaxSize() {
|
||||
GLint max_size;
|
||||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_size);
|
||||
|
||||
return { max_size, max_size };
|
||||
}
|
||||
|
||||
SampleRate Texture::MaxAnisotropySampleRate() {
|
||||
if (!GLAD_GL_ARB_texture_filter_anisotropic)
|
||||
return SampleRate::NONE;
|
||||
|
||||
float anisotropy;
|
||||
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &anisotropy);
|
||||
|
||||
if (anisotropy == 2)
|
||||
return SampleRate::X2;
|
||||
else if (anisotropy == 4)
|
||||
return SampleRate::X4;
|
||||
else if (anisotropy == 8)
|
||||
return SampleRate::X8;
|
||||
else
|
||||
return SampleRate::X16;
|
||||
}
|
||||
|
||||
SampleRate Texture::GetAnisotropySampleRate() const {
|
||||
return anisotropy;
|
||||
}
|
||||
|
63
src/types/TextureAtlas.cpp
Normal file
63
src/types/TextureAtlas.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <JGL/types/TextureAtlas.h>
|
||||
#include <JGL/types/RenderTarget.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
|
||||
using namespace JGL;
|
||||
AtlasRegion TextureAtlas::GetRegion(unsigned int region) const {
|
||||
if (region >= regions.size()) {
|
||||
Logger::Error("Requesting region " + std::to_string(region) + "but it's outside of the texture atlas?");
|
||||
return { {0, 0},{0, 0} };
|
||||
}
|
||||
return regions[region];
|
||||
}
|
||||
|
||||
TextureAtlas::TextureAtlas(const Color4** pixels, const Vector2i** sizes, unsigned int texture_count, FilteringMode filtering_mode) {
|
||||
std::vector<Texture> textures(texture_count);
|
||||
|
||||
int x_length = 0; int largest_height = 0;
|
||||
for (unsigned int i = 0; i < texture_count; i++) {
|
||||
textures[i] = Texture(pixels[i], *sizes[i], FilteringMode::NEAREST);
|
||||
x_length += sizes[i]->x;
|
||||
|
||||
if (sizes[i]->y > largest_height)
|
||||
largest_height = sizes[i]->y;
|
||||
}
|
||||
|
||||
int rows = 1;
|
||||
while (x_length > Texture::MaxSize().x)
|
||||
x_length /= 2, rows += 1;
|
||||
|
||||
auto* result = new Texture(Vector2i(x_length, largest_height * rows), filtering_mode);
|
||||
auto* render_target = new RenderTarget(result);
|
||||
|
||||
int current_x = 0; int current_y = 0;
|
||||
for (unsigned int i = 0; i < texture_count; i++) {
|
||||
if (current_x + sizes[i]->x > x_length)
|
||||
current_x = 0, current_y += largest_height;
|
||||
|
||||
RenderTarget::Blit(&textures[i], render_target, Vector2i(current_x, current_y));
|
||||
regions.emplace_back(AtlasRegion({ current_x, current_y }, { sizes[i]->x, sizes[i]->y }));
|
||||
current_x += sizes[i]->x;
|
||||
}
|
||||
|
||||
render_target->RegenerateMipMaps();
|
||||
delete render_target;
|
||||
|
||||
this->texture_handle = result->GetHandle();
|
||||
this->size = result->GetDimensions();
|
||||
this->invert_y = result->Inverted();
|
||||
this->filtering_mode = result->GetFilteringMode();
|
||||
this->wrapping_mode = result->GetWrappingMode();
|
||||
this->anisotropy = result->GetAnisotropySampleRate();
|
||||
this->format = result->GetFormat();
|
||||
|
||||
// Don't call destructor so the v-ram isn't cleared.
|
||||
operator delete(result);
|
||||
}
|
||||
|
||||
TextureAtlas::TextureAtlas(const Color4* pixels, const Vector2i& size, AtlasRegion** regions, unsigned int region_count, FilteringMode filtering_mode, SampleRate anisotropy) : Texture(pixels, size, filtering_mode, anisotropy) {
|
||||
this->regions.resize(region_count);
|
||||
for (unsigned int i = 0; i < region_count; ++i)
|
||||
this->regions[i] = *regions[i];
|
||||
}
|
||||
|
@@ -170,6 +170,10 @@ std::vector<GLfloat> JGL::VRamList::GetDataF() const {
|
||||
if (element_array_buffer)
|
||||
JGL::Logger::Warning("Getting data as GLfloat but the buffer data is GLuint?");
|
||||
|
||||
bool vertex_array_enabled = glIsEnabled(GL_VERTEX_ARRAY);
|
||||
if (!vertex_array_enabled)
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
|
||||
GLint current_buffer = 0;
|
||||
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, ¤t_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, list_handle);
|
||||
@@ -184,6 +188,8 @@ std::vector<GLfloat> JGL::VRamList::GetDataF() const {
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, current_buffer);
|
||||
|
||||
if (!vertex_array_enabled)
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -289,7 +295,6 @@ JGL::VRamList::VRamList(const JGL::VRamList& rhs) {
|
||||
if (rhs.element_array_buffer) {
|
||||
auto data_array = rhs.GetDataUI();
|
||||
this->load(data_array.data(), data_array.size());
|
||||
return;
|
||||
}
|
||||
|
||||
auto data_array = rhs.GetDataF();
|
||||
|
@@ -1,20 +1,21 @@
|
||||
#include <JGL/types/VertexArray.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
|
||||
using namespace JGL;
|
||||
|
||||
VRamList VertexArray::GetVertices() const {
|
||||
const VRamList* VertexArray::GetVertices() const {
|
||||
return vertices;
|
||||
}
|
||||
|
||||
VRamList VertexArray::GetIndices() const {
|
||||
const VRamList* VertexArray::GetIndices() const {
|
||||
return indices;
|
||||
}
|
||||
|
||||
JGL::VRamList VertexArray::GetNormals() const {
|
||||
const VRamList* VertexArray::GetNormals() const {
|
||||
return normals;
|
||||
}
|
||||
|
||||
VRamList VertexArray::GetTextureCoordinates() const {
|
||||
const VRamList* VertexArray::GetTextureCoordinates() const {
|
||||
return texture_coordinates;
|
||||
}
|
||||
|
||||
@@ -144,46 +145,33 @@ VertexArray::VertexArray(const Vector3* vertex_positions, const long& vp_length,
|
||||
const Normal* vertex_normals, const long& vn_length, const TextureCoordinate* texture_coordinates, const long& vt_length) {
|
||||
|
||||
// TODO decimator. This is a total waste of memory as it sits.
|
||||
vertices = VRamList(vertex_positions, vp_length);
|
||||
this->vertices = new VRamList(vertex_positions, vp_length);
|
||||
|
||||
local_vertices.resize(vp_length);
|
||||
memcpy(local_vertices.data(), vertex_positions, sizeof(Vector3) * vp_length);
|
||||
|
||||
if (vertex_indices && vi_length) {
|
||||
indices = VRamList(vertex_indices, vi_length);
|
||||
indices = new VRamList(vertex_indices, vi_length);
|
||||
local_indices.resize(vi_length);
|
||||
memcpy(local_indices.data(), vertex_indices, sizeof(unsigned int) * vi_length);
|
||||
}
|
||||
|
||||
if (vertex_normals && vn_length) {
|
||||
normals = VRamList(vertex_normals, vn_length);
|
||||
normals = new VRamList(vertex_normals, vn_length);
|
||||
local_normals.resize(vn_length);
|
||||
memcpy(local_normals.data(), vertex_indices, sizeof(Normal) * vn_length);
|
||||
}
|
||||
|
||||
if (texture_coordinates && vt_length) {
|
||||
this->texture_coordinates = VRamList(texture_coordinates, vt_length);
|
||||
local_normals.resize(vt_length);
|
||||
this->texture_coordinates = new VRamList(texture_coordinates, vt_length);
|
||||
local_texture_coordinates.resize(vt_length);
|
||||
memcpy(local_texture_coordinates.data(), texture_coordinates, sizeof(TextureCoordinate) * vt_length);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
VertexArray::VertexArray(const std::vector<Vertex>& vertex_positions, const std::vector<unsigned int>& vertex_indices,
|
||||
const std::vector<Normal>& vertex_normals, const std::vector<TextureCoordinate>& texture_coordinates) {
|
||||
vertices = VRamList(vertex_positions.data(), vertex_positions.size());
|
||||
local_vertices = vertex_positions;
|
||||
|
||||
if (!vertex_indices.empty()) {
|
||||
indices = VRamList(vertex_indices.data(), vertex_indices.size());
|
||||
local_indices = vertex_indices;
|
||||
}
|
||||
|
||||
if (!vertex_normals.empty()) {
|
||||
normals = VRamList(vertex_normals.data(), vertex_normals.size());
|
||||
local_normals = vertex_normals;
|
||||
}
|
||||
|
||||
if (!texture_coordinates.empty()){
|
||||
this->texture_coordinates = VRamList(texture_coordinates.data(), texture_coordinates.size());
|
||||
local_texture_coordinates = texture_coordinates;
|
||||
}
|
||||
VertexArray::VertexArray(const std::vector<Vertex>& vertex_positions, const std::vector<unsigned int>& vertex_indices, const std::vector<Normal>& vertex_normals, const std::vector<TextureCoordinate>& texture_coordinates) {
|
||||
*this = VertexArray(vertex_positions.data(), vertex_positions.size(), vertex_indices.data(), vertex_indices.size(), vertex_normals.data(), vertex_normals.size(), texture_coordinates.data(), texture_coordinates.size());
|
||||
}
|
||||
|
||||
bool VertexArray::Static() {
|
||||
|
7988
src/types/internals/stb_image.h
Normal file
7988
src/types/internals/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
334
src/types/model/WavefrontOBJ.cpp
Normal file
334
src/types/model/WavefrontOBJ.cpp
Normal file
@@ -0,0 +1,334 @@
|
||||
#include <JGL/types/VertexArray.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
|
||||
std::pair<float, unsigned long> ParseNumber(const std::string& file_text, const unsigned long& offset) {
|
||||
std::string number;
|
||||
unsigned long new_offset = offset;
|
||||
bool decimal_used = false;
|
||||
|
||||
if (offset >= file_text.size())
|
||||
return {0.0f, offset};
|
||||
|
||||
for (; new_offset < file_text.size(); new_offset++) {
|
||||
if (file_text[new_offset] == '-') {
|
||||
if (number.empty()) {
|
||||
number.push_back(file_text[new_offset]);
|
||||
continue;
|
||||
}
|
||||
Logger::Error("Error while parsing number, Index: " + std::to_string(new_offset) + " Extra negative sign.");
|
||||
return {0.0f, offset};
|
||||
}
|
||||
|
||||
else if (file_text[new_offset] == '.') {
|
||||
if (!decimal_used) {
|
||||
number.push_back(file_text[new_offset]);
|
||||
decimal_used = true;
|
||||
continue;
|
||||
}
|
||||
Logger::Error("Error parsing number at: " + std::to_string(new_offset) + " Extra decimal point.");
|
||||
return {0.0f, offset};
|
||||
}
|
||||
|
||||
else if (isdigit(file_text[new_offset]))
|
||||
number.push_back(file_text[new_offset]);
|
||||
else
|
||||
break;
|
||||
}
|
||||
return {std::stof(number), new_offset};
|
||||
}
|
||||
|
||||
std::pair<Vector2, unsigned long> ParseVector2(const std::string& file_text, const unsigned long& offset) {
|
||||
auto x_result = ParseNumber(file_text, offset);
|
||||
auto y_result = ParseNumber(file_text, x_result.second + 1);
|
||||
|
||||
// If the new offset is the same as the offset we passed in then we know it didn't work.
|
||||
if (x_result.second == offset || y_result.second == x_result.second)
|
||||
return {Vector2(0, 0), offset};
|
||||
|
||||
return {Vector2(x_result.first, y_result.first), y_result.second + 1};
|
||||
}
|
||||
|
||||
std::pair<Vector3, unsigned long> ParseVector3(const std::string& file_text, const unsigned long& offset) {
|
||||
auto x_result = ParseNumber(file_text, offset);
|
||||
auto y_result = ParseNumber(file_text, x_result.second + 1);
|
||||
auto z_result = ParseNumber(file_text, y_result.second + 1);
|
||||
|
||||
// If the new offset is the same as the offset we passed in then we know it didn't work.
|
||||
if (x_result.second == offset || y_result.second == x_result.second || z_result.second == y_result.second)
|
||||
return {Vector3(0,0,0), offset};
|
||||
|
||||
return {Vector3(x_result.first, y_result.first, z_result.first), z_result.second + 1};
|
||||
}
|
||||
|
||||
std::pair<Vector4, unsigned long> ParseVector4(const std::string& file_text, const unsigned long& offset) {
|
||||
auto x_result = ParseNumber(file_text, offset);
|
||||
auto y_result = ParseNumber(file_text, x_result.second + 1);
|
||||
auto z_result = ParseNumber(file_text, y_result.second + 1);
|
||||
auto w_result = ParseNumber(file_text, z_result.second + 1);
|
||||
|
||||
if (x_result.second == offset || y_result.second == x_result.second || z_result.second == y_result.second || w_result.second == z_result.second)
|
||||
return {Vector4(0, 0, 0, 0), offset};
|
||||
|
||||
return {Vector4(x_result.first, y_result.first, z_result.first, w_result.first), w_result.second + 1};
|
||||
}
|
||||
|
||||
std::pair<std::array<Vector3, 3>, unsigned long> ParseWavefrontFaceData(const std::string& file_text, const unsigned long& offset) {
|
||||
unsigned long new_offset = offset;
|
||||
std::array<Vector3, 3> face_data;
|
||||
|
||||
for (unsigned int i = 0; i < 3; i++) {
|
||||
Vector3 vertex = {-1, -1, -1};
|
||||
|
||||
// Vertex
|
||||
if (std::isdigit(file_text[new_offset])) {
|
||||
auto v_result = ParseNumber(file_text, new_offset);
|
||||
vertex.x = v_result.first;
|
||||
new_offset = v_result.second;
|
||||
}
|
||||
|
||||
// UV
|
||||
if (new_offset < file_text.size() && file_text[new_offset] == '/') {
|
||||
new_offset++;
|
||||
|
||||
if (new_offset < file_text.size() && (std::isdigit(file_text[new_offset]))) {
|
||||
auto vt_result = ParseNumber(file_text, new_offset);
|
||||
vertex.y = vt_result.first;
|
||||
new_offset = vt_result.second;
|
||||
}
|
||||
}
|
||||
|
||||
// Normal
|
||||
if (new_offset < file_text.size() && file_text[new_offset] == '/') {
|
||||
new_offset++;
|
||||
|
||||
if (new_offset < file_text.size() && (std::isdigit(file_text[new_offset]))) {
|
||||
auto vn_result = ParseNumber(file_text, new_offset);
|
||||
vertex.z = vn_result.first;
|
||||
new_offset = vn_result.second;
|
||||
}
|
||||
}
|
||||
|
||||
face_data[i] = vertex;
|
||||
new_offset++;
|
||||
}
|
||||
return {face_data, new_offset};
|
||||
}
|
||||
|
||||
VertexArray VertexArray::LoadWavefrontOBJ(const std::string &file_text) {
|
||||
std::vector<std::array<Vector3, 3>> faceline_data;
|
||||
|
||||
std::vector<TextureCoordinate> temp_uvs;
|
||||
std::vector<Normal> temp_normals;
|
||||
std::vector<Vertex> temp_vertices;
|
||||
|
||||
unsigned long offset = 0;
|
||||
while (offset < file_text.size()) {
|
||||
char c = file_text[offset];
|
||||
|
||||
// TODO the entire line a comment is on should be skipped.
|
||||
// Skip forward to the character after the next newline.
|
||||
if (c == '#' || c == '\n' || c == '\r') {
|
||||
offset++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Vertices
|
||||
if (c == 'v' && offset + 1 < file_text.size() && file_text[offset + 1] == ' ') {
|
||||
offset += 2;
|
||||
|
||||
auto vertex_result = ParseVector3(file_text, offset);
|
||||
if (vertex_result.second != offset) {
|
||||
temp_vertices.push_back(vertex_result.first);
|
||||
offset = vertex_result.second;
|
||||
}
|
||||
}
|
||||
|
||||
// Normals.
|
||||
else if (c == 'v' && offset + 2 < file_text.size() && file_text[offset + 1] == 'n' &&
|
||||
file_text[offset + 2] == ' ') {
|
||||
offset += 3;
|
||||
|
||||
auto normal_result = ParseVector3(file_text, offset);
|
||||
if (normal_result.second != offset) {
|
||||
temp_normals.push_back(normal_result.first);
|
||||
offset = normal_result.second;
|
||||
}
|
||||
}
|
||||
|
||||
// Texture Coordinates
|
||||
else if (c == 'v' && offset + 2 < file_text.size() && file_text[offset + 1] == 't' &&
|
||||
file_text[offset + 2] == ' ') {
|
||||
offset += 3;
|
||||
|
||||
auto uv_result = ParseVector2(file_text, offset);
|
||||
if (uv_result.second != offset) {
|
||||
temp_uvs.push_back(uv_result.first);
|
||||
offset = uv_result.second;
|
||||
}
|
||||
}
|
||||
|
||||
// Face lines.
|
||||
else if (c == 'f' && offset + 1 < file_text.size() && file_text[offset + 1] == ' ') {
|
||||
offset += 2;
|
||||
|
||||
auto faceline_result = ParseWavefrontFaceData(file_text, offset);
|
||||
if (faceline_result.second != offset) {
|
||||
faceline_data.push_back(faceline_result.first);
|
||||
offset = faceline_result.second;
|
||||
}
|
||||
} else
|
||||
offset++;
|
||||
}
|
||||
|
||||
// Pick everything out of the face lines and set up how OpenGL expects.
|
||||
std::vector<Vertex> final_vertices;
|
||||
std::vector<TextureCoordinate> final_uvs;
|
||||
std::vector<Normal> final_normals;
|
||||
std::vector<unsigned int> final_indices;
|
||||
for (const auto &face: faceline_data) {
|
||||
for (const auto &vp: face) {
|
||||
Vertex vertex;
|
||||
TextureCoordinate uv;
|
||||
Normal normal;
|
||||
|
||||
if (vp.x != -1)
|
||||
vertex = temp_vertices[vp.x - 1];
|
||||
if (vp.y != -1)
|
||||
uv = temp_uvs[vp.y - 1];
|
||||
if (vp.z != -1)
|
||||
normal = temp_normals[vp.z - 1];
|
||||
|
||||
final_vertices.push_back(vertex);
|
||||
final_uvs.push_back(uv);
|
||||
final_normals.push_back(normal);
|
||||
final_indices.push_back((unsigned int) final_vertices.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return VertexArray(
|
||||
final_vertices.data(), final_vertices.size(),
|
||||
final_indices.data(), final_indices.size(),
|
||||
final_normals.data(), final_normals.size(),
|
||||
final_uvs.data(), final_uvs.size());
|
||||
}
|
||||
|
||||
/*
|
||||
VertexArray JGL::LoadAMO(const std::string& file_text) {
|
||||
std::vector<Vector3> temp_vertices;
|
||||
std::vector<Vector3> temp_normals;
|
||||
std::vector<Vector2> temp_uvs;
|
||||
std::vector<std::array<Vector3, 3>> faceline_data;
|
||||
std::string name;
|
||||
|
||||
unsigned long offset = 0;
|
||||
while (offset < file_text.size()) {
|
||||
char c = file_text[offset];
|
||||
|
||||
// If we've dropped on a newline character somehow then just skip passed it.
|
||||
if (c == '\n' || c == '\r') {
|
||||
offset++; continue;
|
||||
}
|
||||
|
||||
// File name.
|
||||
else if (offset + 2 < file_text.size() && c == 'a' && file_text[offset + 1] == 'o' && file_text[offset + 2] == ' ') {
|
||||
offset += 3;
|
||||
while (offset < file_text.size()) {
|
||||
if (file_text[offset] != '\n' && file_text[offset] != '\r')
|
||||
name.push_back(file_text[offset]), offset++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Vertices
|
||||
else if (offset + 1 < file_text.size() && c == 'v' && file_text[offset + 1] == ' ') {
|
||||
offset += 2; auto parsed_number = ParseNumber(file_text, offset);
|
||||
|
||||
if (parsed_number.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the Vertex count at: " + std::to_string(offset));
|
||||
|
||||
unsigned long vertex_count = parsed_number.first;
|
||||
offset = parsed_number.second;
|
||||
|
||||
for (unsigned long i = 0; i < vertex_count; i++) {
|
||||
// Skip by newlines.
|
||||
while (file_text[offset] == '\n' || file_text[offset] == '\r')
|
||||
offset++;
|
||||
|
||||
auto parsed_vector3 = ParseVector3(file_text, offset);
|
||||
if (parsed_vector3.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the Vertex at: " + std::to_string(offset));
|
||||
|
||||
temp_vertices.push_back(parsed_vector3.first);
|
||||
offset = parsed_vector3.second;
|
||||
}
|
||||
}
|
||||
|
||||
// UVs
|
||||
else if (offset + 2 < file_text.size() && c == 'v' && file_text[offset + 1] == 't' && file_text[offset + 2] == ' ') {
|
||||
offset += 3; auto parsed_number = ParseNumber(file_text, offset);
|
||||
|
||||
if (parsed_number.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the UV count at: " + std::to_string(offset));
|
||||
|
||||
unsigned long uv_count = parsed_number.first;
|
||||
offset = parsed_number.second;
|
||||
|
||||
for (unsigned long i = 0; i < uv_count; i++) {
|
||||
// Skip by newlines.
|
||||
while (file_text[offset] == '\n' || file_text[offset] == '\r')
|
||||
offset++;
|
||||
|
||||
auto parsed_vector2 = ParseVector2(file_text, offset);
|
||||
if (parsed_vector2.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the UV at: " + std::to_string(offset));
|
||||
|
||||
temp_uvs.push_back(parsed_vector2.first);
|
||||
offset = parsed_vector2.second;
|
||||
}
|
||||
}
|
||||
|
||||
// Normals
|
||||
else if (offset + 2 < file_text.size() && c == 'v' && file_text[offset + 1] == 'n' && file_text[offset + 2] == ' ') {
|
||||
offset += 3; auto parsed_number = ParseNumber(file_text, offset);
|
||||
|
||||
if (parsed_number.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the Normal count at: " + std::to_string(offset));
|
||||
|
||||
unsigned long uv_count = parsed_number.first;
|
||||
offset = parsed_number.second;
|
||||
|
||||
for (unsigned long i = 0; i < uv_count; i++) {
|
||||
// Skip by newlines.
|
||||
while (file_text[offset] == '\n' || file_text[offset] == '\r')
|
||||
offset++;
|
||||
|
||||
auto parsed_vector3 = ParseVector3(file_text, offset);
|
||||
if (parsed_vector3.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the Normal at: " + std::to_string(offset));
|
||||
|
||||
temp_normals.push_back(parsed_vector3.first);
|
||||
offset = parsed_vector3.second;
|
||||
}
|
||||
}
|
||||
|
||||
// Face Lines
|
||||
else if (offset + 1 < file_text.size() && c == 'f' && file_text[offset + 1] == ' ') {
|
||||
offset += 2; auto parsed_number = ParseNumber(file_text, offset);
|
||||
|
||||
if (parsed_number.second == offset)
|
||||
Logger::Fatal("We couldn't interpret the Face Line count at: " + std::to_string(offset));
|
||||
|
||||
unsigned long faceline_count = parsed_number.first;
|
||||
for (unsigned long i = 0; i < faceline_count; i++) {
|
||||
// Skip by newlines.
|
||||
while (file_text[offset] == '\n' || file_text[offset] == '\r')
|
||||
offset++;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user