-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d95c345
commit 768596a
Showing
10 changed files
with
290 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#ifndef GLSPRITERENDERER | ||
#define GLSPRITERENDERER | ||
|
||
#include <jGL/OpenGL/Shader/glShader.h> | ||
#include <jGL/OpenGL/gl.h> | ||
#include <jGL/spriteRenderer.h> | ||
|
||
namespace jGL::GL | ||
{ | ||
class glSpriteRenderer : public SpriteRenderer | ||
{ | ||
public: | ||
|
||
glSpriteRenderer(size_t sizeHint) | ||
: SpriteRenderer(sizeHint) | ||
{ | ||
offsets = std::vector<float>(sizeHint*4+padSprites*4,0.0f); | ||
initGL(); | ||
defaultShader = std::make_shared<glShader>(vertexShader, fragmentShader); | ||
defaultShader->use(); | ||
} | ||
|
||
~glSpriteRenderer() | ||
{ | ||
freeGL(); | ||
} | ||
|
||
void draw(std::shared_ptr<Shader> shader, std::vector<SpriteId> ids); | ||
void draw(std::vector<SpriteId> ids) { draw(defaultShader, ids); } | ||
|
||
private: | ||
|
||
GLuint vao, a_position, a_offset; | ||
|
||
float quad[6*4] = | ||
{ | ||
// positions / texture coords | ||
0.5f, 0.5f, 1.0f, 1.0f, // top right | ||
0.5f, -0.5f, 1.0f, 0.0f, // bottom right | ||
-0.5f, -0.5f, 0.0f, 0.0f, // bottom left | ||
-0.5f, 0.5f, 0.0f, 1.0f, // top left | ||
-0.5f, -0.5f, 0.0f, 0.0f, // bottom left | ||
0.5f, 0.5f, 1.0f, 1.0f // top right | ||
}; | ||
|
||
std::vector<float> offsets; // offset x, y, theta, scale | ||
|
||
size_t padSprites = 64; | ||
|
||
void initGL(); | ||
void freeGL(); | ||
|
||
std::shared_ptr<Shader> defaultShader; | ||
|
||
const char * vertexShader = | ||
"#version " GLSL_VERSION "\n" | ||
"precision lowp float;\n" | ||
"in vec4 a_position;\n" | ||
"in vec4 a_offset;\n" | ||
"uniform mat4 proj;\n" | ||
"out vec2 texCoord;\n" | ||
"void main(void){" | ||
"vec2 pos = a_position.xy*a_offset.w;\n" | ||
"float ct = cos(a_offset.z); float st = sin(a_offset.z);\n" | ||
"mat2 rot = mat2(ct, -st, st, ct);\n" | ||
"pos = rot*pos + a_offset.xy;\n" | ||
"gl_Position = proj*vec4(pos,0.0,1.0);\n" | ||
"texCoord = a_position.zw;\n" | ||
"}"; | ||
|
||
const char * fragmentShader = | ||
"#version " GLSL_VERSION "\n" | ||
"precision lowp float;\n" | ||
"uniform sampler2D sampler;\n" | ||
"in vec2 texCoord;\n" | ||
"out vec4 colour;\n" | ||
"void main(void){\n" | ||
"colour = texture(sampler, texCoord);\n" | ||
"}"; | ||
|
||
}; | ||
} | ||
|
||
#endif /* GLSPRITERENDERER */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef VKSPRITERENDERER | ||
#define VKSPRITERENDERER | ||
|
||
#include <jGL/Vulkan/Device/device.h> | ||
#include <jGL/spriteRenderer.h> | ||
|
||
namespace jGL::Vulkan | ||
{ | ||
class vkSpriteRenderer : public SpriteRenderer | ||
{ | ||
public: | ||
|
||
vkSpriteRenderer(size_t sizeHint) | ||
: SpriteRenderer(sizeHint) | ||
{} | ||
|
||
void draw(glm::mat4 proj) {TODO("jGL::Vulkan::vkSprite::draw");} | ||
|
||
}; | ||
} | ||
|
||
#endif /* VKSPRITERENDERER */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,8 @@ namespace jGL | |
return v; | ||
} | ||
|
||
virtual void use() = 0; | ||
|
||
protected: | ||
|
||
std::string vertex; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef SPRITERENDERER | ||
#define SPRITERENDERER | ||
|
||
#include <jGL/sprite.h> | ||
#include <jGL/shader.h> | ||
|
||
#include <unordered_map> | ||
#include <string> | ||
#include <iterator> | ||
#include <vector> | ||
|
||
namespace jGL | ||
{ | ||
typedef std::string SpriteId; | ||
|
||
class SpriteRenderer | ||
{ | ||
|
||
public: | ||
|
||
SpriteRenderer(size_t sizeHint = 64) | ||
{ | ||
sprites.reserve(sizeHint); | ||
} | ||
|
||
virtual void update(Transform tra, SpriteId id); | ||
|
||
virtual void setTexture(std::shared_ptr<Texture> tex, SpriteId id); | ||
|
||
virtual void draw(std::shared_ptr<Shader> shader, std::vector<SpriteId> ids) = 0; | ||
|
||
virtual void add(Sprite s, SpriteId id) { sprites[id] = s;} | ||
|
||
protected: | ||
|
||
std::unordered_map<SpriteId, Sprite> sprites; | ||
|
||
}; | ||
} | ||
|
||
#endif /* SPRITERENDERER */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include <jGL/OpenGL/glSpriteRenderer.h> | ||
|
||
namespace jGL::GL | ||
{ | ||
void glSpriteRenderer::draw(std::shared_ptr<Shader> shader, std::vector<SpriteId> ids) | ||
{ | ||
uint32_t n = ids.size(); | ||
|
||
if (offsets.size() < 4*n) | ||
{ | ||
offsets.resize(4*n+padSprites*4); | ||
freeGL(); | ||
initGL(); | ||
} | ||
|
||
for (unsigned i = 0; i < n; i++) | ||
{ | ||
const Transform & trans = sprites[ids[i]].getTransform(); | ||
offsets[i*4] = trans.x; | ||
offsets[i*4+1] = trans.y; | ||
offsets[i*4+2] = trans.theta; | ||
offsets[i*4+3] = trans.scale; | ||
} | ||
|
||
shader->use(); | ||
|
||
glBindVertexArray(vao); | ||
|
||
glBufferSubData | ||
( | ||
GL_ARRAY_BUFFER, | ||
0, | ||
4*n*sizeof(float), | ||
&offsets[0] | ||
); | ||
|
||
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, n); | ||
|
||
glBindVertexArray(0); | ||
|
||
} | ||
|
||
void glSpriteRenderer::freeGL() | ||
{ | ||
glDeleteBuffers(1, &a_position); | ||
glDeleteBuffers(1, &a_offset); | ||
glDeleteVertexArrays(1, &vao); | ||
} | ||
|
||
void glSpriteRenderer::initGL() | ||
{ | ||
glGenVertexArrays(1, &vao); | ||
glGenBuffers(1, &a_position); | ||
glGenBuffers(1, &a_offset); | ||
|
||
glBindVertexArray(vao); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, a_position); | ||
|
||
glBufferData | ||
( | ||
GL_ARRAY_BUFFER, | ||
sizeof(quad), | ||
&quad[0], | ||
GL_STATIC_DRAW | ||
); | ||
glEnableVertexAttribArray(0); | ||
glVertexAttribPointer | ||
( | ||
0, | ||
4, | ||
GL_FLOAT, | ||
false, | ||
4*sizeof(float), | ||
0 | ||
); | ||
glVertexAttribDivisor(0,0); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, a_offset); | ||
|
||
glBufferData | ||
( | ||
GL_ARRAY_BUFFER, | ||
sizeof(float)*offsets.size(), | ||
offsets.data(), | ||
GL_DYNAMIC_DRAW | ||
); | ||
|
||
glEnableVertexAttribArray(1); | ||
glVertexAttribPointer | ||
( | ||
1, | ||
4, | ||
GL_FLOAT, | ||
false, | ||
4*sizeof(float), | ||
0 | ||
); | ||
glVertexAttribDivisor(1, 1); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
|
||
|
||
glBindVertexArray(0); | ||
} | ||
} |
Oops, something went wrong.