1
0
forked from josh/JGL

Merge pull request 'dawsh' (#12) from dawsh into master

Reviewed-on: josh/JGL#12
This commit is contained in:
2024-02-20 17:23:59 -05:00
3 changed files with 101 additions and 77 deletions

View File

@@ -47,23 +47,36 @@ namespace JGL {
// TODO: implement correct coloring
using J3ML::u32;
void RenderText(std::string text, float x, float y, float scale, u32 size = 16);
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(const Color3& color, const Triangle2D& tri);
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);
@@ -75,10 +88,7 @@ namespace JGL {
namespace J3D {
void DrawLine3D(const Color3& color, const Vector3 &A, const Vector3 &B, float thickness = 1);
void FillSphere3D();
void WireframeSphere3D(const Sphere& sphere)
{
}
void WireframeSphere3D(const Sphere& sphere);
void DrawString3D(const Color3& color, const std::string& text, const Vector3& pos, float scale, u32 size = 12);
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);

View File

@@ -64,7 +64,7 @@ public:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1024, 768, 0, -1, 1);
glOrtho(0, getSize().x, getSize().y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -82,6 +82,11 @@ public:
{
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});
@@ -95,7 +100,7 @@ public:
JGL::J3D::DrawString3D(JGL::Colors::White, "JGL Sample Text", {1, -120, 0.5f}, 2.f);
JGL::RenderText("William J. Tomasine II ", 0.f, -120.f, 1.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});

View File

@@ -9,7 +9,7 @@
#include <rewindow/types/window.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include "JGL/Color3.h"
#include <JGL/Color3.h>
#include <iostream>
@@ -18,7 +18,6 @@ GLuint texture;
namespace JGL
{
FT_Face face;
FT_Library ft;
@@ -41,65 +40,6 @@ namespace JGL
return true;
}
void RenderText(std::string text, float x, float y, float scale, u32 text_size) {
glUseProgram(0); // Fixed-function pipeline.
glColor3f(1.0f, 1.0f, 1.0f);
FT_Set_Pixel_Sizes(face, 0, text_size);
const char* c;
for (c = text.c_str(); *c; c++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
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;
}
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
}
namespace J2D
{
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
@@ -189,6 +129,16 @@ 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);
@@ -198,6 +148,65 @@ namespace JGL
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.
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
FT_Set_Pixel_Sizes(face, 0, size);
const char* c;
for (c = text.c_str(); *c; c++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
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;
}
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
}
}