Refactor & undo shader commits AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m18s
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m18s
This commit is contained in:
@@ -21,7 +21,6 @@
|
||||
#include <JGL/types/enums.h>
|
||||
#include <JGL/types/FontCache.h>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/types/Shader.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <J3ML/LinearAlgebra/Vector3.hpp>
|
||||
@@ -72,21 +71,9 @@ namespace JGL {
|
||||
/// @see Begin().
|
||||
void End();
|
||||
|
||||
/// Sets the active shader program to be used for rendering.
|
||||
void UseShaderProgram(Shader& shader);
|
||||
void UseShaderProgram(Shader* shader);
|
||||
|
||||
/// Changes back to fixed-function rendering.
|
||||
void UseFixedFunction();
|
||||
|
||||
/// Returns true if we are not using fixed-function rendering.
|
||||
bool IsUsingShaderProgram();
|
||||
|
||||
/// Plots a single pixel on the screen.
|
||||
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
|
||||
/// @param coordinates The pixel-point on-screen at which to plot the pixel.
|
||||
void DrawPoint(const Color3& color, const Vector2& coordinates, float radius = 1.f);
|
||||
void DrawPoint(const Color3& color, float x, float y, float radius = 1.f);
|
||||
void DrawPoint(const Color4& color, const Vector2& coordinates, float radius = 1.f);
|
||||
void DrawPoint(const Color4& color, float x, float y, float radius = 1.f);
|
||||
|
||||
@@ -95,20 +82,15 @@ namespace JGL {
|
||||
/// @param A The starting point of the line segment.
|
||||
/// @param B The end point of the line segment.
|
||||
/// @param thickness The width at which to render the line.
|
||||
void DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawLine(const Color3& color, float x, float y, float w, float h, float thickness = 1);
|
||||
void DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawLine(const Color4& color, float x1, float y1, float x2, float y2, float thickness = 1);
|
||||
|
||||
///Draws a line with a gradient that transitions across it.
|
||||
void DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness = 1);
|
||||
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness = 1);
|
||||
void DrawGradientLine(const Color3& color1, const Color3& color2, float x, float y, float w, float h, float thickness = 1);
|
||||
|
||||
/// Draws an outline of a rectangle on the screen.
|
||||
void OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness = 1);
|
||||
void OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
|
||||
|
||||
/// Draws a sprite to the screen by passing a GLuint that represents a handle to a loaded texture.
|
||||
/// @param texture
|
||||
@@ -153,7 +135,6 @@ namespace JGL {
|
||||
/// @param v3 bottom-right vertex.
|
||||
/// @param v4 top-right vertex.
|
||||
void FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
|
||||
void FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
|
||||
|
||||
/// Draws a non axis-aligned outline rect to the screen.
|
||||
/// @param color
|
||||
@@ -163,76 +144,51 @@ namespace JGL {
|
||||
/// @param v4 top-right vertex.
|
||||
/// @param thickness the thickness of the GL_LINES to be connected together.
|
||||
void OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
|
||||
void OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
|
||||
|
||||
/// Draws a filled rectangle on the screen.
|
||||
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
|
||||
void FillRect(const Color3& color, const Vector2& pos, const Vector2& size);
|
||||
|
||||
/// Draws a filled rectangle where the color transitions across it.
|
||||
void FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
|
||||
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
|
||||
|
||||
/// Draws a filled rectangle with rounded corners on the screen.
|
||||
void FillRoundedRect(const Color4& color, const Vector2 &pos, const Vector2 &size, float radius = 5, unsigned int subdivisions = 8);
|
||||
void FillRoundedRect(const Color3& color, const Vector2& pos, const Vector2& size, float radius = 5, unsigned int subdivisions = 8);
|
||||
|
||||
/// Draws an outline of a circle on the screen.
|
||||
void OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 16, float thickness = 1);
|
||||
void OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 16, float thickness = 1);
|
||||
|
||||
/// Draws a filled circle on the screen.
|
||||
void FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
|
||||
void FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions = 8);
|
||||
|
||||
/// Draws an outline of a triangle on the screen.
|
||||
/// @param color
|
||||
/// @param tri
|
||||
/// @param thickness
|
||||
void OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness = 1);
|
||||
void OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness = 1);
|
||||
void OutlineTriangle(const Color4& color,
|
||||
const Vector2& triA, const Vector2& triB, const Vector2& triC,
|
||||
float thickness = 1);
|
||||
void OutlineTriangle(const Color3& color,
|
||||
const Vector2& triA, const Vector2& triB, const Vector2& triC,
|
||||
float thickness = 1);
|
||||
// TODO: Take more Focalin
|
||||
void OutlineTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC, float thickness = 1);
|
||||
|
||||
/// Draws a filled triangle on the screen.
|
||||
void FillTriangle(const Color4& color, const Triangle2D& tri);
|
||||
void FIllTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC);
|
||||
void FillTriangle(const Color3& color, const Triangle2D& tri);
|
||||
|
||||
/// Draws a triangle where each corner is defined by a given color, Smoothly transitioning between them.
|
||||
void FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri);
|
||||
void FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri);
|
||||
// TODO: Implement an overload that simply takes 3 Vector3's
|
||||
|
||||
|
||||
/// Draws a text string on the screen with a given point-size and font.
|
||||
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
|
||||
void DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
|
||||
|
||||
// TODO: Implement the following:
|
||||
void FillTexturedTriangle();
|
||||
|
||||
|
||||
void DrawCubicBezierCurve(const Color4& color,
|
||||
const Vector2& controlA, const Vector2& pointA,
|
||||
const Vector2& pointB, const Vector2& controlB,
|
||||
int subdivisions = 10,
|
||||
float thickness = 1);
|
||||
void DrawCubicBezierCurve(const Color4& color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
|
||||
int subdivisions = 10, float thickness = 1);
|
||||
|
||||
/// Draws a series of points where the last point always connects to the first point.
|
||||
void OutlinePolygon (const Color4& color, const std::vector<Vector2>& points, float thickness = 1);
|
||||
void OutlinePolygon(const Color4& color, const std::vector<Vector2>& points, float thickness = 1);
|
||||
|
||||
/// Draws a series of points where the last point always connects to the first point and fills it in.
|
||||
///TODO these ones are going to be extremely annoying.
|
||||
void FillPolygon (const Color4& color, const std::vector<Vector2>& points);
|
||||
/// TODO Implement the following. These ones are going to be extremely annoying.
|
||||
void FillPolygon(const Color4& color, const std::vector<Vector2>& points);
|
||||
void FillTexturedPolygon();
|
||||
|
||||
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1);
|
||||
void FillTexturedTriangle();
|
||||
}
|
||||
|
||||
/// Drawing functions for primitive 3D Shapes.
|
||||
|
@@ -45,14 +45,14 @@ public:
|
||||
CachedGlyph* getGlyph(char c);
|
||||
std::map<char, CachedGlyph*> getGlyphs();
|
||||
const GLuint* getTexture();
|
||||
GLsizei getTextureWidth() const;
|
||||
GLsizei getTextureHeight() const;
|
||||
[[nodiscard]] GLsizei getTextureWidth() const;
|
||||
[[nodiscard]] GLsizei getTextureHeight() const;
|
||||
CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index);
|
||||
};
|
||||
|
||||
class JGL::FontCache {
|
||||
private:
|
||||
std::vector<CachedFont*> cachedFonts = {};
|
||||
std::vector<CachedFont*> cachedFonts{};
|
||||
public:
|
||||
std::vector<CachedFont*> getFonts();
|
||||
CachedFont* getFont(unsigned int font_size, unsigned int font_index);
|
||||
|
@@ -1,191 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <glad/glad.h>
|
||||
#include <Color4.hpp>
|
||||
#include <Color3.hpp>
|
||||
|
||||
namespace JGL {
|
||||
class Shader;
|
||||
|
||||
enum class ShaderType : u8 {
|
||||
VERTEX = 0,
|
||||
FRAGMENT = 1,
|
||||
COMBINED = 2,
|
||||
};
|
||||
|
||||
// You could definitely do this with Typename T.
|
||||
// Idk it increases the likelihood for mistakes.
|
||||
class ShaderValue;
|
||||
|
||||
class Uniform_Float;
|
||||
class Uniform_Int;
|
||||
class Uniform_Bool;
|
||||
|
||||
class Uniform_Vector2;
|
||||
class Uniform_Vector3;
|
||||
class Uniform_Vector4;
|
||||
|
||||
class Uniform_Matrix2x2;
|
||||
class Uniform_Matrix3x3;
|
||||
class Uniform_Matrix4x4;
|
||||
|
||||
|
||||
class Attribute_Vector2;
|
||||
class Attribute_Vector3;
|
||||
class Attribute_Vector4;
|
||||
}
|
||||
|
||||
class JGL::ShaderValue {
|
||||
public:
|
||||
std::string name;
|
||||
GLint location = -1;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Float : public ShaderValue {
|
||||
public:
|
||||
float value = 0;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Int : public ShaderValue {
|
||||
public:
|
||||
int value = 0;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Bool : public ShaderValue {
|
||||
public:
|
||||
bool value = false;
|
||||
};
|
||||
|
||||
|
||||
class JGL::Uniform_Vector2 : public ShaderValue {
|
||||
public:
|
||||
Vector2 value;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Vector3 : public ShaderValue {
|
||||
public:
|
||||
Vector3 value;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Vector4 : public ShaderValue {
|
||||
public:
|
||||
Vector4 value;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Matrix2x2 : public ShaderValue {
|
||||
public:
|
||||
Matrix2x2 value;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Matrix3x3 : public ShaderValue {
|
||||
public:
|
||||
Matrix3x3 value;
|
||||
};
|
||||
|
||||
class JGL::Uniform_Matrix4x4 : public ShaderValue {
|
||||
public:
|
||||
Matrix4x4 value;
|
||||
};
|
||||
|
||||
|
||||
class JGL::Attribute_Vector2 : public ShaderValue {
|
||||
public:
|
||||
std::vector<Vector2> value;
|
||||
};
|
||||
|
||||
class JGL::Attribute_Vector3 : public ShaderValue {
|
||||
public:
|
||||
std::vector<Vector3> value;
|
||||
};
|
||||
|
||||
class JGL::Attribute_Vector4 : public ShaderValue {
|
||||
public:
|
||||
std::vector<Vector4> value;
|
||||
};
|
||||
|
||||
class JGL::Shader {
|
||||
private:
|
||||
///Shader program.
|
||||
GLuint shader_program_handle = 0;
|
||||
///Shader type.
|
||||
ShaderType shader_type = ShaderType::COMBINED;
|
||||
|
||||
///Individual shaders which are compiled into the shader program.
|
||||
GLuint vertex_shader = 0;
|
||||
GLuint fragment_shader = 0;
|
||||
|
||||
/// Uniforms allow you to DMA values from the CPU to the GPU for this draw call.
|
||||
/// The Uniform will be the same for both the Vertex Shader & the Fragment Shader.
|
||||
std::vector<Uniform_Float> float_uniforms;
|
||||
std::vector<Uniform_Int> int_uniforms;
|
||||
std::vector<Uniform_Bool> bool_uniforms;
|
||||
|
||||
// TODO v2i, v3i, v4i
|
||||
std::vector<Uniform_Vector2> vector2_uniforms;
|
||||
std::vector<Uniform_Vector3> vector3_uniforms;
|
||||
std::vector<Uniform_Vector4> vector4_uniforms;
|
||||
|
||||
std::vector<Uniform_Matrix2x2> matrix2x2_uniforms;
|
||||
std::vector<Uniform_Matrix3x3> matrix3x3_uniforms;
|
||||
std::vector<Uniform_Matrix4x4> matrix4x4_uniforms;
|
||||
|
||||
/// Vertex Attributes are similar to Uniforms, but are per Vertex instead of per draw call.
|
||||
/// Effectively an array list of elements such as position, normal, color, bone weights etc etc.
|
||||
std::vector<Attribute_Vector2> vector2_attributes;
|
||||
std::vector<Attribute_Vector3> vector3_attributes;
|
||||
std::vector<Attribute_Vector4> vector4_attributes;
|
||||
|
||||
/// Varyings allow you to pass values from the Vertex pass to the Fragment pass.
|
||||
/// They are defined in the shader source only.
|
||||
private:
|
||||
void load(const char* file_path, ShaderType type);
|
||||
void load(const std::string& shader_program_text, ShaderType type);
|
||||
void link();
|
||||
public:
|
||||
[[nodiscard]] GLuint GetGLShaderProgramHandle() const;
|
||||
[[nodiscard]] ShaderType GetShaderType() const;
|
||||
void ListUniforms();
|
||||
public:
|
||||
void SetUF(const std::string& uniform_name, float value);
|
||||
void SetUI(const std::string& uniform_name, int value);
|
||||
void SetUB(const std::string& uniform_name, bool value);
|
||||
|
||||
void SetUV2(const std::string& uniform_name, const Vector2& value);
|
||||
void SetUV3(const std::string& uniform_name, const Vector3& value);
|
||||
void SetUV3(const std::string& uniform_name, const Color3& value);
|
||||
void SetUV4(const std::string& uniform_name, const Vector4& value);
|
||||
void SetUV4(const std::string& uniform_name, const Color4& value);
|
||||
|
||||
void SetUMat2(const std::string& uniform_name, const Matrix2x2& value);
|
||||
void SetUMat3(const std::string& uniform_name, const Matrix3x3& value);
|
||||
void SetUMat4(const std::string& uniform_name, const Matrix4x4& value);
|
||||
|
||||
void SetAV2(const std::string& attribute_name, const std::vector<Vector2>& value);
|
||||
void SetAV2(const std::string& attribute_name, const Vector2* value, size_t size);
|
||||
void SetAV3(const std::string& attribute_name, const std::vector<Vector3>& value);
|
||||
void SetAV3(const std::string& attribute_name, const Vector3* value, size_t size);
|
||||
void SetAV4(const std::string& attribute_name, const std::vector<Vector4>& value);
|
||||
void SetAV4(const std::string& attribute_name, const Vector4* value, size_t size);
|
||||
|
||||
/// Zeroes out all uniforms locally.
|
||||
void ResetLocalUniforms();
|
||||
/// Zeroes out all uniforms locally and on the GPU.
|
||||
void ResetUniforms();
|
||||
|
||||
/// DMA the uniforms into the Shader Program.
|
||||
void SubmitUniforms(bool reset_uniforms_in_local_store = true);
|
||||
|
||||
/// Sets the memory location at which the Shader Program will look for the Attribute list.
|
||||
void SubmitAttributes();
|
||||
|
||||
/// Un-sets the memory location in the Shader Program and clears locally.
|
||||
void ResetAttributes();
|
||||
public:
|
||||
Shader(const char* vertex_shader_file, const char* fragment_shader_file);
|
||||
Shader(const std::string& vertex_shader_text, const std::string& fragment_shader_text);
|
||||
void Erase();
|
||||
Shader() = default;
|
||||
~Shader();
|
||||
};
|
4
main.cpp
4
main.cpp
@@ -5,7 +5,6 @@
|
||||
#include <chrono>
|
||||
#include <J3ML/LinearAlgebra/Vector2.hpp>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/types/Shader.h>
|
||||
#include <JGL/Logger.h>
|
||||
#include <ReTexture/Texture.h>
|
||||
|
||||
@@ -14,7 +13,6 @@ using namespace JGL;
|
||||
|
||||
JGL::Font FreeSans;
|
||||
JGL::Font Jupiteroid;
|
||||
JGL::Shader test_shader;
|
||||
|
||||
class Gizmo
|
||||
{
|
||||
@@ -137,9 +135,7 @@ public:
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glDepthMask(GL_TRUE);
|
||||
test_shader = Shader("assets/shader_programs/test_vertex.glsl", "assets/shader_programs/test_fragment.glsl");
|
||||
image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR);
|
||||
test_shader.SetAV2("position", {{2,2}});
|
||||
}
|
||||
|
||||
Vector3 textAngle = {0,0,0};
|
||||
|
274
src/JGL.cpp
274
src/JGL.cpp
@@ -8,7 +8,6 @@
|
||||
#include <jlog/Logger.hpp>
|
||||
#include <J3ML/Algorithm/Bezier.hpp>
|
||||
|
||||
JGL::Shader* j2d_shader_program = nullptr;
|
||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||
GLfloat baseColor[4] = {1, 1, 1, 1};
|
||||
bool inJ2D = false;
|
||||
@@ -28,32 +27,14 @@ namespace JGL {
|
||||
|
||||
void Update(const Vector2& window_size) {
|
||||
wS = window_size;
|
||||
glViewport(0, 0, wS.x, wS.y);
|
||||
}
|
||||
|
||||
void J2D::UseShaderProgram(Shader& shader) {
|
||||
j2d_shader_program = &shader;
|
||||
glUseProgram(shader.GetGLShaderProgramHandle());
|
||||
}
|
||||
|
||||
void J2D::UseShaderProgram(Shader* shader) {
|
||||
J2D::UseShaderProgram(*shader);
|
||||
}
|
||||
|
||||
void J2D::UseFixedFunction() {
|
||||
j2d_shader_program = nullptr;
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
bool J2D::IsUsingShaderProgram() {
|
||||
return (j2d_shader_program);
|
||||
glViewport(0, 0, (int) wS.x, (int) wS.y);
|
||||
}
|
||||
|
||||
void J2D::Begin() {
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glViewport(0, 0, wS.x, wS.y);
|
||||
glViewport(0, 0, (int) wS.x, (int) wS.y);
|
||||
glOrtho(0, wS.x, wS.y, 0, -1, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
@@ -185,18 +166,17 @@ namespace JGL {
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
|
||||
void J2D::DrawSprite(const Texture& texture, float positionX, float positionY, float originX, float originY, float scaleX, float scaleY, const Color4& color, Inversion inversion)
|
||||
{
|
||||
DrawSprite(texture,
|
||||
{positionX, positionX},
|
||||
{positionX, positionY},
|
||||
{originX, originY},
|
||||
{scaleX, scaleY},
|
||||
color,
|
||||
inversion);
|
||||
color, inversion);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialSprite(const Texture& texture, const Vector2& position, const Vector2& sub_texture_position, const Vector2& sub_texture_size, const Vector2& origin, const Vector2& scale, const Color4& color, Inversion inversion) {
|
||||
@@ -205,7 +185,7 @@ namespace JGL {
|
||||
|
||||
const Vector2 textureSize = texture.GetDimensions();
|
||||
|
||||
// Calculate texture coordinates (correctly relative to the whole texture)
|
||||
// Calculate texture coordinates (relative to the whole texture)
|
||||
std::array<GLfloat, 8> textureCoordinates = {
|
||||
sub_texture_position.x / textureSize.x,
|
||||
sub_texture_position.y / textureSize.y,
|
||||
@@ -217,17 +197,15 @@ namespace JGL {
|
||||
sub_texture_position.y / textureSize.y
|
||||
};
|
||||
|
||||
if (inversion & Inversion::Vertical) {
|
||||
std::swap(textureCoordinates[1], textureCoordinates[3]);
|
||||
if (inversion & Inversion::Vertical)
|
||||
std::swap(textureCoordinates[1], textureCoordinates[3]),
|
||||
std::swap(textureCoordinates[5], textureCoordinates[7]);
|
||||
}
|
||||
if (inversion & Inversion::Horizontal) {
|
||||
std::swap(textureCoordinates[0], textureCoordinates[6]);
|
||||
|
||||
if (inversion & Inversion::Horizontal)
|
||||
std::swap(textureCoordinates[0], textureCoordinates[6]),
|
||||
std::swap(textureCoordinates[2], textureCoordinates[4]);
|
||||
}
|
||||
|
||||
const Vector2 offset = origin * sub_texture_size;
|
||||
|
||||
Vector2 pos2 = position - offset * scale;
|
||||
Vector2 scaled_size = scale * sub_texture_size;
|
||||
Vector2 size2 = scaled_size;
|
||||
@@ -245,7 +223,7 @@ namespace JGL {
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates.data());
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawPartialSprite(const JGL::Texture& texture, float positionX, float positionY, float sub_texture_positionX, float sub_texture_positionY, unsigned int sub_texture_sizeX,
|
||||
@@ -259,18 +237,12 @@ namespace JGL {
|
||||
if (!inJ2D)
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {v1, v2, v3, v4};
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
Vector2 vertices[4] = {v1, v2, v3, v4};
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
|
||||
J2D::FillQuad(Color4(color), v1, v2, v3, v4);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) {
|
||||
@@ -279,37 +251,21 @@ namespace JGL {
|
||||
|
||||
Vector2 vertices[] = {v1, v2, v3, v4};
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, 4);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) {
|
||||
J2D::OutlineQuad(Color4(color), v1, v2, v3, v4);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
|
||||
if (!inJ2D)
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
|
||||
J2D::FillRect({color.r, color.g, color.b, 255}, pos, size);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
|
||||
@@ -317,36 +273,32 @@ namespace JGL {
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
std::vector<GLfloat> colors = {};
|
||||
std::vector<GLfloat> colors{};
|
||||
|
||||
if (gradient == Gradient::Horizontal)
|
||||
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),
|
||||
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||
|
||||
else if (gradient == Gradient::Vertical)
|
||||
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(),
|
||||
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized()};
|
||||
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized()};
|
||||
|
||||
else if (gradient == Gradient::DiagonalBottomLeft)
|
||||
colors = {(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,
|
||||
color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f,
|
||||
(color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f, color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||
colors = {(color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.f, (color1.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f, (color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f,
|
||||
color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), (color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.f,
|
||||
(color1.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f, (color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f, color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||
|
||||
else if (gradient == Gradient::DiagonalTopLeft)
|
||||
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f,
|
||||
(color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(),
|
||||
(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f,(color1.a + color2.a) / 2.f / 255.f};
|
||||
colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),(color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.f,
|
||||
(color1.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f, (color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f, color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(),
|
||||
(color1.RedChannelNormalized() + color2.RedChannelNormalized()) / 2.f, (color1.GreenChannelNormalized() + color2.GreenChannelNormalized()) / 2.f, (color1.BlueChannelNormalized() + color2.BlueChannelNormalized()) / 2.f,(color1.AlphaChannelNormalized() + color2.AlphaChannelNormalized()) / 2.f};
|
||||
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glColorPointer(4, GL_FLOAT, 0, colors.data());
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glColorPointer(4, GL_FLOAT, 0, colors.data());
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
|
||||
J2D::FillGradientRect({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, gradient, pos, size);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::FillRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius, unsigned int subdivisions) {
|
||||
@@ -362,10 +314,6 @@ namespace JGL {
|
||||
J2D::FillCircle(color, {pos.x + size.x - radius, pos.y + size.y - radius}, radius, subdivisions);
|
||||
}
|
||||
|
||||
void J2D::FillRoundedRect(const Color3& color, const J3ML::LinearAlgebra::Vector2& pos, const J3ML::LinearAlgebra::Vector2& size, float radius, unsigned int subdivisions) {
|
||||
J2D::FillRoundedRect({color.r, color.g, color.b, 255}, pos, size, radius, subdivisions);
|
||||
}
|
||||
|
||||
void J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) {
|
||||
if (!inJ2D)
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
@@ -373,17 +321,10 @@ namespace JGL {
|
||||
Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, 4);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness) {
|
||||
J2D::OutlineRect({color.r, color.g, color.b, 255}, pos, size, thickness);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) {
|
||||
@@ -391,60 +332,38 @@ namespace JGL {
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
Vector2 vertices[] = {A, B};
|
||||
|
||||
if (J2D::IsUsingShaderProgram()) {
|
||||
return;
|
||||
}
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
|
||||
J2D::DrawLine({color.r, color.g, color.b, 255}, A, B, thickness);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawLine(const Color4& color, float x, float y, float w, float h, float thickness) {
|
||||
J2D::DrawLine(color, {x, y}, {w, h}, thickness);
|
||||
}
|
||||
|
||||
void J2D::DrawLine(const Color3& color, float x, float y, float w, float h, float thickness) {
|
||||
J2D::DrawLine({color.r, color.g, color.b, 255}, x, y, w, h, thickness);
|
||||
}
|
||||
|
||||
void J2D::DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness) {
|
||||
if (!inJ2D)
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {A, B};
|
||||
GLfloat colors[8] = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),
|
||||
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()};
|
||||
color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized() };
|
||||
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glLineWidth(thickness);
|
||||
glColorPointer(4,GL_FLOAT,sizeof(Color4), colors);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glLineWidth(thickness);
|
||||
glColorPointer(4,GL_FLOAT,sizeof(Color4), colors);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness) {
|
||||
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, A, B, thickness);
|
||||
}
|
||||
void DrawGradientLine(const Color4& color1, const Color4& color2, float x, float y, float w, float h, float thickness) {
|
||||
J2D::DrawGradientLine(color1, color2, {x, y}, {w, h}, thickness);
|
||||
}
|
||||
|
||||
void DrawGradientLine(const Color3& color1, const Color3& color2, float x, float y, float w, float h, float thickness) {
|
||||
J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, {x, y}, {w, h}, thickness);
|
||||
}
|
||||
|
||||
void J2D::OutlinePolygon(const Color4 &color, const std::vector<Vector2>& points, float thickness) {
|
||||
if (!inJ2D)
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
@@ -453,10 +372,10 @@ namespace JGL {
|
||||
throw std::runtime_error("J2D::OutlinePolygon: The first point and the last point must connect.");
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.RedChannelNormalized(), color.GreenChannelNormalized(), color.BlueChannelNormalized(), color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), points.data());
|
||||
glDrawArrays(GL_LINE_LOOP, 0, points.size());
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, (int) points.size());
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawPoint(const Color4& color, const Vector2& coordinates, float radius) {
|
||||
@@ -466,25 +385,14 @@ namespace JGL {
|
||||
Vector2 vertices[] = {coordinates};
|
||||
|
||||
glPointSize(radius);
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_POINTS, 0, 1);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::DrawPoint(const Color3& color, const Vector2& coordinates, float radius) {
|
||||
J2D::DrawPoint({color.r, color.g, color.b, 255}, coordinates);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::DrawPoint(const Color4& color, float x, float y, float radius) {
|
||||
DrawPoint(color, {x, y});
|
||||
}
|
||||
|
||||
void J2D::DrawPoint(const Color3& color, float x, float y, float radius) {
|
||||
DrawPoint({color.r, color.g, color.b, 255}, {x, y});
|
||||
DrawPoint(color, {x, y}, radius);
|
||||
}
|
||||
|
||||
void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
|
||||
@@ -502,17 +410,10 @@ namespace JGL {
|
||||
}
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.RedChannelNormalized(),
|
||||
color.GreenChannelNormalized(),
|
||||
color.BlueChannelNormalized(),
|
||||
color.AlphaChannelNormalized());
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
|
||||
J2D::OutlineCircle({color.r, color.g, color.b, 255}, center, radius, subdivisions, thickness);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, (int) vertices.size());
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions) {
|
||||
@@ -524,18 +425,14 @@ namespace JGL {
|
||||
std::vector<Vector2> vertices{};
|
||||
|
||||
for (angle = 0.0f; angle < (2.f * Math::Pi); angle += step)
|
||||
x = radius * sin(angle) + center.x,
|
||||
y = radius * cos(angle) + center.y,
|
||||
vertices.push_back({x, y});
|
||||
x = radius * std::sin(angle) + center.x,
|
||||
y = radius * std::cos(angle) + center.y,
|
||||
vertices.emplace_back(x, y);
|
||||
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions) {
|
||||
J2D::FillCircle({color.r, color.g, color.b, 255}, center, radius, subdivisions);
|
||||
glDrawArrays(GL_TRIANGLE_FAN, 0, (int) vertices.size());
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness) {
|
||||
@@ -545,14 +442,10 @@ namespace JGL {
|
||||
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINE_LOOP, 0, 3);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness) {
|
||||
J2D::OutlineTriangle({color.r, color.g, color.b, 255}, tri, thickness);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::FillTriangle(const Color4& color, const Triangle2D& tri) {
|
||||
@@ -561,10 +454,10 @@ namespace JGL {
|
||||
|
||||
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri) {
|
||||
@@ -572,40 +465,27 @@ namespace JGL {
|
||||
jlog::Error("Drawing J2D element before J2D begin.");
|
||||
|
||||
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
|
||||
GLfloat colors[] = {a_color.r / 255.f, a_color.g / 255.f, a_color.b / 255.f, a_color.a / 255.f,b_color.r / 255.f,
|
||||
b_color.g / 255.f, b_color.b / 255.f, b_color.a / 255.f,c_color.r / 255.f, c_color.g / 255.f, c_color.b / 255.f,
|
||||
c_color.a / 255.f };
|
||||
GLfloat colors[] = {a_color.RedChannelNormalized(), a_color.GreenChannelNormalized(), a_color.BlueChannelNormalized(), a_color.AlphaChannelNormalized(),
|
||||
b_color.RedChannelNormalized(),b_color.GreenChannelNormalized(), b_color.BlueChannelNormalized(), b_color.AlphaChannelNormalized(),
|
||||
c_color.RedChannelNormalized(), c_color.GreenChannelNormalized(), c_color.BlueChannelNormalized(),c_color.AlphaChannelNormalized()};
|
||||
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glColorPointer(4, GL_FLOAT, sizeof(Color4), colors);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J2D::FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri) {
|
||||
J2D::FillGradientTriangle(Color4(a_color), Color4(b_color), Color4(c_color), tri);
|
||||
}
|
||||
|
||||
|
||||
void J2D::FillTriangle(const Color3& color, const Triangle2D& tri) {
|
||||
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void J2D::DrawCubicBezierCurve(const Color4 &color, const J3ML::LinearAlgebra::Vector2 &controlA,
|
||||
const J3ML::LinearAlgebra::Vector2 &pointA,
|
||||
const J3ML::LinearAlgebra::Vector2 &pointB,
|
||||
const J3ML::LinearAlgebra::Vector2 &controlB, int subdivisions, float thickness) {
|
||||
void J2D::DrawCubicBezierCurve(const Color4 &color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
|
||||
int subdivisions, float thickness) {
|
||||
|
||||
|
||||
Vector2 last = controlA;
|
||||
const Vector2& first = controlB;
|
||||
for (int i = 0; i < subdivisions; ++i)
|
||||
{
|
||||
float alpha = (float)i / (float)subdivisions;
|
||||
float alpha = (float) i / (float) subdivisions;
|
||||
Vector2 step = J3ML::Algorithm::Bezier(alpha, controlA, pointA, pointB, controlB);
|
||||
DrawLine(color, last, step, thickness);
|
||||
last = step;
|
||||
@@ -613,19 +493,12 @@ namespace JGL {
|
||||
|
||||
// Have to manually draw the last segment of the curve.
|
||||
DrawLine(color, last, first, thickness);
|
||||
|
||||
// Display control points
|
||||
DrawPoint(Colors::Red, controlA, 2.f);
|
||||
DrawPoint(Colors::Red, controlB, 2.f);
|
||||
DrawPoint(Colors::Reds::Salmon, pointA, 2.f);
|
||||
DrawPoint(Colors::Reds::Salmon, pointB, 2.f);
|
||||
|
||||
}
|
||||
|
||||
//The 3D projection.
|
||||
std::vector<GLfloat> perspective(float fov, float aspect, float nearPlane, float farPlane) {
|
||||
std::vector<float> result(16);
|
||||
float f = 1.0f / tan(fov * 0.5f * Math::Pi / 180.0f);
|
||||
float f = 1.0f / std::tan(fov * 0.5f * Math::Pi / 180.0f);
|
||||
result[0] = f / aspect;
|
||||
result[5] = f;
|
||||
result[10] = (farPlane + nearPlane) / (nearPlane - farPlane);
|
||||
@@ -698,7 +571,7 @@ namespace JGL {
|
||||
|
||||
if (!inJ2D)
|
||||
inJ3D = true;
|
||||
else { jlog::Error("Attempt to Begin J3D inside of J2D context."); }
|
||||
else { jlog::Error("Can't begin J3D context inside J2D context."); }
|
||||
}
|
||||
|
||||
void J3D::End() {
|
||||
@@ -718,8 +591,7 @@ namespace JGL {
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
||||
|
||||
glColor4fv(oldColor);
|
||||
inJ3D = false;
|
||||
}
|
||||
|
||||
@@ -730,10 +602,10 @@ namespace JGL {
|
||||
Vector3 vertices[] = {A, B};
|
||||
|
||||
glLineWidth(thickness);
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glColor4ubv(color.ptr());
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
glColor4fv(baseColor);
|
||||
}
|
||||
|
||||
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {
|
||||
|
@@ -2,15 +2,15 @@
|
||||
|
||||
|
||||
#if __linux__
|
||||
#include <freetype2/ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_OUTLINE_H
|
||||
|
||||
#include <freetype2/ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_OUTLINE_H
|
||||
#endif
|
||||
|
||||
#if _WIN32
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_OUTLINE_H
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_OUTLINE_H
|
||||
#endif
|
||||
|
||||
#include <JGL/types/Font.h>
|
||||
@@ -31,13 +31,6 @@ namespace JGL {
|
||||
|
||||
CachedFont* cachedFont = fontCache.getFont(size, font.index);
|
||||
|
||||
//Set up the regular font.
|
||||
//for (const auto &f : Font::GetLoadedFonts())
|
||||
// if (f.index == font.index)
|
||||
// font = f;
|
||||
|
||||
|
||||
|
||||
if (font.face == nullptr)
|
||||
return;
|
||||
|
||||
@@ -74,7 +67,7 @@ namespace JGL {
|
||||
|
||||
FT_GlyphSlot g = font.face->glyph;
|
||||
width += g->bitmap.width;
|
||||
max_height = std::max(max_height, (GLsizei)g->bitmap.rows);
|
||||
max_height = std::max(max_height, (GLsizei) g->bitmap.rows);
|
||||
charcode = FT_Get_Next_Char(font.face, charcode, &gindex);
|
||||
}
|
||||
|
||||
@@ -115,7 +108,7 @@ namespace JGL {
|
||||
}
|
||||
}
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
//Texture parameters are restored when the texture_handle is bound
|
||||
glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture());
|
||||
@@ -125,8 +118,9 @@ namespace JGL {
|
||||
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
float x2, y2, w, h;
|
||||
CachedGlyph *glyph = cachedFont->getGlyph(text.c_str()[i]);
|
||||
if (glyph == nullptr) continue;
|
||||
CachedGlyph* glyph = cachedFont->getGlyph(text.c_str()[i]);
|
||||
if (glyph == nullptr)
|
||||
continue;
|
||||
|
||||
x2 = x + glyph->x2offset * scale;
|
||||
y2 = y - glyph->y2offset * scale; // Adjust y-coordinate
|
||||
@@ -155,12 +149,6 @@ namespace JGL {
|
||||
glColor4f(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
|
||||
J2D::DrawString(Color4::FromColor3(color, 255), text, x, y, scale, size, font);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void J3D::DrawString(const Color4& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font) {
|
||||
//TODO figure out what the scale should actually be mathematically.
|
||||
scale = scale * 0.002f;
|
||||
@@ -173,14 +161,8 @@ namespace JGL {
|
||||
glUseProgram(0); // Fixed-function pipeline.
|
||||
glColor4ubv(color.ptr());
|
||||
|
||||
//Font font;
|
||||
//for (auto& f : Font::GetLoadedFonts())
|
||||
//if (f.index == font.index)
|
||||
//font = f;
|
||||
if (font.face == NULL) {
|
||||
std::cout << "null font" << std::endl;
|
||||
return;
|
||||
}
|
||||
if (font.face == nullptr)
|
||||
throw std::runtime_error("FreeType font face is null.");
|
||||
|
||||
FT_Set_Pixel_Sizes(font.face, 0, size);
|
||||
|
||||
|
@@ -1,536 +0,0 @@
|
||||
#include <JGL/types/Shader.h>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <Color3.hpp>
|
||||
#include <Color4.hpp>
|
||||
#include <jlog/Logger.hpp>
|
||||
|
||||
void checkCompilationError(GLuint shader) {
|
||||
GLint success;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success) {
|
||||
GLchar info[512];
|
||||
glGetShaderInfoLog(shader, sizeof(info), nullptr, info);
|
||||
std::cerr << info << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
void checkLinkingError(GLuint shader) {
|
||||
GLint success;
|
||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||
|
||||
if (!success) {
|
||||
GLchar info[512];
|
||||
glGetProgramInfoLog(shader, sizeof(info), nullptr, info);
|
||||
std::cerr << info << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GLuint JGL::Shader::GetGLShaderProgramHandle() const {
|
||||
if (shader_program_handle == 0)
|
||||
throw std::runtime_error("JGL::Shader::GetGLShaderProgramHandle: Returned the default shader?");
|
||||
|
||||
return shader_program_handle;
|
||||
}
|
||||
|
||||
JGL::ShaderType JGL::Shader::GetShaderType() const {
|
||||
return shader_type;
|
||||
}
|
||||
|
||||
void JGL::Shader::Erase() {
|
||||
if (shader_program_handle == 0)
|
||||
throw std::runtime_error("JGL::Shader::Erase: Deleted the default shader?");
|
||||
|
||||
ResetUniforms();
|
||||
ResetAttributes();
|
||||
glDeleteProgram(shader_program_handle);
|
||||
}
|
||||
|
||||
JGL::Shader::~Shader() {
|
||||
Shader::Erase();
|
||||
}
|
||||
|
||||
void JGL::Shader::load(const char* file_path, const JGL::ShaderType type) {
|
||||
std::ifstream file(file_path);
|
||||
|
||||
if (!file.is_open())
|
||||
throw std::runtime_error("JGL::Shader::load: File not found.");
|
||||
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
file.close();
|
||||
|
||||
std::string shaderSource = buffer.str();
|
||||
load(shaderSource, type);
|
||||
}
|
||||
|
||||
void JGL::Shader::load(const std::string& shader_program_text, const JGL::ShaderType type) {
|
||||
const GLchar* source = shader_program_text.c_str();
|
||||
|
||||
if (type == ShaderType::VERTEX) {
|
||||
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex_shader, 1, &source, nullptr);
|
||||
glCompileShader(vertex_shader);
|
||||
checkCompilationError(vertex_shader);
|
||||
}
|
||||
|
||||
if (type == ShaderType::FRAGMENT) {
|
||||
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragment_shader, 1, &source, nullptr);
|
||||
glCompileShader(fragment_shader);
|
||||
checkCompilationError(fragment_shader);
|
||||
}
|
||||
}
|
||||
|
||||
void JGL::Shader::link() {
|
||||
if (shader_program_handle != 0 || vertex_shader == 0 && fragment_shader == 0)
|
||||
throw std::runtime_error("JGL::Shader::link: Linking a shader with no shader source?");
|
||||
|
||||
shader_program_handle = glCreateProgram();
|
||||
|
||||
if (vertex_shader != 0)
|
||||
glAttachShader(shader_program_handle, vertex_shader);
|
||||
|
||||
if (fragment_shader != 0)
|
||||
glAttachShader(shader_program_handle, fragment_shader);
|
||||
|
||||
glLinkProgram(shader_program_handle);
|
||||
checkLinkingError(shader_program_handle);
|
||||
|
||||
GLint num_uniforms;
|
||||
glGetProgramiv(shader_program_handle, GL_ACTIVE_UNIFORMS, &num_uniforms);
|
||||
|
||||
//Set up the uniforms from the shader program after linking.
|
||||
//This is "Active" uniforms. So, If you have a uniform that never receives a value it won't be here iirc.
|
||||
for (GLint i = 0; i < num_uniforms; ++i) {
|
||||
char uniform_name[256];
|
||||
GLsizei length;
|
||||
GLint size;
|
||||
GLenum type;
|
||||
glGetActiveUniform(shader_program_handle, i, sizeof(uniform_name), &length, &size, &type, uniform_name);
|
||||
GLint location = glGetUniformLocation(shader_program_handle, uniform_name);
|
||||
|
||||
if (type == GL_FLOAT)
|
||||
float_uniforms.push_back(Uniform_Float({uniform_name, location}, 0));
|
||||
|
||||
else if (type == GL_INT)
|
||||
int_uniforms.push_back(Uniform_Int({uniform_name, location}, 0));
|
||||
|
||||
else if (type == GL_BOOL)
|
||||
bool_uniforms.push_back(Uniform_Bool({uniform_name, location}, false));
|
||||
|
||||
|
||||
else if (type == GL_FLOAT_VEC2)
|
||||
vector2_uniforms.push_back(Uniform_Vector2({uniform_name, location}, {0, 0}));
|
||||
|
||||
else if (type == GL_FLOAT_VEC3)
|
||||
vector3_uniforms.push_back(Uniform_Vector3({uniform_name, location}, {0, 0, 0}));
|
||||
|
||||
else if (type == GL_FLOAT_VEC4)
|
||||
vector4_uniforms.push_back(Uniform_Vector4({uniform_name, location}, {0, 0, 0, 0}));
|
||||
|
||||
|
||||
else if (type == GL_FLOAT_MAT2)
|
||||
matrix2x2_uniforms.push_back(Uniform_Matrix2x2({uniform_name, location}, {}));
|
||||
|
||||
else if (type == GL_FLOAT_MAT3)
|
||||
matrix3x3_uniforms.push_back(Uniform_Matrix3x3({uniform_name, location}, {}));
|
||||
|
||||
else if (type == GL_FLOAT_MAT4)
|
||||
matrix4x4_uniforms.push_back(Uniform_Matrix4x4({uniform_name, location}, {}));
|
||||
}
|
||||
|
||||
GLint num_attributes;
|
||||
glGetProgramiv(shader_program_handle, GL_ACTIVE_ATTRIBUTES, &num_attributes);
|
||||
|
||||
for (GLint i = 0; i < num_attributes; ++i) {
|
||||
char attribute_name[256];
|
||||
GLsizei length;
|
||||
GLint size;
|
||||
GLenum type;
|
||||
glGetActiveAttrib(shader_program_handle, i, sizeof(attribute_name), &length, &size, &type, attribute_name);
|
||||
GLint location = glGetAttribLocation(shader_program_handle, attribute_name);
|
||||
|
||||
if (type == GL_FLOAT_VEC2)
|
||||
vector2_attributes.push_back(Attribute_Vector2({attribute_name, location}, {}));
|
||||
|
||||
else if (type == GL_FLOAT_VEC3)
|
||||
vector3_attributes.push_back(Attribute_Vector3({attribute_name, location}, {}));
|
||||
|
||||
else if (type == GL_FLOAT_VEC4)
|
||||
vector4_attributes.push_back(Attribute_Vector4({attribute_name, location}, {}));
|
||||
}
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
}
|
||||
|
||||
JGL::Shader::Shader(const char* vertex_shader_file, const char* fragment_shader_file) {
|
||||
shader_type = ShaderType::COMBINED;
|
||||
jlog::Debug("Compiling shader program from files: " + std::string(vertex_shader_file) + ", " + std::string(fragment_shader_file) + "...");
|
||||
load(vertex_shader_file, ShaderType::VERTEX);
|
||||
load(fragment_shader_file, ShaderType::FRAGMENT);
|
||||
link();
|
||||
jlog::Debug("Shader compilation success.");
|
||||
}
|
||||
|
||||
JGL::Shader::Shader(const std::string& vertex_shader_text, const std::string& fragment_shader_text) {
|
||||
shader_type = ShaderType::COMBINED;
|
||||
jlog::Debug("Compiling shader program from strings...");
|
||||
load(vertex_shader_text, ShaderType::VERTEX);
|
||||
load(fragment_shader_text, ShaderType::FRAGMENT);
|
||||
link();
|
||||
jlog::Debug("Shader compilation success.");
|
||||
}
|
||||
|
||||
void JGL::Shader::ResetLocalUniforms() {
|
||||
for (auto& u : float_uniforms)
|
||||
u.value = 0;
|
||||
|
||||
for (auto& u : int_uniforms)
|
||||
u.value = 0;
|
||||
|
||||
for (auto& u : bool_uniforms)
|
||||
u.value = false;
|
||||
|
||||
|
||||
for (auto& u : vector2_uniforms)
|
||||
u.value = {0, 0};
|
||||
|
||||
for (auto& u : vector3_uniforms)
|
||||
u.value = {0, 0, 0};
|
||||
|
||||
for (auto& u : vector4_uniforms)
|
||||
u.value = {0, 0, 0, 0};
|
||||
|
||||
|
||||
for (auto& u : matrix2x2_uniforms)
|
||||
u.value = {};
|
||||
|
||||
for (auto& u : matrix3x3_uniforms)
|
||||
u.value = {};
|
||||
|
||||
//TODO J3ML Matrix4x4 empty braced initializer list does not result in a Matrix4x4 filled with zeroes.
|
||||
for (auto& u : matrix4x4_uniforms)
|
||||
u.value = Matrix4x4();
|
||||
}
|
||||
|
||||
void JGL::Shader::ResetUniforms() {
|
||||
ResetLocalUniforms();
|
||||
SubmitUniforms();
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUF(const std::string& uniform_name, float value) {
|
||||
for (auto& u : float_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUI(const std::string& uniform_name, int value) {
|
||||
for (auto& u : int_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUB(const std::string& uniform_name, bool value) {
|
||||
for (auto& u : bool_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
|
||||
void JGL::Shader::SetUV2(const std::string& uniform_name, const Vector2& value) {
|
||||
for (auto& u : vector2_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUV3(const std::string& uniform_name, const Vector3& value) {
|
||||
for (auto& u : vector3_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUV3(const std::string& uniform_name, const Color3& value) {
|
||||
for (auto& u : vector3_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = {value.RedChannelNormalized(), value.BlueChannelNormalized(), value.RedChannelNormalized()};
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUV4(const std::string& uniform_name, const Vector4& value) {
|
||||
for (auto& u : vector4_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUV4(const std::string& uniform_name, const Color4& value) {
|
||||
for (auto& u : vector4_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = {value.RedChannelNormalized(), value.BlueChannelNormalized(), value.RedChannelNormalized(), value.AlphaChannelNormalized()};
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUMat2(const std::string& uniform_name, const Matrix2x2& value) {
|
||||
for (auto& u : matrix2x2_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUMat3(const std::string& uniform_name, const Matrix3x3& value) {
|
||||
for (auto& u : matrix3x3_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetUMat4(const std::string& uniform_name, const Matrix4x4& value) {
|
||||
for (auto& u : matrix4x4_uniforms) {
|
||||
if (u.name == uniform_name) {
|
||||
u.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting uniform " + uniform_name + " but there is no uniform by that name?");
|
||||
}
|
||||
|
||||
|
||||
void JGL::Shader::SetAV2(const std::string& attribute_name, const std::vector<Vector2>& value) {
|
||||
for (auto& a : vector2_attributes) {
|
||||
if (a.name == attribute_name) {
|
||||
a.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting attribute " + attribute_name + " but there is no attribute by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetAV2(const std::string& attribute_name, const Vector2* value, size_t size) {
|
||||
for (auto& a : vector2_attributes) {
|
||||
if (a.name == attribute_name) {
|
||||
// Jesus, Memory bandwidth go brrrrrrrr.
|
||||
// Guess that's what I get for wanting Fixed-Function-esque Shader Programs. - Redacted
|
||||
a.value.resize(size);
|
||||
// Can't use memcpy because "Vec2 - Vec4 are not trivially copyable?"
|
||||
std::copy(value, value + size, a.value.begin());
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting attribute " + attribute_name + " but there is no attribute by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetAV3(const std::string& attribute_name, const std::vector<Vector3>& value) {
|
||||
for (auto& a : vector3_attributes) {
|
||||
if (a.name == attribute_name) {
|
||||
a.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting attribute " + attribute_name + " but there is no attribute by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetAV3(const std::string& attribute_name, const Vector3* value, size_t size) {
|
||||
for (auto& a : vector3_attributes) {
|
||||
if (a.name == attribute_name) {
|
||||
a.value.resize(size);
|
||||
std::copy(value, value + size, a.value.begin());
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting attribute " + attribute_name + " but there is no attribute by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetAV4(const std::string& attribute_name, const std::vector<Vector4>& value) {
|
||||
for (auto& a : vector4_attributes) {
|
||||
if (a.name == attribute_name) {
|
||||
a.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting attribute " + attribute_name + " but there is no attribute by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SetAV4(const std::string& attribute_name, const Vector4* value, size_t size) {
|
||||
for (auto& a : vector4_attributes) {
|
||||
if (a.name == attribute_name) {
|
||||
a.value.resize(size);
|
||||
std::copy(value, value + size, a.value.begin());
|
||||
return;
|
||||
}
|
||||
}
|
||||
jlog::Error("Setting attribute " + attribute_name + " but there is no attribute by that name?");
|
||||
}
|
||||
|
||||
void JGL::Shader::SubmitUniforms(bool clear_uniforms_from_local_store) {
|
||||
// Make sure "this" is the shader we submit to.
|
||||
GLuint current_program;
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*) ¤t_program);
|
||||
|
||||
if (current_program != shader_program_handle)
|
||||
glUseProgram(shader_program_handle);
|
||||
|
||||
for (const auto& u : float_uniforms)
|
||||
glUniform1f(u.location, u.value);
|
||||
|
||||
for (const auto& u : int_uniforms)
|
||||
glUniform1i(u.location, u.value);
|
||||
|
||||
for (const auto& u : bool_uniforms)
|
||||
glUniform1i(u.location, (int) u.value);
|
||||
|
||||
|
||||
for (const auto& u : vector2_uniforms)
|
||||
glUniform2fv(u.location, 1, u.value.ptr());
|
||||
|
||||
for (const auto& u : vector3_uniforms)
|
||||
glUniform3fv(u.location, 1, u.value.ptr());
|
||||
|
||||
for (const auto& u : vector4_uniforms)
|
||||
glUniform4fv(u.location, 1, u.value.ptr());
|
||||
|
||||
for (const auto& u : matrix2x2_uniforms)
|
||||
glUniformMatrix2fv(u.location, 1, GL_FALSE, u.value.ptr());
|
||||
|
||||
for (const auto& u : matrix3x3_uniforms)
|
||||
glUniformMatrix3fv(u.location, 1, GL_FALSE, u.value.ptr());
|
||||
|
||||
for (const auto& u : matrix4x4_uniforms)
|
||||
glUniformMatrix4fv(u.location, 1, GL_FALSE, u.value.ptr());
|
||||
|
||||
if (clear_uniforms_from_local_store)
|
||||
ResetLocalUniforms();
|
||||
|
||||
// Change back if they were using a different shader before.
|
||||
if (current_program != shader_program_handle)
|
||||
glUseProgram(current_program);
|
||||
}
|
||||
|
||||
void JGL::Shader::ListUniforms() {
|
||||
for (const auto& u : float_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : int_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : bool_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : vector2_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : vector3_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : vector4_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : matrix2x2_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : matrix3x3_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
|
||||
for (const auto& u : matrix4x4_uniforms)
|
||||
std::cout << u.name << std::endl;
|
||||
}
|
||||
|
||||
void JGL::Shader::SubmitAttributes() {
|
||||
GLuint current_program;
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)¤t_program);
|
||||
|
||||
if (current_program != shader_program_handle)
|
||||
glUseProgram(shader_program_handle);
|
||||
|
||||
for (const auto& a : vector2_attributes)
|
||||
if (a.location != -1)
|
||||
glEnableVertexAttribArray(a.location),
|
||||
glVertexAttribPointer(a.location, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), a.value.data());
|
||||
|
||||
for (const auto& a : vector3_attributes)
|
||||
if (a.location != -1)
|
||||
glEnableVertexAttribArray(a.location),
|
||||
glVertexAttribPointer(a.location, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), a.value.data());
|
||||
|
||||
for (const auto& a : vector4_attributes)
|
||||
if (a.location != -1)
|
||||
glEnableVertexAttribArray(a.location),
|
||||
glVertexAttribPointer(a.location, 4, GL_FLOAT, GL_FALSE, sizeof(Vector4), a.value.data());
|
||||
|
||||
if (current_program != shader_program_handle)
|
||||
glUseProgram(current_program);
|
||||
}
|
||||
|
||||
void JGL::Shader::ResetAttributes() {
|
||||
GLuint current_program;
|
||||
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)¤t_program);
|
||||
|
||||
for (auto& a : vector2_attributes)
|
||||
if (a.location != -1)
|
||||
glDisableVertexAttribArray(a.location),
|
||||
a.value.resize(0),
|
||||
a.value.shrink_to_fit();
|
||||
|
||||
for (auto& a : vector3_attributes)
|
||||
if (a.location != -1)
|
||||
glDisableVertexAttribArray(a.location),
|
||||
a.value.resize(0),
|
||||
a.value.shrink_to_fit();
|
||||
|
||||
for (auto& a : vector4_attributes)
|
||||
if (a.location != -1)
|
||||
glDisableVertexAttribArray(a.location),
|
||||
a.value.resize(0),
|
||||
a.value.shrink_to_fit();
|
||||
|
||||
if (current_program != shader_program_handle)
|
||||
glUseProgram(current_program);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user