Przeglądaj źródła

Now the refracting objects have a sepperate stage that runs after PPS. Its actualy part of the blending stage and the code is in r_bs2.cpp

Panagiotis Christopoulos Charitos 16 lat temu
rodzic
commit
18f9554488

+ 4 - 0
shaders/final.glsl

@@ -13,6 +13,10 @@ void main()
 	//if( gl_FragCoord.x > 0.5 ) discard;
 
 	gl_FragColor.rgb = texture2D( raster_image, tex_coords ).rgb;
+
+	/*vec4 c = texture2D( raster_image, tex_coords );
+	if( c.r == 0.0 && c.g == 0.0 && c.b==0.0 && c.a != 0.0 )
+		gl_FragColor.rgb = vec3( c.a );*/
 	//gl_FragColor.rgb = MedianFilter( raster_image, tex_coords );
 	//gl_FragColor.rgb = vec3( gl_FragCoord.xy/tex_size_, 0.0 );
 	//gl_FragColor.rgb = vec3( gl_FragCoord.xy*vec2( 1.0/R_W, 1.0/R_H ), 0.0 );

+ 1 - 1
shaders/simple_texturing.glsl

@@ -28,7 +28,7 @@ void main()
 	_noise = _noise * 2 - 1;
 	_noise *= 7.0;*/
 
-	vec4 _texel = texture2D( diffuse_map, (gl_FragCoord.xy+(normal_v2f.z*100))*vec2( 1.0/R_W, 1.0/R_H ) ) * 0.75;
+	vec4 _texel = texture2D( diffuse_map, (gl_FragCoord.xy+(normal_v2f.z*50))*vec2( 1.0/R_W, 1.0/R_H ) ) * 0.75;
 	//vec4 _texel = texture2D( diffuse_map, gl_FragCoord.xy*vec2( 1.0/R_W, 1.0/R_H ) );
 
 	gl_FragData[0] = _texel;

+ 10 - 103
src/renderer/r_bs.cpp

@@ -20,110 +20,31 @@ namespace bs {
 //=====================================================================================================================================
 // VARS                                                                                                                               =
 //=====================================================================================================================================
-static fbo_t b_fbo; ///< blending models FBO
-static fbo_t r_fbo; ///< refracting models FBO
-
-/*static*/ texture_t r_fai; ///< RGB for color and A for mask (0 doesnt pass, 1 pass)
-static shader_prog_t* r2b_shdr;
+static fbo_t fbo; ///< blending models FBO
 
 
 //=====================================================================================================================================
-// InitB                                                                                                                              =
+// Init                                                                                                                               =
 //=====================================================================================================================================
-static void InitB()
+void Init()
 {
 	// create FBO
-	b_fbo.Create();
-	b_fbo.Bind();
+	fbo.Create();
+	fbo.Bind();
 
 	// inform FBO about the color buffers
-	b_fbo.SetNumOfColorAttachements(1);
+	fbo.SetNumOfColorAttachements(1);
 
 	// attach the texes
 	glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r::is::fai.GetGLID(), 0 );
 	glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,  GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
 
 	// test if success
-	if( !b_fbo.IsGood() )
+	if( !fbo.IsGood() )
 		FATAL( "Cannot create deferred shading blending stage FBO" );
 
 	// unbind
-	b_fbo.Unbind();
-}
-
-
-//=====================================================================================================================================
-// InitR                                                                                                                              =
-//=====================================================================================================================================
-static void InitR()
-{
-	/*uint fbo_id, tex_id, depth_tex_id, stencil_rb;
-
-	glGenFramebuffersEXT( 1, &fbo_id );
-	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo_id );
-
-	// color
-	glGenTextures( 1, &tex_id );
-	glBindTexture( GL_TEXTURE_2D, tex_id );
-	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB8, 100, 100, 0, GL_RGB, GL_FLOAT, NULL );
-	glFramebufferTexture2DEXT( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_id, 0 );
-
-	// depth
-	r::PrintLastError();
-	glGenTextures( 1, &depth_tex_id );
-	glBindTexture( GL_TEXTURE_2D, depth_tex_id );
-	glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 100, 100, 0, GL_DEPTH_STENCIL, GL_FLOAT, NULL );
-	r::PrintLastError();
-	glFramebufferTexture2DEXT( GL_FRAMEBUFFER, GL_DEPTH24_STENCIL8, GL_TEXTURE_2D, depth_tex_id, 0 );
-	r::PrintLastError();
-
-	// stencil
-	glGenRenderbuffersEXT( 1, &stencil_rb );
-	glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, stencil_rb );
-	glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, 100, 100 );
-	glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, stencil_rb );
-
-	if( glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT )
-		FATAL( glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) );*/
-
-
-	/*r::PrintLastError();
-	uint depth_tex_id;
-	glGenTextures( 1, &depth_tex_id );
-	glBindTexture( GL_TEXTURE_2D, depth_tex_id );
-	glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, 100, 100, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL );
-	r::PrintLastError();*/
-
-	// create FBO
-	r_fbo.Create();
-	r_fbo.Bind();
-
-	// texture
-	r_fbo.SetNumOfColorAttachements(1);
-	r_fai.CreateEmpty2D( r::w * r::rendering_quality, r::h * r::rendering_quality, GL_RGBA8, GL_RGBA );
-	glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, r_fai.GetGLID(), 0 );
-
-	// attach the texes
-	glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
-
-	// test if success
-	if( !r_fbo.IsGood() )
-		FATAL( "Cannot create deferred shading blending stage FBO" );
-
-	// unbind
-	r_fbo.Unbind();
-
-	r2b_shdr = rsrc::shaders.Load( "shaders/bs_refract.glsl" );
-}
-
-
-//=====================================================================================================================================
-// Init                                                                                                                               =
-//=====================================================================================================================================
-void Init()
-{
-	InitB();
-	InitR();
+	fbo.Unbind();
 }
 
 
@@ -145,23 +66,9 @@ void RunStage( const camera_t& cam )
 	for( uint i=0; i<scene::mesh_nodes.size(); i++ )
 	{
 		mesh_node_t* mesh_node = scene::mesh_nodes[i];
-		if( mesh_node->material->refracts )
-		{
-			// write to the rFbo
-			r_fbo.Bind();
-			glClear( GL_COLOR_BUFFER_BIT );
-			mesh_node->material->Setup();
-			mesh_node->Render();
-
-			b_fbo.Bind();
-			glDisable( GL_DEPTH_TEST );
-			r2b_shdr->Bind();
-			r2b_shdr->LocTexUnit( r2b_shdr->GetUniformLocation(0), r_fai, 0 );
-			r::DrawQuad( r2b_shdr->GetAttributeLocation(0) );
-		}
-		else if( mesh_node->material->blends )
+		if( mesh_node->material->blends && !mesh_node->material->blends )
 		{
-			b_fbo.Bind();
+			fbo.Bind();
 			mesh_node->material->Setup();
 			mesh_node->Render();
 		}

+ 117 - 0
src/renderer/r_bs2.cpp

@@ -0,0 +1,117 @@
+/**
+ * The file contains functions and vars used for the deferred shading blending stage stage.
+ * The blending stage comes after the illumination stage. All the objects that are transculent will be drawn here.
+ */
+
+#include "renderer.h"
+#include "camera.h"
+#include "scene.h"
+#include "mesh.h"
+#include "r_private.h"
+#include "resource.h"
+#include "fbo.h"
+#include "mesh_node.h"
+#include "material.h"
+
+
+namespace r {
+namespace bs {
+
+//=====================================================================================================================================
+// VARS                                                                                                                               =
+//=====================================================================================================================================
+static fbo_t intermid_fbo, fbo;
+
+static texture_t fai; ///< RGB for color and A for mask (0 doesnt pass, 1 pass)
+static shader_prog_t* shader_prog;
+
+
+//=====================================================================================================================================
+// Init2                                                                                                                              =
+//=====================================================================================================================================
+void Init2()
+{
+	//** 1st FBO **
+	// create FBO
+	fbo.Create();
+	fbo.Bind();
+
+	// inform FBO about the color buffers
+	fbo.SetNumOfColorAttachements(1);
+
+	// attach the texes
+	glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r::pps::fai.GetGLID(), 0 );
+	glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,  GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
+
+	// test if success
+	if( !fbo.IsGood() )
+		FATAL( "Cannot create deferred shading blending stage FBO" );
+
+	// unbind
+	fbo.Unbind();
+
+
+	//** 2nd FBO **
+	intermid_fbo.Create();
+	intermid_fbo.Bind();
+
+	// texture
+	intermid_fbo.SetNumOfColorAttachements(1);
+	fai.CreateEmpty2D( r::w * r::rendering_quality, r::h * r::rendering_quality, GL_RGBA8, GL_RGBA );
+	glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fai.GetGLID(), 0 );
+
+	// attach the texes
+	glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
+
+	// test if success
+	if( !intermid_fbo.IsGood() )
+		FATAL( "Cannot create deferred shading blending stage FBO" );
+
+	// unbind
+	intermid_fbo.Unbind();
+
+	shader_prog = rsrc::shaders.Load( "shaders/bs_refract.glsl" );
+}
+
+
+//=====================================================================================================================================
+// RunStage2                                                                                                                          =
+//=====================================================================================================================================
+void RunStage2( const camera_t& cam )
+{
+	r::SetProjectionViewMatrices( cam );
+	r::SetViewport( 0, 0, r::w*r::rendering_quality, r::h*r::rendering_quality );
+
+
+	glDepthMask( false );
+
+
+	// render the meshes
+	for( uint i=0; i<scene::mesh_nodes.size(); i++ )
+	{
+		mesh_node_t* mesh_node = scene::mesh_nodes[i];
+		if( mesh_node->material->refracts )
+		{
+			// write to the rFbo
+			intermid_fbo.Bind();
+			glEnable( GL_DEPTH_TEST );
+			glClear( GL_COLOR_BUFFER_BIT );
+			mesh_node->material->Setup();
+			mesh_node->Render();
+
+			fbo.Bind();
+			glDisable( GL_DEPTH_TEST );
+			shader_prog->Bind();
+			shader_prog->LocTexUnit( shader_prog->GetUniformLocation(0), fai, 0 );
+			r::DrawQuad( shader_prog->GetAttributeLocation(0) );
+		}
+	}
+
+
+	// restore a few things
+	glDepthMask( true );
+	fbo_t::Unbind();
+}
+
+
+}} // end namespaces

+ 21 - 2
src/renderer/renderer.cpp

@@ -17,8 +17,8 @@ data vars
 */
 
 // misc
-//uint w = 1280, h = 800;
-uint w = 480, h = 360;
+uint w = 1280, h = 800;
+//uint w = 480, h = 360;
 //uint w = 720, h = 480;
 uint frames_num = 0;
 float aspect_ratio = (float)w/(float)h;
@@ -170,6 +170,7 @@ void Init()
 	r::is::Init();
 	r::bs::Init();
 	r::pps::Init();
+	r::bs::Init2();
 	r::dbg::Init();
 
 	PRINT( "Renderer initialization ends" );
@@ -187,6 +188,7 @@ void Render( const camera_t& cam )
 	r::is::RunStage( cam );
 	r::bs::RunStage( cam );
 	r::pps::RunStage( cam );
+	r::bs::RunStage2( cam );
 	r::dbg::RunStage( cam );
 
 	r::SetViewport( 0, 0, r::w, r::h );
@@ -197,6 +199,23 @@ void Render( const camera_t& cam )
 	shdr_final->Bind();
 	shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::pps::fai, 0 );
 
+	/*const int step = 100;
+	if( r::frames_num < step )
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::ms::diffuse_fai, 0 );
+	else if( r::frames_num < step*2 )
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::ms::normal_fai, 0 );
+	else if( r::frames_num < step*3 )
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::ms::specular_fai, 0 );
+	else if( r::frames_num < step*4 )
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::ms::depth_fai, 0 );
+	else if( r::frames_num < step*5 )
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::pps::ssao::blured_fai, 0 );
+	else if( r::frames_num < step*6 )
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::pps::hdr::pass2_fai, 0 );
+	else
+		shdr_final->LocTexUnit( shdr_final->GetUniformLocation(0), r::pps::fai, 0 );*/
+
+
 	r::DrawQuad( shdr_final->GetAttributeLocation(0) );
 }
 

+ 4 - 1
src/renderer/renderer.h

@@ -92,7 +92,10 @@ namespace is
 
 /// blending stage namespace
 namespace bs
-{}
+{
+	extern void Init2();
+	extern void RunStage2( const camera_t& cam );
+}
 
 /// pre-processing stage namespace
 namespace pps