Browse Source

Merge pull request #50 from dariomanesku/master

Updates and fixes.
Branimir Karadžić 12 years ago
parent
commit
2374ea3a15

+ 2 - 2
examples/13-stencil/fs_stencil_color_lightning.sc

@@ -49,7 +49,7 @@ vec3 calcLight(int _idx, vec3 _view, vec3 _normal, vec3 _viewDir)
 	vec4 lc = lit(bln.x, bln.y, u_shininess);
 
 	float dist = max(length(toLight), u_lightPosRadius[_idx].w);
-	float attn = 150.0 * pow(dist, -2.0);
+	float attn = 250.0 * pow(dist, -2.0);
 	vec3 rgb = (lc.y * u_diffuse + lc.z * u_specular) * u_lightRgbInnerR[_idx].rgb * attn;
 
 	return rgb;
@@ -84,5 +84,5 @@ void main()
 	vec3 diffuse = toGamma(lightColor * color);
 	gl_FragColor.xyz = clamp(ambient + diffuse, 0.0, 1.0);
 
-	gl_FragColor.w = 1.0;
+	gl_FragColor.w = u_color.w;
 }

+ 2 - 2
examples/13-stencil/fs_stencil_texture_lightning.sc

@@ -50,7 +50,7 @@ vec3 calcLight(int _idx, vec3 _view, vec3 _normal, vec3 _viewDir)
 	vec4 lc = lit(bln.x, bln.y, u_shininess);
 
 	float dist = max(length(toLight), u_lightPosRadius[_idx].w);
-	float attn = 150.0 * pow(dist, -2.0);
+	float attn = 250.0 * pow(dist, -2.0);
 	vec3 rgb = (lc.y * u_diffuse + lc.z * u_specular) * u_lightRgbInnerR[_idx].rgb * attn;
 
 	return rgb;
@@ -85,5 +85,5 @@ void main()
 	vec3 diffuse = toGamma(lightColor * color);
 	gl_FragColor.xyz = clamp(ambient + diffuse, 0.0, 1.0);
 
-	gl_FragColor.w = 1.0;
+	gl_FragColor.w = u_color.w;
 }

BIN
examples/13-stencil/screenshot.png


+ 25 - 45
examples/13-stencil/stencil.cpp

@@ -335,25 +335,25 @@ struct Uniforms
 		m_params.m_lightCount    = 4.0f;
 		m_params.m_lightIndex    = 4.0f;
 
-		m_ambient[0] = 0.05f;
-		m_ambient[1] = 0.05f;
-		m_ambient[2] = 0.05f;
+		m_ambient[0] = 0.02f;
+		m_ambient[1] = 0.02f;
+		m_ambient[2] = 0.02f;
 		m_ambient[3] = 0.0f; //unused
 
-		m_diffuse[0] = 0.8f;
-		m_diffuse[1] = 0.8f;
-		m_diffuse[2] = 0.8f;
+		m_diffuse[0] = 0.2f;
+		m_diffuse[1] = 0.2f;
+		m_diffuse[2] = 0.2f;
 		m_diffuse[3] = 0.0f; //unused
 
 		m_specular_shininess[0] = 1.0f;
 		m_specular_shininess[1] = 1.0f;
 		m_specular_shininess[2] = 1.0f;
-		m_specular_shininess[3] = 25.0f; //shininess
+		m_specular_shininess[3] = 10.0f; //shininess
 
 		m_color[0] = 1.0f;
 		m_color[1] = 1.0f;
 		m_color[2] = 1.0f;
-		m_color[3] = 1.0;
+		m_color[3] = 1.0f;
 
 		m_time = 0.0f;
 
@@ -409,6 +409,7 @@ struct Uniforms
 		bgfx::destroyUniform(u_ambient);
 		bgfx::destroyUniform(u_diffuse);
 		bgfx::destroyUniform(u_specular_shininess);
+		bgfx::destroyUniform(u_color);
 		bgfx::destroyUniform(u_time);
 		bgfx::destroyUniform(u_lightPosRadius);
 		bgfx::destroyUniform(u_lightRgbInnerR);
@@ -468,7 +469,6 @@ struct RenderState
 	{
 		StencilReflection_CraftStencil = 0,
 		StencilReflection_DrawReflected,
-		StencilReflection_DarkenReflections,
 		StencilReflection_BlendPlane,
 		StencilReflection_DrawScene,
 
@@ -491,7 +491,10 @@ struct RenderState
 static RenderState s_renderStates[RenderState::Count] =
 {
 	{ // StencilReflection_CraftStencil
-		BGFX_STATE_MSAA
+		BGFX_STATE_RGB_WRITE
+		| BGFX_STATE_DEPTH_WRITE
+		| BGFX_STATE_DEPTH_TEST_LESS
+		| BGFX_STATE_MSAA
 		, UINT32_MAX
 		, BGFX_STENCIL_TEST_ALWAYS         // pass always
 		| BGFX_STENCIL_FUNC_REF(1)         // value = 1
@@ -504,6 +507,7 @@ static RenderState s_renderStates[RenderState::Count] =
 	{ // StencilReflection_DrawReflected
 		BGFX_STATE_RGB_WRITE
 		| BGFX_STATE_ALPHA_WRITE
+		| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
 		| BGFX_STATE_DEPTH_WRITE
 		| BGFX_STATE_DEPTH_TEST_LESS
 		| BGFX_STATE_CULL_CW    //reflection matrix has inverted normals. using CCW instead of CW.
@@ -517,22 +521,11 @@ static RenderState s_renderStates[RenderState::Count] =
 		| BGFX_STENCIL_OP_PASS_Z_KEEP
 		, BGFX_STENCIL_NONE
 	},
-	{ // StencilReflection_DarkenReflections
-		BGFX_STATE_RGB_WRITE
-		| BGFX_STATE_DEPTH_WRITE
-		| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_FACTOR)
-		| BGFX_STATE_DEPTH_TEST_LESS
-		| BGFX_STATE_CULL_CCW
-		| BGFX_STATE_MSAA
-		, UINT32_MAX
-		, BGFX_STENCIL_NONE
-		, BGFX_STENCIL_NONE
-	},
 	{ // StencilReflection_BlendPlane
 		BGFX_STATE_RGB_WRITE
 		| BGFX_STATE_DEPTH_WRITE
-		| BGFX_STATE_BLEND_LIGHTEN
-		| BGFX_STATE_DEPTH_TEST_EQUAL
+		| BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_SRC_COLOR)
+		| BGFX_STATE_DEPTH_TEST_LESS
 		| BGFX_STATE_CULL_CCW
 		| BGFX_STATE_MSAA
 		, UINT32_MAX
@@ -1090,6 +1083,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		s_uniforms.m_params.m_lightningPass   = 1.0f;
 		s_uniforms.m_params.m_lightCount      = settings_numLights;
 		s_uniforms.m_params.m_lightIndex      = 0.0f;
+		s_uniforms.m_color[3]                 = settings_reflectionValue;
 		s_uniforms.submitPerFrameUniforms();
 
 		// Time.
@@ -1238,6 +1232,9 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 
 				// Second pass - Draw reflected objects.
 
+				// Clear depth from previous pass.
+				clearView(RENDER_VIEWID_RANGE1_PASS_1, BGFX_CLEAR_DEPTH_BIT, clearValues);
+
 				// Compute reflected matrix.
 				float reflectMtx[16];
 				float plane_pos[3] = { 0.0f, 0.01f, 0.0f };
@@ -1277,37 +1274,20 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 				// Set lights back.
 				memcpy(s_uniforms.m_lightPosRadius, lightPosRadius, numLights * 4*sizeof(float));
 
-				// Third pass - Darken reflected objects.
-
-				uint8_t val = uint8_t(settings_reflectionValue * UINT8_MAX);
-				uint32_t factor = (val << 24)
-					| (val << 16)
-					| (val << 8 )
-					| (val << 0 )
-					;
-				s_renderStates[RenderState::StencilReflection_DarkenReflections].m_blendFactorRgba = factor;
+				// Third pass - Blend plane.
 
 				// Floor.
 				hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_2
-					, floorMtx
-					, programColorBlack
-					, s_renderStates[RenderState::StencilReflection_DarkenReflections]
-					);
-
-				// Fourth pass - Draw plane. (blend plane with what's behind it)
-
-				// Floor.
-				hplaneMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
 					, floorMtx
 					, programTextureLightning
 					, s_renderStates[RenderState::StencilReflection_BlendPlane]
 					, fieldstoneTex
 					);
 
-				// Fifth pass - Draw everything else but the plane.
-				
+				// Fourth pass - Draw everything else but the plane.
+
 				// Bunny.
-				bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_4
+				bunnyMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
 					, bunnyMtx
 					, programColorLightning
 					, s_renderStates[RenderState::StencilReflection_DrawScene]
@@ -1316,7 +1296,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 				// Columns.
 				for (uint8_t ii = 0; ii < 4; ++ii)
 				{
-					columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_4
+					columnMesh.submit(RENDER_VIEWID_RANGE1_PASS_3
 						, columnMtx[ii]
 						, programColorLightning
 						, s_renderStates[RenderState::StencilReflection_DrawScene]

+ 27 - 75
examples/14-shadowvolumes/shadowvolumes.cpp

@@ -208,38 +208,6 @@ void mtxScaleRotateTranslate(float* _result
 	mtxMul(_result, mtxScale, mtxRotateTranslate);
 }
 
-void mtxShadow(float* __restrict _result
-			   , const float* __restrict _ground
-			   , const float* __restrict _light
-			   )
-{
-	float dot = _ground[0] * _light[0]
-			  + _ground[1] * _light[1]
-			  + _ground[2] * _light[2]
-			  + _ground[3] * _light[3]
-			  ;
-
-	_result[ 0] =  dot - _light[0] * _ground[0];
-	_result[ 1] = 0.0f - _light[1] * _ground[0];
-	_result[ 2] = 0.0f - _light[2] * _ground[0];
-	_result[ 3] = 0.0f - _light[3] * _ground[0];
-
-	_result[ 4] = 0.0f - _light[0] * _ground[1];
-	_result[ 5] =  dot - _light[1] * _ground[1];
-	_result[ 6] = 0.0f - _light[2] * _ground[1];
-	_result[ 7] = 0.0f - _light[3] * _ground[1];
-
-	_result[ 8] = 0.0f - _light[0] * _ground[2];
-	_result[ 9] = 0.0f - _light[1] * _ground[2];
-	_result[10] =  dot - _light[2] * _ground[2];
-	_result[11] = 0.0f - _light[3] * _ground[2];
-
-	_result[12] = 0.0f - _light[0] * _ground[3];
-	_result[13] = 0.0f - _light[1] * _ground[3];
-	_result[14] = 0.0f - _light[2] * _ground[3];
-	_result[15] =  dot - _light[3] * _ground[3];
-}
-
 void mtxBillboard(float* __restrict _result
 				  , const float* __restrict _view
 				  , const float* __restrict _pos
@@ -651,7 +619,7 @@ static RenderState s_renderStates[RenderState::Count]  =
 
 struct ViewState
 {
-	ViewState(uint32_t _width  = 1280, uint32_t _height = 720)
+	ViewState(uint32_t _width = 1280, uint32_t _height = 720)
 		: m_width(_width)
 		, m_height(_height)
 	{
@@ -1318,19 +1286,17 @@ struct ShadowVolume
 	bool m_cap;
 };
 
-void shadowVolumeTransform(float* __restrict _outMtx
-						   , float* __restrict _outLightPos
-						   , const float* __restrict _scale
-						   , const float* __restrict _rotate
-						   , const float* __restrict _translate
-						   , const float* __restrict _lightPos // world pos
-						   )
+void shadowVolumeLightTransform(float* __restrict _outLightPos
+							  , const float* __restrict _scale
+							  , const float* __restrict _rotate
+							  , const float* __restrict _translate
+							  , const float* __restrict _lightPos // world pos
+							  )
 {
 	/**
 	 * Instead of transforming all the vertices, transform light instead:
 	 * mtx = pivotTranslate -> rotateZYX -> invScale
 	 * light = mtx * origin
-	 * _outMtx = scale -> rotateXYZ -> translate
 	 */
 
 	float pivot[16];
@@ -1360,34 +1326,8 @@ void shadowVolumeTransform(float* __restrict _outMtx
 	float mtx[16];
 	mtxMul(mtx, tmp0, invScale);
 
-	float light[3];
 	float origin[3] = { 0.0f, 0.0f, 0.0f };
-	vec3MulMtx(light, origin, mtx);
-	memcpy(_outLightPos, light, 3*sizeof(float) );
-
-	float scale[16];
-	mtxScale(scale
-		, _scale[0]
-		, _scale[1]
-		, _scale[2]
-		);
-
-	float mxyz[16];
-	mtxRotateXYZ(mxyz
-		, _rotate[0]
-		, _rotate[1]
-		, _rotate[2]
-		);
-
-	float translate[16];
-	mtxTranslate(translate
-		, _translate[0]
-		, _translate[1]
-		, _translate[2]
-		);
-
-	mtxMul(tmp0, scale, mxyz);
-	mtxMul(_outMtx, tmp0, translate);
+	vec3MulMtx(_outLightPos, origin, mtx);
 }
 
 void shadowVolumeCreate(ShadowVolume& _shadowVolume
@@ -1548,16 +1488,16 @@ void shadowVolumeCreate(ShadowVolume& _shadowVolume
 			const float* v1 = edge.m_v1;
 
 			int16_t k = 0;
-			float s;
 			for (uint8_t ii = 0; ii < edge.m_faceIndex; ++ii)
 			{
 				const Edge::Plane& face = edge.m_faces[ii];
-				s = fsign(vec3Dot(face.m_plane, _light) + face.m_plane[3]);
+
+				int16_t s = (int16_t)fsign(vec3Dot(face.m_plane, _light) + face.m_plane[3]);
 				if (face.m_reverseVertexOrder)
 				{
 					s = -s;
 				}
-				k += uint16_t(s);
+				k += s;
 			}
 
 			if (k == 0)
@@ -2593,11 +2533,9 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 				}
 				s_uniforms.m_svparams.m_dfail = float(ShadowVolumeImpl::DepthFail == shadowVolumeImpl); 
 
-				// Compute transform for shadow volume.
-				float shadowVolumeMtx[16];
+				// Compute virtual light position for shadow volume generation.
 				float transformedLightPos[3];
-				shadowVolumeTransform(shadowVolumeMtx
-					, transformedLightPos
+				shadowVolumeLightTransform(transformedLightPos
 					, instance.m_scale
 					, instance.m_rotation
 					, instance.m_pos
@@ -2608,6 +2546,20 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 				memcpy(s_uniforms.m_virtualLightPos_extrusionDist, transformedLightPos, 3*sizeof(float) );
 				s_uniforms.m_virtualLightPos_extrusionDist[3] = instance.m_svExtrusionDistance;
 
+				// Compute transform for shadow volume.
+				float shadowVolumeMtx[16];
+				mtxScaleRotateTranslate(shadowVolumeMtx
+						, instance.m_scale[0]
+						, instance.m_scale[1]
+						, instance.m_scale[2]
+						, instance.m_rotation[0]
+						, instance.m_rotation[1]
+						, instance.m_rotation[2]
+						, instance.m_pos[0]
+						, instance.m_pos[1]
+						, instance.m_pos[2]
+						);
+
 				GroupArray& groups = model->m_mesh.m_groups;
 				const uint16_t stride = model->m_mesh.m_decl.getStride();
 				for (GroupArray::iterator it = groups.begin(), itEnd = groups.end(); it != itEnd; ++it)

BIN
examples/runtime/shaders/dx11/fs_stencil_color_lightning.bin


BIN
examples/runtime/shaders/dx11/fs_stencil_texture_lightning.bin


BIN
examples/runtime/shaders/dx9/fs_stencil_color_lightning.bin


BIN
examples/runtime/shaders/dx9/fs_stencil_texture_lightning.bin


BIN
examples/runtime/shaders/gles/fs_stencil_color_lightning.bin


BIN
examples/runtime/shaders/gles/fs_stencil_texture_lightning.bin


BIN
examples/runtime/shaders/glsl/fs_stencil_color_lightning.bin


BIN
examples/runtime/shaders/glsl/fs_stencil_texture_lightning.bin