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

Reviewed-on: #3
This commit is contained in:
2024-02-16 13:29:17 -05:00
7 changed files with 166 additions and 221 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(
@@ -58,6 +58,6 @@ include_directories(${J3ML_SOURCE_DIR}/include)
include_directories(${ReWindow_SOURCE_DIR}/include)
include_directories(${glad_SOURCE_DIR}/include)
target_link_libraries(JGL_Demo PRIVATE ${FREETYPE_LIBRARIES})
target_include_directories(JGL_Demo PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL PRIVATE ${FREETYPE_LIBRARIES})
target_include_directories(JGL PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(JGL_Demo PUBLIC JGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} J3ML ReWindowLibrary GLEW glad)

View File

@@ -0,0 +1,10 @@
# Josh Graphics Library
# Goals
* Provide single-function-calls to render various graphics primitives in 2D and 3D.
* Integrated directly with our other toolkits (ReWindow, J3ML)
# Non-Goals
* Full Rendering Engine
* OpenGL/Vulkan Wrapper
* Asset Loading & Management

View File

@@ -6,6 +6,8 @@
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include <string>
#include <iostream>
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
namespace JGL {
@@ -243,6 +245,13 @@ namespace JGL {
// TODO: Implement (CORRECT!!!) matrix transformation
}
bool InitTextEngine();
// TODO: implement correct coloring
void RenderText(std::string text, float x, float y, float scale);
namespace J2D {
void DrawPixel2D(const Color3& color, const Vector2 &coordinates);
void DrawPixel2D(const Color3& color, float x, float y);

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);

252
main.cpp
View File

@@ -2,8 +2,6 @@
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <rewindow/types/window.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
#include <LearnOpenGL/Shader.h>
@@ -21,39 +19,39 @@ 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"
GLuint VAO, VBO;
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;
struct point {
GLfloat x;
GLfloat y;
GLfloat s;
GLfloat t;
};
class JGLDemoWindow : public ReWindow::RWindow
{
public:
LearnOpenGL::Shader shader;
JGLDemoWindow() : ReWindow::RWindow()
{
@@ -62,201 +60,28 @@ public:
void initGL()
{
gladLoadGL();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1024, 768, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
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))
{
std::cout << "Error::FREETYPE: " << std::endl;
return;
}
FT_Face face;
if (FT_New_Face(ft, "content/FreeSans.ttf", 0, &face))
{
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
}
FT_Set_Pixel_Sizes(face, 0, 48);
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER))
{
std::cout << "Error::FREETYPE: Failed to load Glyph" << std::endl;
}
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);
JGL::InitTextEngine();
}
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);
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);
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();
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);
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);
glFlush(); // Render now
}
void RenderText(std::string text, float x, float y, float scale, JGL::Color3 color)
void display()
{
// 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);
// iterate through all characters
std::string::const_iterator 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;
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 },
{ xpos, ypos + h, 0.0f, 0.0f },
{ xpos + w, ypos, 1.0f, 1.0f },
{ xpos + w, ypos + h, 1.0f, 0.0f }
};
// render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// update contents of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // be sure to use glBufferSubData and not glBufferData
glBindBuffer(GL_ARRAY_BUFFER, 0);
// render quad
glDrawArrays(GL_TRIANGLES, 0, 6);
// now advance cursors for next glyph (note that advance is number of 1/64 pixels)
x += (ch.Advance >> 6) * scale;
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glClear(GL_COLOR_BUFFER_BIT);
JGL::RenderText("WHATS BOPPIN muth ~~~ niger ~~~ aphuqqa____?", 0.f, -100.f, 3.f);
JGL::RenderText("CHINESE CHARCTERS DONT WORK", 0.f, -100.f, 1.f);
}
void OnRefresh(float elapsed) override
@@ -264,9 +89,10 @@ public:
display();
this->glSwapBuffers();
}
void OnResize()
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override
{
//reshape();
return true;
}
};
@@ -274,7 +100,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

@@ -1,15 +1,27 @@
//
// Created by dawsh on 1/17/24.
//
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <GL/glut.h>
#include "J3ML/LinearAlgebra/Transform2D.h"
#include <rewindow/types/window.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
GLuint program;
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
@@ -18,21 +30,98 @@ 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
};
}
FT_Face face;
FT_Library ft;
using namespace J3ML;
bool InitTextEngine() {
constexpr u32 default_font_size = 16;
if (FT_Init_FreeType(&ft))
{
std::cout << "Error::FREETYPE: " << std::endl;
return false;
}
if (FT_New_Face(ft, "content/FreeSans.ttf", 0, &face))
{
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) {
glUseProgram(0); // Fixed-function pipeline.
glColor3f(1.0f, 1.0f, 1.0f);
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);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
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);
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) {
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);
}