DrawSprite
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m21s
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m21s
This commit is contained in:
@@ -74,6 +74,9 @@ endif()
|
||||
|
||||
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
#Don't expose this one because it's only to be used in the demo program.
|
||||
include_directories(${ReTexture_SOURCE_DIR}/include)
|
||||
|
||||
target_include_directories(JGL PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${OPENGL_INCLUDE_DIRS}
|
||||
@@ -82,7 +85,6 @@ target_include_directories(JGL PUBLIC
|
||||
${ReWindow_SOURCE_DIR}/include
|
||||
${glad_SOURCE_DIR}/include
|
||||
${jlog_SOURCE_DIR}/include
|
||||
${ReTexture_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
add_executable(JGL_Demo main.cpp)
|
||||
|
BIN
assets/sprites/Re3D.png
Normal file
BIN
assets/sprites/Re3D.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.0 KiB |
10
include/JGL/Inversion.h
Normal file
10
include/JGL/Inversion.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <J3ML/J3ML.h>
|
||||
namespace JGL {
|
||||
enum class Inversion : u8 {
|
||||
None = 0,
|
||||
Vertical = 1,
|
||||
Horizontal = 2,
|
||||
Both = 3
|
||||
};
|
||||
}
|
@@ -16,6 +16,7 @@
|
||||
#include <iostream>
|
||||
#include <JGL/Color4.h>
|
||||
#include <JGL/Gradient.h>
|
||||
#include <JGL/Inversion.h>
|
||||
#include <JGL/FontCache.h>
|
||||
#include <JGL/Font.h>
|
||||
#include <J3ML/LinearAlgebra.h>
|
||||
@@ -102,8 +103,8 @@ namespace JGL {
|
||||
void OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
|
||||
|
||||
///Draws a sprite to the screen.
|
||||
void DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size);
|
||||
void DrawSprite(GLuint texture, float x, float y, float w, float h);
|
||||
void DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size, u8 opacity = 255, Inversion inversion = Inversion::None);
|
||||
void DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity = 255, Inversion inversion = Inversion::None);
|
||||
|
||||
/// Draws a filled rectangle on the screen.
|
||||
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
|
||||
|
23
main.cpp
23
main.cpp
@@ -4,11 +4,14 @@
|
||||
#include <JGL/Colors.h>
|
||||
#include <J3ML/LinearAlgebra/Vector2.h>
|
||||
#include <JGL/Font.h>
|
||||
#include "jlog/jlog.hpp"
|
||||
#include <jlog/jlog.hpp>
|
||||
#include <ReTexture/rTexture.h>
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL;
|
||||
|
||||
RTexture* image;
|
||||
GLuint imageID;
|
||||
//The Re3D style base projection.
|
||||
std::vector<GLfloat> perspective(float fov, float aspect, float nearPlane, float farPlane) {
|
||||
std::vector<float> result(16);
|
||||
@@ -86,6 +89,21 @@ public:
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glDepthMask(GL_TRUE);
|
||||
|
||||
image = new RTexture("assets/sprites/Re3D.png");
|
||||
glGenTextures(1, &imageID);
|
||||
glBindTexture(GL_TEXTURE_2D, imageID);
|
||||
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);
|
||||
|
||||
if (image->format == RTextureFormat::RGBA)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->width, image->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixelData.data());
|
||||
if (image->format == RTextureFormat::RGB)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixelData.data());
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
Vector3 textAngle = {0,0,0};
|
||||
@@ -105,9 +123,9 @@ public:
|
||||
J3D::DrawString(JGL::Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f},textAngle, 1.f, 32, FreeSans);
|
||||
J3D::End();
|
||||
|
||||
|
||||
J2D::Begin();
|
||||
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
|
||||
J2D::DrawSprite(imageID, {0, 52}, {(float) image->width, (float) image->height}, 128);
|
||||
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
|
||||
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
|
||||
J2D::FillRoundedRect(JGL::Colors::Red, {200, 52}, {100, 100}, 8, 8);
|
||||
@@ -121,7 +139,6 @@ public:
|
||||
J2D::DrawGradientLine(JGL::Colors::Red, JGL::Colors::Blue, {105, 375}, {200, 275}, 2);
|
||||
|
||||
auto result = Jupiteroid.MeasureString("Jupiteroid Font", 16);
|
||||
//DEBUG(std::format("Result: {},{}", result.x, result.y ));
|
||||
|
||||
J2D::FillRect(JGL::Colors::Gray, {0, 0}, result);
|
||||
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid);
|
||||
|
56
src/JGL.cpp
56
src/JGL.cpp
@@ -7,7 +7,8 @@
|
||||
#include <JGL/Color3.h>
|
||||
#include <jlog/jlog.hpp>
|
||||
|
||||
GLfloat oldColor[4] = {0, 0, 0, 255};
|
||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||
GLfloat baseColor[4] = {1, 1, 1, 1};
|
||||
bool inJ2D = false;
|
||||
bool inJ3D = false;
|
||||
bool wasTexture2DEnabled = false;
|
||||
@@ -37,6 +38,10 @@ namespace JGL {
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
//Get what the draw color was before we did anything.
|
||||
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
|
||||
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
|
||||
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
|
||||
if (activeTextureUnit != 0)
|
||||
@@ -111,40 +116,49 @@ namespace JGL {
|
||||
if (wasColorArrayEnabled)
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
|
||||
|
||||
//Select whatever texture unit was selected before.
|
||||
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
|
||||
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
||||
|
||||
inJ2D = false;
|
||||
}
|
||||
|
||||
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size) {
|
||||
//TODO rotation, I'm unsure if @josh wants degrees or radians.
|
||||
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size, u8 opacity, Inversion inversion) {
|
||||
if (!inJ2D)
|
||||
ERROR("Attempt to Render J2D element before J2D begin.")
|
||||
|
||||
const std::vector<Vector2> textureCoordinates = {{0,0}, {1, 0}, {1,1}, {0,1}};
|
||||
const std::vector<Vector2> vertices = {pos, {pos.x + size.x, pos.y}, {pos.x + size.x, pos.y + size.y}, {pos.x, pos.y + size.y}};
|
||||
std::array<Vector2, 4> textureCoordinates;
|
||||
const 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}};
|
||||
|
||||
switch (inversion) {
|
||||
case Inversion::None:
|
||||
textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};
|
||||
break;
|
||||
case Inversion::Vertical:
|
||||
textureCoordinates = {Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), Vector2(1, 1)};
|
||||
break;
|
||||
case Inversion::Horizontal:
|
||||
textureCoordinates = {Vector2(1, 0), Vector2(1, 1), Vector2(0, 1), Vector2(0, 0)};
|
||||
break;
|
||||
case Inversion::Both:
|
||||
textureCoordinates = {Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(0, 1)};
|
||||
break;
|
||||
}
|
||||
|
||||
glColor4f(baseColor[0],baseColor[1],baseColor[2],opacity / 255.f);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
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]);
|
||||
}
|
||||
|
||||
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h) {
|
||||
J2D::DrawSprite(texture, {x, y}, {w, h});
|
||||
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity, Inversion inversion) {
|
||||
J2D::DrawSprite(texture, {x, y}, {w, h}, opacity, inversion);
|
||||
}
|
||||
|
||||
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
|
||||
@@ -155,6 +169,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
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) {
|
||||
@@ -191,6 +206,7 @@ namespace JGL {
|
||||
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) {
|
||||
@@ -224,6 +240,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
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) {
|
||||
@@ -240,6 +257,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
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) {
|
||||
@@ -268,6 +286,7 @@ namespace JGL {
|
||||
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]);
|
||||
}
|
||||
|
||||
void J2D::DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness) {
|
||||
@@ -290,6 +309,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 1);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
|
||||
@@ -322,6 +342,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
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) {
|
||||
@@ -344,6 +365,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
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) {
|
||||
@@ -360,6 +382,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
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) {
|
||||
@@ -375,6 +398,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J2D::FillTriangle(const Color3& color, const Triangle2D &tri) {
|
||||
@@ -385,6 +409,7 @@ namespace JGL {
|
||||
|
||||
//Get what the draw color was before we did anything.
|
||||
glGetFloatv(GL_CURRENT_COLOR, oldColor);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
|
||||
wasDepthTestEnabled = false;
|
||||
if (glIsEnabled(GL_DEPTH_TEST))
|
||||
@@ -452,6 +477,7 @@ namespace JGL {
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
|
||||
glDrawArrays(GL_LINES, 0, 2);
|
||||
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
|
||||
}
|
||||
|
||||
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {
|
||||
|
@@ -153,6 +153,7 @@ namespace JGL {
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat) * 2, texcoords.data());
|
||||
glDrawArrays(GL_TRIANGLES, 0, vertices.size() * 6);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
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) {
|
||||
@@ -167,7 +168,7 @@ namespace JGL {
|
||||
float x = pos.x;
|
||||
float y = pos.y;
|
||||
float z = pos.z;
|
||||
std::vector<GLuint> textures(text.length());;
|
||||
std::vector<GLuint> textures(text.length());
|
||||
glUseProgram(0); // Fixed-function pipeline.
|
||||
glColor4f(color.r, color.g, color.b, 1.0f);
|
||||
|
||||
@@ -243,7 +244,7 @@ namespace JGL {
|
||||
glDeleteTextures(1, &texture);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
|
||||
|
||||
glColor4f(1, 1, 1, 1);
|
||||
glPopMatrix();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user