src/gl/gridprogram.cpp

changeset 70
f21b800b02a4
parent 64
f99d52b1646b
child 72
7c27cda03747
equal deleted inserted replaced
69:a36913fc552a 70:f21b800b02a4
62 } 62 }
63 )"; 63 )";
64 64
65 static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; 65 static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
66 66
67 GridProgram::GridProgram(QObject* parent) :
68 QObject{parent},
69 buffer{QOpenGLBuffer::VertexBuffer},
70 vertexShader{QOpenGLShader::Vertex},
71 fragmentShader{QOpenGLShader::Fragment}
72 {
73 }
74
75 void GridProgram::initialize()
76 {
77 if (not isInitialized)
78 {
79 this->initializeOpenGLFunctions();
80 this->isInitialized = true;
81 this->program.emplace(this);
82 gl::buildShaders(&*this->program, ::vertexShaderSource, ::fragmentShaderSource);
83 this->program->bind();
84 this->buffer.create();
85 this->buffer.bind();
86 this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
87 this->buffer.allocate(data, countof(data) * sizeof data[0]);
88 this->vertexArrayObject.create();
89 this->vertexArrayObject.bind();
90 this->program->enableAttributeArray(0);
91 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
92 this->program->setUniformVector("gridColor", this->gridColor);
93 this->vertexArrayObject.release();
94 this->buffer.release();
95 this->program->release();
96 this->checkForGLErrors();
97 }
98 }
99
100 void GridProgram::setViewMatrix(const glm::mat4& newViewMatrix)
101 {
102 this->setMatrix("view", newViewMatrix);
103 }
104
105 void GridProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix)
106 {
107 this->setMatrix("projection", newProjectionMatrix);
108 }
109
110 void GridProgram::setModelMatrix(const glm::mat4& newModelMatrix)
111 {
112 this->setMatrix("model", newModelMatrix);
113 }
114
115 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) 67 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix)
116 { 68 {
117 this->setMatrix("grid", newGridMatrix); 69 this->setMatrix("grid", newGridMatrix);
118 }
119
120 void GridProgram::setMatrix(const char* name, const glm::mat4& matrix)
121 {
122 Q_ASSERT(this->isInitialized);
123 this->program->bind();
124 this->program->setUniformMatrix(name, matrix);
125 this->program->release();
126 this->checkForGLErrors();
127 } 70 }
128 71
129 void GridProgram::setGridColor(const QColor& newGridColor) 72 void GridProgram::setGridColor(const QColor& newGridColor)
130 { 73 {
131 const glm::vec4 vec = gl::colorToVector4(newGridColor); 74 const glm::vec4 vec = gl::colorToVector4(newGridColor);
140 { 83 {
141 this->gridColor = vec; 84 this->gridColor = vec;
142 } 85 }
143 } 86 }
144 87
145 void GridProgram::draw() 88 const char* GridProgram::vertexShaderSource() const
146 { 89 {
147 this->program->bind(); 90 return ::vertexShaderSource;
148 this->vertexArrayObject.bind();
149 glDrawArrays(GL_QUADS, 0, countof(data));
150 this->vertexArrayObject.release();
151 this->program->release();
152 this->checkForGLErrors();
153 } 91 }
154 92
155 void GridProgram::teardown() 93 const char* GridProgram::fragmentShaderSource() const
156 { 94 {
157 this->vertexArrayObject.destroy(); 95 return ::fragmentShaderSource;
158 this->buffer.destroy();
159 this->program.reset();
160 } 96 }
161 97
162 void GridProgram::checkForGLErrors() 98 const void* GridProgram::vertexData() const
163 { 99 {
164 gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent())); 100 return ::data;
165 } 101 }
102
103 int GridProgram::vertexSize() const
104 {
105 return sizeof data[0];
106 }
107
108 int GridProgram::vertexCount() const
109 {
110 return countof(data);
111 }
112
113 void GridProgram::setupVertexArrays()
114 {
115 this->program->enableAttributeArray(0);
116 this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
117 this->program->setUniformVector("gridColor", this->gridColor);
118 }
119
120 GLenum GridProgram::drawMode() const
121 {
122 return GL_QUADS;
123 }

mercurial