Compare commits

...

31 Commits

Author SHA1 Message Date
cf72d92c28 Merge branch 'master' of https://git.redacted.cc/josh/JGL 2024-05-29 22:18:41 -04:00
5d99f8ec1f update 2024-05-29 22:18:40 -04:00
a388ee8021 Implement proper GL state management in DrawString2D, DrawString3D 2024-05-29 15:27:24 -04:00
41916a4089 Update JGL.cpp 2024-05-29 15:05:06 -04:00
222dd346fb Fix memleak 2024-05-27 20:48:22 -04:00
e6e567725b Color 2024-05-23 12:05:00 -04:00
93612bb816 Update JGL 2024-05-23 10:57:48 -04:00
9ac22a8a87 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	CMakeLists.txt
2024-05-01 20:30:50 -04:00
1eaa9c6574 Migrating dependencies to latest releases 2024-05-01 20:30:21 -04:00
Redacted
f5f590164a Update CMakeLists.txt
J3ML v20
2024-03-21 15:19:53 -04:00
c45485cabd Merge remote-tracking branch 'origin/master'
# Conflicts:
#	CMakeLists.txt
2024-02-29 02:18:05 -05:00
143703dea3 Migrating Sources to LearnOpenGL 2024-02-29 02:17:42 -05:00
Redacted
48493cb2fc Update CMakeLists.txt 2024-02-24 07:56:25 -05:00
5b7829b1f8 Implement LearnOpenGL::Mesh 2024-02-22 18:39:50 -05:00
868f52464e Fixed broken migration 2024-02-21 23:42:55 -05:00
d1d2493435 Migrate to latest ReWindow 2024-02-21 23:27:10 -05:00
da30a7eedd Merge pull request 'dawsh' (#12) from dawsh into master
Reviewed-on: #12
2024-02-20 17:23:59 -05:00
5d906d6f99 Move RenderText to J2D::DrawString2D 2024-02-20 17:20:54 -05:00
6107e9a762 Small Fix (Make Another Release) 2024-02-20 15:20:08 -05:00
a38a83692f Fix render to full viewport 2024-02-20 05:18:32 -05:00
c4d6426587 Merge pull request 'dawsh' (#11) from dawsh into master
Reviewed-on: #11
2024-02-20 04:47:38 -05:00
817b0166c0 Implement J3D::DrawString3D 2024-02-20 04:01:29 -05:00
2a98857bab Fix Implementation of J2D::DrawLine2D & J3D::DrawLine3D 2024-02-20 00:30:38 -05:00
29a64160e9 Semi-Working Text 2024-02-16 15:43:35 -05:00
f676ef5332 Currently Fucked 2024-02-16 13:58:51 -05:00
d343c6ca45 Merge pull request 'dawsh' (#3) from dawsh into master
Reviewed-on: #3
2024-02-16 13:29:17 -05:00
7a7e73a829 Start README 2024-02-16 13:28:23 -05:00
9406a9d429 Partial Implement RenderText 2024-02-16 13:11:18 -05:00
d0eb1f34ef SemiBrokend 2024-02-15 18:51:25 -05:00
406abbb5bd Attempting Text Input 2024-02-14 19:23:29 -05:00
328245f81a Implement LearnOpenGL::Shader 2024-02-14 19:23:18 -05:00
9 changed files with 596 additions and 377 deletions

View File

@@ -21,25 +21,38 @@ include(cmake/CPM.cmake)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-7.zip
URL https://git.redacted.cc/josh/j3ml/archive/Release-6.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.30.zip
)
CPMAddPackage(
NAME GLAD
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_mt.zip
)
file(COPY "assets" DESTINATION "${PROJECT_BINARY_DIR}")
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp" )
file(GLOB_RECURSE ASSETS "assets/*")
add_library(JGL SHARED ${SOURCES}
include/JGL/JGL.h
src/JGL/JGL.cpp)
add_library(JGL SHARED ${SOURCES})
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(Freetype REQUIRED)
add_executable(JGL_Demo main.cpp)
set_target_properties(JGL_Demo PROPERTIES LINK_FLAGS "-Wl,-rpath,./lib")
include_directories(${PROJECT_SOURCE_DIR}/include
${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS}
${J3ML_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${OPENGL_INCLUDE_DIRS})
include_directories(${J3ML_SOURCE_DIR}/include)
include_directories(${ReWindow_SOURCE_DIR}/include)
include_directories(${glad_SOURCE_DIR}/include)
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} J3ML)
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL PRIVATE ${FREETYPE_LIBRARIES})
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} J3ML ReWindowLibrary glad)

View File

@@ -0,0 +1,11 @@
# Josh Graphics Library
# Goals
* Provide single-function-calls to render various graphics primitives in 2D and 3D.
* Integrated directly with our other toolkits (ReWindow, J3ML)
* Quick Rendering of Debug Text, Geometric Widgets, Textures, and so forth.
# Non-Goals
* Full Rendering Engine
* OpenGL/Vulkan Wrapper
* Asset Loading & Management

BIN
assets/fonts/FreeSans.ttf Normal file

Binary file not shown.

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

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

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

@@ -0,0 +1,11 @@
#pragma once
namespace JGL
{
struct Color4{
int r;
int g;
int b;
int a;
};
}

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

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

View File

@@ -5,216 +5,31 @@
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include "J3ML/LinearAlgebra/Vector3.h"
#include <J3ML/LinearAlgebra/Vector3.h>
#include <string>
#include <iostream>
#include <JGL/Color3.h>
#include <J3ML/Geometry/Sphere.h>
#include <J3ML/Geometry/OBB.h>
#include <J3ML/Geometry/Capsule.h>
#include <J3ML/Geometry/Plane.h>
#include <J3ML/Geometry/TriangleMesh.h>
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
namespace JGL {
// All functions accept coordinates in pixel-screen space [0, 600]
// and are internally transformed to OpenGL clip space [-1, +1]
using namespace LinearAlgebra;
struct RGBTuple {
int r; int g; int b;
};
struct RGBATuple {
int r; int g; int b; int a;
};
struct HSVTuple {
int h; int s; int v;
};
struct HSLTuple {
int h; int s; int l;
};
struct Color3 {
int r;
int g;
int b;
Color3 Lerp(const Color3& rhs, float alpha) const;
};
struct Color4{
int r;
int g;
int b;
int a;
};
namespace Colors {
namespace Primary {
static const Color3 Red {255, 0, 0};
static const Color3 Green { 0, 255, 0};
static const Color3 Blue { 0, 0, 255};
static const Color3 White {255, 255, 255};
static const Color3 Black { 0, 0, 0};
static const Color3 Gray {128, 128, 128};
static const Color3 DarkGray {192, 192, 192};
static const Color3 LightGray { 64, 64, 64};
static const Color3 Yellow {255, 255, 0};
}
using namespace Primary;
namespace Reds {
static const Color3 Fuchsia {};
static const Color3 LightSalmon {255, 160, 122};
static const Color3 Salmon {250, 128, 114};
static const Color3 DarkSalmon {233, 150, 122};
static const Color3 LightCoral {240, 128, 128};
static const Color3 IndianRed {205, 92, 92};
static const Color3 Crimson {220, 20, 60};
static const Color3 Firebrick {178, 34, 34};
static const Color3 DarkRed {139, 0, 0};
}
namespace Oranges {
static const Color3 Coral {255, 127, 80};
static const Color3 Tomato {255, 99, 71};
static const Color3 OrangeRed {255, 69, 0};
static const Color3 Gold {255, 215, 0};
static const Color3 Orange {255, 165, 0};
static const Color3 DarkOrange {255, 140, 0};
}
namespace Yellows {
static const Color3 LightYellow {255, 255, 224};
static const Color3 LemonChiffon {255, 250 ,205};
static const Color3 LightGoldenrodYellow {250, 250, 210};
static const Color3 PapayaWhip {255, 239, 213};
static const Color3 Moccasin {255, 228, 181};
static const Color3 PeachPuff {255, 218, 185};
static const Color3 PaleGoldenrod {238, 232, 170};
static const Color3 Khaki {240, 230, 140};
static const Color3 DarkKhaki {189, 183, 107};
}
namespace Greens {
static const Color3 LawnGreen {124, 252, 0};
static const Color3 Chartreuse {127, 255, 0};
static const Color3 LimeGreen {50, 205, 50};
static const Color3 ForestGreen {34, 139, 34};
static const Color3 DarkGreen {0, 100, 0};
static const Color3 GreenYellow{173, 255, 47};
static const Color3 YellowGreen{154, 205, 50};
static const Color3 SpringGreen{0, 255, 127};
static const Color3 MediumSpringGreen{0, 250, 154};
static const Color3 LightGreen{144, 238, 144};
static const Color3 PaleGreen{152, 251, 152};
static const Color3 DarkSeaGreen{143, 188, 143};
static const Color3 MediumSeaGreen{60, 179, 113};
static const Color3 SeaGreen{46, 139, 87};
static const Color3 DarkOliveGreen{85, 107, 47};
static const Color3 OliveDrab{107, 142, 35};
static const Color3 Lime{0, 255, 0};
static const Color3 Olive{128, 128, 0};
}
namespace Cyans {
static const Color3 LightCyan{224, 255, 255};
static const Color3 Cyan{0, 255, 255};
static const Color3 Aqua{0, 255, 255};
static const Color3 Aquamarine{127, 255, 212};
static const Color3 MediumAquamarine{102, 205, 170};
static const Color3 PaleTurquoise{175, 238, 238};
static const Color3 Turquoise{64, 224, 208};
static const Color3 MediumTurquoise{72, 209, 204};
static const Color3 DarkTurquoise{0, 206, 209};
static const Color3 LightSeaGreen{32, 178, 170};
static const Color3 CadetBlue{95, 158, 160};
static const Color3 DarkCyan{0, 139, 139};
static const Color3 Teal{0, 128, 128};
}
namespace Blues {
static const Color3 PowderBlue {176, 224, 230};
static const Color3 LightBlue {173, 216, 230};
static const Color3 LightSkyBlue {135, 206, 250};
static const Color3 SkyBlue {135, 206, 235};
static const Color3 DeepSkyBlue {0, 191, 255};
static const Color3 LightSteelBlue {176, 196, 222};
static const Color3 DodgerBlue {30, 144, 255};
static const Color3 CornflowerBlue {100, 149, 237};
static const Color3 SteelBlue {70, 130, 180};
static const Color3 RoyalBlue {65, 105, 225};
static const Color3 MediumBlue {0, 0, 205};
static const Color3 DarkBlue {0, 0, 139};
static const Color3 Navy {0, 0, 128};
static const Color3 MidnightBlue {25, 25, 112};
static const Color3 MediumSlateBlue {123, 104, 238};
static const Color3 SlateBlue {106, 90, 205};
static const Color3 DarkSlateBlue {72, 61, 139};
}
namespace Purples {
static const Color3 Lavender {230, 230, 250};
static const Color3 Thistle {216, 191, 216};
static const Color3 Plum {221, 160, 221};
static const Color3 Violet {238, 160, 221};
static const Color3 Orchid {218, 112, 214};
static const Color3 Fuchsia {255, 0, 255};
static const Color3 Magenta {255, 0, 255};
static const Color3 MediumOrchid {186, 85, 211};
static const Color3 MediumPurple {147, 112, 219};
static const Color3 BlueViolet {138, 43, 226};
static const Color3 DarkViolet {148, 0, 211};
static const Color3 DarkOrchid {153, 50, 204};
static const Color3 DarkMagenta {139, 0, 128};
static const Color3 Purple {128, 0, 128};
static const Color3 Indigo {75, 0, 130};
}
namespace Pinks {
static const Color3 Pink {255, 129, 203};
static const Color3 LightPink {255, 182, 193};
static const Color3 HotPink {255, 105, 180};
static const Color3 DeepPink {255, 20, 147};
static const Color3 PaleVioletRed {219, 112, 147};
static const Color3 MediumVioletRed {199, 21, 133};
}
namespace Whites {
static const Color3 Snow {255, 250, 250};
static const Color3 Honeydew {240, 255, 240};
static const Color3 MintCream {245, 255, 250};
static const Color3 Azure {240, 255, 255};
static const Color3 AliceBlue {240, 248, 255};
static const Color3 GhostWhite {248, 248, 255};
static const Color3 WhiteSmoke {245, 245, 245};
static const Color3 SeaShell {255, 245, 238};
static const Color3 Beige {245, 245, 220};
static const Color3 OldLace {253, 245, 230};
static const Color3 FloralWhite {255, 250, 240};
static const Color3 Ivory {255, 255, 240};
static const Color3 AntiqueWhite {250, 240, 215};
static const Color3 Linen {250, 240, 230};
static const Color3 LavenderBlush {255, 240, 245};
static const Color3 MistyRose {255, 228, 255};
}
namespace Grays {
static const Color3 Gainsboro {220, 220, 220};
static const Color3 LightGray {211, 211, 211};
static const Color3 Silver {192, 192, 192};
static const Color3 DimGray {105, 105, 105};
static const Color3 LightSlateGray {119, 136, 153};
static const Color3 SlateGray {112, 128, 144};
static const Color3 DarkSlateGray {47, 79, 79};
}
namespace Browns {
static const Color3 CornSilk {255, 248, 220};
static const Color3 BlanchedAlmond {255, 235, 205};
static const Color3 Bisque {255, 228, 196};
static const Color3 NavajoWhite {255, 222, 173};
static const Color3 Wheat {254, 222, 179};
static const Color3 BurlyWood {222, 184, 135};
static const Color3 Tan {210, 180, 140};
static const Color3 RosyBrown {188, 143, 143};
static const Color3 SandyBrown {244, 164, 96};
static const Color3 GoldenRod {218, 165, 32};
static const Color3 Peru {205, 133, 63};
static const Color3 Chocolate {210, 105, 30};
static const Color3 SaddleBrown {139, 69, 19};
static const Color3 Sienna {160, 82, 45};
static const Color3 Brown {164, 42, 42};
static const Color3 Maroon {128, 0, 0};
}
}
using J3ML::LinearAlgebra::Vector2;
using J3ML::LinearAlgebra::Vector3;
using J3ML::LinearAlgebra::Matrix3x3;
using J3ML::LinearAlgebra::Matrix4x4;
using J3ML::LinearAlgebra::AxisAngle;
using J3ML::LinearAlgebra::Quaternion;
using J3ML::Geometry::Sphere;
using J3ML::Geometry::OBB;
using J3ML::Geometry::Capsule;
using J3ML::Geometry::TriangleMesh;
using J3ML::Geometry::Plane;
struct HSV {
float hue;
@@ -226,7 +41,7 @@ namespace JGL {
{
Vector2 A;
Vector2 B;
Vector3 C;
Vector2 C;
};
struct Triangle3D
@@ -236,27 +51,26 @@ namespace JGL {
Vector3 C;
};
Vector2 ScreenToViewport(const Vector2& v);
Vector2 ViewportToScreen(const Vector2& v)
{
// TODO: Implement (CORRECT!!!) matrix transformation
}
bool InitTextEngine();
// TODO: implement correct coloring
namespace J2D {
void DrawPixel2D(const Color3& color, const Vector2 &coordinates);
void DrawPixel2D(const Color3& color, float x, float y);
void DrawLine2D(const Color3& color, const Vector2 &A, const Vector2 &B, float thickness = 1);
void DrawLine2D(const Color3& color, float x, float y, float w, float h, float thickness = 1);
void DrawPixel2D(const Color3 &color, const Vector2 &coordinates);
void DrawPixel2D(const Color3 &color, float x, float y);
void DrawLine2D(const Color3 &color, const Vector2 &A, const Vector2 &B, float thickness = 1);
void DrawLine2D(const Color3 &color, float x, float y, float w, float h, float thickness = 1);
void DrawCubicBezierCurve2D();
void OutlineCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions, float thickness = 1);
void FillCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions);
void OutlineTriangle2D();
void FillTriangle2D();
void OutlineCircle2D(const Color3 &color, const Vector2 &center, float radius, int subdivisions,
float thickness = 1);
void FillCircle2D(const Color3 &color, const Vector2 &center, float radius, int subdivisions);
void OutlineTriangle2D(const Color3 &color, const Triangle2D &tri);
void FillTriangle2D(const Color3 &color, const Triangle2D &tri);
void FillTexturedTriangle2D();
void FillTexturedPolygon2D();
void DrawSprite2D();
void DrawPartialSprite2D();
void DrawString2D();
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size = 16);
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size);
void OutlineRect2D ( const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void FillRoundedRect2D (const Color3& color, const Vector2& pos, const Vector2& size, float radius);
@@ -267,9 +81,15 @@ namespace JGL {
}
namespace J3D {
void DrawLine3D(const Color3& color, const Vector3 &A, const Vector3 &B, float thickness = 1);
void FillSphere3D();
void WireframeSphere3D();
void DrawString3D();
void FillSphere3D(const Color3& color, const Sphere& sphere);
void WireframeSphere3D(const Color3& color, const Sphere& sphere, float thickness = 1);
void FillOBB3D(const Color3& color, const OBB& obb);
void WireframeOBB3D(const Color3& color, const OBB& obb, float thickness = 1);
void FillCapsule3D(const Color3& color, const Capsule& capsule);
void WireframeCapsule3D(const Color3& color, const Capsule& cap, float thickness = 1);
void FillTriangleMesh3D(const Color3& color, const TriangleMesh& mesh);
void WireframeTriangleMesh3D(const Color3& color, const TriangleMesh& mesh, float thickness = 1);
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size = 12);
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
void DrawMatrixGizmo (const Matrix4x4&);

216
main.cpp
View File

@@ -1,142 +1,120 @@
#include <GL/glut.h>
#include "JGL/JGL.h"
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <rewindow/types/window.h>
#include <JGL/Colors.h>
#include <J3ML/LinearAlgebra/Vector2.h>
using J3ML::LinearAlgebra::Vector2;
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
void initGL()
struct Character
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glOrtho(-100.f, 100.f, -100.f, 100.f, -100.f, 100.f);
}
unsigned int TextureID; // ID handle of the glyph texture
Vector2 Size; // Size of glyph
Vector2 Bearing; // Offset from baseline to left/top of glyph
unsigned int Advance; // Offset to advance to next glyph
};
std::map<char, Character> Characters;
GLuint VAO, VBO;
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glMatrixMode(GL_MODELVIEW); // To operate on the Model-View matrix
glLoadIdentity(); // Reset the model-view matrix.
using J3ML::LinearAlgebra::Matrix4x4;
struct point {
GLfloat x;
GLfloat y;
GLfloat s;
GLfloat t;
};
// Define shapes enclosed within a pair of glBegin and glEnd
class JGLDemoWindow : public ReWindow::RWindow
{
public:
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.f, 0.f, 0.f);
glVertex2f(-0.8f, 0.1f);
glVertex2f(-0.2f, 0.1f);
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);
JGLDemoWindow() : ReWindow::RWindow() {}
JGLDemoWindow(const std::string& title, int width, int height) :
ReWindow::RWindow(title, width, height)
{
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.9f, -0.7f);
glColor3f(1.f, 1.f, 1.f); // White
glVertex2f(-0.5f, -0.7f);
glColor3f(.2f, .2f, .2f); // Dark Gray
glVertex2f(-0.5f, -0.3f);
glColor3f(1.f, 1.f, 1.f); // White
glVertex2f(-0.9f, -0.3f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.f); // Blue
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
glColor3f(1.f, 0.f, 0.f); // Red
glVertex2f(0.3f, -0.4f);
glColor3f(0.0f, 1.0f, 0.f); // Green
glVertex2f(0.9f, -0.4f);
glColor3f(0.f, 0.f, 1.f); // Blue
glVertex2f(0.6f, -0.9f);
glEnd();
glBegin(GL_POLYGON); // These verts form a closed polygon
glColor3f(1.f, 1.f, 0.f); // Yellow
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
JGL::J2D::FillRect2D(JGL::Colors::White, {0, 0}, {128, 128});
JGL::J2D::OutlineRect2D(JGL::Colors::Red, {0, 0}, {128, 128}, 4);
JGL::J2D::DrawPixel2D(JGL::Colors::Green, {0, 0});
JGL::J2D::FillCircle2D(JGL::Colors::Purples::DarkViolet, {0, 0}, 0.75f, 64);
glFlush(); // Render now
}
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
bool fullscreenMode = true;
void specialKeys(int key, int x, int y) {
switch(key) {
case GLUT_KEY_F1:
fullscreenMode = !fullscreenMode;
if (fullscreenMode) {
windowPosX = glutGet(GLUT_WINDOW_X);
windowPosY = glutGet(GLUT_WINDOW_Y);
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
} else {
glutReshapeWindow(windowWidth, windowHeight);
glutPositionWindow(windowPosX, windowPosY);
}
break;
}
}
// Handler for window re-size event. Called back when the window first appears
// and whenever the window is re-sized with its new width and height
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat) width / (GLfloat) height;
void initGL()
{
gladLoadGL();
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, getSize().x, getSize().y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // Operate on the Projection Matrix;
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
// Aspect >= 1, set the height from -1 to 1
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
} else {
// Aspect < 1, set the width from -1 to 1, with larger height
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
JGL::InitTextEngine();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, getSize().x, getSize().y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
JGL::J2D::FillRect2D(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
JGL::J2D::FillRect2D(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
JGL::Triangle2D tri
{
{140, 200},
{135, 100},
{105, 100}
};
JGL::J2D::FillTriangle2D(JGL::Colors::Yellow, tri);
JGL::J3D::DrawString3D(JGL::Colors::Red, "JGL Sample Text", {1, -120, 0.5f}, 2.f);
JGL::J2D::DrawString2D(JGL::Colors::Green, "William J. Tomasine II ", 0.f, -120.f, 1.f);
JGL::J2D::DrawLine2D(JGL::Colors::Greens::DarkGreen, {10, 10}, {200, 300});
JGL::J3D::DrawLine3D(JGL::Colors::Red, {0,0,0}, {1,0,0});
JGL::J3D::DrawLine3D(JGL::Colors::Red, {0,0,0}, {1,0,0});
//glFlush();
}
void OnRefresh(float elapsed) override
{
display();
this->glSwapBuffers();
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override
{
return true;
}
};
int main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Vertex, Primitive, & Color"); // Create window with given title
glutInitWindowSize(320, 320); // Set the window's initial width & height - non-square
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
glutSpecialFunc(specialKeys); // Register callback handler for special-key event
initGL(); // Our own OpenGL initialization
glutMainLoop();
auto* window = new JGLDemoWindow("JGL Demo Window", 1280, 720);
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
window->initGL();
window->setResizable(true);
while (window->isAlive())
{
window->pollEvents();
window->refresh();
}
return 0;
}

View File

@@ -1,38 +1,48 @@
//
// Created by dawsh on 1/17/24.
//
#include <vector>
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <GL/glut.h>
#include "J3ML/LinearAlgebra/Transform2D.h"
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include <JGL/Color3.h>
#include <iostream>
GLuint program;
namespace JGL
{
Vector2 ScreenToViewport(const Vector2 &v) {
// TODO: Implement (CORRECT!!!) matrix transformation
FT_Face face;
FT_Library ft;
//Transform2D transform;
using namespace J3ML;
//transform = transform.Translate(1.f, 1.f); // Center
//transform = transform.Scale(0.5f, 0.5f); // Scale by half
//transform = transform.Scale(viewportWidth, viewportHeight); // Scale by screen size
bool InitTextEngine() {
constexpr u32 default_font_size = 16;
if (FT_Init_FreeType(&ft))
{
std::cout << "Error::FREETYPE: " << std::endl;
return false;
}
//return transform.Transform(v);
if (FT_New_Face(ft, "assets/fonts/FreeSans.ttf", 0, &face))
{
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return false;
}
float x = v.x / 600.f;
float y = v.y / 600.f;
return {
x - .5f, y - .5f
};
return true;
}
namespace J2D
{
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = ScreenToViewport(pos);
auto vp_size = ScreenToViewport(size);
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_QUADS);
glColor3f(color.r, color.g, color.b);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(vp_pos.x, vp_pos.y);
glVertex2f(vp_pos.x, vp_pos.y + vp_size.y);
glVertex2f(vp_pos.x + vp_size.x, vp_pos.y + vp_size.y);
@@ -41,8 +51,8 @@ namespace JGL
}
void OutlineRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size, float thickness) {
auto vp_pos = ScreenToViewport(pos);
auto vp_size = ScreenToViewport(size);
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_LINE_LOOP);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
@@ -54,10 +64,10 @@ namespace JGL
}
void DrawLine2D(const Color3 &color, const Vector2 &A, const Vector2 &B, float thickness) {
auto vp_a = ScreenToViewport(A);
auto vp_b = ScreenToViewport(B);
auto vp_a = A;
auto vp_b = B;
glBegin(GL_LINE);
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_a.x, vp_a.y);
@@ -75,7 +85,7 @@ namespace JGL
}
void DrawPixel2D(const Color3 &color, const Vector2 &coordinates) {
auto vp_pos = ScreenToViewport(coordinates);
auto vp_pos = coordinates;
glBegin(GL_POINT);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_pos.x, vp_pos.y);
@@ -114,6 +124,96 @@ namespace JGL
}
glEnd();
}
void OutlineTriangle2D(const Color3& color, const Triangle2D& tri)
{
glBegin(GL_LINE_LOOP);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
}
void FillTriangle2D(const Color3& color, const Triangle2D& tri)
{
glBegin(GL_LINE_LOOP);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
}
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size) {
glUseProgram(0); // Fixed-function pipeline.
GLfloat currentColor[4];
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
FT_Set_Pixel_Sizes(face, 0, size);
GLuint textures[text.size()];
for (int i = 0; i < text.length(); i++)
{
if (FT_Load_Char(face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textures[i]);
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
float x2 = x + g->bitmap_left * scale;
float y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
float w = g->bitmap.width * scale;
float h = g->bitmap.rows * scale;
glBegin(GL_TRIANGLES);
glTexCoord2f(0, 0);
glVertex2f(x2, y2);
glTexCoord2f(0, 1);
glVertex2f(x2, y2 + h);
glTexCoord2f(1, 1);
glVertex2f(x2 + w, y2 + h);
glTexCoord2f(0, 0);
glVertex2f(x2, y2);
glTexCoord2f(1, 1);
glVertex2f(x2 + w, y2 + h);
glTexCoord2f(1, 0);
glVertex2f(x2 + w, y2);
glEnd();
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
for (auto& t : textures)
glDeleteTextures(1, &t);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
}
}
@@ -121,12 +221,88 @@ namespace JGL
{
void DrawLine3D(const Color3& color, const Vector3& A, const Vector3& B, float thickness)
{
glBegin(GL_LINE);
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex3f(A.x, A.y, A.z);
glVertex3f(B.x, B.y, B.z);
glEnd();
}
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size)
{
float x = pos.x;
float y = pos.y;
float z = pos.z;
GLfloat currentColor[4];
GLuint textures[text.size()];
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glUseProgram(0); // Fixed-function pipeline.
glColor4f(color.r, color.g, color.b, 1.0f);
FT_Set_Pixel_Sizes(face, 0, size);
//for (c = text.c_str(); *c; c++)
for (int i = 0; i < text.length(); i++)
{
if (FT_Load_Char(face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textures[i]);
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
float x2 = x + g->bitmap_left * scale;
float y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
float z2 = z;
float w = g->bitmap.width * scale;
float h = g->bitmap.rows * scale;
glBegin(GL_TRIANGLES);
glTexCoord2f(0, 0);
glVertex3f(x2, y2, z2);
glTexCoord2f(0, 1);
glVertex3f(x2, y2 + h, z2);
glTexCoord2f(1, 1);
glVertex3f(x2 + w, y2 + h, z2);
glTexCoord2f(0, 0);
glVertex3f(x2, y2, z2);
glTexCoord2f(1, 1);
glVertex3f(x2 + w, y2 + h, z2);
glTexCoord2f(1, 0);
glVertex3f(x2 + w, y2, z2);
glEnd();
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
for (auto& t : textures)
glDeleteTextures(1, &t);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
}
}
}