Chapter 3
Creating a hierarchical, 3D, wire frame model
Now that the basic OpenGL and GLUT structure and routines has been explained, it is time to go on and try to put this new knowledge in work. The goal of this chapter is the creation of a hierarchical, wire frame model of a man.
Previously acquired knowledge, like modeling and projection transformations will be used in order to create and animate this model. This chapter is divided into two sections.
In the first section, in order to discuss and explain hierarchical models and their creation, without having to cope with an overcomplicated example, instead of trying to create the whole model of the man, only its lower part will be constructed including its base, legs and feet.
This incomplete model will then be animated. A walking function will be created for this reason. When this example will be finished and enough knowledge and experience will be accumulated, the second section will follow naturally.
In the second section, the incomplete model of the man will be completed and by the end of the section a complete, three dimensional, wire-frame model of man (based on rectangles and spheres) will be ready. The walking function from the previous section will be slightly modified in order to accommodate the whole body.
3.1 Building a basic hierarchical model
This section will start by describing not the code of the program but the structure of and the concepts behind hierarchical models.
Imaging that it was required to build a car for some simulation reasons. For the sake of this example this car is composed of the cars body, four wheels and six windows. There is a front window, a back window and two windows on each side of the car. The side windows are symmetrical and each wheel has five bolts (Plate 3.1).
<<<<<<<***** 3.1 pic 1 *****>>>>>>>
For the purposes of this example only the right (visible in the picture) side components will be used. That is, two wheels, ten bolts and two side windows.
In order to avoid repetition it would be also be desirable to build the car in a hierarchical way; meaning that when the five bolts are correctly placed on the wheel, they should move in accordance with the wheel without any exterior help. Also it would be desirable that when the body of the car moves, its parts would not stay behind but follow its movement.
In order to achieve this a hierarchy has to be built with the cars body being the topmost item in it and then following it, the windows and the wheels, with the bolts being subordinate to the wheels. This hierarchy has the result of when the cars body is moving, the windows and wheels will move in accordance with it. Further on when the wheels rotate the bolts will rotate also.
OpenGL provides the means to build hierarchical models through the functions glPushMatrix and glPopMatrix. If it assumed that functions are available that each one of them draws a part of the car i.e. bolt, wheel, window and body then Plate 3.2 demonstrates the needed hierarchy and example 3.1 the appropriate pseudo-code to achieve it.
Example 3.1 Pseudo-code that demonstrates the cars hierarchy
function draw_car
{
glPushMatrix
draw_body_of_car
glPushMatrix
go_to_side_window_front_position
draw_side_window
go_to_side_window_back_position
inverse_axes
draw_side_window
inverse_axes
go_to_front_wheel_position
draw_wheel_and_bolts
go_to_back_wheel_position
draw_wheel_and_bolts
glPopMatrix
glPopMatrix
}
function draw_wheel_and_bolts
{
glPushMatrix
draw_wheel
glPushMatrix
for counter = 1 up to 5 do
{
go_to_bolt_position
draw_bolt
}
glPopMatrix
glPopMatrix
}
<<<<<<<<<******* 3.1 pic 2 *******>>>>>>>>>
In example 3.1 the function go_to* is used to translate to the needed point every time. The function inverse_axes is used to inverse the x-axis in order to use the draw_window function to draw the side back window (as it is the mirror of the side front window). If now a glTranslate routine is issued just before the function draw_car, the whole car will be moved including the windows and wheels and if furthermore a relation exists that when the car moves the wheels rotate, the bolts will also rotate in accordance with wheels.
Furthermore, the commands glPushMatrix and glPopMatrix can be used to save time when positioning parts of a scene. For example lets say that the body of the car has a length of 100 units and that the co-ordinates system is positioned at the center of the car, so the car co-ordinates lie from 50 to 50. Lets also assume that the wheels have to be positioned both at ten points before the boundaries of the car. This can be done in two ways.
Without using the matrix stack, a glTranslate(40, 0, 0) should be issued in order to move the center of the coordinates forty units on the x-axis, then the wheel would be drawn by calling draw_wheel_and_bolts and then the center of the co-ordinates should be moved eighty units back in order to position the second wheel, by calling glTranslate(-80, 0, 0).
If the matrix stack is used, and the appropriate commands glPushMatrix and glPopMatrix, the same can be done in the following, more robust way. A call to glPushMatrix can be done in order to save the current matrix and then a call to glTranslate(40, 0, 0) and a call to draw_wheel_and_bolts can be done in order to draw the first wheel in the correct position. Then a call to glPopMatrix can be done in order to retrieve the prior to the translation matrix and then a call to glTranslate(-40, 0, 0) and a call to draw_wheel_and_bolts can be done in order to position and draw the second wheel.
Now that some understanding of hierarchical models and the matrix stack has been acquired, the actual design of the basic model can start. Plate 3.3 shows the parts of this first basic model and their hierarchical relation.
As it is seen in Plate 3.3, in this occasion the top most item in the hierarchy is the base of the body, followed by the upper leg joint, the upper leg, the lower leg joint, the lower leg, the foot joint and finally the foot. Joints are depicted as spheres and the other parts of the body as rectangles.
<<<<<<<<****** 3.1 pic 3 ********>>>>>>>>>
So now if a rotation is applied to the base, all other parts are going to be rotated whereas if a rotation is applied to the lower leg joint, only the joint and the parts lower from it will rotate (Plate 3.4).
Now is the appropriate time to go on and start constructing the code that will draw and latter on animate this model.
The project at this point is split into three files. As always the main program will residue in the file called main.c. Another file will be used called model.c that will contain all the functions that will be needed in order to position, draw and animate this model. The file model.h contains the function definitions of the file model.c.
In order to construct this basic model, some relative metrics have to be calculated that will approximate a human body. As the point of this example was not accuracy but a demonstration of hierarchical model creation, the relative heights and widths of the body parts were based on a not so accurate hand-drawn sketch of a man. Accurate modeling will be the subject of a latter chapter. Example 3.2 shows part of the file model.h, were the relative metrics of the body can be found.
Example 3.2 Size definitions of the models parts
#define FOOT_JOINT_SIZE HEAD_JOINT_SIZE
#define FOOT_HEIGHT FOOT_JOINT_SIZE * 2.0
#define FOOT_WIDTH LO_LEG_WIDTH
#define FOOT FOOT_WIDTH * 2.0
#define UP_ARM_HEIGHT TORSO_HEIGHT * 0.625
#define UP_ARM_WIDTH TORSO_WIDTH/4.0
#define UP_ARM_JOINT_SIZE HEAD_JOINT_SIZE * 2.0
#define LO_ARM_HEIGHT TORSO_HEIGHT * 0.5
#define LO_ARM_WIDTH UP_ARM_WIDTH
#define LO_ARM_JOINT_SIZE UP_ARM_JOINT_SIZE * 0.75
#define HAND_HEIGHT LO_ARM_HEIGHT / 2.0
#define HAND_WIDTH LO_ARM_WIDTH
#define HAND LO_ARM_WIDTH /
没有合适的资源?快使用搜索试试~ 我知道了~
(Computer Graphics) A 3D Case Study Using Opengl.zip
共82个文件
bmp:57个
txt:25个
4星 · 超过85%的资源 需积分: 10 73 下载量 101 浏览量
2008-07-02
11:20:08
上传
评论 1
收藏 1015KB ZIP 举报
温馨提示
(Computer Graphics) A 3D Case Study Using Opengl.zip
资源推荐
资源详情
资源评论
收起资源包目录
(Computer Graphics) A 3D Case Study Using Opengl.zip (82个子文件)
A 3D Case Study using OpenGL
Chapter 3 A Hierarchycal, 3D, Wireframe Model
3.1_3a.bmp 122KB
3.1_5.bmp 123KB
3.1_6.bmp 128KB
3.1_1.bmp 122KB
3.1_7.bmp 326KB
3.1_8.bmp 41KB
3.1_10.bmp 379KB
3.2_1.bmp 222KB
3.2 Improving the Model.txt 10KB
3.2_2.bmp 377KB
3.1_4.bmp 122KB
3.1_3b.bmp 129KB
3.2_3.bmp 134KB
3.1_2.bmp 122KB
3.1_9.bmp 261KB
3.1 A Basic Hierarchycal Model.txt 30KB
Chapter 4 Lighting
4.5 Adding Lights to the Model.txt 5KB
4.1 Getting Started with Lighting.txt 10KB
4.4_3a.bmp 201KB
4.5.bmp 377KB
4.4_5b.bmp 120KB
4.4_4.bmp 349KB
4.4 The Materials-Lighs Application.txt 13KB
4.1.bmp 156KB
4.2_2.bmp 139KB
4.3 Settign Material Properties.txt 4KB
4.4_5a.bmp 421KB
4.4_3b.bmp 206KB
4.3.bmp 139KB
4.4_1.bmp 194KB
4.4_2.bmp 104KB
4.2_1.bmp 139KB
4.2 Color Tracking.txt 3KB
Chapter 2 Simple Graphics with OpenGL
2.3 Flat Versus Smooth Shading.txt 9KB
2.2.bmp 158KB
2.2 Creating and Showing a Cube.txt 5KB
2.3_2.bmp 161KB
2.3_3.bmp 248KB
2.3.bmp 161KB
2.4_4.bmp 415KB
2.1 Opening a Windo using OpenGL.txt 9KB
2.4 Transformations.txt 16KB
2.4_3.bmp 160KB
2.3_4.bmp 161KB
2.4_2.bmp 155KB
2.1.bmp 128KB
2.4_1.bmp 160KB
Chapter 7 Conclusions
7.1.txt 3KB
Chapter 5 Improving the Model
5.2_2.bmp 73KB
5.2_3b.bmp 144KB
5.2_3a.bmp 177KB
5.2_5a.bmp 144KB
5.1 Getting Started with Lighting.txt 4KB
5.2_5b.bmp 415KB
5.1_2a.bmp 139KB
5.2_4.bmp 118KB
5.1_2b.bmp 155KB
5.2_1.bmp 150KB
5.1_1.bmp 331KB
5.2 Color Tracking.txt 10KB
Chapter 6 Texture Mapping
6.3_4b.bmp 145KB
6.4 A Texture Mapped Model.txt 4KB
6.1_3.bmp 150KB
6.3_2.bmp 149KB
6.3_4a.bmp 144KB
6.3_3.bmp 144KB
6.3 Creating a Texture.txt 8KB
6.2 Opening Multiple Windows.txt 3KB
6.1_1.bmp 177KB
6.2.bmp 321KB
6.4.bmp 227KB
6.1 Opening a Bitmap File.txt 9KB
6.1_2.bmp 161KB
6.3_1.bmp 188KB
6.3_4c.bmp 266KB
Chapter 1 Introduction
1.1 About This Report.txt 920B
1.5 What is OpenGL.txt 3KB
1.6 Discussions with a Supervisor.txt 564B
1.4 Aims and Objectives.txt 3KB
1.2 Style Conventions.txt 703B
1.3 Background Material.txt 773B
1.7 The Structure of this Report.txt 3KB
共 82 条
- 1
资源评论
- smallstar872012-02-28是text的。。不过谢谢分享
shakefork
- 粉丝: 1
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功