第二章——灯光使用模块

二、 灯光模块的使用方法。

2.1 定向光

代码清单如下:
  final DirectionalLight directionalLight = new DirectionalLight();
  directionalLight.setLookAt(MathC.Vector.ZERO);
  directionalLight.setPower(1.5f);
  directionalLight.enableLookAt();
  addLight(directionalLight);

  Material sphereMaterial = new Material();
  SpecularMethod.Phong phongMethod = new SpecularMethod.Phong();
  phongMethod.setShininess(180);
  sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
  sphereMaterial.setSpecularMethod(phongMethod);
  sphereMaterial.enableLighting(true);
  1. 首先创建一个定向光对象,这个是不会陌生的,这里不在赘述。
  2. 设置定向光源的方向,setLookAt(MathC.Vector.ZERO),可以直接设置规定好的几个方向,可以点击这个枚举类中查看一下,有z轴负方向跟x轴正方向等,也可以设置一个方向向量,setLookAt(float x, float y, float z)。
    @param x y z 这个接口接受一个方向向量。
  3. setPower(1.5f)设置光源的半径,也就是光源的半径是多少。
    @param radio 该参数接受一个float类型的参数来规定光源的半径是多少。
  4. addLight(directionalLight)将光源对象加入到渲染队列中进行渲染。
  5. 创建一个材质对象,准备将光源的渲染对象插入到材质对象的着色器中,进行光源的渲染。
  6. 创建一个phone光源模型,这里不在赘述,知道需要这么创建就可以了。
  7. 设定模型对象表面的粗糙度,这参数跟镜面光的强度有关系,如果粗糙度越低,那么镜面光越强,镜面光照也被称为高光。
    @param shininess 表示模型表面粗糙度的参数。
  8. 创建一个Lambert光照模型,这样光照的强度会随着距离的远近来调节,这是lambert光照模型(知道步骤即可)并将其设置到模型的材质中。
  9. 将phone光照模型设置到模型的材质中,并且打开光源的开光,将光源渲染加入到场景渲染中。
说明:

将创建好的材质设置到模型材质中,这样就可以给模型添加灯光了。这一点可以直接参考前面是如果给ui设置材质函数的设置,这里不在赘述这些步骤了。

2.2 定向光

代码清单如下:
PointLight pointLight = new PointLight();
pointLight.translateAbs(0,0,0);
pointLight.setPower(1.5f);
pointLight.setLookAt(0, 0, 0);
addLight(pointLight);

Material sphereMaterial = new Material();
sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
SpecularMethod.Phong phongMethod = new SpecularMethod.Phong();
phongMethod.setShininess(180);
sphereMaterial.setSpecularMethod(phongMethod);
sphereMaterial.setAmbientIntensity(0, 0, 0);
sphereMaterial.enableLighting(true);

注意:接口在上面大部分都介绍过,这边都不在赘述一些重复的接口了,下面就介绍一些上面没有介绍过的接口。

  1. translateAbs(0,0,0)设置光源的位置,因为光照对象及ANode的子类,当然你拥有模型的某些属性。
    @param x y z 设置光源在3D场景中的位置。
  2. setLookAt(0, 0, 0)设置光源的观测点,也就是光源的直照的点。
  3. setAmbientIntensity(0, 0, 0)设置环境光的强度,这个值是环境光的设置接口参数。
    @param r g b 三个颜色通道的颜色,这边直接设置0-1之间就可以。
说明:这里介绍了接口的使用,可以直接查看接口中的方法,这样就可以更好的使用。

2.2 聚光灯

    final SpotLight spotLight = new SpotLight();
    spotLight.setPower(1.5f);
    spotLight.enableLookAt();
    spotLight.setPosition(0, 4.0, 0.0);
    spotLight.setLookAt(0, 0, 0);
    addLight(spotLight);

    Material sphereMaterial = new Material();
    sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    SpecularMethod.Phong phongMethod = new SpecularMethod.Phong();
    phongMethod.setShininess(180);
    sphereMaterial.setSpecularMethod(phongMethod);
    sphereMaterial.setAmbientIntensity(0, 0, 0);
    sphereMaterial.enableLighting(true);

注意:大部分的接口都介绍过了,这里不在赘述了,可以查看上面的接口设置。

  1. setCameraW(float[] cameraW)这个接口可以将摄像机与定向光与聚光灯绑定到一起。
    @param cameraW 这个参数是摄像机的w向量值。

你可能感兴趣的:(第二章——灯光使用模块)