src/gl/axesprogram.cpp

changeset 215
34c6e7bc4ee1
parent 118
8e1c9f18ae15
child 216
c7241f504117
--- a/src/gl/axesprogram.cpp	Sun Jun 12 20:47:04 2022 +0300
+++ b/src/gl/axesprogram.cpp	Sun Jun 12 23:59:37 2022 +0300
@@ -18,24 +18,22 @@
 
 #include "axesprogram.h"
 
-const char vertexShaderSource[] = R"(
+static constexpr char vertexShaderSource[] = R"(
 #version 330 core
 
 layout (location = 0) in vec3 in_position;
 layout (location = 1) in vec3 in_color;
-uniform mat4 view;
-uniform mat4 projection;
-uniform mat4 model;
+uniform mat4 mvp;
 smooth out vec3 ex_color;
 
 void main()
 {
-	gl_Position = projection * view * model * vec4(in_position, 1.0);
+	gl_Position = mvp * vec4(in_position, 1.0);
 	ex_color = in_color;
 }
 )";
 
-const char fragmentShaderSource[] = R"(
+static constexpr char fragmentShaderSource[] = R"(
 #version 330 core
 
 out vec4 color;
@@ -47,73 +45,54 @@
 }
 )";
 
-namespace
+void AxesLayer::initializeGL()
 {
-	struct AxesVertex
+	constexpr struct VertexType
 	{
 		glm::vec3 position;
 		glm::vec3 color;
+	} data[] = {
+		{{10000, 0, 0}, {1, 0, 0}},
+		{{0, 0, 0}, {1, 0, 0}},
+		{{-10000, 0, 0}, {0.5, 0, 0}},
+		{{0, 0, 0}, {0.5, 0, 0}},
+		{{0, 10000, 0}, {0, 1, 0}},
+		{{0, 0, 0}, {0, 1, 0}},
+		{{0, -10000, 0}, {0, 0.5, 0}},
+		{{0, 0, 0}, {0, 0.5, 0}},
+		{{0, 0, 10000}, {0, 0, 1}},
+		{{0, 0, 0}, {0, 0, 1}},
+		{{0, 0, -10000}, {0, 0, 0.5}},
+		{{0, 0, 0}, {0, 0, 0.5}},
 	};
-}
-
-static const AxesVertex data[] =
-{
-	AxesVertex{{10000, 0, 0}, {1, 0, 0}},
-	AxesVertex{{0, 0, 0}, {1, 0, 0}},
-	AxesVertex{{-10000, 0, 0}, {0.5, 0, 0}},
-	AxesVertex{{0, 0, 0}, {0.5, 0, 0}},
-	AxesVertex{{0, 10000, 0}, {0, 1, 0}},
-	AxesVertex{{0, 0, 0}, {0, 1, 0}},
-	AxesVertex{{0, -10000, 0}, {0, 0.5, 0}},
-	AxesVertex{{0, 0, 0}, {0, 0.5, 0}},
-	AxesVertex{{0, 0, 10000}, {0, 0, 1}},
-	AxesVertex{{0, 0, 0}, {0, 0, 1}},
-	AxesVertex{{0, 0, -10000}, {0, 0, 0.5}},
-	AxesVertex{{0, 0, 0}, {0, 0, 0.5}},
-};
-
-const char* AxesProgram::vertexShaderSource() const
-{
-	return ::vertexShaderSource;
-}
-
-const char* AxesProgram::fragmentShaderSource() const
-{
-	return ::fragmentShaderSource;
+	constexpr int stride = sizeof(VertexType);
+	this->shader.initialize(
+		::vertexShaderSource,
+		::fragmentShaderSource,
+		QOpenGLBuffer::StaticDraw,
+		{
+			GLAttributeSpec{
+				.type = GL_FLOAT,
+				.offset = offsetof(VertexType, position),
+				.tuplesize = 3,
+				.stride = stride,
+			},
+			{
+				.type = GL_FLOAT,
+				.offset = offsetof(VertexType, color),
+				.tuplesize = 3,
+				.stride = stride,
+			},
+		});
+	this->shader.bufferData(&data[0], countof(data), sizeof data[0]);
 }
 
-const void* AxesProgram::vertexData() const
-{
-	return ::data;
-}
-
-GLenum AxesProgram::drawMode() const
+void AxesLayer::paintGL()
 {
-	return GL_LINES;
-}
-
-int AxesProgram::vertexSize() const
-{
-	return sizeof data[0];
+	this->shader.draw(GL_LINES);
 }
 
-int AxesProgram::vertexCount() const
-{
-	return countof(data);
-}
-
-void AxesProgram::setupVertexArrays()
+void AxesLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix)
 {
-	for (int i : {0, 1})
-	{
-		this->program->enableAttributeArray(i);
-	}
-	const int stride = this->vertexSize();
-	this->program->setAttributeBuffer(0, GL_FLOAT, offsetof(AxesVertex, position), 3, stride);
-	this->program->setAttributeBuffer(1, GL_FLOAT, offsetof(AxesVertex, color), 3, stride);
+	this->shader.setMvpMatrix(mvpMatrix);
 }
-
-QOpenGLBuffer::UsagePattern AxesProgram::usagePattern() const
-{
-	return QOpenGLBuffer::StaticDraw;
-}

mercurial