Skip to content

Commit

Permalink
Transform now has scale in x and y
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Aug 3, 2024
1 parent 75e43cc commit 6c3b0a8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/Shape/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
39 changes: 24 additions & 15 deletions include/jGL/OpenGL/glSpriteRenderer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef GLSPRITERENDERER
#define GLSPRITERENDERER
#ifndef GLSPRITERENDERER_H
#define GLSPRITERENDERER_H

#include <jGL/OpenGL/Shader/glShader.h>
#include <jGL/OpenGL/gl.h>
Expand All @@ -18,7 +18,8 @@ namespace jGL::GL
glSpriteRenderer(size_t sizeHint)
: SpriteRenderer(sizeHint)
{
offsets = std::vector<float>(sizeHint*offsetDim+padSprites*offsetDim,0.0f);
xytheta = std::vector<float>(sizeHint*xythetaDim+padSprites*xythetaDim,0.0f);
scale = std::vector<float>(sizeHint*scaleDim+padSprites*scaleDim,0.0f);
textureRegion = std::vector<float>(sizeHint*textureRegionDim+padSprites*textureRegionDim,0.0f);
textureOptions = std::vector<float>(sizeHint*textureOptionsDim+padSprites*textureOptionsDim,0.0f);
initGL();
Expand All @@ -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] =
{
Expand All @@ -49,14 +50,21 @@ namespace jGL::GL
0.5f, 0.5f, 1.0f, 1.0f // top right
};

std::vector<float> offsets; // offset x, y, theta, scale
size_t offsetDim = 4;
std::vector<float> xytheta;
size_t xythetaDim = 3;
size_t xythetaAttribute = 1;

std::vector<float> textureRegion; // tx, ty (region coords), lx, ly (region scale)
std::vector<float> scale;
size_t scaleDim = 2;
size_t scaleAttribute = 2;

std::vector<float> textureRegion;
size_t textureRegionDim = 4;

size_t textureRegionAttribute = 3;

std::vector<float> textureOptions; // texture unit, alpha
size_t textureOptionsDim = 2;
size_t textureOptionsAttribute = 4;

size_t padSprites = 8;

Expand All @@ -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"
Expand Down Expand Up @@ -113,4 +122,4 @@ namespace jGL::GL
};
}

#endif /* GLSPRITERENDERER */
#endif /* GLSPRITERENDERER_H */
11 changes: 8 additions & 3 deletions include/jGL/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/jGL/sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
4 changes: 2 additions & 2 deletions src/jGL/OpenGL/glShapeRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
93 changes: 68 additions & 25 deletions src/jGL/OpenGL/glSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 6c3b0a8

Please sign in to comment.