Panagiotis Christopoulos Charitos 15 лет назад
Родитель
Сommit
788a7cbb06

+ 2 - 1
shaders/simple_texturing.glsl

@@ -21,6 +21,7 @@ void main()
 uniform sampler2D diffuseMap, noise_map;
 //varying vec2 texCoords_v2f;
 varying vec3 normal_v2f;
+uniform vec2 rendererSize;
 
 void main()
 {
@@ -28,7 +29,7 @@ void main()
 	_noise = _noise * 2 - 1;
 	_noise *= 7.0;*/
 
-	vec4 _texel = texture2D( diffuseMap, (gl_FragCoord.xy+(normal_v2f.z*50))*vec2( 1.0/R_W, 1.0/R_H ) ) * 0.75;
+	vec4 _texel = texture2D( diffuseMap, (gl_FragCoord.xy+(normal_v2f.z*50))*vec2( 1.0/rendererSize.x, 1.0/rendererSize.y ) ) * 0.75;
 	//vec4 _texel = texture2D( diffuseMap, gl_FragCoord.xy*vec2( 1.0/R_W, 1.0/R_H ) );
 
 	gl_FragData[0] = _texel;

+ 1 - 0
src/Main.cpp

@@ -205,6 +205,7 @@ void init()
 	RendererInitializer initializer;
 	initializer.width = 1280;
 	initializer.height = 800;
+	initializer.ms.earlyZ.enabled = false;
 	initializer.dbg.enabled = true;
 	initializer.is.sm.bilinearEnabled = true;
 	initializer.is.sm.enabled = true;

+ 1 - 1
src/Math/Mat4.h

@@ -41,7 +41,7 @@ class Mat4
 		Mat4& operator +=( const Mat4& b );
 		Mat4  operator - ( const Mat4& b ) const;
 		Mat4& operator -=( const Mat4& b );
-		Mat4  operator * ( const Mat4& b ) const;
+		Mat4  operator * ( const Mat4& b ) const; ///< 64 muls, 48 adds
 		Mat4& operator *=( const Mat4& b );
 		Mat4  operator / ( const Mat4& b ) const;
 		Mat4& operator /=( const Mat4& b );

+ 7 - 5
src/Math/Mat4.inl.h

@@ -667,11 +667,13 @@ inline void Mat4::setIdentity()
 // combineTransformations
 inline Mat4 Mat4::combineTransformations( const Mat4& m0, const Mat4& m1 )
 {
-	/* the clean code is:
-	Mat3 rot = m0.getRotationPart() * m1.getRotationPart();  // combine the rotations
-	Vec3 tra = (m1.getTranslationPart()).Transformed( m0.getTranslationPart(), m0.getRotationPart(), 1.0 );
-	return Mat4( tra, rot );
-	and the optimized: */
+	/*
+	 * The clean code is:
+	 * Mat3 rot = m0.getRotationPart() * m1.getRotationPart();  // combine the rotations
+	 * Vec3 tra = (m1.getTranslationPart()).Transformed( m0.getTranslationPart(), m0.getRotationPart(), 1.0 );
+	 * return Mat4( tra, rot );
+	 * and the optimized:
+	 */
 	DEBUG_ERR( !isZero( m0(3,0)+m0(3,1)+m0(3,2)+m0(3,3)-1.0 ) ||
 	           !isZero( m1(3,0)+m1(3,1)+m1(3,2)+m1(3,3)-1.0 ) ); // one of the 2 mat4 doesnt represent transformation
 

+ 1 - 1
src/Renderer/Dbg.cpp

@@ -211,7 +211,7 @@ void Renderer::Dbg::renderCube( bool cols, float size )
 //=====================================================================================================================================
 void Renderer::Dbg::init()
 {
-	// create FBO
+	// create FBO Captain Blood
 	fbo.create();
 	fbo.bind();
 

+ 34 - 3
src/Renderer/EarlyZ.cpp

@@ -1,4 +1,7 @@
 #include "Renderer.h"
+#include "App.h"
+#include "MeshNode.h"
+#include "Scene.h"
 
 
 //=====================================================================================================================================
@@ -6,9 +9,9 @@
 //=====================================================================================================================================
 void Renderer::Ms::EarlyZ::init()
 {
-	if( !enabled ) return;
-
-	// create FBO
+	//
+	// init FBO
+	//
 	fbo.create();
 	fbo.bind();
 
@@ -21,3 +24,31 @@ void Renderer::Ms::EarlyZ::init()
 
 	fbo.unbind();
 }
+
+
+//=====================================================================================================================================
+// run                                                                                                                                =
+//=====================================================================================================================================
+void Renderer::Ms::EarlyZ::run()
+{
+	fbo.bind();
+
+	Renderer::setViewport( 0, 0, r.width, r.height );
+
+	glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
+	glEnable( GL_DEPTH_TEST );
+	glDisable( GL_BLEND );
+
+	for( Vec<MeshNode*>::iterator it=app->getScene()->meshNodes.begin(); it!=app->getScene()->meshNodes.end(); it++ )
+	{
+		MeshNode* meshNode = (*it);
+		if( meshNode->material->blends || meshNode->material->refracts ) continue;
+
+		DEBUG_ERR( meshNode->material->dpMtl == NULL );
+
+		r.setupMaterial( *meshNode->material->dpMtl, *meshNode, *r.cam );
+		meshNode->render();
+	}
+
+	glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
+}

+ 2 - 0
src/Renderer/MainRenderer.cpp

@@ -56,6 +56,8 @@ void MainRenderer::init( const RendererInitializer& initializer_ )
 	glDisable( GL_TEXTURE_2D );
 	glDisable( GL_BLEND );
 	glPolygonMode( GL_FRONT, GL_FILL );
+	glDepthMask( true );
+	glDepthFunc( GL_LESS );
 
 	glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS_EXT, &maxColorAtachments );
 	sProg.customLoad( "shaders/final.glsl" );

+ 16 - 11
src/Renderer/Ms.cpp

@@ -56,16 +56,18 @@ void Renderer::Ms::run()
 {
 	const Camera& cam = *r.cam;
 
-	#if defined( _EARLY_Z_ )
-		// run the early z pass
-		r.Ms::earlyz::runPass( cam );
-	#endif
+	if( earlyZ.enabled )
+	{
+		earlyZ.run();
+	}
 
 	fbo.bind();
 
-	#if !defined( _EARLY_Z_ )
+	if( !earlyZ.enabled )
+	{
 		glClear( GL_DEPTH_BUFFER_BIT );
-	#endif
+	}
+
 	r.setProjectionViewMatrices( cam );
 	Renderer::setViewport( 0, 0, r.width, r.height );
 
@@ -73,10 +75,12 @@ void Renderer::Ms::run()
 	app->getScene()->skybox.Render( cam.getViewMatrix().getRotationPart() );
 	//glDepthFunc( GL_LEQUAL );
 
-	#if defined( _EARLY_Z_ )
+	// if earlyZ then change the default depth test and disable depth writting
+	if( earlyZ.enabled )
+	{
 		glDepthMask( false );
 		glDepthFunc( GL_EQUAL );
-	#endif
+	}
 
 	// render the meshes
 	for( uint i=0; i<app->getScene()->meshNodes.size(); i++ )
@@ -91,11 +95,12 @@ void Renderer::Ms::run()
 
 	glPolygonMode( GL_FRONT, GL_FILL ); // the rendering above fucks the polygon mode
 
-
-	#if defined( _EARLY_Z_ )
+	// restore depth
+	if( earlyZ.enabled )
+	{
 		glDepthMask( true );
 		glDepthFunc( GL_LESS );
-	#endif
+	}
 
 	fbo.unbind();
 }

+ 1 - 0
src/Renderer/Renderer.cpp

@@ -29,6 +29,7 @@ Renderer::Renderer():
 void Renderer::init( const RendererInitializer& initializer )
 {
 	// set from the initializer
+	ms.earlyZ.enabled = initializer.ms.earlyZ.enabled;
 	is.sm.enabled = initializer.is.sm.enabled;
 	is.sm.pcfEnabled = initializer.is.sm.pcfEnabled;
 	is.sm.bilinearEnabled = initializer.is.sm.bilinearEnabled;

+ 3 - 0
src/Renderer/Renderer.h

@@ -50,6 +50,9 @@ class Renderer
 				 */
 				class EarlyZ: public RenderingStage
 				{
+					friend class Renderer;
+					friend class Ms;
+
 					PROPERTY_R( bool, enabled, isEnabled )
 
 					private:

+ 9 - 0
src/Renderer/RendererInitializer.h

@@ -8,6 +8,15 @@
  */
 struct RendererInitializer
 {
+	// Ms
+	struct Ms
+	{
+		struct EarlyZ
+		{
+			bool enabled;
+		} earlyZ;
+	} ms;
+
 	// Is
 	struct Is
 	{

+ 1 - 3
src/Renderer/Sm.cpp

@@ -85,9 +85,7 @@ void Renderer::Is::Sm::run( const Camera& cam )
 
 		DEBUG_ERR( meshNode->material->dpMtl == NULL );
 
-		//meshNode->material->dpMtl->setup();
-		//meshNode->renderDepth();
-		r.setupMaterial( *meshNode->material, *meshNode, cam );
+		r.setupMaterial( *meshNode->material->dpMtl, *meshNode, cam );
 		meshNode->render();
 	}