Visit of a 3D House Using OpenGL
Page
1
of
12
VISIT OF A 3D HOUSE USING OPENGL
Francois Uzan and Weijian Chai
Abstract
–
This paper introduces the basic application of computer
graphics using OpenGL. Some basic 3D visualization tools of OpenGL
are reviewed first. Then, we build a simple tool

kit to
design a 3D
scene. The user can visit the 3D house we built with a flying camera.
We also build a little “library” to manipulate points, vectors and
matrices.
Key Words
–
OpenGL, 3D scene, drawing functions, flying camera.
1.
INTRODUCTION
................................
................................
..
2
2.
A “FLAT” HOUSE
................................
................................
3
3.
ADDING SOME PERSPECTIVE
................................
..............
4
4.
BUILDING A CAMERA
................................
..........................
7
5.
LIGHTING THE SCENE
................................
.........................
8
6.
TOOLS TO BUILD OUR HOUSE
................................
.............
9
7.
TO A BETTER HOUSE
................................
..........................
10
8.
RESULTS AND CONCLUSION
................................
...............
11
Visit of a 3D House Using OpenGL
Page
2
of
12
1.
INTRODUCTION
Because playing games on personal computers
has become so
popular, and so many dazzling animations are appearing in movies,
peoples are particularly interested in developing 3D graphics
applications. OpenGL is a very useful tool to design a 3D scene.
OpenGL emerged from Silicon Graphics, Inc., in 1
992 and has become
a widely adopted graphics application programming interface (API). It
provides the actual drawing tools through a collection of functions that
are called within an application. It is available (usually through free
downloads over the Int
ernet) for all types of computer systems.
OpenGL is easy to install and learn. One aspect of OpenGL that makes
it so well suited for use in a computer graphics is its “device
independence”, or portability. An OpenGL program can be developed
and run on diff
erent computers.
OpenGL offers a rich and highly usable API for 2D, 3D graphics and
image manipulation. Using it, people can progress rapidly and produce
stunning animation in a short period.
Transformations are of central importance in computer graphics
. We
use affine transformations in 3D case, including translate, scale,
rotations in 3D. We have designed tools used to shift, scale, and rotate
figures through the “current transformation,” and OpenGL’s matrix
operations are used to facilitate this featur
e. An overview of the
OpenGL viewing pipeline is developed, and the roles of the model
view, projection, and view port transformations are described briefly.
The drawing of 3D objects using OpenGL’s tools is developed.
We also designed tools for the flex
ible viewing of the 3D scene. The
“flying camera” that forms perspective views is defined, and its
relationship to the parallel views of the scene is also discussed.
Several convenient classes are built, for example, wall class, table
class, TV class, cam
era class, light class and so on. These classes
make it easy to build a 3D scene and to “fly” the camera through a
scene in an animation.
Our last try is to make a better house through adding lights and simple
shadows. Also we try to alter the surface mat
erial properties of
objects. These ways make our 3D scene looks more realistic. Methods
for adding these features are presented. But the main purpose of this
Visit of a 3D House Using OpenGL
Page
3
of
12
paper is to implement the computer graphics through designing a 3D
scene and visit the scene in an
animation. We did not focus on a
perfect, very realistic and beautiful scene.
2.
A “FLAT” HOUSE
In this part, we first try to use the basic 3D drawing functions of
OpenGL to draw a simple “house”. This house only has three walls and
one object inside. W
e use parallel projections to visualize an example
3D scene comprised of simple wire

frame objects.
This “flat house” drawing based on the simple “parallel projection”,
shown in figure 1. The view volume is a rectangular parallelepiped (i.e.
a box), whose
four sidewalls are determined by the border of the
window and whose other two walls are determined by a near plane and
a far plane. So the 3D point (x, y, z) projects to (x, y, 0) in this
projection.
OpenGL provides three functions glScaled (..), glRotat
ed (..), and
glTranslated (..) for applying modeling transformations to a shape.
OpenGL also provides functions for defining the view volume and its
position in the scene. These functions are used in our design of the
house and camera. We followed the grap
hics pipeline implemented by
OpenGL as follows so far:
modelview projection viewport viewport
matrix matrix matrix
FIGURE 1 The OpenGL pipeline
There are two
functions that we would like to introduce :
glPushMatrix
() and
glPopMarix
().These two functions are used to
manage several different stacks of matrices. Most objects need their
own modeling matrix in order to rotate and position them as desired.
Before ea
ch modeling transformation is established, a
glPushMatrix
()
is used to remember the current transformation, and after the object
has been drawn, the current transformation is restored with a
glPopMatrix
(). Thus, the code to draw object is enclosed within i
n a
glPushMatrix
() and
glPopMatrix
() pair.
VM
P
clip
V
p
Visit of a 3D House Using OpenGL
Page
4
of
12
3.
ADDING SOME PERSPECTIVE
We are now able to represent a 3D scene but this scene is not
very…realistic. For example when you’re getting further to an object,
its size doesn’t diminish! It’s a very inconvenient “fe
ature” when you
want to move around a scene.
To solve this problem we must use a perspective projection instead of
a parallel one. The main idea is to understand the difference between
parallel and perspective projection.
The best way to understand this d
ifference is to use drawings:
Parallel projection
FIGURE 2 Parallel projection
With a parallel projection, the viewer loose information concerning
distances of objects.
Window
Near
Plane
Far
Plane
Window
Principles of parallel projection
Visit of a 3D House Using OpenGL
Page
5
of
12
Perspective Projection
FIGURE 3 Perspective projection
With the perspective projection, the impression of distance is
conserved. The view is more realistic.
How to do it with OpenGL
First we built a little library comprised of several basic classes we’ll
need:
Poin
t3
: can be translated
Vector3
: can be normalized, cross & dot product available
Matrix3
: can be applied to a point.
Then we’ll have to design our camera, and we’ll need to deal with
several basic notion:
The
Eye
: it represents the position of our cam
era
Window
Window
Near
Plane
Far
Plane
Principles of perspective
projection
Visit of a 3D House Using OpenGL
Page
6
of
12
The
View Volume
: it is a truncated pyramid whose apex is at
the eye
The
View Angle
: it defines the opening of the pyramid
The
Near Plane
and the
Far Plane
: those two planes intersect
the pyramid to form a rectangular window. An object is visible if
loc
ated between those two windows.
The
Aspect Ratio
: each of the previously defined windows has a
height and width that give its aspect ratio
The
View Plane
: When points are inside the View Volume, they
are projected over this View Plane
The way points are p
rojected over the view planes differentiate the
parallel projection from the perspective projection.
FIGURE 4 Non

linear projection over a plane
It’s important to notice that this is
not
a linear transform: we will have
to
use homogeneous divide.
To specify what values we will work with, our
Camera::setShape
function uses the GL_PROJECTION matrix provided by OpenGL and 4
arguments:
The View Angle
The Aspect Ratio
The Near Plane location
The Far Plane location
With the
gluP
erspective
function:
glMatrixMode(GL_PROJECTION)
;
glLoadIdentity()
;
Q
P
P
’
Q
’
Eye
Window
Non

linear projection over a plane
Visit of a 3D House Using OpenGL
Page
7
of
12
gluPerspective(viewAngle, aspectRatio, NearPlane, FarPlane)
We now have a well defined view volume and we have to build and set
our camera in order to choose what we want to see.
4.
B
UILDING A CAMERA
Basically what we now want to say is: “I’m here, standing this way and
I’m looking there”.
Thus, we need to specify:
An eye point that will represent the camera location
A lookAt point that will represent the point I’m looking at
A upVec
tor that will represent the way we’re standing (Where is
up and where is down).
Those values will determine where our camera is and how to aim it by
building the modelview matrix. OpenGl provide the function
gluLookAt
to do so, but we preferred to specif
y by ourselves what the modelview
matrix would look like.
This matrix has to perform two transformations:
Convert the world coordinates to camera coordinates
Transform the camera’s coordinates system into the generic
position for the camera.
That’s what
the
Camera::setModelViewMatrix
function does: with the
function
Camera::set
() we specify where is the eye, the
lookAt
point
and the
upVector
and then we call the
setModelViewMatrix
function
that set the viewmodel matrix.
How to control our camera
Our cam
era has 4 main functions:
Moving backward and forward: keys “i” and “j”
Moving up and down: keys “a” and “z”
Looking up and down: keys “s” and “x”
Looking left and right: keys “j” and “l”
Go back to original position by pressing the space bar
To perform th
ose movement we have build our own function
comKey
.
This function “listens” to the key events; when an event is received,
two function can be called:
Visit of a 3D House Using OpenGL
Page
8
of
12
P
oint3::translate(int sens,float step, Vector3 direction)
: this
function translate a point in a direction
defined by the Vector3
direction, backward or forward. The amplitude of the move is
determined by the step value.
Camera::rotate(Point3 *point1,Point3 * point2, float stepAngle,
int rotation type, Vector3& vUp, Vector3& direction):
this
function perform either an horizontal or a vertical rotation
(determined by the rotation value) of point1 against point2 of a
stepAngle
angle
After vertical rotation, the
vUp
vector is updated. After each move of
the camera, our image
is redisplayed with the
glutPostRedisplay
()
function.
FIGURE 5 The possible movements of the camera
There is a major problem that we have not dealt with: the user can
walk “through” a wall. We plan to deal with this issue l
ater but for now
the camera is totally free and has basically no restrictions.
5.
LIGHTING THE SCENE
Now we need to add some light sources to light our scene. Basically
we put at least one light in each room in our 3D house and we need to
compute the ambien
t, diffuse, and specular light contribution to the
scene. Here is the formula to sum the three light contributions
—
diffuse, specular, and ambient
—
to form the total amount of light
I
that reaches the eye from point
P
,
Camera
The possible movements of the camera
Object
Camera
Object
Visit of a 3D House Using OpenGL
Page
9
of
12
How to do
it with OpenGL
We use several functions provided in OpenGL:
a.
Creating a Light Source
We can define up to eight sources, which are referred by the names
GL_LIGHT0, GL_LIGHT1, etc. Each source is invested with various
properties and must be enabled. Each pro
perty has a default value.
We can use the array
myLightPosition
[] specified the location of the
light source and it is passed to
glLightfv
() along with the name
GL_LIGHT0, to attach it to the particular source denoted by
GL_LIGHT0.
b.
Deciding the properties
of Light
Some sources, such as a desk lamp, are in the scene, whereas others,
like the sun, are infinitely remote. OpenGL allows you to create both
types by using homogeneous coordinates to specify the position of the
source. So we have (x,y,z,1): a local
light source at the position
(x,y,z) and (x,y,z,0): a vector to an infinitely remote light source in
the direction (x,y,z). Arrays are defined to hold the colors emitted by
light sources and are passed to
glLightfv
().
c.
Lighting model
OpenGL allows three
parameters to be set that specify general rules
for applying the lighting model. These parameters are passed to
variations of the function
glLightModel
. In order to make the objects in
the scene visible even if we have not invoked any of the lighting
funct
ions, we set the ambient source to a non

zero value. After we
built the light class, we can still move the light sources through
translating, rotating and so on.
6.
TOOLS TO BUILD OUR HOUSE
We’re now going to build several objects we’ll assemble to build ou
r
house.
Walls
: that’s the basis of our house. Basically they’re just
scalable boxes
Room
: comprised of walls. Each room has a door that can face
any direction.
Couch:
if stretched, can become a seat
Chair
Table
TV
Basically, all those objects have 2 main
methods:
Visit of a 3D House Using OpenGL
Page
10
of
12
One to initialize the main characteristics of the objects
(proportional size of the object, location of the center of the
object)
One to display it at a selected altitude (useful if we have a multi

stories house)
We build our objects thanks to s
caled translated or rotated shapes.
They are initially facing one direction but their
set() method has an
argument that allows us to rotate the object around the y axis.
Since polygonal meshes were out of the scope of our paper, we
designed very basic piec
es of furniture, essentially made of cubes.
Here is the basic “blue print” of our house:
FIGURE 6 The blue print of the house
We’ve decided to build a “proportionnal ” house: we start with two
dimensions,
dimHomeX &
dimHomeZ
and all the oth
er dimensions in
the house (the width and length of rooms, their location) are
proportional to those two dimensions.
Our rooms and walls are stored
in arrays. All the elements of our scene are initialized in the
sceneDisplay
function.
7.
TO A BETTER HOUSE
F
or the moment the house is pretty…simple.
Several improvements can be made:
We could try to add texture on the walls, instead of plain colors
All our objects are comprised of cubes. A more precise study on
3d solid based on polygonal meshes could allow us
to built more
realistic objects.
Visit of a 3D House Using OpenGL
Page
11
of
12
If we had more time we could build families of objects built of
the inheritance structure of C++
We could design several pre

designed path for the camera, like
circles around the house or a specific visit
A deeper work on
the light could allow us to offer a time or
weather based light.
8.
RESULTS AND CONCLUSION
Our initial project was more ambitious than the one we finally realized.
We spent a lot of time studying the basis of OpenGL and designing the
camera and the light. W
e finally decided that rather than focusing on
designing very detailed pieces of furniture or adding useless
capabilities to our camera, we would focus on our first goal: building a
basic house and being able to walk through it.
Here are a couple of scre
en captures that present the project in its
latest version:
General view of the house
The TV set
The table and its chairs
The couch and the seats
Visit of a 3D House Using OpenGL
Page
12
of
12
The bedroom
FIGURE 7 The
screen captures
of the house
Through the whole process, we now h
ave a good understanding of the
computer graphics, how it can be implemented to draw pictures and
how the graphics pipeline works.
Rather than “digging” into specific aspect of OpenGL, we tried to
present several basic features of this powerful tool. So,
if we want to
use it to fulfill some goals in the future, we know which aspect we
should plunge into. Further effort can be made to a more realistic and
beautiful scene with a more powerful camera.
REFERENCES
1.
COMPUTER GRAPHICS USING OPENGL, F.S.HILL, JR.
, Prentice
Hall
2.
http://www.opengl.org/
3.
http://www.sgi.com/software/opengl
4.
OPENGL PROGRAMMING GUIDE: SECOND EDITION, MASS:
ADDISION
–
WESLEY DEVELOPERS PRESS, WOO, MASON,
et.al. 1997
Enter the password to open this PDF file:
File name:

File size:

Title:

Author:

Subject:

Keywords:

Creation Date:

Modification Date:

Creator:

PDF Producer:

PDF Version:

Page Count:

Preparing document for printing…
0%
Commentaires 0
Connectezvous pour poster un commentaire