Implement J3D::DrawString3D

This commit is contained in:
2024-02-20 04:01:29 -05:00
parent 2a98857bab
commit 817b0166c0
7 changed files with 315 additions and 243 deletions

View File

@@ -5,10 +5,11 @@
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <GL/glut.h>
#include "J3ML/LinearAlgebra/Transform2D.h"
#include <J3ML/LinearAlgebra/Transform2D.h>
#include <rewindow/types/window.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include "JGL/Color3.h"
#include <iostream>
@@ -17,26 +18,6 @@ GLuint texture;
namespace JGL
{
Vector2 ScreenToViewport(const Vector2 &v) {
// TODO: Implement (CORRECT!!!) matrix transformation
Vector2 windowSize = ReWindow::RWindow::CurrentWindow()->getSize();
//Transform2D transform;
//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
//return transform.Transform(v);
float x = (v.x) / windowSize.x;
float y = (v.y) / windowSize.y;
return {
x, y
};
}
FT_Face face;
FT_Library ft;
@@ -122,8 +103,8 @@ namespace JGL
namespace J2D
{
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = pos;//ScreenToViewport(pos);
auto vp_size = size;//ScreenToViewport(size);
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_QUADS);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
glVertex2f(vp_pos.x, vp_pos.y);
@@ -134,8 +115,8 @@ namespace JGL
}
void OutlineRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size, float thickness) {
auto vp_pos = pos;//ScreenToViewport(pos);
auto vp_size = size;//ScreenToViewport(size);
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_LINE_LOOP);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
@@ -147,8 +128,8 @@ namespace JGL
}
void DrawLine2D(const Color3 &color, const Vector2 &A, const Vector2 &B, float thickness) {
auto vp_a = A;//ScreenToViewport(A);
auto vp_b = B;//ScreenToViewport(B);
auto vp_a = A;
auto vp_b = B;
glBegin(GL_LINES);
glLineWidth(thickness);
@@ -168,7 +149,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);
@@ -233,5 +214,69 @@ namespace JGL
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;
glUseProgram(0); // Fixed-function pipeline.
glColor3f(1.0f, 1.0f, 1.0f);
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 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;
}
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
}
}
}