
| Lesson
1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16 Lesson 17 Lesson 18 Lesson 19 |
Lesson 5 Light and MaterialObjects illuminated by light appear more realistic. By default, the zeLight object uses object color to calculate its final color. You can change this behavior using zeMaterial. You can also instruct zeLight to use a light model instead of object color for lighting. The effects of light and material can be confined within a node by forcing the node to save and restore all the OpenGL attributes.
require("register")
render, scene, node, shape, xyz, nor, light, material
= zeGrf.new("render" ,"scene", "node", "polygon",
"vertex", "vertex", "light", "material")
render:add(scene)
scene:set{node = node}
node:add(light, material, shape)
shape:set{vertex = xyz, vertex_normal = nor, color = {0, 1, 1, 0}, type = "triangles"}
light:set{position = {90, 90, 90}}
material:set{ambient = {0.19125, 0.0735, 0.0225, 1, 0},
diffuse = {0.7038, 0.2705, 0.0828, 1, 0},
specular = {0.2567, 0.1376, 0.0860, 1, 0},
shininess = {12.8, 0}}
--[[The utility library has functions to create various shapes.
zeArray objects of double floating type are used for input and output of
vertex, normal, and texture coordinate.]]
arr = zeUtl.new("double")
zeMake.sphere2(arr, 100, 5)
--Transfer data from array to vertex
xyz:add(arr)
--Shift normal data to the front and transfer them to the normal
arr:shift(3)
nor:add(arr)
--At first we use light only.
material:set{enable = false}
render:tofile("sphere1.png")
-- then we color the shape white and activate the light model.
shape:set{color = {1, 1, 1, 1}}
light:set{ambient = {0, 0.6, 0, 1}, lightmodel = true}
render:tofile("sphere2.png")
-- and finally use the material.
material:set{enable = true}
light:set{ambient = {0.2, 0.2, 0.2, 1}}
render:tofile("sphere3.png")
sphere1.png, sphere2.png, and sphere3.png Here, we use the zeMaker to create the sphere surface consisting of triangles. The code creates both a light object and a material object. But in rendering to sphere1.png, the material object is disabled and the white light illuminates the colored sphere. In the second output, the sphere is white; and the light model determines its color. In the last output the white sphere is assigned a copper material. |