绘制6个球体,利用light函数和lighting函数设置光照效果。程序中光源方向由p=[1,-2,1]决定,光线的颜色为白色,光线平行照射在球体上。 light('color','w','style','infinite','position',p) subplot(2,3,3) sphere(N); light('color','w','style','infinite','position',p) lighting flat shading flat subplot(2,3,4) sphere(N); light('color','w','style','infinite','position',p) lighting phong shading interp subplot(2,3,5) sphere(N); light('color','w','style','infinite','position',p) lighting gouraud shading interp subplot(2,3,6) sphere(N); light('color','w','style','infinite','position',p) lighting none 要求:把style由“infinte”改为“local”,即由平行光改为点光源,观察有什么不同;把 shading flat和shading interp注释掉,分析不同,为什么?
时间: 2023-06-09 14:04:17 浏览: 124
好的,您的第一个问题是如何绘制球体并设置光照效果,以下是代码:
``` matlab
% 绘制6个球体,利用light函数和lighting函数设置光照效果
% 程序中光源方向由p=[1,-2,1]决定,光线的颜色为白色,光线平行照射在球体上
p = [1, -2, 1]; % 光源方向
N = 30; % 球面细分级别
% 绘制第一个球体
subplot(2,3,1);
sphere(N);
light('color', 'w', 'style', 'infinite', 'position', p);
lighting flat;
shading flat;
% 绘制第二个球体
subplot(2,3,2);
sphere(N);
light('color', 'w', 'style', 'infinite', 'position', p);
lighting phong;
shading interp;
% 绘制第三个球体
subplot(2,3,3);
sphere(N);
light('color', 'w', 'style', 'infinite', 'position', p);
lighting gouraud;
shading interp;
% 绘制第四个球体
subplot(2,3,4);
sphere(N);
light('color', 'w', 'style', 'infinite', 'position', p);
lighting flat;
shading interp;
% 绘制第五个球体
subplot(2,3,5);
sphere(N);
light('color', 'w', 'style', 'infinite', 'position', p);
lighting phong;
shading flat;
% 绘制第六个球体
subplot(2,3,6);
sphere(N);
light('color', 'w', 'style', 'infinite', 'position', p);
lighting none;
shading interp;
```
若要改为点光源,只需要将 `style` 参数改为 `local` 即可,修改后的代码如下:
``` matlab
p = [1, -2, 1]; % 光源方向
N = 30; % 球面细分级别
% 绘制第一个球体
subplot(2,3,1);
sphere(N);
light('color', 'w', 'style', 'local', 'position', p);
lighting flat;
shading flat;
% ... 绘制其他球体的代码省略
```
从结果来看,变化很明显,球体上的明暗效果变成了点光源照射下的效果,与平行光不同。
接下来注释掉 `shading flat` 和 `shading interp` ,分析不同,代码如下:
``` matlab
% 绘制第一个球体
subplot(2,3,1);
sphere(N);
light('color', 'w', 'style', 'local', 'position', p);
lighting flat;
% shading flat;
% ... 绘制其他球体的代码省略
```
在上述代码中,我们将 `shading flat` 或 `shading interp`注释掉,这样就不会渲染出不同类型的明暗效果,只有平淡的颜色效果。当我们去掉 `shading flat` 后,可以看到球体明暗效果变成了一些斑点,这是因为默认情况下球体会采用 Gouraud 着色模式,用三角网格拟合曲面,并在三角形上进行线性插值,使得较大的光照区域较暗的区域之间出现明显的过渡。而去掉 `shading interp` 后,球体的颜色均匀且不带有任何暗斑点,这是因为它采用的是平面着色模式,将每个像素的颜色设定为整个面的颜色。
总之,`shading` 函数是决定明暗处理的关键函数。通过修改它的参数,可以使得球体绘制效果完全不同。
阅读全文
相关推荐















