| 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 } |
|