wired renderstyle up

Sat, 01 Feb 2020 15:33:57 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 01 Feb 2020 15:33:57 +0200
changeset 37
90443fb328ea
parent 36
bbb901b97404
child 38
bb60dda3093f

wired renderstyle up

locale/fi.ts file | annotate | diff | comparison | revisions
src/gl/common.h file | annotate | diff | comparison | revisions
src/gl/compiler.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.h file | annotate | diff | comparison | revisions
--- a/locale/fi.ts	Fri Jan 31 00:25:35 2020 +0200
+++ b/locale/fi.ts	Sat Feb 01 15:33:57 2020 +0200
@@ -209,7 +209,7 @@
 <context>
     <name>PartRenderer</name>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="156"/>
+        <location filename="../src/gl/partrenderer.cpp" line="179"/>
         <source>Rendering error</source>
         <translation type="unfinished"></translation>
     </message>
@@ -256,22 +256,22 @@
 <context>
     <name>gl::Compiler</name>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="95"/>
+        <location filename="../src/gl/compiler.cpp" line="112"/>
         <source>Vertex shader:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="100"/>
+        <location filename="../src/gl/compiler.cpp" line="117"/>
         <source>Fragment shader:</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="104"/>
+        <location filename="../src/gl/compiler.cpp" line="121"/>
         <source>Shader compile error</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/gl/compiler.cpp" line="104"/>
+        <location filename="../src/gl/compiler.cpp" line="121"/>
         <source>Could not compile shaders.</source>
         <translation type="unfinished"></translation>
     </message>
--- a/src/gl/common.h	Fri Jan 31 00:25:35 2020 +0200
+++ b/src/gl/common.h	Sat Feb 01 15:33:57 2020 +0200
@@ -141,4 +141,13 @@
 		BfcRedGreen,
 		RandomColors
 	};
+
+	// These are also defined in shaders
+	enum class FragmentStyle
+	{
+		Normal = 0,
+		BfcGreen = 1,
+		BfcRed = 2,
+		RandomColors = 3,
+	};
 }
--- a/src/gl/compiler.cpp	Fri Jan 31 00:25:35 2020 +0200
+++ b/src/gl/compiler.cpp	Sat Feb 01 15:33:57 2020 +0200
@@ -37,12 +37,29 @@
 uniform mat4 modelMatrix;
 uniform mat4 viewMatrix;
 uniform mat4 projectionMatrix;
+uniform int fragmentStyle;
+
+const int FRAGSTYLE_Normal = 0;
+const int FRAGSTYLE_BfcGreen = 1;
+const int FRAGSTYLE_BfcRed = 2;
+const int FRAGSTYLE_Random = 3;
 
 void main()
 {
 	mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
 	vNormal = normalize(normalMatrix * normal);
-	vColor = color;
+	if (fragmentStyle == FRAGSTYLE_BfcGreen)
+	{
+		vColor = vec4(0.2, 0.9, 0.2, 1.0);
+	}
+	else if (fragmentStyle == FRAGSTYLE_BfcRed)
+	{
+		vColor = vec4(0.9, 0.2, 0.2, 1.0);
+	}
+	else
+	{
+		vColor = color;
+	}
 	vFragPos = vec3(modelMatrix * vec4(position, 1.0));
 	gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0);
 }
--- a/src/gl/partrenderer.cpp	Fri Jan 31 00:25:35 2020 +0200
+++ b/src/gl/partrenderer.cpp	Sat Feb 01 15:33:57 2020 +0200
@@ -103,26 +103,49 @@
 	glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	glEnable(GL_DEPTH_TEST);
-	glEnable(GL_LIGHTING);
 	glEnable(GL_POLYGON_OFFSET_FILL);
 	glPolygonOffset(1.0f, 1.0f);
 	switch (this->renderStyle)
 	{
 	case gl::RenderStyle::Normal:
+		this->setFragmentStyle(gl::FragmentStyle::Normal);
+		this->renderAllArrays();
+		break;
 	case gl::RenderStyle::BfcRedGreen:
+		glEnable(GL_CULL_FACE);
+		glCullFace(GL_BACK);
+		this->setFragmentStyle(gl::FragmentStyle::BfcGreen);
+		renderVao(gl::ArrayClass::Triangles);
+		renderVao(gl::ArrayClass::Quads);
+		glCullFace(GL_FRONT);
+		this->setFragmentStyle(gl::FragmentStyle::BfcRed);
+		renderVao(gl::ArrayClass::Triangles);
+		renderVao(gl::ArrayClass::Quads);
+		glDisable(GL_CULL_FACE);
+		this->setFragmentStyle(gl::FragmentStyle::Normal);
+		renderVao(gl::ArrayClass::Lines);
 	case gl::RenderStyle::RandomColors:
+		this->setFragmentStyle(gl::FragmentStyle::RandomColors);
+		this->renderAllArrays();
 		break;
 	case gl::RenderStyle::Wireframe:
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+		this->setFragmentStyle(gl::FragmentStyle::Normal);
+		this->renderAllArrays();
 		break;
 	}
+	glDisable(GL_POLYGON_OFFSET_FILL);
+}
+
+void PartRenderer::renderAllArrays()
+{
 	// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
 	renderVao(gl::ArrayClass::Triangles);
 	renderVao(gl::ArrayClass::Quads);
 	renderVao(gl::ArrayClass::Lines);
-	glDisable(GL_POLYGON_OFFSET_FILL);
 }
 
+
 void PartRenderer::updateViewMatrix()
 {
 	// I'm not quite sure why using the exponent function on the zoom factor causes linear zoom behavior
@@ -188,6 +211,19 @@
 	this->update();
 }
 
+/**
+ * @brief Changes the color of rendered fragments
+ * @param newFragmentStyle new fragment style to use
+ */
+void PartRenderer::setFragmentStyle(gl::FragmentStyle newFragmentStyle)
+{
+	this->compiler->setUniform("fragmentStyle", static_cast<int>(newFragmentStyle));
+}
+
+/**
+ * @brief Changes the way the scene is rendered
+ * @param newStyle new render style to use
+ */
 void PartRenderer::setRenderStyle(const gl::RenderStyle newStyle)
 {
 	this->renderStyle = newStyle;
--- a/src/gl/partrenderer.h	Fri Jan 31 00:25:35 2020 +0200
+++ b/src/gl/partrenderer.h	Sat Feb 01 15:33:57 2020 +0200
@@ -29,6 +29,8 @@
 	void mouseMoveEvent(QMouseEvent* event) override;
 	void wheelEvent(QWheelEvent* event) override;
 private:
+	void setFragmentStyle(gl::FragmentStyle fragStyle);
+	void renderAllArrays();
 	void renderScene();
 	void updateViewMatrix();
 	Model* const model;

mercurial