Date post: | 04-Apr-2018 |
Category: |
Documents |
Upload: | nguyenngoc |
View: | 215 times |
Download: | 2 times |
GRAPHICS
PROGRAMMING
Content
The Sierpinski Gasket
The Opengl Application Programming Interface
- Graphics Functions
- Graphics pipeline
- Coordinates System
-The OpenGL Interface
Primitives and attributes
Text
Colors
The Sierpinski Gasket
The Sierpinski gasket is an object that can be defined as
attractive fixed set with the overall shape of an equilateral
triangle, subdivided recursively into smaller equilateral
triangles.
Suppose that we start with three points in space. As long as
the points are not collinear, they are the vertices of a unique
triangle and also define a unique plane.We assume that this
plane is the plane z = 0 and that these points, as specified in
some convenient coordinate system, are (x1, y1, 0), (x2, y2, 0),
and (x3, y3, 0).
The Sierpinski Gasket con..
The construction proceeds as follows:
1. Pick an initial point p = (x, y, 0) at random inside the triangle.
2. Select one of the three vertices at random.
3. Find the point q halfway between p and the randomly selected
vertex.
4. Display q by putting some sort of marker, such as a small
circle, at the corresponding location on the display.
5. Replace p with q.
6. Return to step 2
The Graphics execution Approaches
Immediate Mode:
Primitives (vectors , points) flow through the system and
produce images. These data are lost.
New images are created by re-executing the display function
and regenerating the primitives .
Retained Mode:
The primitives are stored in a display list (in compiler form).
Images can be recreated by “executing “ the display list even
without a network between the server and client ,display lists
should be more efficient then repeated executions of the
display function .
The Opengl Application Programming Interface
OpenGL’s structure is similar to that of most modern APIs,
such as DirectX.
Hence, any effort that you put into learning OpenGL will
carry over to other software systems. Although OpenGL is
easy to learn, compared with other APIs, it is nevertheless
powerful.
Our prime goal is to study computer graphics; we are using an
API to help us attain that goal
Graphics System as a Black Box
We can think of the graphics system as a black box whose
inputs are function calls from an application program;
measurements from input devices, such as the mouse and
keyboard; and possibly other input, such as messages from
The outputs are primarily the graphics sent to our output
devices.
Graphics Functions
A graphics system performs multiple tasks to produce output and handle
user input.
An API for interfacing with this system can contain hundreds of individual
functions. It will be helpful to divide these functions into seven major
groups:
1. Primitive functions
2. Attribute functions
3. Viewing functions
4. Transformation functions
5. Input functions
6. Control functions
7. Query functions
Primitive functions
define the low-level objects or atomic entities that our system
can display.
Depending on the API, the primitives can include points, line
segments, polygons, pixels, text, and various types of curves
and surfaces. OpenGL supports a very limited set of
primitives directly, only points, line segments, and triangles.
For the most important objects such as Regular polyhedra,
quadrics, and Bezier curves and surfaces that are not directly
supported by OpenGL, there are libraries that provide the
necessary code.
Attribute functions
If primitives are the what of an API then attributes are the
how.
The attributes govern the way that a primitive appears on the
display. Attribute functions allow us to perform
operations ranging from choosing the color with which we
display a line segment, to picking a pattern with which to fill
the inside of a polygon, to selecting a typeface for the titles
on a graph.
Viewing functions
Allow us to specify various views, although APIs differ in the
degree of flexibility they provide in choosing a view.
OpenGL does not provide any viewing functions but relies on
the use of transformations in the shaders to provide the desired
view.
Transformation functions
One of the characteristics of a good API is that it provides the
user with a set of transformation functions that allows her to
carry out transformations of objects, such as rotation,
translation, and scaling.
Input functions
For interactive applications, an API must provide a set of
input functions to allow us to deal with the diverse forms of
input that characterize modern graphics systems.
We need functions to deal with devices such as keyboards,
mice, and data tablets
Control functions
In any real application, we also have to worry about handling
the complexities of working in a multiprocessing, multi
window environment usually an environment where we are
connected to a network and there are other users.
The control functions enable us to communicate with the
window system, to initialize our programs, and to deal with
any errors that take place during the execution of our
programs.
Query functions
within our applications we can often use other information
within the API, including camera parameters or values in the
frame buffer.
A good API provides this information through a set of query
functions.
Coordinates Systems
The object coordinate system
The World coordinate system
The Screen coordinate system
The OpenGL Interface
OpenGL functions are in a single library named GL (or OpenGL
in Windows). Function names begin with the letters gl.
We use two available libraries, the OpenGL Extension
Wrangler (GLEW) and the OpenGL Utility Toolkit (GLUT).
GLEW removes operating system dependencies.
GLUT provides the minimum functionality that should be
expected in any modern windowing system.
Library organization
Control Function (GLUT)
OpenGL Utility Toolkit(GLUT) Is a library of functions that
provides a simple interface between the systems.
provides the facilities for interaction that OpenGL lacks.
It provides functions for managing windows on the display
screen, and handling input events from the mouse and
keyboard.
The application programs that we produce using GLUT
should run under multiple window systems.
All GLUT function names start with “glut”.
Include Files
For all OpenGL applications, you mustto include the gl.h header file in
every file. Almost all OpenGL applications use GLU, the aforementioned
OpenGL Utility Library, which also requires inclusion of the glu.h header
file. So almost every OpenGL source file begins with:
#include <GL/gl.h>
#include <GL/glu.h>
If you are using the OpenGL Utility Toolkit (GLUT) for managing your
window manager tasks, you should include:
#include <GL/glut.h>
Note that glut.h guarantees that gl.h and glu.h are properly included for
you so including these three files is redundant. To make your GLUT
programs portable, include glut.h and do not include gl.h or glu.h
explicitly.
Setting Up Compilers
Windows Using MS Visual C++
Most of the following files (ie. OpenGL and GLU) will
already be present if you have installed MS Visual C++ v5.0
or later. The following GLUT files will need to be copied into
the specified directories.
Install libraries
libraries (place in the lib\ subdirectory of Visual C++)
opengl32.lib
glu32.lib
glut32.lib
include files (place in the include\GL\ subdirectory of Visual C++)
gl.h
glu.h
glut.h
dynamically-linked libraries (place in the \Windows\System
subdirectory
opengl32.dll
glu32.dll
glut32.dll
Install libraries
Compiling OpenGL/GLUT Programs
Create a new project:
choose File | New from the File Menu
select the Projects tab
choose Win32 Console Application
fill in your Project name
Primitives and attributes
We can separate primitives into two classes: geometric
primitives and image , or raster, primitives.
Geometric primitives are specified in the problem domain and
include points, line segments, polygons, curves, and surfaces.
These primitives pass through a geometric pipeline, as shown
in Figure where they are subject to a series of geometric
operations that determine whether a primitive is visible, where
on the display it appears if it is visible, and the rasterization of
the primitive into pixels in the frame buffer.
Opengl Pipeline
Primitives and attributes con..
The basic OpenGL geometric primitives are specified by sets
of vertices.
When we want to display some geometry, we execute
functions whose parameters specify how the vertices are to be
interpreted.
OpenGL geometric primitives
All OpenGL geometric primitives are variants of points, line
segments, and triangular polygons.
A point can be displayed as a single pixel or a small group of
pixels.
Finite sections of lines between two vertices, called line
segments .
Points and Line segment type
If we wish to display points or line segments, we have a few
choices in OpenGL The primitives and their type
specifications include the following:
Polygon in Opengl
The only OpenGL polygons that OpenGL supports are
triangles.
Triangle types
Polygon in Opengl
Triangle strip and triangle fan
Text
Graphical output in applications such as data analysis and
display requires annotation, such as labels on graphs
In nongraphical programs textual output is the norm, text in
computer graphics is problematic.
In computer graphics we often wish to display text in a
multitude of fashions by controlling type styles, sizes, colors,
and other parameters.
There are two forms of text: stroke and raster.
Stroke Text
Is constructed as are other geometric objects. We use vertices
to specify line segments or curves that outline each character.
In systems that support stroke text as a primitive, there is a
variety of attributes they include the direction of the text
string,the height and width of the characters, the font, and the
style (bold, italic, underlined)
Raster Text
Is simple and fast. Characters are defined as rectangles
of bits called bit blocks. Each block defines a single
character by the pattern of 0 and1 bits in the block.
Colors
Color is one of the most interesting aspects of both human
perception and computer graphics .
Additive color is the primary colors add together to give the
perceived color, examples projectors and slide (positive)
film.
Subtractive color is more appropriate. We start with a white
surface, such as a sheet of paper. Colored pigments remove
color components from light that is striking the surface