|
|
11 |
| Collaboration and development with OpenGL |
|
|
OpenGL (Open Graphics Library) is a library for developing applications that use 3D graphics, and by creating plugins in conjunction with the UC-win/Road SDK, it becomes possible to achieve attractive 3D representations. This time, we will explain how to use OpenGL with the SDK using sample code.
|
OpenGL (Open Graphics Library) is a library for developing applications that use 3D graphics. By using OpenGL, you can develop programs with 3D capabilities using programming languages such as C and C++, or development tools like Delphi, as explained in this book [Figure 01]. UC-win/Road was also developed using OpenGL.
 |
| ■ Figure 01: An example of a 3D image rendered using OpenGL |
Programs developed using OpenGL can not only generate three-dimensional images, but also animate and display them in real time. OpenGL is based on a library called IRIS GL, originally developed by SGI (formerly Silicon Graphics). This library was designed for developing 3D graphics programs that operate in real time. However, the technology was made public to companies other than SGI, leading to joint development efforts across various companies, which marked the beginning of OpenGL.
Since the release of OpenGL 1.0, it has evolved along with advancements in 3D graphics technology, and is now available up to version 4.0. Furthermore, OpenGL can be used not only on Windows but also on Mac OS, UNIX, and operating systems for mobile devices such as cell phones.
Let's explain the drawing mechanism of OpenGL in a simple way. In OpenGL, even complex three-dimensional figures are drawn by combining very simple shapes called "primitives," such as points, lines, polylines, triangles, connected triangles, quadrilaterals, connected quadrilaterals, and polygons. Only these primitive shapes are directly responsible for drawing within OpenGL itself.
When a function is used to call another function that draws OpenGL primitives, data such as vertex coordinates and normal vectors are sent to the graphics board. This data then goes through a "geometry stage," where processes like coordinate transformations for each vertex and lighting calculations are performed, and a "rasterization stage," where the color of each pixel on a surface is calculated from the vertex colors, textures are mapped onto the object to create a more realistic appearance, and these operations are carried out pixel by pixel. Finally, an image is generated that is displayed on the screen.
The calculations performed at these stages are executed in parallel for each vertex or pixel, enabling the generation of three-dimensional images at high speed. This is a significant advantage when developing 3D applications using 3D graphics libraries such as OpenGL.
|
What can be done with OpenGL? |
|
Using OpenGL, developers can efficiently and easily create 3D image generation processes for applications, enabling real-time animation, interactive and realistic 3D representations, and other features. It also includes shader functions for expressing shadows on 3D objects, which allows for even more realistic visual effects to be achieved with ease. Due to these characteristics, OpenGL is used in the development of various 3D graphics applications, such as driving simulators, flight simulators, design support software like 3D CAD, and entertainment applications for games and mobile phones.
In FORUM8 products, including UC-win/Road, OpenGL is used in the development of civil engineering and architectural design applications within the UC-1 series. For example, in UC-win/Road, it's utilized for features ranging from landscapes depicting cityscapes, roads, cars, and buildings, to elements like fire, smoke, headlights, and weather effects. Furthermore, by using the UC-win/Road SDK, developers can utilize OpenGL to implement their own functionalities within UC-win/Road.
|
Examples of UC-win/Road plugins using OpenGL |
|
Some FORUM8 products, such as the Exodus plugin, the xpswmm plugin, and the Speed Meter plugin (one of the sample plugins included with the UC-win/Road SDK), use plugins to call OpenGL drawing commands and perform direct rendering on UC-win/Road
For example, when using the xpswmm plugin, the results of simulations of river flooding performed in xpswmm are displayed on the UC-win/Road interface as contours and arrows. This is achieved by the plugin itself, which uses OpenGL to draw these elements on the UC-win/Road screen based on the data from the xpswmm simulation results. In this way, it becomes possible to visually confirm the simulation results in a VR environment.
|
Using OpenGL with the UC-win/Road SDK |
|
To use OpenGL drawing processing with the UC-win/Road SDK, you must first have the plugin you create inherit from the IF8OpenGLPlugin interface. Add IF8OpenGLPlugin to the inheritance source of the class declaration for the plugin you are creating.
When inheriting this interface, you must обязательно add 3 new functions: PaintScene, PaintHUD, and SetupCamera.
These functions should be added as public attributes within the class declaration, and the necessary processing should be written inside each function. Even if no processing is required, an empty function must be defined.
PaintScene is a function used to incorporate OpenGL rendering processes into the 3D simulation screen of UC-win/Road. PaintHUD is used when you want to perform 2D drawing on the screen after the drawing in 3D space has been completed.
For example, this might involve placing a speedometer on the driving simulation screen or rendering filters in a semi-transparent manner to process images. The SetupCamera section is where other calculations and update processes are implemented.
|
Examples of integration between the UC-win/Road SDK and OpenGL |
|
Let's learn how to use OpenGL with the SDK by actually creating sample plugins.
This time, we will create a simple plugin that only places a cube on the initial screen of UC-win/Road.
■Drawing processing using OpenGL
Before explaining the implementation of the SDK, let's provide a brief overview of OpenGL rendering processes. The rendering process in OpenGL generally follows this sequence:
1. Set the color material properties for the shapes being drawn
2. Configure coordinate transformations
3. Enter the drawing process by enclosing `glBegin` and `glEnd`
4. Specify the normal vectors and vertex coordinates of the shapes to be drawn |
Material properties
To simulate lighting from a light source, you need to set the material properties of the shapes being drawn. In OpenGL, realistic color changes due to lighting are simulated by using material attributes for both the light source and the surface of the object.
To set the material properties of an object in OpenGL, you use the function `glMaterialfv`, which takes 3 arguments. The first argument specifies which surface of the object (front or back) to apply the properties to. The second argument specifies whether to set the diffuse reflection, specular reflection, or ambient light components. Finally, the third argument is an array with 4 elements that specify the color of the property using red, green, blue, and opacity (alpha value). For the opacity, a value of 1.0 (opaque) is usually acceptable.
Coordinate transformation
If you want to move, rotate, or scale a 3D object, you need to perform coordinate transformations. OpenGL internally manages these transformations using matrices. Specifically, the "model-view transformation matrix" is responsible for transforming the vertex coordinates of an object into the view coordinate system. While it's possible to specify these transformation matrices from outside, basic coordinate transformations such as moving, rotating, and scaling are supported by OpenGL functions.
glBegin/glEnd
Next, we will proceed to the actual process of rendering using OpenGL. Within this rendering process, we specify parameters such as the normal vectors and coordinates of the shapes to be drawn. At the beginning of the rendering process, the `glBegin` function is used to initiate the drawing and specify the basic shape to be rendered; then, the vertices that make up the shape are specified using functions, and finally, `glEnd` is used to declare the end of the rendering.
`glBegin` takes 1 argument, which specifies the basic geometric shape (primitive) to be drawn. This can include points, line segments, polylines or closed polylines, triangles or a series of connected triangles, quadrilaterals or a series of connected quadrilaterals, and convex polygons.
Specifying normal vectors and vertices
Within the `glBegin` and `glEnd` functions for drawing, you specify the vertices of the shape to be drawn using a function called `glVertex`. This function has various versions, such as `glVertex2f`, `glVertex3f`, and `glVertex3fv`.
For `glVertex2`, specify two-dimensional coordinates (x, y), and for `gl`
For a Vertex3f object, you specify three-dimensional coordinates (x, y, z). Since this is a three-dimensional case, we use glVertex3fv and pass the three coordinates as an array.
Furthermore, OpenGL requires normal vectors for each vertex of a three-dimensional object in order to calculate lighting. The `glNormal3fv` function is used to specify these normal vectors. While there are several versions of this function, we will use this particular version. The arguments consist of an array containing the x, y, and z components of the normalized normal vector, where the length of each vector is 1.
Since the normal vectors are the same at each vertex of a face, it is sufficient to set them only once for each face when arranging the vertices.
■ Placing a cube on the initial screen of UC-win/Road
Now we will create sample programs using OpenGL with the UC-win/Road SDK.
Declaration section
First, copy the BasePlugin and change the name of the folder.
Next, in the plugin's declaration section, we add the GL unit and the F8OpenGL unit to the "uses" section to enable the use of OpenGL functions. The GL unit contains definitions for various OpenGL functions, while the F8OpenGL unit defines SDK-related OpenGL controls, types, and functions.
In the class declaration section, the plugin's class should inherit from the IF8OpenGLPlugin interface.
| TAPlugin = class(TupfVisualPlugin, IF8UserPlugin, IF8OpenGLPlugin) |
Add the function declarations that need to be defined within this interface to the public section.
public
...
procedure PaintScene;
procedure PaintHUD;
procedure SetupCamera; |
Also, since the location where the cube is placed is approximately 10 meters in front of the initial viewpoint position, we will create private variables to store the displayed positions. These variables will be of type GLPointType. This type is an array with 4 elements, each of which is a GLfloat value, and it is defined within the F8OpenGL unit.
We also create a boolean variable called "firstTime" that is initially set to True. This is because, within the PaintScene function, we only want to calculate the position 10 meters away from the viewpoint once, and store that value in a variable that will be used to determine the display location. After this initial calculation, this variable should be set to False.
procedure TAPlugin.upfVisualPluginCreate(Sender: TObject);
begin
Supports(ApplicationServices, IF8ApplicationServices, winRoadApplication);
position[_x] := 5000.0;
position[_z] := 5000.0;
end; |
|
Initialize 2 variables
|
|
Processing within PaintScene
In the drawing process, only the coordinates 10 meters away from the viewpoint are obtained initially. To achieve this, the difference between the camera's viewpoint and the point of focus is calculated, which is then used to determine the distance between the viewpoint and the point of focus. This distance is then used to obtain the x and z coordinates that are 10 meters away in the xz plane. These values are then set as the x and z coordinates for display.
The height of the cube's position is obtained using the function GetActualHeightAt. This function takes x and z coordinates as input and returns the ground level at that location. Since the cube is drawn with its center at the origin and a side length of 1 meter, we add 0.5 to this value to determine the height for display purposes.
After that, the material properties of the cube to be drawn are specified using glMaterialfv, and then the actual drawing is performed. When drawing, first, the current model-view transformation matrix is saved onto a stack using glPopMatrix. Then, finally, the model-view transformation matrix is restored using glPopMatrix. This is done so that subsequent transformation operations are only used in this specific section of code.
After saving the transformation matrix, use the glTranslatef function to perform a translation to the desired display position. The arguments of the function should be set to the display position that was saved earlier. This will cause the cube to translate parallel to the desired location and be drawn correctly. Then, proceed with the drawing process for the cube using OpenGL. Enclose the drawing commands within glBegin and glglEnd, and within those commands, specify the normal vectors and four vertices of each face of the cube. The vertex coordinates, normal vectors, and index numbers of the vertices for each face are stored in arrays, which are then used for rendering. Since each face is a quadrilateral, GL_QUADS is passed as an argument to glBegin.
In OpenGL programs, it is common to perform other operations such as setting up light sources and projection transformations. However, these operations are all handled within UC-win/Road, so they do not need to be performed here.
procedure TAPlugin.PaintScene;
const
| vertices : array [0..7, 0..2] of GLfloat = |
| Coordinates of the vertices of a cube |
(
(-0.5, -0.5, 0.5),
( 0.5, -0.5, 0.5),
( 0.5, 0.5, 0.5),
(-0.5, 0.5, 0.5),
( 0.5, -0.5, -0.5),
(-0.5, -0.5, -0.5),
(-0.5, 0.5, -0.5),
( 0.5, 0.5, -0.5)
);
| normalVectors : array [0..5, 0..2] of GLfloat = |
(
( 0.0, 0.0, 1.0),
( 0.0, 0.0, -1.0),
( 1.0, 0.0, 0.0),
(-1.0, 0.0, 0.0),
( 0.0, 1.0, 0.0),
( 0.0, -1.0, 0.0)
);
indeces : array [0..5, 0..3] of integer =
(
(0, 1, 2, 3),
(4, 5, 6, 7),
(1, 4, 7, 2),
(5, 0, 3, 6),
(3, 2, 7, 6),
(1, 0, 5, 4)
);
diffuse : array [0..2] of GLfloat = (1.0, 0.0 , 0.0);
ambient : array [0..2] of GLfloat = (0.25, 0.25, 0.25);
specular : array [0..2] of GLfloat = (1.0, 1.0, 1.0);
shininess : array [0..0] of GLfloat = (32.0);
var
i : integer;
j : integer;
y : single;
dx : single;
dz : single;
viewDistance : single;
begin
if firstTime then
begin;
dx := winRoadApplication.MainOpenGL.Camera.ViewPoint.x -
winRoadApplication.MainOpenGL.Camera.Eye.x;
dz := winRoadApplication.MainOpenGL.Camera.ViewPoint.z -
winRoadApplication.MainOpenGL.Camera.Eye.z;
viewDistance := sqrt(dx * dx + dz * dz);
position[_x] := winRoadApplication.MainOpenGL.Camera.Eye.x +
dx * 10.0 / viewDistance;
position[_z] := winRoadApplication.MainOpenGL.Camera.Eye.z +
dz * 10.0 / viewDistance;
position[_y] := winRoadApplication.project.GetActualHeightAt(
position[_x], position[_z]) + 0.5; |
| Calculate the position 10 meters ahead of the viewpoint and store it in a variable |
firstTime := false;
end;
glMaterialfv(GL_FRONT, GL_DIFFUSE, @diffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, @ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, @specular);
glMaterialfv(GL_FRONT, GL_SHININESS, @shininess); |
| Material properties at vertices |
glPushMatrix();
| glTranslatef(position[_x], position[_y] , position[_z]); |
| Move and transform to the desired position for drawing |
glBegin(GL_QUADS);
for i := 0 to 5 do
begin
glNormal3fv(@normalVectors[i]);
for j := 0 to 3 do
begin
glVertex3fv(@vertices[indeces[i, j]]);
end;
end;
glEnd(); |
glPopMatrix();
end; |

PaintHUD and processing within SetupCamera
In these functions, since we are not performing any specific operations in this instance, they are defined as empty functions using the following syntax.
procedure TAPlugin.PaintHUD;
begin
end;
procedure TAPlugin.SetupCamera;
begin
end; |
 |
| ■ Figure 3: Screenshot of the plugin execution screen for placing a cube on the initial screen of UC-win/Road |
Announcement of paid seminars
| UC-win/Road SDK seminar |
| • Date and time |
March 15, 2013 (Friday), 9:30 AM to 4:30 PM |
| • Course fees |
For 1 person: ¥18,000 (excluding tax) |
| • This venue |
FORUM8 Tokyo headquarters, GT Tower seminar room
※TV conferencing systemThe event will be held simultaneously in Tokyo, Osaka, Nagoya, Fukuoka, and Sendai |
|
|
| (Featured in the New Year issue of Up&Coming '13) |
 |
|







>> Product comprehensive catalog

>> Premium membership services
>> Financial support
| Recently released products |
|
|
|