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

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

Chapter 7 : Lighting

这一章的内容相对来说比较简单,没有什么复杂的步骤,但也需要多尝试得到不同的视觉效果。

1. Modify the lighting demo of this chapter so that the directional light only emits red light, the point light only emits

green light, and the spotlight only emits blue light. Using colored lights can be useful for different game moods;

for example, a red light might be used to signify emergency situations.

在LightApp::LightApp中按要求修改mDirLight, mPointLight, mSpotLight的值即可,具体改哪些要根据实际情况。

例如,修改方向光为红光,是修改ambient为红光还是修改diffuse为红光,还是specular?

视觉效果上,ambient设为红光,山岭红色要深很多且比较均匀,而且拖动鼠标左键绕y轴旋转场景时,山岭的所有面都是红的(原色为绿色)。

再把ambient改回为原先的值,试试设置diffuse为红光,这时由于漫反射还需要根据郎伯余弦定律来决定每个方向反射回光的强度,所以拖动鼠标左键绕y轴旋转场景时,山背向的一面仍然是绿的,且红光照射到的部分红色显然是扩散开的,非均匀,显然修改diffuse比较有现实感,而diffuse呢则次要一点,你如果想把整个场景调得更红一点就可以改diffuse为淡红色。

而specular的值呢,对山岭这样的粗糙表面没什么影响,但影响到光照射到水面上反射过来的颜色,所以还需要specular模拟光照在水面上的效果。

只修改diffuse为红光的效果图如下:

再设置specular为红光,这里我觉得场景有点暗,又调高了一点ambient。

PointLight和SpotLight也同理。

根据自己的喜好即可,重要的是模拟出自己想要的效果。

在参数上我是这样设置的:

......
	// Directional light.
	mDirLight.Ambient  = XMFLOAT4(0.3f, 0.3f, 0.3f, 1.0f);
	mDirLight.Diffuse  = XMFLOAT4(0.8f, 0.0f, 0.0f, 1.0f);
	mDirLight.Specular = XMFLOAT4(1.0f, 0.2f, 0.2f, 1.0f);

	mDirLight.Direction = XMFLOAT3(0.57735f, -0.57735f, 0.57735f);

	// Point light--position is changed every frame to animate in UpdateScene function.
	mPointLight.Ambient  = XMFLOAT4(0.3f, 0.3f, 0.3f, 1.0f);
	mPointLight.Diffuse  = XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f);
	mPointLight.Specular = XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f);

	mPointLight.Att      = XMFLOAT3(0.0f, 0.1f, 0.0f);
	mPointLight.Range    = 25.0f;

	// Spot light--position and direction changed every frame to animate in UpdateScene function.
	mSpotLight.Ambient  = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
	mSpotLight.Diffuse  = XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f);
	mSpotLight.Specular =  XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f);
......

2. Modify the lighting demo of this chapter by changing the specular power material component, which controls the

“shininess” of the surface. Try p = 8 p = 32, p = 64, p = 128, p = 256, and p = 512.

比如说修改水面的光泽度,

	mWavesMat.Specular = XMFLOAT4(0.8f, 0.8f, 0.8f, 256.0f);

把w分量分别设置成8,32,...,512看看效果就会发现p越大则水面看起来越有光泽。

p=16

p=512

重点观察水波的形状(颜色是因为我拖得角度不大对...),显然p=16时水波包括水面看起来要比p=512来得"稠"(或者说粗糙)一点。

3. One characteristic of toon lighting is the abrupt transition from one color shade to the next (in contrast with a

smooth transition) as shown in Figure 7.23. This can be implemented by computing kd and ks in the usual way,

but then transforming them by discrete functions like the following before using them in the pixel shader:

Modify the lighting demo of this chapter to use this sort of toon shading. (Note: The functions f and g previously are

just sample functions to start with, and can be tweaked until you get the results you want.)

本题模拟卡通着色(toon shading)中的一个特性:颜色突变,就是说把像素点看成一大块一大块的区域,这些区域颜色相同,要做到这个效果,只需要应用题中所给的函数离散处理ks和kd。

这里我在LightHelper.fx中修改了ComputeDirectionalLight:

void ComputeDirectionalLight(Material mat, DirectionalLight L,
                             float3 normal, float3 toEye,
					         out float4 ambient,
						     out float4 diffuse,
						     out float4 spec)
{
	// Initialize outputs.
	ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
	diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
	spec    = float4(0.0f, 0.0f, 0.0f, 0.0f);

	// The light vector aims opposite the direction the light rays travel.
	float3 lightVec = -L.Direction;

	// Add ambient term.
	ambient = mat.Ambient * L.Ambient;	

	// Add diffuse and specular term, provided the surface is in
	// the line of site of the light.

	float diffuseFactor = dot(lightVec, normal);
	if(diffuseFactor <= 0.0f)
		diffuseFactor = 0.4f;
	else if(diffuseFactor <= 0.5f)
		diffuseFactor = 0.6f;
	else
		diffuseFactor = 1.0f;

	// Flatten to avoid dynamic branching.
	[flatten]
	if( diffuseFactor > 0.0f )
	{
		float3 v         = reflect(-lightVec, normal);
		float specFactor = pow(max(dot(v, toEye), 0.0f), mat.Specular.w);

		if(specFactor <= 0.1f)
			specFactor = 0.0f;
		else if(specFactor <= 0.8f)
			specFactor = 0.5f;
		else
			specFactor = 0.8f;

		diffuse = diffuseFactor * mat.Diffuse * L.Diffuse;
		spec    = specFactor * mat.Specular * L.Specular;
	}
}

下图供大家与原图对比,看看是否颜色突变比较粗糙了。

4. Modify the lighting demo of this chapter so that the angle of the spotlight’s cone can be increased or decreased

based on user keyboard input.

获取键盘输入我是直接用的windows api:

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)  

0x8000 即 0x1000 0000 0000 0000

自对GetAsyncKeyState函数的上一次调用以来,如键已被按过,则位0设为1;否则设为0。

在UpdateScene里加上两句:

	if(KEYDOWN(VK_UP))
		mSpotLight.Spot -= 1.0f;
	if(KEYDOWN(VK_DOWN))
		mSpotLight.Spot += 1.0f; 

就可以使用方向键控制聚光灯的照射范围了~

当然你还可以设置Spot的上限和下限,或者让每次按键Spot增大/减小的速度变慢等等。

时间: 2024-08-05 01:03:28

龙书D3D11章节习题答案(第七章)的相关文章

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

以下答案仅供参考,有错欢迎留言. Chapter 6:Drawing in Direct3D 提醒:记得多备份几份原书提供的BoxDemo.cpp和ShapeDemo.cpp,下面的题目基本上都由此基础上改动. 同类文章: DirectX 11游戏编程学习笔记之8: 第6章Drawing in Direct3D(在Direct3D中绘制)(习题解答) 找到同类的文章实属不易,大家可以借鉴一番这篇文章(作者提供了完整的代码下载,而我比较懒..下面只会说我大致的步骤) 勘误:题中D3D10_INPU

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

以下答案仅供参考,有错欢迎留言. Chapter 8 : Texturing 1. Experiment with the "Crate" demo by changing the texture coordinates and using different address mode combinations and filtering options. In particular, reproduce the images in Figures 8.7, 8.9, 8.10, 8.

龙书D3D11章节习题答案(不定时更新)

以下答案仅供参考,勿以为真,欢迎提问. Chapter 4:Direct3D Initialzation 4.7 EXERCISES (作者的目的大概是让我们熟悉DXGI,打开Common框架代码里面的d3dApp.cpp文件操作即可) 1. Modify the previous exercise solution by disabling the ALT-ENTER functionality to switch between full screen and windowed mode;

转自邓凡平 《深入理解Android:Wi-Fi,NFC和GPS》章节连载[节选]--第七章 深入理解Wi-Fi P2P部分节选

本章主要内容: 介绍Wi-Fi P2P相关知识: 介绍Android中WifiP2pService.wpa_supplicant的相关代码. 7.1  概述 承接第6章介绍的WSC,本章将继续介绍Wi-Fi Alliance(Wi-Fi联盟)推出的另外一项重要技术规范Wi-Fi P2P.该规范的商品名为Wi-Fi Direct,它支持多个Wi-Fi设备在没有AP的情况下相互连接. 在Android平台的Wi-Fi相关模块中,P2P的功能点主要集中在: Android Framework中的Wif

龙书D3D11 Demo配置(VS2017+win7)

首先要感谢此博主的文章:https://blog.csdn.net/tjj00686/article/details/49110501  帮助了我. 我的龙书示例Demo代码来源:https://github.com/DrinkMoon/directx11-pratices 之前一直用VS 2010,突然心血来潮装了VS 2017,结果就有了此片随笔备忘. 先说结果:win7下面虽然能用VS2017编译成功,但是运行不了Demo. 提示:Demo的d3dDemo.sln文件位置:Exercise

c++ Primer 第五版习题答案第三章

3.2 编写程序,从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词. void readByLine () {    string line; ?    while (getline (cin, line)) {        cout << line << endl;   }   } void readByWord () {    string word; ?    while (cin >> word) {        cout << wo

概率论与数理统计 第四版 课后习题答案 习题解析

<概率论与数理统计第四版>是普通高等教育“十一五”国家级规划教材,在2001年出版的概率论与数理统计(第三版)的基础上增订而成. 本次修订新增的内容有:在数理统计中应用Excel,bootstrap方法,P值检验法,箱线图等:同时吸收了国内外优秀教材的优点对习题的类型和数量进行了渊整和充实. 获取方式见文末 概率论与数理统计(第四版) 课后习题解析 第1章 概率论的基本概念课后习题答案 第2章 随机变量及其分布课后习题 第3章 多维随机变量及其分布课后习题 第4章 随机变量的数字特征课后习题

龙书11_chapter_4 一: GameTime解读

看龙书DX11,首先是第四章,本文对GameTime类进行解释 问:此类主要实现了什么功能? 答:Returns the total time elapsed since Reset() was called, NOT counting any time when the clock is stopped. 从渲染窗口Reset开始记时,记录总共的时间.不包括此间的pause时间. 问:关键时间接口? 答: 1. //获取频率(取决于主板 OS相关,不是CPU的主频)参考:http://www.

《七周七语言:理解多种编程范型》のruby课后习题答案

本系列是<七周七语言>的课后习题答案.这本书不拘泥于语法细节,而是横向比较各种编程语言(非热门)之间的编程范式. 是本对编程觉悟能有所帮助的好书,这里就不多做介绍了,感兴趣的同学不妨去看一下. 不得不说,Ruby的风格很黑客. 1. 打印字符串"Hello, world." puts "Hello, world." 2. 在字符串“Hello, Ruby.”中,找出"Ruby."所在下标. puts "Hello, Ruby