Skip to content

Commit

Permalink
g# This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
set projection in example

must rebind textures before draw, redesign it
  • Loading branch information
Jerboa-app authored and harveydevereux committed Nov 2, 2023
1 parent b91ca9b commit 8679baf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 66 deletions.
47 changes: 7 additions & 40 deletions examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,26 @@ int main(int argv, char ** argc)

std::shared_ptr<jGL::Texture> heart = jGLInstance->createTexture
(
"resource/texture/HEART.png",
"resource/texture/saturn.png",
jGL::Texture::Type::RGBA
);

heart->bind(0);

std::shared_ptr<jGL::Texture> Pi = jGLInstance->createTexture
(
"resource/texture/Pi.png",
jGL::Texture::Type::RGBA
);

Pi->bind(1);

std::shared_ptr<jGL::Texture> random = jGLInstance->createTexture
(
"resource/texture/random.png",
jGL::Texture::Type::RGBA
);

random->bind(2);

std::shared_ptr<jGL::SpriteRenderer> sprites = jGLInstance->createSpriteRenderer
(
3
1
);

sprites->setProjection(camera.getVP());

sprites->add
(
{
jGL::Transform(0.0f, 0.0f, 0.0f, 1.0f),
jGL::Transform(0.5f, 0.5f, 0.0f, 0.5f),
jGL::TextureOffset(0.0f, 0.0f, 0)
},
"sHeart"
);

sprites->add
(
{
jGL::Transform(0.0f, 0.0f, 0.0f, 1.0f),
jGL::TextureOffset(0.0f, 0.0f, 0)
},
"sPi"
);

sprites->add
(
{
jGL::Transform(0.0f, 0.0f, 0.0f, 1.0f),
jGL::TextureOffset(0.0f, 0.0f, 0)
},
"sRandom"
);

double delta = 0.0;

while (display.isOpen())
Expand All @@ -88,7 +54,8 @@ int main(int argv, char ** argc)

jGLInstance->clear();

sprites->draw({"sHeart", "sPi", "sRandom"});
heart->bind(0);
sprites->draw({"sHeart"});

delta = 0.0;
for (int n = 0; n < 60; n++)
Expand Down
Binary file added examples/Sprite/resource/texture/saturn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions include/jGL/OpenGL/glSpriteRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace jGL::GL
offsets = std::vector<float>(sizeHint*4+padSprites*4,0.0f);
textureOffsets = std::vector<float>(sizeHint*3+padSprites*3,0.0f);
initGL();
defaultShader = std::make_shared<glShader>(vertexShader, fragmentShader);
defaultShader = std::static_pointer_cast<Shader>
(
std::make_shared<glShader>(vertexShader, fragmentShader)
);
defaultShader->use();
}

Expand Down Expand Up @@ -45,8 +48,7 @@ namespace jGL::GL
};

std::vector<float> offsets; // offset x, y, theta, scale
std::vector<float> textureOffsets; // tx, ty (atlas coords)
std::vector<uint8_t> textureUnits; // texture unit
std::vector<float> textureOffsets; // tx, ty (atlas coords), texture unit

size_t padSprites = 8;

Expand Down
2 changes: 1 addition & 1 deletion include/jGL/spriteRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace jGL

public:

SpriteRenderer(size_t sizeHint = 64)
SpriteRenderer(size_t sizeHint = 8)
{
sprites.reserve(sizeHint);
}
Expand Down
54 changes: 32 additions & 22 deletions src/jGL/OpenGL/glSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,46 @@ namespace jGL::GL
shader->use();

shader->setUniform<glm::mat4>("proj", projection);
shader->setUniform<Sampler2D>("sampler", 0);
shader->setUniform<Sampler2D>("sampler", Sampler2D(0));

glBindVertexArray(vao);

glBindBuffer(GL_ARRAY_BUFFER, a_offset);
glBufferSubData
(
GL_ARRAY_BUFFER,
0,
4*n*sizeof(float),
&offsets[0]
);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, a_textureOffset);
glBindBuffer(GL_ARRAY_BUFFER, a_offset);
glBufferSubData
(
GL_ARRAY_BUFFER,
0,
4*n*sizeof(float),
&offsets[0]
);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, a_textureOffset);

glBufferSubData
(
GL_ARRAY_BUFFER,
0,
3*n*sizeof(float),
&textureOffsets[0]
);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glBufferSubData
(
GL_ARRAY_BUFFER,
0,
3*n*sizeof(float),
&textureOffsets[0]
);
glBindVertexArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao);

glDrawArraysInstanced(GL_TRIANGLES, 0, 6, n);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);

glDrawArraysInstanced(GL_TRIANGLES, 0, 6, n);

glBindVertexArray(0);

glError("sprite draw");

}

void glSpriteRenderer::freeGL()
Expand All @@ -84,7 +94,7 @@ namespace jGL::GL
glBufferData
(
GL_ARRAY_BUFFER,
sizeof(quad),
sizeof(float)*6*4,
&quad[0],
GL_STATIC_DRAW
);
Expand Down

0 comments on commit 8679baf

Please sign in to comment.