vlod 4 yıl önce
ebeveyn
işleme
700aea48fe

+ 3 - 2
gl3d/main.cpp

@@ -24,7 +24,7 @@ int h = 640;
 
 gl3d::GpuMaterial material = gl3d::GpuMaterial().setDefaultMaterial();
 
-#define USE_GPU_ENGINE 0
+#define USE_GPU_ENGINE 1
 
 #pragma region gpu
 extern "C"
@@ -53,7 +53,7 @@ int main()
 		std::cout << "err initializing glfw";
 	}
 
-	glfwWindowHint(GLFW_SAMPLES, 4);
+	glfwWindowHint(GLFW_SAMPLES, 1);
 
 	//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
 	//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
@@ -615,6 +615,7 @@ int main()
 			renderer.lightShader.normalMap = normalMap;
 			
 			ImGui::Checkbox("SSAO", &renderer.lightShader.useSSAO);
+			ImGui::Checkbox("Bloom", &renderer.lightShader.bloom);
 
 			ImGui::Checkbox("Display item border", &borderItem);
 			ImGui::Checkbox("Display normals", &showNormals);

+ 8 - 5
gl3d/shaders/deferred/geometryPass.frag

@@ -90,10 +90,13 @@ subroutine (GetAlbedoFunc) vec4 sampledAlbedo()
 		if(color.w <= 0.1)
 			discard;
 
-	color.rgb = pow(color.rgb , vec3(2.2,2.2,2.2)).rgb; //gamma corection
-		
-	color *= vec4(mat[u_materialIndex].kd.r, mat[u_materialIndex].kd.g, mat[u_materialIndex].kd.b, 1); //(option) multiply texture by kd
-		
+	//color.rgb = pow(color.rgb , vec3(2.2,2.2,2.2)).rgb; //gamma corection
+	//color *= vec4(mat[u_materialIndex].kd.r, mat[u_materialIndex].kd.g, mat[u_materialIndex].kd.b, 1); //(option) multiply texture by kd
+	//color.rgb = pow(color.rgb, vec3(1.0/2.2)); //back to gama space 
+
+	 //(option) multiply texture by kd, this is a simplified version of the above code
+	color.rgb *= pow( vec3(mat[u_materialIndex].kd.r, mat[u_materialIndex].kd.g, mat[u_materialIndex].kd.b), vec3(1.0/2.2) );
+
 
 	return color;
 }
@@ -102,7 +105,7 @@ subroutine (GetAlbedoFunc) vec4 notSampledAlbedo()
 {
 	vec4 c = vec4(mat[u_materialIndex].kd.r, mat[u_materialIndex].kd.g, mat[u_materialIndex].kd.b, 1);	
 
-	c.rgb = pow(c.rgb , vec3(2.2,2.2,2.2)).rgb;
+	//c.rgb = pow(c.rgb , vec3(2.2,2.2,2.2)).rgb;
 
 	return c;
 }

+ 11 - 12
gl3d/shaders/deferred/lightingPass.frag

@@ -127,7 +127,7 @@ void main()
 	vec3 pos = texture(u_positions, v_texCoord).xyz;
 	vec3 normal = texture(u_normals, v_texCoord).xyz;
 	vec3 albedo = texture(u_albedo, v_texCoord).xyz;
-	//albedo  = pow(albedo , vec3(2.2,2.2,2.2)).rgb; //gamma corection
+	albedo  = pow(albedo , vec3(2.2,2.2,2.2)).rgb; //gamma corection
 
 
 	vec3 material = texture(u_materials, v_texCoord).xyz;
@@ -142,8 +142,8 @@ void main()
 		ssaof = 1;
 	}
 
-	float ssao_ambient = pow(ssaof, 4.3);
-	float ssao_finalColor = pow(ssaof, 2.7);
+	float ssao_ambient = pow(ssaof, 6.3);
+	float ssao_finalColor = pow(ssaof, 6);
 
 
 	vec3 viewDir = normalize(u_eyePosition - pos);
@@ -168,32 +168,31 @@ void main()
 
 	}
 
-	vec3 ambientColor = vec3(0.05);
+	vec3 ambientColor = vec3(0.03);
 	ambientColor.rgb *= ssao_ambient;
 	vec3 ambient = ambientColor * albedo.rgb * material.b; //this value is made up
 	vec3 color   = Lo + ambient; 
 
 	color *= ssao_finalColor;
 
+
+	float lightIntensity = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722));	
+
 	//HDR 
 	//color = color / (color + vec3(1.0));
 	float exposure = 1;
 	color = vec3(1.0) - exp(-color  * exposure);
 	
-	
-
-	//todo move gamma 
-	//gamma correction
-	color = pow(color, vec3(1.0/2.2));
+	//gama correction is done in the post process step
 
 
 	//color.rgb =  material.bbb;
 
-	float lightIntensity = Lo.r + Lo.g + Lo.b;
-	lightIntensity /= 3;
-	if(lightIntensity > 3)
+	if(lightIntensity > 0.9)
 	{
 		a_outBloom = clamp(vec4(color.rgb, 1), 0, 1);
+		a_outColor = clamp(vec4(color.rgb, 1), 0, 1);	
+
 	}else
 	{
 		a_outColor = clamp(vec4(color.rgb, 1), 0, 1);	

+ 3 - 0
gl3d/shaders/postProcess/postProcess.frag

@@ -16,4 +16,7 @@ void main()
 
 	a_color = bloom + color;
 
+	//gamma correction
+	a_color = pow(a_color, vec3(1.0/2.2));
+
 }

+ 2 - 2
gl3d/shaders/ssao/ssao.frag

@@ -14,7 +14,7 @@ uniform mat4 u_view; // camera view matrix
 
 
 const int kernelSize = 64;
-float radius = 0.6;
+float radius = 0.3;
 float bias = 0.025;
 
 void main()
@@ -37,7 +37,7 @@ void main()
 	float occlusion = 0.0;
 
 
-	const int samplesTestSize = 16;
+	const int samplesTestSize = 18;
 	int begin = int((kernelSize - samplesTestSize) * abs(randomVec.x));
 
 	for(int i = begin; i < begin + samplesTestSize; ++i)

+ 1 - 1
gl3d/src/GraphicModel.cpp

@@ -810,7 +810,6 @@ namespace gl3d
 		gl3dAssertComment(indexSize % 3 == 0, "Index count must be multiple of 3");
 		if (indexSize % 3 != 0)return;
 
-
 		glGenVertexArrays(1, &vertexArray);
 		glBindVertexArray(vertexArray);
 
@@ -818,6 +817,7 @@ namespace gl3d
 		glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
 		glBufferData(GL_ARRAY_BUFFER, vertexSize, vercies, GL_STATIC_DRAW);
 
+		//todo if the object doesn't have texture data we should not render any material to it or just refuze to load it
 		if (noTexture)
 		{
 			glEnableVertexAttribArray(0);

+ 1 - 0
gl3d/src/Shader.h

@@ -97,6 +97,7 @@ namespace gl3d
 
 		bool normalMap = 1; 
 		bool useSSAO = 1;
+		bool bloom = 1;
 
 		//todo clear
 	};

+ 125 - 44
gl3d/src/gl3d.cpp

@@ -15,6 +15,7 @@ namespace gl3d
 
 		lightShader.create();
 		skyBox.createGpuData();
+		vao.createVAOs();
 
 		showNormalsProgram.shader.loadShaderProgramFromFile("shaders/showNormals.vert",
 		"shaders/showNormals.geom", "shaders/showNormals.frag");
@@ -105,6 +106,21 @@ namespace gl3d
 
 	}
 
+	void Renderer3D::VAO::createVAOs()
+	{
+		glGenVertexArrays(1, &posNormalTexture);
+		glBindVertexArray(posNormalTexture);
+
+		glEnableVertexAttribArray(0);
+		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
+		glEnableVertexAttribArray(1);
+		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
+		glEnableVertexAttribArray(2);
+		glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
+
+		glBindVertexArray(0);
+
+	}
 	
 	Material Renderer3D::createMaterial(glm::vec3 kd, float roughness, float metallic, float ao
 	, std::string name)
@@ -729,32 +745,31 @@ namespace gl3d
 	{
 		glBindFramebuffer(GL_FRAMEBUFFER, gBuffer.gBuffer);
 
-
 		auto found = std::find(graphicModelsIndexes.begin(), graphicModelsIndexes.end(), o.id_);
-		if(found == graphicModelsIndexes.end())
+		if (found == graphicModelsIndexes.end())
 		{
 			gl3dAssertComment(found == graphicModelsIndexes.end(), "invalid render object");
 			return;
 		}
 		int id = found - graphicModelsIndexes.begin();
-	
-		auto &model = graphicModels[id]; 
-	
-	
+
+		auto &model = graphicModels[id];
+
+
 		if (model.models.empty())
 		{
 			return;
 		}
-	
+
 		auto projMat = camera.getProjectionMatrix();
 		auto viewMat = camera.getWorldToViewMatrix();
 		auto transformMat = gl3d::getTransformMatrix(position, rotation, scale);
-	
+
 		auto modelViewProjMat = projMat * viewMat * transformMat;
 		//auto modelView = viewMat * transformMat;
-	
+
 		lightShader.geometryPassShader.bind();
-	
+
 
 		lightShader.getSubroutines();
 
@@ -769,7 +784,7 @@ namespace gl3d
 		//glUniform1i(lightShader.skyBoxSamplerLocation, 2);
 		glUniform1i(lightShader.RMASamplerLocation, 3);
 
-	
+
 
 		//material buffer
 		glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightShader.materialBlockBuffer);
@@ -777,16 +792,37 @@ namespace gl3d
 			, &materials[0], GL_STREAM_DRAW);
 		glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, lightShader.materialBlockBuffer);
 
+		//glBindVertexArray(vao.posNormalTexture);
+
+		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+		for (auto &i : model.models)
+		{
+			
+			glBindVertexArray(i.vertexArray);
+			//glBindBuffer(GL_ARRAY_BUFFER, i.vertexBuffer);
+
+			if (i.indexBuffer)
+			{
+				//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i.indexBuffer);
+				glDrawElements(GL_TRIANGLES, i.primitiveCount, GL_UNSIGNED_INT, 0);
+			}
+			else
+			{
+				glDrawArrays(GL_TRIANGLES, 0, i.primitiveCount);
+			}
+		}
+
+		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+		glDepthFunc(GL_EQUAL);
 
 		GLsizei n;
 		glGetProgramStageiv(lightShader.geometryPassShader.id,
 		GL_FRAGMENT_SHADER,
 		GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS,
 		&n);
-	
+
 		GLuint *indices = new GLuint[n]{ 0 };
-		bool changed = 1;
-		
+		bool changed = 1;	
 	
 		for (auto &i : model.models)
 		{
@@ -905,13 +941,16 @@ namespace gl3d
 				{
 					glDrawArrays(GL_TRIANGLES, 0, i.primitiveCount);
 				}
-				glBindVertexArray(0);
 			}
 	
 		}
-	
+
+		glBindVertexArray(0);
+
 		delete[] indices;
 	
+		glDepthFunc(GL_LESS);
+
 		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 
 	}
@@ -1119,6 +1158,8 @@ namespace gl3d
 		glBindVertexArray(lightShader.quadVAO);
 		
 	#pragma region ssao
+		glViewport(0, 0, w / 2, h / 2);
+
 		glUseProgram(ssao.shader.id);
 
 		glUniformMatrix4fv(ssao.u_projection, 1, GL_FALSE,
@@ -1145,9 +1186,13 @@ namespace gl3d
 		glUniform1i(ssao.u_texNoise, 2);
 
 		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+	
+		glViewport(0, 0, w, h);
 	#pragma endregion
 
-	#pragma region ssao blur
+	#pragma region ssao "blur" (more like average blur)
+		glViewport(0, 0, w/4, h/4);
+
 		glBindFramebuffer(GL_FRAMEBUFFER, ssao.blurBuffer);
 		ssao.blurShader.bind();
 		glClear(GL_COLOR_BUFFER_BIT);
@@ -1155,6 +1200,8 @@ namespace gl3d
 		glBindTexture(GL_TEXTURE_2D, ssao.ssaoColorBuffer);
 		glUniform1i(ssao.u_ssaoInput, 0);
 		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+		glViewport(0, 0, w, h);
 	#pragma endregion
 
 	#pragma region render into the bloom post processing fbo
@@ -1211,14 +1258,33 @@ namespace gl3d
 	#pragma endregion
 
 	#pragma region bloom blur
-		glBindFramebuffer(GL_FRAMEBUFFER, postProcess.blurFbo);
-		postProcess.gausianBLurShader.bind();
-		glClear(GL_COLOR_BUFFER_BIT);
-		glActiveTexture(GL_TEXTURE0);
-		glBindTexture(GL_TEXTURE_2D, postProcess.colorBuffers[1]);
-		glUniform1i(postProcess.u_toBlurcolorInput, 0);
-		glUniform1i(postProcess.u_horizontal, 1);
-		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+		
+		if(lightShader.bloom)
+		{
+			int blurs = 16;
+			bool horizontal = 1; bool firstTime = 1;
+			postProcess.gausianBLurShader.bind();
+			glActiveTexture(GL_TEXTURE0);
+			glUniform1i(postProcess.u_toBlurcolorInput, 0);
+			glViewport(0, 0, w/2, h/2);
+			for (int i = 0; i < blurs; i++)
+			{
+				glBindFramebuffer(GL_FRAMEBUFFER, postProcess.blurFbo[horizontal]);
+				glClear(GL_COLOR_BUFFER_BIT);
+				glUniform1i(postProcess.u_horizontal, horizontal);
+
+				glBindTexture(GL_TEXTURE_2D,
+					firstTime ? postProcess.colorBuffers[1] : postProcess.bluredColorBuffer[!horizontal]);
+
+				glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+				horizontal = !horizontal;
+				firstTime = false;
+
+			}
+			glViewport(0, 0, w, h);
+
+		}
 
 	#pragma endregion
 
@@ -1235,7 +1301,13 @@ namespace gl3d
 		//bloom data
 		glUniform1i(postProcess.u_bloomTexture, 1);
 		glActiveTexture(GL_TEXTURE1);
-		glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer); //!!!!!!!!!!!!!!1
+		if(lightShader.bloom)
+		{
+			glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer[1]); 
+		}else
+		{
+			glBindTexture(GL_TEXTURE_2D, postProcess.colorBuffers[1]);
+		}
 
 		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
@@ -1284,9 +1356,9 @@ namespace gl3d
 		
 		//ssao
 		glBindTexture(GL_TEXTURE_2D, ssao.ssaoColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/2, h/2, 0, GL_RED, GL_FLOAT, NULL);
 		glBindTexture(GL_TEXTURE_2D, ssao.blurColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/4, h/4, 0, GL_RED, GL_FLOAT, NULL);
 
 		//bloom
 		for (int i = 0; i < 2; i++)
@@ -1295,8 +1367,12 @@ namespace gl3d
 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 		}
 
-		glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+		for(int i=0;i<2;i++)
+		{
+			glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer[i]);
+			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w/2, h/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+		}
+
 
 	}
 
@@ -1356,8 +1432,8 @@ namespace gl3d
 
 		glGenTextures(1, &ssaoColorBuffer);
 		glBindTexture(GL_TEXTURE_2D, ssaoColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/2, h/2, 0, GL_RED, GL_FLOAT, NULL);
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -1383,8 +1459,8 @@ namespace gl3d
 		glBindFramebuffer(GL_FRAMEBUFFER, blurBuffer);
 		glGenTextures(1, &blurColorBuffer);
 		glBindTexture(GL_TEXTURE_2D, blurColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/4, h/4, 0, GL_RED, GL_FLOAT, NULL);
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -1425,17 +1501,22 @@ namespace gl3d
 		u_horizontal = getUniform(gausianBLurShader.id, "u_horizontal");
 
 
-		glGenFramebuffers(1, &blurFbo);
-		glBindFramebuffer(GL_FRAMEBUFFER, blurFbo);
+		glGenFramebuffers(2, blurFbo);
+		glGenTextures(2, bluredColorBuffer);
 
-		glGenTextures(1, &bluredColorBuffer);
-		glBindTexture(GL_TEXTURE_2D, bluredColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, bluredColorBuffer, 0);
+		for(int i=0;i <2; i++)
+		{
+			glBindFramebuffer(GL_FRAMEBUFFER, blurFbo[i]);
+
+			glBindTexture(GL_TEXTURE_2D, bluredColorBuffer[i]);
+			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w/2, h/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, bluredColorBuffer[i], 0);
+		}
+		
 
 		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 

+ 8 - 2
gl3d/src/gl3d.h

@@ -108,7 +108,12 @@ namespace gl3d
 
 	#pragma endregion
 
+		struct VAO
+		{
+			GLuint posNormalTexture;
 
+			void createVAOs();
+		}vao;
 
 		std::vector< GpuMultipleGraphicModel > graphicModels;
 		std::vector<int> graphicModelsIndexes;
@@ -176,9 +181,10 @@ namespace gl3d
 			GLint u_horizontal;
 
 			GLuint fbo;
-			GLuint blurFbo;
+			GLuint blurFbo[2];
+
 			GLuint colorBuffers[2]; // 0 for color, 1 for bloom
-			GLuint bluredColorBuffer;
+			GLuint bluredColorBuffer[2];
 			void create(int w, int h);
 
 		}postProcess;

+ 127 - 46
headerOnly/gl3d.cpp

@@ -1,6 +1,6 @@
 ////////////////////////////////////////////////
 //gl32 --Vlad Luta -- 
-//built on 2021-04-14
+//built on 2021-04-20
 ////////////////////////////////////////////////
 
 #include "gl3d.h"
@@ -1684,7 +1684,6 @@ namespace gl3d
 		gl3dAssertComment(indexSize % 3 == 0, "Index count must be multiple of 3");
 		if (indexSize % 3 != 0)return;
 
-
 		glGenVertexArrays(1, &vertexArray);
 		glBindVertexArray(vertexArray);
 
@@ -1692,6 +1691,7 @@ namespace gl3d
 		glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
 		glBufferData(GL_ARRAY_BUFFER, vertexSize, vercies, GL_STATIC_DRAW);
 
+		//todo if the object doesn't have texture data we should not render any material to it or just refuze to load it
 		if (noTexture)
 		{
 			glEnableVertexAttribArray(0);
@@ -1773,6 +1773,7 @@ namespace gl3d
 
 		lightShader.create();
 		skyBox.createGpuData();
+		vao.createVAOs();
 
 		showNormalsProgram.shader.loadShaderProgramFromFile("shaders/showNormals.vert",
 		"shaders/showNormals.geom", "shaders/showNormals.frag");
@@ -1863,6 +1864,21 @@ namespace gl3d
 
 	}
 
+	void Renderer3D::VAO::createVAOs()
+	{
+		glGenVertexArrays(1, &posNormalTexture);
+		glBindVertexArray(posNormalTexture);
+
+		glEnableVertexAttribArray(0);
+		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
+		glEnableVertexAttribArray(1);
+		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
+		glEnableVertexAttribArray(2);
+		glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
+
+		glBindVertexArray(0);
+
+	}
 	
 	Material Renderer3D::createMaterial(glm::vec3 kd, float roughness, float metallic, float ao
 	, std::string name)
@@ -2487,32 +2503,31 @@ namespace gl3d
 	{
 		glBindFramebuffer(GL_FRAMEBUFFER, gBuffer.gBuffer);
 
-
 		auto found = std::find(graphicModelsIndexes.begin(), graphicModelsIndexes.end(), o.id_);
-		if(found == graphicModelsIndexes.end())
+		if (found == graphicModelsIndexes.end())
 		{
 			gl3dAssertComment(found == graphicModelsIndexes.end(), "invalid render object");
 			return;
 		}
 		int id = found - graphicModelsIndexes.begin();
-	
-		auto &model = graphicModels[id]; 
-	
-	
+
+		auto &model = graphicModels[id];
+
+
 		if (model.models.empty())
 		{
 			return;
 		}
-	
+
 		auto projMat = camera.getProjectionMatrix();
 		auto viewMat = camera.getWorldToViewMatrix();
 		auto transformMat = gl3d::getTransformMatrix(position, rotation, scale);
-	
+
 		auto modelViewProjMat = projMat * viewMat * transformMat;
 		//auto modelView = viewMat * transformMat;
-	
+
 		lightShader.geometryPassShader.bind();
-	
+
 
 		lightShader.getSubroutines();
 
@@ -2527,7 +2542,7 @@ namespace gl3d
 		//glUniform1i(lightShader.skyBoxSamplerLocation, 2);
 		glUniform1i(lightShader.RMASamplerLocation, 3);
 
-	
+
 
 		//material buffer
 		glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightShader.materialBlockBuffer);
@@ -2535,16 +2550,37 @@ namespace gl3d
 			, &materials[0], GL_STREAM_DRAW);
 		glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, lightShader.materialBlockBuffer);
 
+		//glBindVertexArray(vao.posNormalTexture);
+
+		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+		for (auto &i : model.models)
+		{
+			
+			glBindVertexArray(i.vertexArray);
+			//glBindBuffer(GL_ARRAY_BUFFER, i.vertexBuffer);
+
+			if (i.indexBuffer)
+			{
+				//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i.indexBuffer);
+				glDrawElements(GL_TRIANGLES, i.primitiveCount, GL_UNSIGNED_INT, 0);
+			}
+			else
+			{
+				glDrawArrays(GL_TRIANGLES, 0, i.primitiveCount);
+			}
+		}
+
+		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+		glDepthFunc(GL_EQUAL);
 
 		GLsizei n;
 		glGetProgramStageiv(lightShader.geometryPassShader.id,
 		GL_FRAGMENT_SHADER,
 		GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS,
 		&n);
-	
+
 		GLuint *indices = new GLuint[n]{ 0 };
-		bool changed = 1;
-		
+		bool changed = 1;	
 	
 		for (auto &i : model.models)
 		{
@@ -2663,13 +2699,16 @@ namespace gl3d
 				{
 					glDrawArrays(GL_TRIANGLES, 0, i.primitiveCount);
 				}
-				glBindVertexArray(0);
 			}
 	
 		}
-	
+
+		glBindVertexArray(0);
+
 		delete[] indices;
 	
+		glDepthFunc(GL_LESS);
+
 		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 
 	}
@@ -2877,6 +2916,8 @@ namespace gl3d
 		glBindVertexArray(lightShader.quadVAO);
 		
 	#pragma region ssao
+		glViewport(0, 0, w / 2, h / 2);
+
 		glUseProgram(ssao.shader.id);
 
 		glUniformMatrix4fv(ssao.u_projection, 1, GL_FALSE,
@@ -2903,9 +2944,13 @@ namespace gl3d
 		glUniform1i(ssao.u_texNoise, 2);
 
 		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+	
+		glViewport(0, 0, w, h);
 	#pragma endregion
 
-	#pragma region ssao blur
+	#pragma region ssao "blur" (more like average blur)
+		glViewport(0, 0, w/4, h/4);
+
 		glBindFramebuffer(GL_FRAMEBUFFER, ssao.blurBuffer);
 		ssao.blurShader.bind();
 		glClear(GL_COLOR_BUFFER_BIT);
@@ -2913,6 +2958,8 @@ namespace gl3d
 		glBindTexture(GL_TEXTURE_2D, ssao.ssaoColorBuffer);
 		glUniform1i(ssao.u_ssaoInput, 0);
 		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+		glViewport(0, 0, w, h);
 	#pragma endregion
 
 	#pragma region render into the bloom post processing fbo
@@ -2969,14 +3016,33 @@ namespace gl3d
 	#pragma endregion
 
 	#pragma region bloom blur
-		glBindFramebuffer(GL_FRAMEBUFFER, postProcess.blurFbo);
-		postProcess.gausianBLurShader.bind();
-		glClear(GL_COLOR_BUFFER_BIT);
-		glActiveTexture(GL_TEXTURE0);
-		glBindTexture(GL_TEXTURE_2D, postProcess.colorBuffers[1]);
-		glUniform1i(postProcess.u_toBlurcolorInput, 0);
-		glUniform1i(postProcess.u_horizontal, 1);
-		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+		
+		if(lightShader.bloom)
+		{
+			int blurs = 16;
+			bool horizontal = 1; bool firstTime = 1;
+			postProcess.gausianBLurShader.bind();
+			glActiveTexture(GL_TEXTURE0);
+			glUniform1i(postProcess.u_toBlurcolorInput, 0);
+			glViewport(0, 0, w/2, h/2);
+			for (int i = 0; i < blurs; i++)
+			{
+				glBindFramebuffer(GL_FRAMEBUFFER, postProcess.blurFbo[horizontal]);
+				glClear(GL_COLOR_BUFFER_BIT);
+				glUniform1i(postProcess.u_horizontal, horizontal);
+
+				glBindTexture(GL_TEXTURE_2D,
+					firstTime ? postProcess.colorBuffers[1] : postProcess.bluredColorBuffer[!horizontal]);
+
+				glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+
+				horizontal = !horizontal;
+				firstTime = false;
+
+			}
+			glViewport(0, 0, w, h);
+
+		}
 
 	#pragma endregion
 
@@ -2993,7 +3059,13 @@ namespace gl3d
 		//bloom data
 		glUniform1i(postProcess.u_bloomTexture, 1);
 		glActiveTexture(GL_TEXTURE1);
-		glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer); //!!!!!!!!!!!!!!1
+		if(lightShader.bloom)
+		{
+			glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer[1]); 
+		}else
+		{
+			glBindTexture(GL_TEXTURE_2D, postProcess.colorBuffers[1]);
+		}
 
 		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
@@ -3042,9 +3114,9 @@ namespace gl3d
 		
 		//ssao
 		glBindTexture(GL_TEXTURE_2D, ssao.ssaoColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/2, h/2, 0, GL_RED, GL_FLOAT, NULL);
 		glBindTexture(GL_TEXTURE_2D, ssao.blurColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/4, h/4, 0, GL_RED, GL_FLOAT, NULL);
 
 		//bloom
 		for (int i = 0; i < 2; i++)
@@ -3053,8 +3125,12 @@ namespace gl3d
 			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 		}
 
-		glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+		for(int i=0;i<2;i++)
+		{
+			glBindTexture(GL_TEXTURE_2D, postProcess.bluredColorBuffer[i]);
+			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w/2, h/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+		}
+
 
 	}
 
@@ -3114,8 +3190,8 @@ namespace gl3d
 
 		glGenTextures(1, &ssaoColorBuffer);
 		glBindTexture(GL_TEXTURE_2D, ssaoColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/2, h/2, 0, GL_RED, GL_FLOAT, NULL);
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -3141,8 +3217,8 @@ namespace gl3d
 		glBindFramebuffer(GL_FRAMEBUFFER, blurBuffer);
 		glGenTextures(1, &blurColorBuffer);
 		glBindTexture(GL_TEXTURE_2D, blurColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_FLOAT, NULL);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/4, h/4, 0, GL_RED, GL_FLOAT, NULL);
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -3183,17 +3259,22 @@ namespace gl3d
 		u_horizontal = getUniform(gausianBLurShader.id, "u_horizontal");
 
 
-		glGenFramebuffers(1, &blurFbo);
-		glBindFramebuffer(GL_FRAMEBUFFER, blurFbo);
+		glGenFramebuffers(2, blurFbo);
+		glGenTextures(2, bluredColorBuffer);
 
-		glGenTextures(1, &bluredColorBuffer);
-		glBindTexture(GL_TEXTURE_2D, bluredColorBuffer);
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, bluredColorBuffer, 0);
+		for(int i=0;i <2; i++)
+		{
+			glBindFramebuffer(GL_FRAMEBUFFER, blurFbo[i]);
+
+			glBindTexture(GL_TEXTURE_2D, bluredColorBuffer[i]);
+			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w/2, h/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, bluredColorBuffer[i], 0);
+		}
+		
 
 		glBindFramebuffer(GL_FRAMEBUFFER, 0);
 

+ 10 - 3
headerOnly/gl3d.h

@@ -1,6 +1,6 @@
 ////////////////////////////////////////////////
 //gl32 --Vlad Luta -- 
-//built on 2021-04-14
+//built on 2021-04-20
 ////////////////////////////////////////////////
 
 
@@ -267,6 +267,7 @@ namespace gl3d
 
 		bool normalMap = 1; 
 		bool useSSAO = 1;
+		bool bloom = 1;
 
 		//todo clear
 	};
@@ -633,7 +634,12 @@ namespace gl3d
 
 	#pragma endregion
 
+		struct VAO
+		{
+			GLuint posNormalTexture;
 
+			void createVAOs();
+		}vao;
 
 		std::vector< GpuMultipleGraphicModel > graphicModels;
 		std::vector<int> graphicModelsIndexes;
@@ -701,9 +707,10 @@ namespace gl3d
 			GLint u_horizontal;
 
 			GLuint fbo;
-			GLuint blurFbo;
+			GLuint blurFbo[2];
+
 			GLuint colorBuffers[2]; // 0 for color, 1 for bloom
-			GLuint bluredColorBuffer;
+			GLuint bluredColorBuffer[2];
 			void create(int w, int h);
 
 		}postProcess;