SemiBrokend

This commit is contained in:
2024-02-15 18:51:25 -05:00
parent 406abbb5bd
commit d0eb1f34ef
5 changed files with 182 additions and 161 deletions

View File

@@ -21,12 +21,12 @@ include(cmake/CPM.cmake)
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-17.zip
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-18.zip
)
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.12.zip
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.13.zip
)
CPMAddPackage(

View File

@@ -49,6 +49,8 @@ namespace LearnOpenGL {
void setMat3(const std::string& name, const Matrix3x3 &mat) const;
void setMat4(const std::string& name, const Matrix4x4 &mat) const;
GLint getAttribute(const std::string& name) const;
GLint getUniform(const std::string& name) const;
private:
void checkCompileErrors(GLuint shader, std::string type);

316
main.cpp
View File

@@ -22,38 +22,43 @@ struct Character
std::map<char, Character> Characters;
unsigned int VAO, VBO;
const std::string vertexShader = "#version 330 core\n"
"layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>\n"
"out vec2 TexCoords;\n"
const std::string vertexShader = "attribute vec4 coord;\n"
"varying vec2 texpos;\n"
"\n"
"uniform mat4 projection;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
" TexCoords = vertex.zw;\n"
"} ";
const std::string fragmentShader = "#version 330 core\n"
"layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>\n"
"out vec2 TexCoords;\n"
"void main(void) {\n"
" gl_Position = vec4(coord.xy, 0, 1);\n"
" texpos = coord.zw;\n"
"}\n"
"";
const std::string fragmentShader = "varying vec2 texpos;\n"
"uniform sampler2D tex;\n"
"uniform vec4 color;\n"
"\n"
"uniform mat4 projection;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
" TexCoords = vertex.zw;\n"
"} ";
"void main(void) {\n"
" gl_FragColor = vec4(1, 1, 1, texture2D(tex, texpos).a) * color;\n"
"}";
using J3ML::LinearAlgebra::Matrix4x4;
GLuint program;
GLint attribute_coord;
GLint uniform_tex;
GLint uniform_color;
struct point {
GLfloat x;
GLfloat y;
GLfloat s;
GLfloat t;
};
class JGLDemoWindow : public ReWindow::RWindow
{
public:
LearnOpenGL::Shader shader;
FT_Face face;
FT_Library ft;
JGLDemoWindow() : ReWindow::RWindow()
{
@@ -66,13 +71,7 @@ public:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shader = LearnOpenGL::Shader(vertexShader, fragmentShader);
Matrix4x4 projection = Matrix4x4::D3DOrthoProjLH(0.0f, 1.0f, 1280, 720);
shader.use();
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, projection.ptr());
//initGL(); // Our own OpenGL initialization
FT_Library ft;
if (FT_Init_FreeType(&ft))
{
@@ -80,154 +79,146 @@ public:
return;
}
FT_Face face;
if (FT_New_Face(ft, "content/FreeSans.ttf", 0, &face))
{
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return;
}
FT_Set_Pixel_Sizes(face, 0, 48);
shader = LearnOpenGL::Shader(vertexShader, fragmentShader);
Matrix4x4 projection = Matrix4x4::OpenGLOrthoProjRH(0.0f, 0.0f, getSize().x, getSize().y);
//shader.use();
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER))
{
std::cout << "Error::FREETYPE: Failed to load Glyph" << std::endl;
}
attribute_coord = shader.getAttribute("coord");
uniform_tex = shader.getUniform("tex");
uniform_color = shader.getUniform("color");
//glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, projection.ptr());
if(attribute_coord == -1 || uniform_tex == -1 || uniform_color == -1)
return;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction
for (unsigned char c = 0; c < 128; c++)
{
// load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYPE: Failed to load Glyph!" << std::endl;
continue;
}
// Generate Texture
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// set textured options
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);
// now store character for later use
Character character = {
texture,
{(float)face->glyph->bitmap.width, (float)face->glyph->bitmap.rows},
{(float)face->glyph->bitmap_left, (float)face->glyph->bitmap_top},
(unsigned int)face->glyph->advance.x
};
Characters.insert(std::pair<char, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
FT_Done_Face(face);
FT_Done_FreeType(ft);
// configure VAO/VBO for texture quads
glGenBuffers(6*4, &VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)* 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glGenBuffers(1, &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.
// Define shapes enclosed within a pair of glBegin and glEnd
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);
shader.use();
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);
float scaleX = 2.f / getSize().x;
float scaleY = 2.f / getSize().y;
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();
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
/* Enable blending, necessary for our alpha texture */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
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);
FT_Set_Pixel_Sizes(face, 0, 48);
GLfloat black[4] = { 0, 0, 0, 1 };
glUniform4fv(uniform_color, 1, black);
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);
RenderText("WHATS BOPPIN", 25.f, 25.f, 1.f, JGL::Colors::White);
//JGL::J2D::DrawPixel2D(JGL::Colors::Green, {0, 0});
//JGL::J2D::FillCircle2D(JGL::Colors::Purples::DarkViolet, {0, 0}, 0.75f, 64);
glFlush(); // Render now
RenderText("WHATS BOPPIN muth ~~~ niger ~~~ aphuqqa____?", 0.f, 0.f, scaleX, JGL::Colors::Black);
JGL::J2D::FillRect2D(JGL::Colors::Greens::DarkOliveGreen, {0, 0}, {128, 128});
JGL::J2D::FillRect2D(JGL::Colors::Greens::DarkSeaGreen, {0, 0}, {64, 64});
JGL::J2D::FillRect2D(JGL::Colors::Greens::DarkGreen, {0, 0}, {32, 32});
JGL::J2D::FillRect2D(JGL::Colors::Greens::Chartreuse, {0, 0}, {1, 1});
JGL::J2D::FillRect2D(JGL::Colors::Yellows::Moccasin, {-128, -128}, {128, 128});
JGL::J2D::FillRect2D(JGL::Colors::Yellows::PeachPuff, {-64, -64}, {64, 64});
JGL::J2D::FillRect2D(JGL::Colors::Yellows::LemonChiffon, {-32, -32}, {32, 32});
JGL::J2D::FillRect2D(JGL::Colors::Yellows::Khaki, {-1, -1}, {1, 1});
//glFlush(); // Render now
}
void RenderText(std::string text, float x, float y, float scale, JGL::Color3 color)
{
const char* p;
// TODO: implement shader.pushColor();
shader.use();
glUniform3f(glGetUniformLocation(shader.ID, "textColor"), color.r, color.g, color.b);
glActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_VERTEX_ARRAY, VAO);
GLuint tex;
FT_GlyphSlot g = face->glyph;
//glUniform3f(glGetUniformLocation(shader.ID, "textColor"), color.r/255.f, color.g/255.f, color.b/255.f);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(uniform_tex, 0);
// We require 1-byte alignment when uploading texture data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Clamping to edges is important to prevent artifacts when scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Linear filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnableVertexAttribArray(attribute_coord);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0);
// Loop through all characters
for (p = text.c_str(); *p; p++)
{
if (FT_Load_Char(face, *p, FT_LOAD_RENDER))
continue;
// Upload the "bitmap" which contains an 8-bit grayscale image, as an alpha texture
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_ALPHA,
g->bitmap.width,
g->bitmap.rows,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
g->bitmap.buffer
);
// Calculate the vertex and texture coordinates
float x2 = x + g->bitmap_left * scale;
float y2 = -y - g->bitmap_top * scale;
float w = g->bitmap.width * scale;
float h = g->bitmap.rows * scale;
point box[4] = {
{x2, -y2, 0, 0},
{x2 + w, -y2, 1, 0},
{x2, -y2 - h, 0, 1},
{x2 + w, -y2 - h, 1, 1},
};
// Draw the character on the screen
glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Advance the cursor to the start of the next character */
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
glDisableVertexAttribArray(attribute_coord);
glDeleteTextures(1, &tex);
/*
// iterate through all characters
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
for (c = text.begin(); c != text.end(); c++) {
Character ch = Characters[*c];
float xpos = x + ch.Bearing.x * scale;
float ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
@@ -235,15 +226,30 @@ public:
float w = ch.Size.x * scale;
float h = ch.Size.y * scale;
float vertices[6][4] = {
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos, ypos, 0.0f, 1.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
char valu = *c;
if (valu != ' ')
{
std::cout << valu << ", " << xpos << ", " << ypos << ", " << w << "," << h << std::endl;
JGL::J2D::FillRect2D({valu, 0, valu}, {xpos, ypos}, {w, h});
}
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
float fxpos = xpos / getSize().x;
float fypos = ypos / getSize().y;
float fw = w / getSize().x;
float fh = h / getSize().y;
float vertices[6][4] = {
{fxpos, fypos + fh, 0.0f, 0.0f},
{fxpos, fypos, 0.0f, 1.0f},
{fxpos + fw, fypos, 1.0f, 1.0f},
{fxpos, fypos + fh, 0.0f, 0.0f},
{fxpos + fw, fypos, 1.0f, 1.0f},
{fxpos + fw, fypos + fh, 1.0f, 0.0f}
};
// render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// update contents of VBO memory
@@ -257,6 +263,7 @@ public:
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
*/
}
void OnRefresh(float elapsed) override
@@ -264,9 +271,10 @@ public:
display();
this->glSwapBuffers();
}
void OnResize()
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override
{
//reshape();
return true;
}
};
@@ -274,7 +282,7 @@ int main(int argc, char** argv)
{
JGLDemoWindow* window = new JGLDemoWindow();
window->init(RenderingAPI::OPENGL, "Window", 800, 600, false);
window->init(RenderingAPI::OPENGL, "Window", 1280, 720, false);
window->initGL();
window->setResizable(true);
while (window->isAlive())

View File

@@ -4,12 +4,14 @@
#include <JGL/JGL.h>
#include <GL/glut.h>
#include "J3ML/LinearAlgebra/Transform2D.h"
#include <rewindow/types/window.h>
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
@@ -18,11 +20,11 @@ namespace JGL
//return transform.Transform(v);
float x = v.x / 600.f;
float y = v.y / 600.f;
float x = (v.x) / windowSize.x;
float y = (v.y) / windowSize.y;
return {
x - .5f, y - .5f
x, y
};
}
@@ -32,7 +34,7 @@ namespace JGL
auto vp_pos = ScreenToViewport(pos);
auto vp_size = ScreenToViewport(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);

View File

@@ -129,6 +129,15 @@ namespace LearnOpenGL
glUseProgram(ID);
}
GLint Shader::getAttribute(const std::string& name) const
{
return glGetAttribLocation(ID, name.c_str());
}
GLint Shader::getUniform(const std::string &name) const {
return glGetUniformLocation(ID, name.c_str());
}
void Shader::setBool(const std::string &name, bool value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
}