TazGraph Project v0.1.0
Loading...
Searching...
No Matches
LineRenderer.h
1#pragma once
2
3#include "../../GLSLProgram.h"
4#include <GL/glew.h>
5#include <glm/glm.hpp>
6#include <vector>
7
8#include "../../Vertex.h"
9
10
11
12
14public:
16
17 }
18 RenderLineBatch(GLuint Offset, GLuint NumIndices) : offset(Offset),
19 numIndices(NumIndices){
20
21 }
22 GLuint offset = 0;
23 GLuint numIndices = 0;
24};
25
26class LineGlyph {
27
28public:
29 LineGlyph() {};
30 LineGlyph(const glm::vec3& fromPosition, const glm::vec3& toPosition, const Color& srcColor, const Color& destColor)
31 {
32
33 fromV.color = srcColor;
34 fromV.setPosition(fromPosition);
35
36 toV.color = destColor;
37 toV.setPosition(toPosition);
38 };
39
40 ColorVertex fromV;
41 ColorVertex toV;
42};
43
45 //todo avoid rotating every point individually, takes a lot of time
46 //todo instead rotate them from transform using a global angle(based on aimpos) that is added to all transforms
47 //todo then rotation in glyph is just taking that transform
48public:
49 SquareGlyph() {};
50 SquareGlyph(const glm::vec4& destRect, const Color& color, float angle, float mdepth)
51 {
52 glm::vec3 atopLeft(destRect.x, destRect.y, mdepth);
53 glm::vec3 abottomLeft(destRect.x, destRect.y + destRect.w, mdepth);
54 glm::vec3 abottomRight(destRect.x + destRect.z, destRect.y + destRect.w, mdepth);
55 glm::vec3 atopRight(destRect.x + destRect.z, destRect.y, mdepth);
56
57 topLeft.color = color;
58 topLeft.setPosition(atopLeft);
59
60 bottomLeft.color = color;
61 bottomLeft.setPosition(abottomLeft);
62
63 bottomRight.color = color;
64 bottomRight.setPosition(abottomRight);
65
66 topRight.color = color;
67 topRight.setPosition(atopRight);
68
69 };
70
71 ColorVertex topLeft;
72 ColorVertex bottomLeft;
73 ColorVertex bottomRight;
74 ColorVertex topRight;
75};
76
77class BoxGlyph {
78
79public:
80 BoxGlyph() {};
81 BoxGlyph(const glm::vec3& origin, const glm::vec3& size, const Color& color, float angle)
82 {
83
84 glm::vec3 atopLeft(origin.x, origin.y, origin.z);
85 glm::vec3 abottomLeft(origin.x, origin.y + size.y, origin.z);
86 glm::vec3 abottomRight(origin.x + size.x, origin.y + size.y, origin.z);
87 glm::vec3 atopRight(origin.x + size.x, origin.y, origin.z);
88
89 a_topLeft.color = color;
90 a_topLeft.setPosition(atopLeft);
91
92 a_bottomLeft.color = color;
93 a_bottomLeft.setPosition(abottomLeft);
94
95 a_bottomRight.color = color;
96 a_bottomRight.setPosition(abottomRight);
97
98 a_topRight.color = color;
99 a_topRight.setPosition(atopRight);
100
101 glm::vec3 btopLeft(origin.x, origin.y, origin.z + size.z);
102 glm::vec3 bbottomLeft(origin.x, origin.y + size.y, origin.z + size.z);
103 glm::vec3 bbottomRight(origin.x + size.x, origin.y + size.y, origin.z + size.z);
104 glm::vec3 btopRight(origin.x + size.x, origin.y, origin.z + size.z);
105
106 b_topLeft.color = color;
107 b_topLeft.setPosition(btopLeft);
108
109 b_bottomLeft.color = color;
110 b_bottomLeft.setPosition(bbottomLeft);
111
112 b_bottomRight.color = color;
113 b_bottomRight.setPosition(bbottomRight);
114
115 b_topRight.color = color;
116 b_topRight.setPosition(btopRight);
117
118 };
119
120 ColorVertex a_topLeft;
121 ColorVertex a_bottomLeft;
122 ColorVertex a_bottomRight;
123 ColorVertex a_topRight;
124
125 ColorVertex b_topLeft;
126 ColorVertex b_bottomLeft;
127 ColorVertex b_bottomRight;
128 ColorVertex b_topRight;
129};
130
132public:
133 const char* VERT_SRC = R"(#version 400
134
135in vec3 vertexPosition; //vec3 is array of 3 floats
136in vec4 vertexColor;
137
138out vec4 fragmentColor;
139
140uniform mat4 u_ViewProjection;
141
142void main() {
143 gl_Position = u_ViewProjection * vec4(vertexPosition.xyz, 1.0);
144
145 fragmentColor = vertexColor;
146})";
147
148 const char* FRAG_SRC = R"(#version 400
149
150in vec4 fragmentColor;
151
152out vec4 color; //rgb value
153
154void main() {
155 color = vec4(fragmentColor.rgb, fragmentColor.a);
156})";
157
158 LineRenderer();
160
161 void init();
162 void begin();
163
164 void end();
165
166 void initBatchLines(size_t msize);
167
168 void initBatchSquares(size_t msize);
169
170 void initBatchBoxes(size_t msize);
171
172 void drawLine(size_t v_index, const glm::vec3 srcPosition, const glm::vec3 destPosition, const Color& srcColor, const Color& destColor);
173 void drawRectangle(size_t v_index, const glm::vec4& destRect, const Color& color, float angle, float zIndex =0.0f);
174 void drawBox(size_t v_index, const glm::vec3& origin, const glm::vec3& size, const Color& color, float angle);
175 void drawCircle(const glm::vec2& center, const Color& color, float radius);
176
177 void initBatchSize();
178 void renderBatch(float lineWidth);
179
180 void dispose();
181
182 int box_edgePairs[12][2] = {
183 {0, 1}, {1, 2}, {2, 3}, {3, 0}, // Bottom face
184 {4, 5}, {5, 6}, {6, 7}, {7, 4}, // Top face
185 {0, 4}, {1, 5}, {2, 6}, {3, 7} // Vertical edges
186 };
187
188private:
189 void createRenderBatches();
190 void createVertexArray();
191
192 GLuint _vbo = 0, _vao = 0, _ibo = 0;
193
194 std::vector<ColorVertex> _vertices;
195 std::vector<GLuint> _indices;
196
197 size_t _lineGlyphs_size = 0;
198 size_t _squareGlyphs_size = 0;
199 size_t _boxGlyphs_size = 0;
200
201 size_t _rectangles_verticesOffset = 0;
202 size_t _lines_verticesOffset = 0;
203 size_t _boxes_verticesOffset= 0;
204
205 size_t _rectangles_indicesOffset = 0;
206 size_t _lines_indicesOffset = 0;
207 size_t _boxes_indicesOffset = 0;
208
209 std::vector<RenderLineBatch> _renderBatches;
210};
Definition LineRenderer.h:77
Definition LineRenderer.h:26
Definition LineRenderer.h:131
Definition LineRenderer.h:13
Definition LineRenderer.h:44
Definition Vertex.h:98
Definition Vertex.h:47