Gradient FillRect + GradientLine
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
cmake_minimum_required(VERSION 3.18)
|
||||
project(JGL
|
||||
VERSION 1.0
|
||||
LANGUAGES CXX
|
||||
@@ -31,7 +31,7 @@ CPMAddPackage(
|
||||
|
||||
CPMAddPackage(
|
||||
NAME jlog
|
||||
URL https://git.redacted.cc/josh/jlog/Prerelease-9.zip
|
||||
URL https://git.redacted.cc/josh/jlog/Prerelease-12.zip
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
|
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <JGL/Color4.h>
|
||||
#include <J3ML/J3ML.h>
|
||||
|
||||
namespace JGL
|
||||
@@ -10,16 +9,9 @@ namespace JGL
|
||||
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; }
|
||||
|
@@ -1,8 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <JGL/Color3.h>
|
||||
|
||||
namespace JGL
|
||||
{
|
||||
struct Color4{
|
||||
class Color4 {
|
||||
public:
|
||||
explicit Color4(const Color3& color3, unsigned int alpha) {r = color3.r; g = color3.g; b = color3.b; a = alpha;}
|
||||
Color4(int red, int green, int blue, int alpha = 127) : r(red), g(green), b(blue), a(alpha) {}
|
||||
static Color4 FromColor3(const Color3& color3, unsigned int alpha = 127) {return Color4(color3, alpha);}
|
||||
int r;
|
||||
int g;
|
||||
int b;
|
||||
|
12
include/JGL/Gradient.h
Normal file
12
include/JGL/Gradient.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/J3ML.h>
|
||||
|
||||
namespace JGL {
|
||||
enum class Gradient : u8 {
|
||||
Vertical = 0,
|
||||
Horizontal = 1,
|
||||
DiagonalTopLeft = 2,
|
||||
DiagonalBottomLeft = 3
|
||||
};
|
||||
}
|
@@ -5,7 +5,8 @@
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <JGL/Color3.h>
|
||||
#include <JGL/Color4.h>
|
||||
#include <JGL/Gradient.h>
|
||||
#include <JGL/FontCache.h>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
@@ -88,6 +89,12 @@ namespace JGL {
|
||||
void DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawLine(const Color4& color, float x1, float y1, float x2, float y2, float thickness = 1);
|
||||
|
||||
///Draws a line with a gradient that transitions across it.
|
||||
void DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness = 1);
|
||||
void DrawGradientLine(const Color3& color1, const Color3& color2, float x, float y, float w, float h, float thickness = 1);
|
||||
|
||||
/// Draws an outline of a rectangle on the screen.
|
||||
void OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness = 1);
|
||||
void OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
|
||||
@@ -96,21 +103,21 @@ namespace JGL {
|
||||
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
|
||||
void FillRect(const Color3& color, const Vector2& pos, const Vector2& size);
|
||||
|
||||
|
||||
/// Draws an outline of a rectangle with rounded corners on the screen.
|
||||
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1);
|
||||
/// Draws a filled rectangle where the color transitions across it.
|
||||
void FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
|
||||
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
|
||||
|
||||
/// Draws a filled rectangle with rounded corners on the screen.
|
||||
void FillRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius = 5, unsigned int subdivisions = 10);
|
||||
void FillRoundedRect(const Color3& color, const Vector2& pos, const Vector2& size, float radius = 5, unsigned int subdivisions = 10);
|
||||
void FillRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius = 5, unsigned int subdivisions = 8);
|
||||
void FillRoundedRect(const Color3& color, const Vector2& pos, const Vector2& size, float radius = 5, unsigned int subdivisions = 8);
|
||||
|
||||
/// Draws an outline of a circle on the screen.
|
||||
void OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 16, float thickness = 1);
|
||||
void OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 16, float thickness = 1);
|
||||
|
||||
/// Draws a filled circle on the screen.
|
||||
void FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 16);
|
||||
void FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 16);
|
||||
void FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
|
||||
void FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
|
||||
|
||||
/// Draws an outline of a triangle on the screen.
|
||||
void OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness = 1);
|
||||
@@ -133,8 +140,9 @@ namespace JGL {
|
||||
void DrawCubicBezierCurve();
|
||||
void OutlinePolygon (const Color4& color, std::vector<Vector2> points);
|
||||
void FillPolygon (const Color4& color, std::vector<Vector2> points, float thickness = 1);
|
||||
void GradientFillRect ();
|
||||
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1);
|
||||
}
|
||||
|
||||
namespace J3D {
|
||||
void Begin();
|
||||
void End();
|
||||
|
27
main.cpp
27
main.cpp
@@ -88,7 +88,7 @@ public:
|
||||
|
||||
Vector3 textAngle = {0,0,0};
|
||||
void display() {
|
||||
textAngle.y += 1.0f;
|
||||
textAngle.y += 2.0f;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
@@ -99,21 +99,26 @@ public:
|
||||
J3D::Begin();
|
||||
J3D::DrawLine(JGL::Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
|
||||
J3D::DrawLine(JGL::Colors::Red, {-0.33,-0.125,1}, {-0.33,0.25,1});
|
||||
J3D::DrawString(JGL::Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f},textAngle , 1.f, 32, FreeSans);
|
||||
J3D::DrawString(JGL::Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f},textAngle, 1.f, 32, FreeSans);
|
||||
J3D::End();
|
||||
|
||||
J2D::Begin();
|
||||
J2D::FillRect(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
|
||||
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
|
||||
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
|
||||
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
|
||||
J2D::FillRoundedRect(JGL::Colors::Red, {200, 52}, {100, 100}, 8, 8);
|
||||
J2D::FillRoundedRect(JGL::Colors::Purples::BlueViolet, {300, 52}, {100, 100}, 8, 4);
|
||||
|
||||
J2D::FillRoundedRect(JGL::Colors::Red, {256, 256}, {100.5, 100.5}, 8, 8);
|
||||
//J2D::FillRect({255,0,0,255/2}, {100.5, 100.5}, {32, 32});
|
||||
J2D::FillCircle(JGL::Colors::White, {52, 204}, 50, 24);
|
||||
J2D::OutlineCircle(JGL::Colors::White, {153, 204}, 50, 24);
|
||||
|
||||
//J2D::OutlineTriangle(JGL::Colors::Yellow, {{140, 200},{135, 100},{105, 100}});
|
||||
J2D::FillCircle(JGL::Colors::White, {120, 200}, 20, 16);
|
||||
J2D::DrawLine(JGL::Colors::Green, {10, 10}, {200, 300});
|
||||
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, -48.f, 1.f, 16, Jupiteroid);
|
||||
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, -65, 1,16, Jupiteroid);
|
||||
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, -82, 1,16, Jupiteroid);
|
||||
J2D::FillTriangle(Colors::Red, {{0, 275}, {0, 375}, {100, 375}});
|
||||
J2D::OutlineTriangle(Colors::Blue, {{100, 275}, {0, 275}, {100, 375}});
|
||||
J2D::DrawGradientLine(JGL::Colors::Red, JGL::Colors::Blue, {105, 375}, {200, 275}, 2);
|
||||
|
||||
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 16, 1.f, 16, Jupiteroid);
|
||||
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 33, 1,16, Jupiteroid);
|
||||
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 50, 1,16, Jupiteroid);
|
||||
J2D::End();
|
||||
}
|
||||
|
||||
|
178
src/JGL.cpp
178
src/JGL.cpp
@@ -5,11 +5,18 @@
|
||||
#include <JGL/JGL.h>
|
||||
#include <glad/glad.h>
|
||||
#include <JGL/Color3.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
|
||||
GLfloat oldColor[4] = {0, 0, 0, 255};
|
||||
bool inJ2D = false;
|
||||
bool inJ3D = false;
|
||||
bool wasTexture2DEnabled = false;
|
||||
bool wasTextureCoordArrayEnabled = false;
|
||||
bool wasDepthTestEnabled = false;
|
||||
bool wasVertexArraysEnabled = false;
|
||||
bool wasCullFaceEnabled = false;
|
||||
bool wasBlendEnabled = false;
|
||||
bool wasColorArrayEnabled = false;
|
||||
|
||||
namespace JGL {
|
||||
using namespace J3ML;
|
||||
@@ -18,12 +25,9 @@ namespace JGL {
|
||||
bool Update(const Vector2& window_size) {
|
||||
wS = window_size;
|
||||
return JGL::InitTextEngine();
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
void J2D::Begin() {
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
@@ -53,6 +57,25 @@ namespace JGL {
|
||||
wasBlendEnabled = false,
|
||||
glEnable(GL_BLEND),
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
wasTexture2DEnabled = true;
|
||||
if (!glIsEnabled(GL_TEXTURE_2D))
|
||||
wasTexture2DEnabled = false,
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
wasTextureCoordArrayEnabled = true;
|
||||
if (!glIsEnabled(GL_TEXTURE_COORD_ARRAY))
|
||||
wasTextureCoordArrayEnabled = false,
|
||||
glEnable(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
wasColorArrayEnabled = false;
|
||||
if (glIsEnabled(GL_COLOR_ARRAY))
|
||||
wasColorArrayEnabled = true,
|
||||
glDisable(GL_COLOR_ARRAY);
|
||||
|
||||
if (!inJ3D)
|
||||
inJ2D = true;
|
||||
else { ERROR("Attempt to Begin J2D inside of J3D context.") }
|
||||
}
|
||||
|
||||
void J2D::End() {
|
||||
@@ -73,11 +96,27 @@ namespace JGL {
|
||||
|
||||
if (!wasBlendEnabled)
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
if (!wasTexture2DEnabled)
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
if (!wasTextureCoordArrayEnabled)
|
||||
glDisable(GL_TEXTURE_COORD_ARRAY);
|
||||
|
||||
if (wasColorArrayEnabled)
|
||||
glEnable(GL_COLOR_ARRAY);
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
||||
|
||||
inJ2D = false;
|
||||
}
|
||||
|
||||
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
@@ -87,7 +126,46 @@ namespace JGL {
|
||||
J2D::FillRect({color.r, color.g, color.b, 255}, pos, size);
|
||||
}
|
||||
|
||||
void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
std::vector<GLfloat> colors = {};
|
||||
|
||||
if (gradient == Gradient::Horizontal)
|
||||
colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,
|
||||
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
|
||||
|
||||
else if (gradient == Gradient::Vertical)
|
||||
colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f,
|
||||
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f};
|
||||
|
||||
else if (gradient == Gradient::DiagonalBottomLeft)
|
||||
colors = {(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,
|
||||
color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f,
|
||||
(color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
|
||||
|
||||
else if (gradient == Gradient::DiagonalTopLeft)
|
||||
colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f,
|
||||
(color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f,
|
||||
(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f,(color1.a + color2.a) / 2.f / 255.f};
|
||||
|
||||
glEnable(GL_COLOR_ARRAY);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glColorPointer(4, GL_FLOAT, 0, colors.data());
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glDisable(GL_COLOR_ARRAY);
|
||||
}
|
||||
|
||||
void J2D::FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
|
||||
J2D::FillGradientRect({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, gradient, pos, size);
|
||||
}
|
||||
|
||||
void J2D::FillRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius, unsigned int subdivisions) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
J2D::FillRect(color, {pos.x + radius, pos.y}, {size.x - 2 * radius, size.y});
|
||||
J2D::FillRect(color, {pos.x, pos.y + radius}, {size.x, size.y - 2 * radius});
|
||||
|
||||
@@ -102,6 +180,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
|
||||
glLineWidth(thickness);
|
||||
@@ -115,6 +196,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {A, B};
|
||||
|
||||
glLineWidth(thickness);
|
||||
@@ -135,7 +219,37 @@ namespace JGL {
|
||||
J2D::DrawLine({color.r, color.g, color.b, 255}, x, y, w, h, thickness);
|
||||
}
|
||||
|
||||
void J2D::DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {A, B};
|
||||
GLfloat colors[8] = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,
|
||||
color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f};
|
||||
|
||||
glEnable(GL_COLOR_ARRAY);
|
||||
glLineWidth(thickness);
|
||||
glColorPointer(4,GL_FLOAT,sizeof(GL_FLOAT) * 4, colors);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glDisable(GL_COLOR_ARRAY);
|
||||
}
|
||||
|
||||
void J2D::DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness) {
|
||||
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, A, B, thickness);
|
||||
}
|
||||
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness) {
|
||||
J2D::DrawGradientLine(color1, color2, {x, y}, {w, h}, thickness);
|
||||
}
|
||||
|
||||
void DrawGradientLine(const Color3& color1, const Color3& color2, float x, float y, float w, float h, float thickness) {
|
||||
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, {x, y}, {w, h}, thickness);
|
||||
}
|
||||
|
||||
void J2D::DrawPixel(const Color4& color, const Vector2& coordinates) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {coordinates};
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
@@ -156,6 +270,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
float step = (2.f * M_PI) / (float) subdivisions;
|
||||
std::vector<Vector2> vertices{};
|
||||
GLfloat angle, x, y;
|
||||
@@ -177,6 +294,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J2D::FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
float step = (2.f * M_PI) / (float) subdivisions;;
|
||||
std::vector<Vector2> vertices{};
|
||||
GLfloat angle, x, y;
|
||||
@@ -196,6 +316,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J2D::OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
|
||||
|
||||
glLineWidth(thickness);
|
||||
@@ -209,6 +332,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J2D::FillTriangle(const Color4& color, const Triangle2D &tri) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
@@ -221,7 +347,9 @@ namespace JGL {
|
||||
}
|
||||
|
||||
void J3D::Begin() {
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
//Get what the draw color was before we did anything.
|
||||
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
||||
|
||||
wasDepthTestEnabled = false;
|
||||
if (glIsEnabled(GL_DEPTH_TEST))
|
||||
@@ -232,6 +360,29 @@ namespace JGL {
|
||||
if (!glIsEnabled(GL_VERTEX_ARRAY))
|
||||
wasVertexArraysEnabled = false,
|
||||
glEnable(GL_VERTEX_ARRAY);
|
||||
|
||||
wasTexture2DEnabled = true;
|
||||
if (!glIsEnabled(GL_TEXTURE_2D))
|
||||
wasTexture2DEnabled = false,
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
//TODO We won't always want this but for now we do.
|
||||
wasCullFaceEnabled = false;
|
||||
if (glIsEnabled(GL_CULL_FACE))
|
||||
wasCullFaceEnabled = true,
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
wasBlendEnabled = true;
|
||||
if (!glIsEnabled(GL_BLEND))
|
||||
wasBlendEnabled = false,
|
||||
glEnable(GL_BLEND),
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
|
||||
|
||||
if (!inJ2D)
|
||||
inJ3D = true;
|
||||
else { ERROR("Attempt to Begin J3D inside of J2D context.") }
|
||||
}
|
||||
|
||||
void J3D::End() {
|
||||
@@ -242,9 +393,26 @@ namespace JGL {
|
||||
|
||||
if (!wasVertexArraysEnabled)
|
||||
glDisable(GL_VERTEX_ARRAY);
|
||||
|
||||
if (wasTexture2DEnabled)
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
if (wasBlendEnabled)
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
if (wasCullFaceEnabled)
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
||||
|
||||
inJ3D = false;
|
||||
}
|
||||
|
||||
void J3D::DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness) {
|
||||
if (!inJ3D)
|
||||
ERROR("Attempt to Render J3D element before J3D begin.")
|
||||
|
||||
Vector3 vertices[] = {A, B};
|
||||
|
||||
glLineWidth(thickness);
|
||||
|
@@ -48,10 +48,10 @@ namespace JGL {
|
||||
for (int i = 0; i < faces.size(); i++)
|
||||
if (faces[i].index == font_index)
|
||||
FT_Done_Face(faces[i].face),
|
||||
faces.erase(faces.begin() + i);
|
||||
faces.erase(faces.begin() + i);
|
||||
}
|
||||
|
||||
FontCache fontCache;
|
||||
|
||||
void J2D::DrawString(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index) {
|
||||
glUseProgram(0); // Fixed-function pipeline.
|
||||
|
||||
@@ -71,19 +71,12 @@ namespace JGL {
|
||||
if (font.face == nullptr)
|
||||
return;
|
||||
|
||||
GLfloat currentColor[4];
|
||||
glGetFloatv(GL_CURRENT_COLOR, currentColor);
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, 1.0f);
|
||||
|
||||
FT_Set_Pixel_Sizes(font.face, 0, size);
|
||||
|
||||
std::vector<GLuint> textures(text.length());
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
//For each character
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
float x2, y2, w, h;
|
||||
@@ -100,7 +93,7 @@ namespace JGL {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
||||
|
||||
x2 = x + glyph->x2offset * scale;
|
||||
y2 = -y - glyph->y2offset * scale; // Adjust y-coordinate
|
||||
y2 = y - glyph->y2offset * scale; // Adjust y-coordinate
|
||||
w = glyph->w * scale;
|
||||
h = glyph->h * scale;
|
||||
x += glyph->advanceX * scale;
|
||||
@@ -130,49 +123,25 @@ namespace JGL {
|
||||
cachedFont->appendGlyph(new CachedGlyph(textures.at(i), text.c_str()[i], g->bitmap_left, g->bitmap_top, g->bitmap.width, g->bitmap.rows, (g->advance.x >> 6), (g->advance.y >> 6)));
|
||||
}
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
GLfloat vertices[12] = {x2, y2, x2, y2 + h, x2 + w, y2 + h,x2, y2, x2 + w, y2 + h, x2 + w, y2};
|
||||
|
||||
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();
|
||||
GLfloat textureCoordinates[12] = {0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &textureCoordinates);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &vertices);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
}
|
||||
|
||||
//for (unsigned int& texture : textures)
|
||||
//glDeleteTextures(1, &texture);
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, unsigned int font_index)
|
||||
{
|
||||
void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, unsigned int font_index) {
|
||||
//TODO figure out what the scale should actually be mathematically.
|
||||
scale = scale * 0.002f;
|
||||
scale = -scale;
|
||||
|
||||
float x = pos.x;
|
||||
float y = pos.y;
|
||||
float z = pos.z;
|
||||
GLfloat currentColor[4];
|
||||
std::vector<GLuint> textures(text.length());;
|
||||
glGetFloatv(GL_CURRENT_COLOR, currentColor);
|
||||
glUseProgram(0); // Fixed-function pipeline.
|
||||
glColor4f(color.r, color.g, color.b, 1.0f);
|
||||
|
||||
@@ -202,10 +171,6 @@ namespace JGL {
|
||||
continue;
|
||||
|
||||
FT_GlyphSlot g = font.face->glyph;
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glGenTextures(1, &textures.at(i));
|
||||
glBindTexture(GL_TEXTURE_2D, textures[i]);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
@@ -251,10 +216,7 @@ namespace JGL {
|
||||
for (unsigned int& texture : textures)
|
||||
glDeleteTextures(1, &texture);
|
||||
|
||||
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.
|
||||
|
||||
glPopMatrix();
|
||||
}
|
||||
|
Reference in New Issue
Block a user