关闭

龙书D3D11章节习题答案(第九章)

567人阅读 评论(0) 收藏 举报
分类:

以下答案仅供参考,有错欢迎留言。


Chapter 9 : Blending


1. Modify the “Transparent Water” demo by using the following line before drawing the water:

md3dImmediateContext->OMSetBlendState(TransparentBS, blendFactors, 0xfffffffe);

This turns off the first bit which disables the first sample. Because we are not using multisampling (which is like using multisampling with one sample), this prevents the water pixels from being drawn.


在创建交换链时,程序中mEnable4xMsaa默认为false,不使用多重采样,采样器Count = 1。

	// Use 4X MSAA? 
	if( mEnable4xMsaa )
	{
		sd.SampleDesc.Count   = 4;
		sd.SampleDesc.Quality = m4xMsaaQuality-1;
	}
	// No MSAA
	else
	{
		sd.SampleDesc.Count   = 1;
		sd.SampleDesc.Quality = 0;
	}

Blend Demo中的mEnable4xMsaa值为false,默认不使用多重采样,故采样器只有1个,由OMSetBlendState的第三个参数UINT SampleMask的第1位控制采样器开关,第1位取0以后即表示不绘制。

	md3dImmediateContext->OMSetBlendState(RenderStates::TransparentBS, blendFactor, 0x00000000); //同0xfffffffe




2. Experiment with different blend operations and blend factor combinations.

略。




3. Modify the “Blend” demo by drawing the water first. Explain the results.


原先的绘制顺序是Box-->Hills-->Waves


修改为Waves-->Box-->Hills:


原因是开启了深度测试以后,后绘制的对象会遮盖到先绘制的对象,当最先绘制Waves以后,在相同的坐标上,Waves像素点的深度值大于Box和Hills,所以在绘制时,Waves的像素点通不过深度测试,无法进行Blend效果的渲染。





4. Suppose fogStart = 10 and fogRange = 200. Compute foggedColor for when
(a) dist(p, E) = 160
(b) dist(p, E) = 110
(c) dist(p, E) = 60
(d) dist(p, E) = 30


(a) s = (160-10)/200 = 0.75, foggedColor = 0.75*fogColor + (1-0.75)*litColor.

其他类似。




5. Verify the effect techniques with gAlphaClip=false which do not have a discard instruction, and the techniques with gAlphaClip=true which do, by looking at the generated shader assembly. The discard instruction corresponds to the HLSL clip instruction.


Shader Model 5.0指令手册

HLSL:

		if(gAlphaClip)
		{
			// Discard pixel if texture alpha < 0.1.  Note that we do this
			// test as soon as possible so that we can potentially exit the shader 
			// early, thereby skipping the rest of the shader code.
			clip(texColor.a - 0.1f);
		}

fxc编译为汇编代码如下: (fxc编译时需要指定选项/Fc以生成.cod汇编文件)

                add r1.x, r0.w, l(-0.100000)
                lt r1.x, r1.x, l(0.000000)
                and r1.x, r1.x, l(-1)
                discard r1.x
可以看到最后一行的discard指令,其对应于HLSL里的clip指令,验证完成。



6. Modify the “Blend” demo by creating and applying a blend render state that disables color writes to the red and green color channels.


修改RenderStates.cpp中:

	transparentDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALPHA | D3D11_COLOR_WRITE_ENABLE_BLUE;
这样,设置RenderStates::TransparentBS为BlendState时,在Blend过程中会禁止对红绿通道的值进行写入。

效果当然是只看得见透明度不一的蓝色。




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23027次
    • 积分:459
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:3篇
    • 译文:1篇
    • 评论:12条
    文章分类
    最新评论