3.3 Light
There are all kinds of lights in the theater, and the same in three-dimensional rendering scene, there can be more than one light. Lighting and cameras are a must-have for three-dimensional rendering scenarios, and if not specified (like the 3.1.1_rendercylinder example, we don't assign cameras and lighting to renderer), Vtkrenderer automatically creates default lighting and cameras. VTK uses class Vtklight to represent the lighting in the rendered scene. Similar to real-world lighting, vtklight instances in VTK can also be turned on, off, set the color of the light, illuminate the position (i.e. focus), where the light is located, intensity, etc.
Vtklight can be divided into positional illumination (positional light, i.e. spotlight) and directional illumination (Direction light). Position lighting is a position of the light source in the rendered scene, you can specify the attenuation value of the light, cone angle, etc., the direction light is the light source position at infinity, the light can be considered parallel, such as the natural sun light. The location of the light source and the focus line define the direction of the light, and the default vtklight is the directional light.
The usual methods of Vtklight are:
SetColor ()-Sets the color of the light, specifying the color as RGB.
SetPosition ()-Sets the lighting position.
Setfocalpoint ()-Sets the lighting focus.
setintensity ()-Sets the intensity of the light.
Setswitch ()/ Switchon()/ Switchoff()-turns the corresponding light on or off.
When we talk about Vtkprop, the class has methods setvisibility (int)/ getvisibility()/Visibilityon()/ Visibilityoff() to control the visible and invisible properties of the Vtkprop object. Similarly, there are similar naming styles in Vtklight:setswitch()/getswitch()/Switchon()/ Switchoff ()。 It is not difficult to find that in VTK, a property is set to take this kind of method, take vtklight as an example, Switchon () and Setswitch (1) Implementation of the same effect, Switchoff () is the same as Setswitch (0), Getswitch () is the value used to get the Vtklight object to close or open this property. If a class has a setxxx () method provided, the GetXXX () method is generally provided to get the corresponding value, as we have already learned in the previous section. For example, Vtklight also offers:setpositional()/ getpositional()/ Positionalon()/ Positionaloff() A class of methods to set location lighting.
In the header file VtkLight.h of Class Vtklight, there is no prototype for a method like Setswitch ()/getswitch (), but you can see the following lines:
Vtksetmacro (Switch,int);
Vtkgetmacro (Switch,int);
Vtkbooleanmacro (Switch,int);
As the name implies, Vtksetmacro/vtkgetmacro/vtkbooleanmacro are macros, the expansion of these macros is the prototype of the above function. To ensure the cleanliness and reusability of the Code, VTK in the file vtkSetGet.h defined a large number of macros, when you can not find a prototype of a function, perhaps these functions are defined by the macro expansion later, so, when reading the VTK source code, you may want to right click on a function or keyword, and then click on the pop-up menu "Go to Definition ... ", it's clear at a glance.
How to use the Illumination Vtklight (shown in program execution result 3.5):
Vtksmartpointer<vtklight> mylight= vtksmartpointer<vtklight>::new ();
Mylight->setcolor (0,1,0);
Mylight->setposition (0,0,1);
Mylight->setfocalpoint (Renderer->getactivecamera ()->getfocalpoint ());
Renderer->addlight (Mylight);
Vtksmartpointer<vtklight> mylight2= vtksmartpointer<vtklight>::new ();
Mylight2->setcolor (0,0,1);
Mylight2->setposition (0,0,-1);
Mylight2->setfocalpoint (Renderer->getactivecamera ()->getfocalpoint ());
Renderer->addlight (MYLIGHT2);
The above defines two Vtklight objects, one for the green light, the position in the (0,0,1), the focus on the camera focus, the other for the blue light, the position in the (0,0,-1), the focus on the camera focus, and the last two light calls Vtkrenderer method Addlight () Added to the render scene because there can be multiple lights in the renderer, so the interface provided by VTK is Addlight () instead of setlight ().
Figure 3.5 Example of the effect of 3.1.1_rendercylinder adding green and blue illumination
3.4 Camera
The viewer's eye is like a camera in a three-dimensional rendering scene, and VTK is a Vtkcamera class to represent a camera in a three-dimensional rendering scene. Vtkcamera is responsible for projecting three-dimensional scenes into two-dimensional planes, such as screens and images. Figure 3.6 is the camera projection.
As shown in Figure 3.6, the main factors related to camera projection are:
L Camera Position: that is, the location of the camera, using the method Vtkcamera::setposition() settings.
L Camera Focus: Use the method Vtkcamera::setfocuspoint() setting, the default focus position at the origin of the world coordinate system.
L Upward Direction: that is, which direction is the camera facing up. Just as we look upright, the direction is head up, the things we see are also upright, if we look at something upside down, then the direction of the head facing down, the thing we see is upside down. Camera position, camera focus, and top three factors determine the camera's actual orientation, which is to determine the camera's view.
Figure 3.6 Camera Vtkcamera projection
L Projection Direction: the vector direction of the camera position to the camera focus is the projection direction.
L Projection Method: Determines how the actor is mapped to a planar image. Vtkcamera defines two projection methods, the orthogonal projection (orthographicprojection), also called parallel projection (Parallel Projection), in which the light entering the camera is parallel to the projection direction. The other is perspective projection (perspectiveprojection), where all rays intersect at a point.
L Angle of view: the perspective projection needs to specify the camera's viewing angle (view Angle), the default view size is 30º, you can use the method Vtkcamera::setviewangle() settings.
L Front and rear clipping plane: The clipping plane intersects the projection direction, and the projection direction is also perpendicular. The clipping plane is primarily used to evaluate the distance between the actor and the camera, and only the actor between the front and back clipping planes is visible. The location of the clipping plane can be set using the method Vtkcamera::setclippingrange().
If you want to get the default camera in Vtkrenderer, you can use the method Vtkrenderer::getactivecamera().
How to use the camera Vtkcamera:
Vtksmartpointer<vtkcamera>mycamera = Vtksmartpointer<vtkcamera>::new ();
Mycamera->setclippingrange (0.0475,2.3786); These values are arbitrarily set, in order to demonstrate usage.
Mycamera->setfocalpoint (0.0573,-0.2134,-0.0523);
Mycamera->setposition (0.3245,-0.1139,-0.2932);
Mycamera->computeviewplanenormal();
Mycamera->setviewup ( -0.2234,0.9983, 0.0345);
Renderer->Setactivecamera(Mycamera);
Above we use Setclippingrange (); Setfocalpoint (); SetPosition () sets the front and back clipping plane, focus and position of the camera respectively. The Computeviewplanenormal () method recalculates the normal vector of the view plane (view Plane) based on information such as the camera position, focus, and so on. In general, the normal vector and the visual plane are perpendicular, if not vertical, actors and so on will appear to have some special effects, such as the wrong cut. The Setviewup () method is used to set the camera in the upward direction. Finally, the camera is set to the rendering scene using the method Vtkrenderer::setactivecamera ().
In addition to providing a method for setting the camera projection factor, Vtkcamera provides a number of ways to control the motion of the camera, such as: Vtkcamera::Dolly(), Vtkcamera:: Roll (), Vtkcamera::Azimuth(), Vtkcamera::Yaw(), Vtkcamera::elevation(), Vtkcamera::Pitch (), Vtkcamera::Zoom(). These methods specifically indicate how the camera is moving, as well as the relative position or direction of movement, refer to Figure 3.7 or the document description of the class Vtkcamera.
Figure 3.7 Camera Motion direction
Figure 3.7 is the execution result of the script file Camera.tcl (in the VTK source directory: VTK\HYBRID\TESTING\TCL\CAMERA.TCL) and CAMERA2.TCL. In the 1th chapter, we mentioned TCL script file, it is recommended that you install Vtk-5.10.0-win32-x86.exe this program, if you install the program, you can now execute the script in Figure 3.7.
There are two ways to run a TCL script. The first is to open the VTK Tcl script from the Windows Start menu, and the interface shown in Figure 3.8 appears.
Figure 3.8 Vtk-5.10.0-win32-x86 Program Run interface
Next, enter "source Xxxx.tcl" after "%" and then enter to execute the corresponding TCL script file. XXXX.TCL If you enter an absolute path, be aware that the slash in the path is "/" instead of "\", and if the path has spaces, enclose the entire path in English double quotation marks.
For example: Source "D:/programfiles/vtk/camera.tcl" (then enter, you can run Tcl script)
The second method is: After installing the Vtk-5.10.0-win32-x86.exe program, add the path of the Vtk.exe to PATH environment variable, then open the cmd window, in the cmd window directly input: VTK XXXX.TCL, enter the same can execute TCL script file. The *.tcl file can be opened in Notepad.
3.5 Colors
In the previous section we mentioned the actor's attribute, which is one of the more important attributes of the actor. The VTK uses RGB and HSV two color systems to describe colors.
The RGB color system is represented by a combination of three color components: Red (R), Green (G), and Blue (B), in VTK the values for these three components are from 0 to 1, (0, 0, 0) for Black, (1, 1, 1) for White. Vtkproperty::setcolor (r,g, B) uses the RGB color system to set the Color property value.
The HSV color system is also determined by the three components of the color, namely: Hue (hue), is the basic property of the color, is what we usually say the name of the color, such as red, yellow, etc., the saturation (saturation), refers to the purity of the color, the higher the value of the purer; values (value, i.e. intensity intensity or brightness bright), a value of 0 usually represents black, and a value of 1 indicates the brightest color. The value range for these three components is also 0 to 1. The Vtklookuptable class provides methods for the HSV color system settings.
Table 3.1 lists the comparison of common color RGB and HSV values.
Color |
Rgb |
Hsv |
Dark Black |
0, 0, 0 |
*, *, 0 |
White |
1, 1, 1 |
*, 0, 1 |
Red |
1, 0, 0 |
0, 1, 1 |
Green Greens |
0, 1, 0 |
1/3, 1, 1 |
Blues Blue |
0, 0, 1 |
2/3, 1, 1 |
Yellow Yellow |
1, 1, 0 |
1/6, 1, 1 |
Blue-Green Cyan |
0, 1, 1 |
1/2, 1, 1 |
Magenta Magenta |
1, 0, 1 |
5/6, 1, 1 |
Blue sky Blue |
1/2, 1/2, 1 |
2/3, 1/2, 1 |
Table 3.1 Comparison of common color RGB and HSV values
The VTK class associated with color settings has vtkcolortransferfunction in addition to vtkproperty,vtklookuptable. Both Vtklookuptable and vtkcolortransferfunction derive from Vtkscalarstocolors.
3.6 Coordinate systems and spatial transformations 3.6.1 coordinate system
In computer graphics, there are four main coordinate systems, namely: Model coordinate system, world coordinate system, view coordinate system and display coordinate system, and two ways of representing coordinate points: screen pixel value and normalized coordinate value (each axis value is [-1, 1]). The relationship between them is shown in 3.9.
The model coordinate system is the coordinate system used to define the models, usually local Cartesian coordinate systems. For example, to define an actor that represents a sphere, the general practice is to define the sphere in a column coordinate system.
The world coordinate system is a three-dimensional space coordinate frame where actors are placed, and one of the actors is responsible for transforming the model from the models coordinate system to the world coordinate system. Each of these models can have its own model coordinate system, but only one of the world coordinate systems, and each actor must transform the model coordinates into the world system by scaling, rotating, panning, and so on. The world coordinates are also the coordinate systems in which the camera and the light are located.
The view coordinate system represents the coordinate system that the camera sees. The X, y, Z axis values are [-1, 1],x, y values represent positions on the image plane, and the Z-value represents the distance to the camera. The camera is responsible for transforming the world coordinate system into the view coordinate system.
Figure 3.9Model, world, view, and display coordinate systems
The display coordinate system is similar to the view coordinate system, but the values for each axis are not [-1, 1], but the screen pixel values are used. The size of the different Windows displayed on the screen affects the mapping of the view coordinate system's coordinate values [-1, 1] to the display coordinate system. Different rendering scenes can be displayed in the same window, for example, in a window, divided into the left and right two rendering scenes, the rendering scene (Vtkrenderer) is a different viewport (Viewport). Example 3.6.1_viewport demonstrates dividing a window into four viewports, using Vtkrenderer::setviewport() to set the viewport's range (value [0, 1]):
Renderer1->setviewport (0.0,0.0,0.5,0.5);
Renderer2->setviewport (0.5,0.0,1.0,0.5);
Renderer3->setviewport (0.0,0.5,0.5,1.0);
Renderer4->setviewport (0.5,0.5,1.0,1.0);
Program execution results are shown in 3.10.
In VTK, the model coordinate system is used relatively little, and the other three coordinate systems are used frequently. The transformations between them are managed by the class vtkcoordinate. Coordinate systems can be divided according to coordinate point units, range of values, etc.:
The coordinates of the DISPLAY -X and Y-axes are the pixel values of the rendered window. The coordinate origin is located in the lower left corner of the render window, which is the same for all two-dimensional coordinate systems in the VTK, and the coordinate system in the VTK is in the right-handed coordinates.
The range of Normalizeddisplay -X and y-axis coordinates is [0, 1], which is defined in the rendering window, just like display.
The coordinate values of L VIEWPORT-X and y are defined in the viewport or renderer (Renderer).
L normalizedviewport -X, y-coordinate values are defined in the viewport or renderer with a value range of [0, 1].
The VIEW-X, Y, and z coordinate values are defined in the coordinate system where the camera is located and the value range is [-1, 1],z value indicates depth information.
The world-X, Y-, and z-coordinate values are defined in the global coordinate system, as referenced in Figure 3.9.
L userdefined-user custom coordinate system.
The Vtkcoordinate class provides a way to set up the above coordinate system:
Setcoordinatesystemtodisplay ()
Setcoordinatesystemtonormalizeddisplay ()
Setcoordinatesystemtoviewport ()
Setcoordinatesystemtonormalizedviewport ()
Setcoordinatesystemtoview ()
Setcoordinatesystemtoworld ()
Figure 3.10 Example 3.6.1_viewport execution results
3.6.2 Spatial Transformations
We define the three-dimensional model in three-dimensional space, the final display is projected to a two-dimensional plane, such as on-screen display, the generation of two-dimensional images and so on. Three-dimensional to two-dimensional projections include perspective projection (perspectiveprojection) and orthogonal projections (orthogonal Projection, orthogonal projections are also called parallel projections).
Whether it is the perspective projection or the orthogonal projection, the basic elements of the transformation are three-dimensional coordinate points, in computer graphics, three-dimensional coordinate points are usually represented by homogeneous coordinates. For example, there is a coordinate point (x,y,z) in the three-dimensional space, which is expressed in homogeneous coordinates (xh,yh,zh,wh), where
Style 3-1
If wh = 0, the point is infinitely far away.
Using homogeneous coordinates, the spatial transformation can be represented by a 4x4 matrix. For example, affine transformations, such as translation, rotation, scaling, and so on, can be expressed as matrix multiplication:
L Pan. Sets the point (x, y, z) of the Cartesian coordinate system, seeking the result of the Keihei shift vector (tx, ty,tz) (x ' ,y ',z ').
First, the translation matrix TTis constructed first:
Style 3-2
You have to translate the result to:
Style 3-3
If the Cartesian coordinate system is expressed as:
Style 3-4
L Zoom. Similarly, we can also construct a scaling matrix TS.
Style 3-5
Wherein Sx, Sy, Sz are the scaling factor of x, Y, Z axis respectively.
L Rotate. The rotation angle of the X, Y, and Z axes is α,β,γ, respectively:
Style 3-6
The classes associated with spatial transformations in VTK are: Vtktransform2d,vtktransform,vtkperspectivetransform,vtkgeneraltransform,vtktransformfilter, Vtkmatrix4x4 and so on.
3.7 Summary of this chapter
This chapter starts with a slightly more complex VTK program and provides an insight into some of the basic concepts of VTK.
First we learned from Vtkcylindersource a group of VTK classes: Vtkxxxsource. This set of classes derives from Vtkpolydataalgorithm, and they output data types that are Vtkpolydata, and all classes are VTK predefined graphical models.
Next, we use the output of the Vtkxxxsource as the input of the vtkpolydatamapper, and the function of Mapper is to convert the input data into geometric elements (points, lines, polygons) for rendering.
Then we instantiate an actor, because we know that the visual representation of the data in the VTK rendering scene is in the Vtkprop subclass. The corresponding mapper can be set by the actor's Method Setmapper ().
Then, we instantiate the rendering window, renderer, interaction type and other objects, and introduce them one by one. So we understand that the rendering window is the VTK window we see, and the renderer renderer can be added to the render window by means of the render window Addrenderer (), and the interface is used by the method Setrenderwindow () to specify the window of interaction. Interactive styles and interactions are associated with methods Setinteractorstyle (), and several actor objects can be added to the renderer using Method Addactor (). Together, the simple and complex relationship between them can be 3.11来 with the figure. (It can be found that these objects are connected to each other in a line structure, which is a very important concept in VTK: VTK pipeline structure.) )
Figure 3.11 Examples of the relationship between 3.1.1_rendercylinder objects
We then looked at objects such as lighting and cameras in the rendered scene, knowing that there could be multiple lights in the rendering scene, but only one camera, and how to set the lighting and the camera.
When we introduced Vtkactor, we changed the actor's color by Vtkproperty object, so we introduced the color model used by VTK, respectively, RGB color model and HSV color model.
Finally we talk about the "coordinate system and the spatial transformation" that seems to be not the same as the previous content, and understand that VTK uses the coordinate system of model, world, view and display, which can be transformed by class vtkcoordinate. Then it introduces the concept of using homogeneous coordinates to do spatial transformation in VTK, and lists the transformation matrices such as translation, zooming and rotation. In fact, this part of the content is closely related to the previous introduction, for example, the actor in the rendering scene through the camera projection to the image plane, the use of these content. The Vtkrenderer Method Viewtoworld () and Worldtoview () will also use spatial transformations.
The content of this chapter is VTK the most basic things, it can be no exaggeration to say that the following three-dimensional examples of the basic will use the concept described in this chapter, so, the importance of mastering the content of this chapter is conceivable.
3.8 References in this chapter
[1] VTK User's Guide (11th Edition)
[2] "The visualization toolkit–anobject-oriented approach to 3D Graphics (4th Edition)"
========== Welcome reprint, Reprint, please retain the statement information ========== Copyright@ Dong Ling StudioAll, for more information please visitDong Ling Studio
03-VTK Basic Concepts (2)