jGL
Loading...
Searching...
No Matches
glTextRenderer.h
Go to the documentation of this file.
1#ifndef TEXTRENDERER_H
2#define TEXTRENDERER_H
3
4#include <jGL/OpenGL/gl.h>
7
8#include <glm/glm.hpp>
9#include <glm/gtc/matrix_transform.hpp>
10
11namespace jGL::GL
12{
13
15 {
16
17 public:
18
19 TextRenderer(glm::vec2 res);
20
22 {
23 glDeleteBuffers(1,&VBO);
24 glDeleteVertexArrays(1,&VAO);
25 }
26
27 void renderText
28 (
29 glFont font,
30 std::string text,
31 glm::vec2 position,
32 float scale,
33 glm::vec4 colour,
34 glm::vec2 res,
35 glm::bvec2 centre = glm::bvec2(false,false)
36 );
37
38 void setProjection(glm::mat4 p)
39 {
40
41 projection = p;
42
43 shader.use();
44 shader.setUniform<glm::mat4>("proj", projection);
45
46 }
47
48 void clear()
49 {
50 vertices.clear();
51 charactersUploaded = 0;
52 setBufferSize(512);
53 }
54
55 private:
56
57 const char * vert = "#version " GLSL_VERSION "\n"
58 "precision lowp float;\n precision lowp int;\n"
59 "layout(location=0) in vec4 posTex;\n"
60 "uniform mat4 proj;\n"
61 "out vec2 texCoords;\n"
62 "void main()\n"
63 "{\n"
64 " gl_Position = proj * vec4(posTex.xy, 0.0, 1.0);\n"
65 " texCoords = posTex.zw;\n"
66 "}\n";
67
68 const char * frag = "#version " GLSL_VERSION "\n"
69 "precision lowp float;\n precision lowp int;\n"
70 "in vec2 texCoords;\n"
71 "layout(location=0) out vec4 colour;\n"
72 "uniform vec4 textColour;\n"
73 "uniform sampler2D glyph;\n"
74 "void main()\n"
75 "{\n"
76 " vec4 glpyhSample = vec4(1.0,1.0,1.0, texture(glyph,texCoords.xy).r);\n"
77 " colour = textColour*glpyhSample;\n"
78 "}";
79
80 glShader shader;
81 GLuint VAO;
82 GLuint VBO;
83
84 uint16_t charactersUploaded;
85 std::vector<float> vertices;
86
87 glm::mat4 projection;
88 glm::vec2 res;
89
90
91 void setBufferSize(uint16_t s);
92
93 };
94}
95#endif /* TEXTRENDERER_H */
Definition glTextRenderer.h:15
void setProjection(glm::mat4 p)
Definition glTextRenderer.h:38
~TextRenderer()
Definition glTextRenderer.h:21
void clear()
Definition glTextRenderer.h:48
void renderText(glFont font, std::string text, glm::vec2 position, float scale, glm::vec4 colour, glm::vec2 res, glm::bvec2 centre=glm::bvec2(false, false))
Definition glTextRenderer.cpp:60
Definition glFont.h:13
Definition gl.h:41
An OpenGL implementation of Shader.
Definition glShader.h:15
void use()
Use the shader program (and compile if required).
Definition glShader.cpp:119
void setUniform(std::string name, T value)
Set a Uniform to a value.
Definition shader.h:95