Fix Implementation of J2D::DrawLine2D & J3D::DrawLine3D

This commit is contained in:
2024-02-20 00:30:38 -05:00
parent 29a64160e9
commit 2a98857bab
6 changed files with 82 additions and 23 deletions

View File

@@ -42,7 +42,9 @@ add_library(JGL SHARED ${SOURCES}
include/JGL/JGL.h
src/JGL/JGL.cpp
include/LearnOpenGL/Shader.h
src/LearnOpenGL/Shader.cpp)
src/LearnOpenGL/Shader.cpp
include/LearnOpenGL/Texture2D.h
src/LearnOpenGL/Texture2D.cpp)
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
find_package(OpenGL REQUIRED)

View File

@@ -12,8 +12,7 @@
// 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]
struct RGBTuple {
int r; int g; int b;
};
@@ -250,7 +249,8 @@ namespace JGL {
bool InitTextEngine();
// TODO: implement correct coloring
void RenderText(std::string text, float x, float y, float scale);
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);

View File

@@ -0,0 +1,23 @@
#pragma once
namespace {
// Texture2D is able to store and configure a texture in OpenGL
// It also hosts utility functions for easy management.
class Texture2D {
public:
unsigned int ID; // ID of the texture object
unsigned int Width, Height;
unsigned int InternalFormat;
unsigned int ImageFormat;
unsigned int WrapS;
unsigned int WrapT;
unsigned int Filter_Min;
unsigned int Filter_Max;
Texture2D();
void Generate(unsigned int width, unsigned int height, unsigned char *data);
void Bind() const;
};
}

View File

@@ -38,8 +38,6 @@ const std::string fragmentShader = "varying vec2 texpos;\n"
using J3ML::LinearAlgebra::Matrix4x4;
struct point {
GLfloat x;
GLfloat y;
@@ -81,20 +79,24 @@ public:
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
JGL::J2D::FillRect2D(JGL::Colors::Yellow, {32, 32}, {100.5, 100.5});
JGL::RenderText("WHATS BOPPIN muth ~~~ niger ~~~ aphuqqa____?", 0.f, -100.f, 2.f);
JGL::RenderText("Chinese characters don't work", 0.f, -120.f, 1.f);
JGL::J2D::FillRect2D(JGL::Colors::Yellow, {32, 32}, {100.5, 100.5});
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
{
{10, -200},
{105, 10},
{-105, 10}
{140, 200},
{135, 100},
{105, 100}
};
JGL::J2D::FillTriangle2D(JGL::Colors::Yellow, tri);
JGL::RenderText("JGL Sample Text", 0.f, -100.f, 1.f, 32);
JGL::RenderText("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::Reds::Firebrick, {10, 10, -1.f}, {200, 200, 10.f});
//glFlush();
}
@@ -105,7 +107,6 @@ public:
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override
{
return true;
}
};

View File

@@ -56,14 +56,16 @@ namespace JGL
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return false;
}
FT_Set_Pixel_Sizes(face, 0, default_font_size);
return true;
}
void RenderText(std::string text, float x, float y, float scale) {
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++)
{
@@ -145,10 +147,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;//ScreenToViewport(A);
auto vp_b = B;//ScreenToViewport(B);
glBegin(GL_LINE);
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_a.x, vp_a.y);
@@ -222,12 +224,14 @@ 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();
}
}
}

View File

@@ -0,0 +1,29 @@
#include <LearnOpenGL/Texture2D.h>
#include <glad/glad.h>
#include <GL/glut.h>
Texture2D::Texture2D()
: Width(0), Height(0), InternalFormat(GL_RGB), ImageFormat(GL_RGB), WrapS(GL_REPEAT), WrapT(GL_REPEAT),
Filter_Min(GL_LINEAR), Filter_Max(GL_LINEAR) {
glGenTextures(1, &this->ID);
}
void Texture2D::Generate(unsigned int width, unsigned int height, unsigned char *data) {
this->Width = width;
this->Height = height;
glBindTexture(GL_TEXTURE_2D, this->ID);
glTexImage2D(GL_TEXTURE_2D, 0, this->InternalFormat, width, height, 0, this->ImageFormat, GL_UNSIGNED_BYTE,
data);
// set Texture wrap and filter modes
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->WrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->WrapT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, this->Filter_Min);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, this->Filter_Max);
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture2D::Bind() const
{
glBindTexture(GL_TEXTURE_2D, this->ID);
}