jGL
Loading...
Searching...
No Matches
glSpriteRenderer.h
Go to the documentation of this file.
1#ifndef GLSPRITERENDERER_H
2#define GLSPRITERENDERER_H
3
5#include <jGL/OpenGL/gl.h>
7
8namespace jGL::GL
9{
15 {
16 public:
17
18 glSpriteRenderer(size_t sizeHint)
19 : SpriteRenderer(sizeHint)
20 {
21 xytheta = std::vector<float>(sizeHint*xythetaDim+padSprites*xythetaDim,0.0f);
22 scale = std::vector<float>(sizeHint*scaleDim+padSprites*scaleDim,0.0f);
23 textureRegion = std::vector<float>(sizeHint*textureRegionDim+padSprites*textureRegionDim,0.0f);
24 textureOptions = std::vector<float>(sizeHint*textureOptionsDim+padSprites*textureOptionsDim,0.0f);
25 initGL();
26 shader = std::make_shared<glShader>(vertexShader, fragmentShader);
27 shader->use();
28 }
29
31 {
32 freeGL();
33 }
34
35 private:
36
37 void draw
38 (
39 std::shared_ptr<Shader> shader,
40 std::vector<std::pair<Info, Sprite>> & sprites
41 );
42
43 GLuint vao, a_position, a_xytheta, a_scale, a_textureRegion, a_textureOption;
44
45 float quad[6*4] =
46 {
47 // positions / texture coords
48 0.5f, 0.5f, 1.0f, 1.0f, // top right
49 0.5f, -0.5f, 1.0f, 0.0f, // bottom right
50 -0.5f, -0.5f, 0.0f, 0.0f, // bottom left
51 -0.5f, 0.5f, 0.0f, 1.0f, // top left
52 -0.5f, -0.5f, 0.0f, 0.0f, // bottom left
53 0.5f, 0.5f, 1.0f, 1.0f // top right
54 };
55
56 std::vector<float> xytheta;
57 size_t xythetaDim = 3;
58 size_t xythetaAttribute = 1;
59
60 std::vector<float> scale;
61 size_t scaleDim = 2;
62 size_t scaleAttribute = 2;
63
64 std::vector<float> textureRegion;
65 size_t textureRegionDim = 4;
66 size_t textureRegionAttribute = 3;
67
68 std::vector<float> textureOptions; // texture unit, alpha
69 size_t textureOptionsDim = 2;
70 size_t textureOptionsAttribute = 4;
71
72 size_t padSprites = 8;
73
74 void initGL();
75 void freeGL();
76
77 const char * vertexShader =
78 "#version " GLSL_VERSION "\n"
79 "precision lowp float; precision lowp int;\n"
80 "layout(location=0) in vec4 a_position;\n"
81 "layout(location=1) in vec3 a_xytheta;\n"
82 "layout(location=2) in vec2 a_scale;\n"
83 "layout(location=3) in vec4 a_textureOffset;\n"
84 "layout(location=4) in vec2 a_textureOptions;\n"
85 "uniform mat4 proj;\n"
86 "out vec2 texCoord;\n"
87 "flat out float alpha;\n"
88 "flat out int tex;\n"
89 "void main(){"
90 "vec2 pos = a_position.xy*a_scale;\n"
91 "float ct = cos(a_xytheta.z); float st = sin(a_xytheta.z);\n"
92 "mat2 rot = mat2(ct, -st, st, ct);\n"
93 "pos = rot*pos + a_xytheta.xy;\n"
94 "gl_Position = proj*vec4(pos,0.0,1.0);\n"
95 "texCoord.x = (a_position.z * a_textureOffset.z)+a_textureOffset.x;\n"
96 "texCoord.y = (a_position.w * a_textureOffset.w)+a_textureOffset.y;\n"
97 "tex = int(a_textureOptions.x);\n"
98 "alpha = a_textureOptions.y;\n"
99 "}";
100
101 const char * fragmentShader =
102 "#version " GLSL_VERSION "\n"
103 "precision lowp float; precision lowp int;\n"
104 "uniform sampler2D sampler0;\n"
105 "uniform sampler2D sampler1;\n"
106 "uniform sampler2D sampler2;\n"
107 "uniform sampler2D sampler3;\n"
108 "in vec2 texCoord;\n"
109 "flat in float alpha;\n"
110 "flat in int tex;\n"
111 "layout(location=0) out vec4 colour;\n"
112 "void main(){\n"
113 // is this mental?
114 "if (tex == 0) {colour = texture(sampler0, texCoord);}\n"
115 "else if (tex == 1) {colour = texture(sampler1, texCoord);}\n"
116 "else if (tex == 2) {colour = texture(sampler2, texCoord);}\n"
117 "else if (tex == 3) {colour = texture(sampler3, texCoord);}\n"
118 "else {colour = vec4(0.0,0.0,0.0,alpha);}\n"
119 "colour.a = colour.a*alpha;\n"
120 ";\n"
121 "}";
122
123 };
124}
125
126#endif /* GLSPRITERENDERER_H */
Opengl implementation of SpriteRenderer.
Definition glSpriteRenderer.h:15
~glSpriteRenderer()
Definition glSpriteRenderer.h:30
glSpriteRenderer(size_t sizeHint)
Definition glSpriteRenderer.h:18
Renders sprites in batches, with optional render priority.
Definition spriteRenderer.h:31
virtual void draw()
Draw with default shader and priority.
Definition spriteRenderer.h:97
std::shared_ptr< Shader > shader
Definition spriteRenderer.h:109
Definition gl.h:41