Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Visit of a 3D House Using OpenGL

Page 1 of 13

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.

INTRODUCTION...............................................................................................................2 A FLAT HOUSE.............................................................................................................3 ADDING SOME PERSPECTIVE.......................................................................................4 BUILDING A CAMERA....................................................................................................7 LIGHTING THE SCENE....................................................................................................8 TOOLS TO BUILD OUR HOUSE.....................................................................................9 TO A BETTER HOUSE....................................................................................................10 RESULTS AND CONCLUSION......................................................................................11

Visit of a 3D House Using OpenGL

Page 2 of 13

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 1992 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 Internet) 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 different 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 OpenGLs matrix operations are used to facilitate this feature. 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 OpenGLs tools is developed. We also designed tools for the flexible 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, camera 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 material 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 13

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.

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. We 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 (..), glRotated (..), 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 graphics pipeline implemented by OpenGL as follows so far:

VM modelview matrix

P projection matrix

clip

Vp viewport matrix viewport

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 each 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 in a glPushMatrix() and glPopMatrix() pair.

Visit of a 3D House Using OpenGL

Page 4 of 13

ADDING SOME PERSPECTIVE


We are now able to represent a 3D scene but this scene is not very realistic. For example when youre getting further to an object, its size doesnt diminish! Its a very inconvenient feature 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 difference is to use drawings: Parallel projection
Principles of parallel projection

Near Plane Window

Far Plane

Window

FIGURE 2 Parallel projection With a parallel projection, the viewer loose information concerning distances of objects.

Visit of a 3D House Using OpenGL

Page 5 of 13

Perspective Projection
Principles of perspective projection

Near Plane

Window

Far Plane

Window

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 need: available little library comprised of several basic classes well Point3 : can be translated Vector3 : can be normalized, cross & dot product Matrix3 : can be applied to a point. to deal with

Then well have to design our camera, and well need several basic notion:

Visit of a 3D House Using OpenGL

Page 6 of 13

The Eye: it represents the position of our camera 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 located 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 projected over the view planes differentiate the parallel projection from the perspective projection.
Non-linear projection over a plane

P Q

Window

FIGURE 4 Non-linear projection over a plane Its 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 gluPerspective function:

Visit of a 3D House Using OpenGL

Page 7 of 13

glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; 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.

BUILDING A CAMERA
Basically what we now want to say is: Im here, standing this way and Im looking there. Thus, we need to specify: An eye point that will represent the camera location A lookAt point that will represent the point Im looking at A upVector that will represent the way were 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 specify 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 cameras coordinates system into the generic position for the camera. Thats 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 camera 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 those movement we have build our own function comKey.

Visit of a 3D House Using OpenGL

Page 8 of 13

This function listens to the key events; when an event is received, two function can be called: Point3::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.
The possible movements of the camera

Object

Camera Object Camera

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 later but for now the camera is totally free and has basically no restrictions.

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 ambient, diffuse, and specular light contribution to the scene. Here is the formula to sum the three light contributions

Visit of a 3D House Using OpenGL

Page 9 of 13

diffuse, specular, and ambient to form the total amount of light I that reaches the eye from point P,

I = I +I +I
a a d d sP

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 property 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 functions, 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.

TOOLS TO BUILD OUR HOUSE


Were now going to build several objects well assemble to build our house. Walls: thats the basis of our house. Basically theyre just scalable boxes Room: comprised of walls. Each room has a door that can face any direction. Couch: if stretched, can become a seat

Visit of a 3D House Using OpenGL

Page 10 of 13

Chair Table TV

Basically, all those objects have 2 main methods: 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 scaled 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 pieces of furniture, essentially made of cubes. Here is the basic blue print of our house:

FIGURE 6 The blue print of the house Weve decided to build a proportionnal house: we start with two dimensions, dimHomeX & dimHomeZ and all the other 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.

TO A BETTER HOUSE
For the moment the house is prettysimple.

Visit of a 3D House Using OpenGL

Page 11 of 13

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. 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.

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. We 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 screen captures that present the project in its latest version:

General view of the house

The TV set

Visit of a 3D House Using OpenGL

Page 12 of 13

The table and its chairs

The couch and the seats

The bedroom FIGURE 7 The screen captures of the house Through the whole process, we now have 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

Visit of a 3D House Using OpenGL

Page 13 of 13

4. OPENGL PROGRAMMING GUIDE: SECOND EDITION, MASS: ADDISION WESLEY DEVELOPERS PRESS, WOO, MASON, et.al. 1997

You might also like