
| 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 2 Render, Scene, and NodezeRender, zeScene, and zeNode are the foundamental objects that a scene must have. The zeRender object determines the background color and the size of the image, which by default are white and 512 by 512 pixels, respectively. The zeRender object is responsible for saving the image of a scene to a file or send the image to a window. You can have more than one zeScene objects in a zeRender object, which is a way to put multiple subplots in a graph. The zeScene object determines the view port and the default foreground color of all objects in the scene. When only one scene is used, the view port of the scene should usually be set to cover the entire rendering area. This is done automatically when a scene is added to a render. But if you change the image size after added a scene, you should reset the view port of the scene as well. By default the view port is x=0, y=0, width=512, and height=512 in pixel unit. The x and y of the view port origin are referenced to the lower left corner of the rendering area of zeRender. A zeScene object can have only one zeNode object as the root node. The zeNode object can further alternate the foreground color. Each object in a node may have its own color as well. Any object whose color has never been set will use the color of node or scene that hosts it. All types of zeGraph object except zeRender and zeScene can be added to a zeNode object. An attempt to put a zeRender or a zeScene into a zeNode will be quietly ignored. The zeNode object has a very important function: it can save all the OpenGL attributes before sending the rendering command to its child objects and restore the attributes in the end. You can use this feature in such a situation as confining blending effect within the node. The following code uses the Lorens abstractor function to demonstrate the basics of using these three objects. Paul Bourke has an amazing web site introducing 3D visualizations. The Lorenz equation is one of the many fractal equations in his site.
require("register")
-- Create necessary objects.
render, scene, node, dot, xyz, rgb =
zeGrf.new("render", "scene", "node", "point", "vertex", "color")
-- Construct the scene
render:add(scene)
scene:set{node =node}
node:add(dot)
dot:set{vertex = xyz, vertex_color = rgb}
-- Generate dots using the Lorenz equation.
a = 10; b = 28; c = 8 / 3; dt = 0.01
x0 = 1; y0 = 1; z0 = 1
for t = 0, 30000 do
x1 = x0 + dt * a * (y0 - x0)
y1 = y0 + dt * (x0 * (b - z0) - y0)
z1 = z0 + dt * (x0 * y0 - c * z0)
xyz:add(x1, y1, z1)
-- simply use cosine function to confine colors
-- in the range of 0 and 1.
rgb:add(math.cos(z1), math.cos(y1), math.cos(x1))
x0 = x1; y0 = y1; z0 = z1
end
-- The ranges of x and y are too small.
-- We scale the root node to have a better view.
node:scale(7, 5, 1)
render:tofile("lorenz.png")
lorenz.png This is probably one of the shortest code you can find to generate a fairly complex graph. The dots are colored by simply applying Lua's sine and cosine math functions to the x-y-z values of vertices to confined them between 0 and 1, which is the required the range of red, green, blue, and alpha components of color. The image can be saved as other formats by changing the file extension to ",jpg", ".tif", or ".bmp". |