src/gl/axesprogram.cpp

changeset 215
34c6e7bc4ee1
parent 118
8e1c9f18ae15
child 216
c7241f504117
equal deleted inserted replaced
214:8e1fe64ce4e3 215:34c6e7bc4ee1
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #include "axesprogram.h" 19 #include "axesprogram.h"
20 20
21 const char vertexShaderSource[] = R"( 21 static constexpr char vertexShaderSource[] = R"(
22 #version 330 core 22 #version 330 core
23 23
24 layout (location = 0) in vec3 in_position; 24 layout (location = 0) in vec3 in_position;
25 layout (location = 1) in vec3 in_color; 25 layout (location = 1) in vec3 in_color;
26 uniform mat4 view; 26 uniform mat4 mvp;
27 uniform mat4 projection;
28 uniform mat4 model;
29 smooth out vec3 ex_color; 27 smooth out vec3 ex_color;
30 28
31 void main() 29 void main()
32 { 30 {
33 gl_Position = projection * view * model * vec4(in_position, 1.0); 31 gl_Position = mvp * vec4(in_position, 1.0);
34 ex_color = in_color; 32 ex_color = in_color;
35 } 33 }
36 )"; 34 )";
37 35
38 const char fragmentShaderSource[] = R"( 36 static constexpr char fragmentShaderSource[] = R"(
39 #version 330 core 37 #version 330 core
40 38
41 out vec4 color; 39 out vec4 color;
42 smooth in vec3 ex_color; 40 smooth in vec3 ex_color;
43 41
45 { 43 {
46 color = vec4(ex_color, 1); 44 color = vec4(ex_color, 1);
47 } 45 }
48 )"; 46 )";
49 47
50 namespace 48 void AxesLayer::initializeGL()
51 { 49 {
52 struct AxesVertex 50 constexpr struct VertexType
53 { 51 {
54 glm::vec3 position; 52 glm::vec3 position;
55 glm::vec3 color; 53 glm::vec3 color;
54 } data[] = {
55 {{10000, 0, 0}, {1, 0, 0}},
56 {{0, 0, 0}, {1, 0, 0}},
57 {{-10000, 0, 0}, {0.5, 0, 0}},
58 {{0, 0, 0}, {0.5, 0, 0}},
59 {{0, 10000, 0}, {0, 1, 0}},
60 {{0, 0, 0}, {0, 1, 0}},
61 {{0, -10000, 0}, {0, 0.5, 0}},
62 {{0, 0, 0}, {0, 0.5, 0}},
63 {{0, 0, 10000}, {0, 0, 1}},
64 {{0, 0, 0}, {0, 0, 1}},
65 {{0, 0, -10000}, {0, 0, 0.5}},
66 {{0, 0, 0}, {0, 0, 0.5}},
56 }; 67 };
68 constexpr int stride = sizeof(VertexType);
69 this->shader.initialize(
70 ::vertexShaderSource,
71 ::fragmentShaderSource,
72 QOpenGLBuffer::StaticDraw,
73 {
74 GLAttributeSpec{
75 .type = GL_FLOAT,
76 .offset = offsetof(VertexType, position),
77 .tuplesize = 3,
78 .stride = stride,
79 },
80 {
81 .type = GL_FLOAT,
82 .offset = offsetof(VertexType, color),
83 .tuplesize = 3,
84 .stride = stride,
85 },
86 });
87 this->shader.bufferData(&data[0], countof(data), sizeof data[0]);
57 } 88 }
58 89
59 static const AxesVertex data[] = 90 void AxesLayer::paintGL()
60 { 91 {
61 AxesVertex{{10000, 0, 0}, {1, 0, 0}}, 92 this->shader.draw(GL_LINES);
62 AxesVertex{{0, 0, 0}, {1, 0, 0}},
63 AxesVertex{{-10000, 0, 0}, {0.5, 0, 0}},
64 AxesVertex{{0, 0, 0}, {0.5, 0, 0}},
65 AxesVertex{{0, 10000, 0}, {0, 1, 0}},
66 AxesVertex{{0, 0, 0}, {0, 1, 0}},
67 AxesVertex{{0, -10000, 0}, {0, 0.5, 0}},
68 AxesVertex{{0, 0, 0}, {0, 0.5, 0}},
69 AxesVertex{{0, 0, 10000}, {0, 0, 1}},
70 AxesVertex{{0, 0, 0}, {0, 0, 1}},
71 AxesVertex{{0, 0, -10000}, {0, 0, 0.5}},
72 AxesVertex{{0, 0, 0}, {0, 0, 0.5}},
73 };
74
75 const char* AxesProgram::vertexShaderSource() const
76 {
77 return ::vertexShaderSource;
78 } 93 }
79 94
80 const char* AxesProgram::fragmentShaderSource() const 95 void AxesLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix)
81 { 96 {
82 return ::fragmentShaderSource; 97 this->shader.setMvpMatrix(mvpMatrix);
83 } 98 }
84
85 const void* AxesProgram::vertexData() const
86 {
87 return ::data;
88 }
89
90 GLenum AxesProgram::drawMode() const
91 {
92 return GL_LINES;
93 }
94
95 int AxesProgram::vertexSize() const
96 {
97 return sizeof data[0];
98 }
99
100 int AxesProgram::vertexCount() const
101 {
102 return countof(data);
103 }
104
105 void AxesProgram::setupVertexArrays()
106 {
107 for (int i : {0, 1})
108 {
109 this->program->enableAttributeArray(i);
110 }
111 const int stride = this->vertexSize();
112 this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride);
113 this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride);
114 }
115
116 QOpenGLBuffer::UsagePattern AxesProgram::usagePattern() const
117 {
118 return QOpenGLBuffer::StaticDraw;
119 }

mercurial