From 6c3b0a8c7e4296f39ac00812fca757a895deff39 Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sat, 3 Aug 2024 12:20:35 +0100 Subject: [PATCH] Transform now has scale in x and y --- examples/Shape/main.cpp | 2 +- examples/Sprite/main.cpp | 2 +- include/jGL/OpenGL/glSpriteRenderer.h | 39 ++++++----- include/jGL/primitive.h | 11 +++- include/jGL/sprite.h | 2 +- src/jGL/OpenGL/glShapeRenderer.cpp | 4 +- src/jGL/OpenGL/glSpriteRenderer.cpp | 93 ++++++++++++++++++++------- 7 files changed, 105 insertions(+), 48 deletions(-) diff --git a/examples/Shape/main.cpp b/examples/Shape/main.cpp index 1760e139..da5eb5d9 100644 --- a/examples/Shape/main.cpp +++ b/examples/Shape/main.cpp @@ -81,7 +81,7 @@ int main(int argv, char ** argc) tr.x+dt*(rng.nextFloat()-0.5), tr.y+dt*(rng.nextFloat()-0.5), tr.theta, - tr.scale + tr.scaleX ); } diff --git a/examples/Sprite/main.cpp b/examples/Sprite/main.cpp index 2e5af7e6..f4d8c407 100644 --- a/examples/Sprite/main.cpp +++ b/examples/Sprite/main.cpp @@ -79,7 +79,7 @@ int main(int argv, char ** argc) {"sJerboa", jGL::Transform(0.1f, 0.9f, 0.0f, 0.1f)}, {"sPi", jGL::Transform(0.1f, 0.1f, 0.0f, 0.1f)}, {"sHeart", jGL::Transform(0.5f, 0.5f, 0.0f, 0.1f)}, - {"sRandom", jGL::Transform(0.5f, 0.25f, 0.0f, 0.1f)}, + {"sRandom", jGL::Transform(0.5f, 0.25f, 0.0f, 0.1f, 0.05f)}, {"lowest", jGL::Transform(0.5f, 0.1f, 0.0f, 0.1f)}, {"middle", jGL::Transform(0.55f, 0.15f, 0.0f, 0.1f)}, {"highest", jGL::Transform(0.6f, 0.2f, 0.0f, 0.1f)} diff --git a/include/jGL/OpenGL/glSpriteRenderer.h b/include/jGL/OpenGL/glSpriteRenderer.h index bf6bc196..8d4c7add 100644 --- a/include/jGL/OpenGL/glSpriteRenderer.h +++ b/include/jGL/OpenGL/glSpriteRenderer.h @@ -1,5 +1,5 @@ -#ifndef GLSPRITERENDERER -#define GLSPRITERENDERER +#ifndef GLSPRITERENDERER_H +#define GLSPRITERENDERER_H #include #include @@ -18,7 +18,8 @@ namespace jGL::GL glSpriteRenderer(size_t sizeHint) : SpriteRenderer(sizeHint) { - offsets = std::vector(sizeHint*offsetDim+padSprites*offsetDim,0.0f); + xytheta = std::vector(sizeHint*xythetaDim+padSprites*xythetaDim,0.0f); + scale = std::vector(sizeHint*scaleDim+padSprites*scaleDim,0.0f); textureRegion = std::vector(sizeHint*textureRegionDim+padSprites*textureRegionDim,0.0f); textureOptions = std::vector(sizeHint*textureOptionsDim+padSprites*textureOptionsDim,0.0f); initGL(); @@ -36,7 +37,7 @@ namespace jGL::GL private: - GLuint vao, a_position, a_offset, a_textureRegion, a_textureOption; + GLuint vao, a_position, a_xytheta, a_scale, a_textureRegion, a_textureOption; float quad[6*4] = { @@ -49,14 +50,21 @@ namespace jGL::GL 0.5f, 0.5f, 1.0f, 1.0f // top right }; - std::vector offsets; // offset x, y, theta, scale - size_t offsetDim = 4; + std::vector xytheta; + size_t xythetaDim = 3; + size_t xythetaAttribute = 1; - std::vector textureRegion; // tx, ty (region coords), lx, ly (region scale) + std::vector scale; + size_t scaleDim = 2; + size_t scaleAttribute = 2; + + std::vector textureRegion; size_t textureRegionDim = 4; - + size_t textureRegionAttribute = 3; + std::vector textureOptions; // texture unit, alpha size_t textureOptionsDim = 2; + size_t textureOptionsAttribute = 4; size_t padSprites = 8; @@ -69,18 +77,19 @@ namespace jGL::GL "#version " GLSL_VERSION "\n" "precision lowp float; precision lowp int;\n" "layout(location=0) in vec4 a_position;\n" - "layout(location=1) in vec4 a_offset;\n" - "layout(location=2) in vec4 a_textureOffset;\n" - "layout(location=3) in vec2 a_textureOptions;\n" + "layout(location=1) in vec3 a_xytheta;\n" + "layout(location=2) in vec2 a_scale;\n" + "layout(location=3) in vec4 a_textureOffset;\n" + "layout(location=4) in vec2 a_textureOptions;\n" "uniform mat4 proj;\n" "out vec2 texCoord;\n" "flat out float alpha;\n" "flat out int tex;\n" "void main(){" - "vec2 pos = a_position.xy*a_offset.w;\n" - "float ct = cos(a_offset.z); float st = sin(a_offset.z);\n" + "vec2 pos = a_position.xy*a_scale;\n" + "float ct = cos(a_xytheta.z); float st = sin(a_xytheta.z);\n" "mat2 rot = mat2(ct, -st, st, ct);\n" - "pos = rot*pos + a_offset.xy;\n" + "pos = rot*pos + a_xytheta.xy;\n" "gl_Position = proj*vec4(pos,0.0,1.0);\n" "texCoord.x = (a_position.z * a_textureOffset.z)+a_textureOffset.x;\n" "texCoord.y = (a_position.w * a_textureOffset.w)+a_textureOffset.y;\n" @@ -113,4 +122,4 @@ namespace jGL::GL }; } -#endif /* GLSPRITERENDERER */ +#endif /* GLSPRITERENDERER_H */ diff --git a/include/jGL/primitive.h b/include/jGL/primitive.h index 0d8c059f..0ee2f5e9 100644 --- a/include/jGL/primitive.h +++ b/include/jGL/primitive.h @@ -44,17 +44,22 @@ namespace jGL { Transform(double x, double y, double t, double s) - : x(x), y(y), theta(t), scale(s) + : x(x), y(y), theta(t), scaleX(s), scaleY(s) + {} + + Transform(double x, double y, double t, double sx, double sy) + : x(x), y(y), theta(t), scaleX(sx), scaleY(sy) {} Transform() - : x(0.0f), y(0.0f), theta(0.0f), scale(0.0f) + : x(0.0), y(0.0), theta(0.0), scaleX(0.0), scaleY(0.0) {} double x; double y; double theta; - double scale; + double scaleX; + double scaleY; }; /** diff --git a/include/jGL/sprite.h b/include/jGL/sprite.h index a207b7b6..eebdfa98 100644 --- a/include/jGL/sprite.h +++ b/include/jGL/sprite.h @@ -108,7 +108,7 @@ namespace jGL float ct = std::cos(transform.theta); float st = std::sin(transform.theta); glm::mat2 rot(ct, -st, st, ct); glm::vec2 pos(transform.x, transform.y); - glm::vec2 scale(transform.scale, transform.scale); + glm::vec2 scale(transform.scaleX, transform.scaleY); for (uint8_t i = 0; i < wbb.vertices.size(); i++) diff --git a/src/jGL/OpenGL/glShapeRenderer.cpp b/src/jGL/OpenGL/glShapeRenderer.cpp index 0cada01d..6dc2806c 100644 --- a/src/jGL/OpenGL/glShapeRenderer.cpp +++ b/src/jGL/OpenGL/glShapeRenderer.cpp @@ -25,7 +25,7 @@ namespace jGL::GL offsets[i*4] = trans.x; offsets[i*4+1] = trans.y; offsets[i*4+2] = trans.theta; - offsets[i*4+3] = trans.scale; + offsets[i*4+3] = trans.scaleX; colours[i*4] = col.r; colours[i*4+1] = col.g; colours[i*4+2] = col.b; @@ -98,7 +98,7 @@ namespace jGL::GL offsets[i*4] = trans.x; offsets[i*4+1] = trans.y; offsets[i*4+2] = trans.theta; - offsets[i*4+3] = trans.scale; + offsets[i*4+3] = trans.scaleX; colours[i*4] = col.r; colours[i*4+1] = col.g; colours[i*4+2] = col.b; diff --git a/src/jGL/OpenGL/glSpriteRenderer.cpp b/src/jGL/OpenGL/glSpriteRenderer.cpp index 21fabdd6..8d4a767a 100644 --- a/src/jGL/OpenGL/glSpriteRenderer.cpp +++ b/src/jGL/OpenGL/glSpriteRenderer.cpp @@ -7,9 +7,10 @@ namespace jGL::GL uint32_t n = ids.size(); - if (offsets.size() < offsetDim*n) + if (xytheta.size() < xythetaDim*n) { - offsets.resize(offsetDim*n+padSprites*offsetDim); + xytheta.resize(xythetaDim*n+padSprites*xythetaDim); + scale.resize(scaleDim*n+padSprites*scaleDim); textureRegion.resize(textureRegionDim*n+padSprites*textureRegionDim); textureOptions.resize(textureOptionsDim*n+padSprites*textureOptionsDim); freeGL(); @@ -42,14 +43,18 @@ namespace jGL::GL const NormalisedTextureRegion toff = sprite.getNormalisedTextureRegion(); const float alpha = sprite.getAlpha(); - offsets[i*offsetDim] = trans.x; - offsets[i*offsetDim+1] = trans.y; - offsets[i*offsetDim+2] = trans.theta; - offsets[i*offsetDim+3] = trans.scale; + xytheta[i*xythetaDim] = trans.x; + xytheta[i*xythetaDim+1] = trans.y; + xytheta[i*xythetaDim+2] = trans.theta; + + scale[i*scaleDim] = trans.scaleX; + scale[i*scaleDim+1] = trans.scaleY; + textureRegion[i*textureRegionDim] = toff.tx; textureRegion[i*textureRegionDim+1] = toff.ty; textureRegion[i*textureRegionDim+2] = toff.lx; textureRegion[i*textureRegionDim+3] = toff.ly; + textureOptions[i*textureOptionsDim] = float(std::distance(batch.begin(), std::find(batch.begin(), batch.end(), textureIndex))); textureOptions[i*textureOptionsDim+1] = alpha; @@ -84,16 +89,28 @@ namespace jGL::GL glBindVertexArray(vao); - glBindBuffer(GL_ARRAY_BUFFER, a_offset); + glBindBuffer(GL_ARRAY_BUFFER, a_xytheta); glBufferSubData ( GL_ARRAY_BUFFER, 0, - offsetDim*batchSize*sizeof(float), - &offsets[current*offsetDim] + xythetaDim*batchSize*sizeof(float), + &xytheta[current*xythetaDim] ); glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, a_scale); + + glBufferSubData + ( + GL_ARRAY_BUFFER, + 0, + scaleDim*batchSize*sizeof(float), + &scale[current*scaleDim] + ); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, a_textureRegion); glBufferSubData @@ -144,7 +161,8 @@ namespace jGL::GL void glSpriteRenderer::freeGL() { glDeleteBuffers(1, &a_position); - glDeleteBuffers(1, &a_offset); + glDeleteBuffers(1, &a_xytheta); + glDeleteBuffers(1, &a_scale); glDeleteBuffers(1, &a_textureRegion); glDeleteBuffers(1, &a_textureOption); glDeleteVertexArrays(1, &vao); @@ -154,7 +172,8 @@ namespace jGL::GL { glGenVertexArrays(1, &vao); glGenBuffers(1, &a_position); - glGenBuffers(1, &a_offset); + glGenBuffers(1, &a_xytheta); + glGenBuffers(1, &a_scale); glGenBuffers(1, &a_textureRegion); glGenBuffers(1, &a_textureOption); @@ -183,27 +202,51 @@ namespace jGL::GL glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, a_offset); + glBindBuffer(GL_ARRAY_BUFFER, a_xytheta); + + glBufferData + ( + GL_ARRAY_BUFFER, + sizeof(float)*xytheta.size(), + xytheta.data(), + GL_DYNAMIC_DRAW + ); + + glEnableVertexAttribArray(xythetaAttribute); + glVertexAttribPointer + ( + xythetaAttribute, + xythetaDim, + GL_FLOAT, + false, + xythetaDim*sizeof(float), + 0 + ); + glVertexAttribDivisor(xythetaAttribute, 1); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glBindBuffer(GL_ARRAY_BUFFER, a_scale); glBufferData ( GL_ARRAY_BUFFER, - sizeof(float)*offsets.size(), - offsets.data(), + sizeof(float)*scale.size(), + scale.data(), GL_DYNAMIC_DRAW ); - glEnableVertexAttribArray(1); + glEnableVertexAttribArray(scaleAttribute); glVertexAttribPointer ( - 1, - offsetDim, + scaleAttribute, + scaleDim, GL_FLOAT, false, - offsetDim*sizeof(float), + scaleDim*sizeof(float), 0 ); - glVertexAttribDivisor(1, 1); + glVertexAttribDivisor(scaleAttribute, 1); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -217,17 +260,17 @@ namespace jGL::GL GL_DYNAMIC_DRAW ); - glEnableVertexAttribArray(2); + glEnableVertexAttribArray(textureRegionAttribute); glVertexAttribPointer ( - 2, + textureRegionAttribute, textureRegionDim, GL_FLOAT, false, textureRegionDim*sizeof(float), 0 ); - glVertexAttribDivisor(2, 1); + glVertexAttribDivisor(textureRegionAttribute, 1); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, a_textureOption); @@ -240,17 +283,17 @@ namespace jGL::GL GL_DYNAMIC_DRAW ); - glEnableVertexAttribArray(3); + glEnableVertexAttribArray(textureOptionsAttribute); glVertexAttribPointer ( - 3, + textureOptionsAttribute, textureOptionsDim, GL_FLOAT, false, textureOptionsDim*sizeof(float), 0 ); - glVertexAttribDivisor(3, 1); + glVertexAttribDivisor(textureOptionsAttribute, 1); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0);