Chapter 2 Computer Graphics and Visualization SSE, Mukka
Slide 2
Programming oriented approach is used. Minimal application
programmer's interface (API) is used which allow to program many
interesting two- and three- dimensional problems, and to
familiarize with the basic graphics concepts. 2-D graphics is
regarded as a special case of 3-D graphics. Hence the 2-D code will
execute without modification on a 3- D system. A simple but
informative problem called: The Sierpinski gasket is used. 2-D
programs that do not require user interaction can be written with
knowledge presented here. The chapter is concluded with an example
of a 3-D application. in slides on Chapter 2 Adapted from Angel:
Interactive Computer Graphics 5E Addison- Wesley 2009 2 Highlights
of the chapter
Slide 3
The Sierpinski gasket is an object that can be defined
recursively and randomly in the limit, however, it has properties
that are not at all random Consider the three vertices in the
plane. Assume that their locations, as specified in some convenient
coordinate system, are (Xl, Y1), (X2, Y2), and (X3, Y3). The
construction proceeds as follows: The Sierpinski Gasket
Problem
Slide 4
1. Pick an initial point at random inside the triangle. 2.
Select one of the three vertices at random. 3. Find the point half
way between the initial point and the randomly selected vertex. 4.
Display this new point by putting some sort of marker, such as a
small circle, at its location.. 5. Replace the initial point with
this new point. 6. Return to step 2. Thus, each time a point that
is generated, it is displayed on the output device. In the figure
p0 is the initial point, and Pl and P2 are the first two points
generated by the algorithm. Construction of Gasket
Slide 5
main( ) { initialize_the_system(); for (some_number_of_points)
{ pt = generate_a_point(); display_the_point(pt); } cleanup(); } A
possible form for our graphics program
Slide 6
To produce the image of a 3-D object on 2-D pad of pen- plotter
model, the positions of 2-D points corresponding to points on 3-D
object are to be specified. These two-dimensional points are the
projections of points in three-dimensional space. The mathematical
process of determining projections is an application of
trigonometry. An API allows users to work directly in the domain of
their problems, and to use computers to carry out the details of
the projection process automatically, without the users having any
trigonometric calculations within the application program
Programming 2-D applications
Slide 7
For 2-D applications, such as the Sierpinski gasket, discussion
is started with a three-dimensional world. Mathematically, a 2-D
plane or a simple 2-D curved surface is viewed as a subspace of a
three-dimensional space. Hence, statements-both practical and
abstract -about the bigger 3-D world will hold for the simpler 2-D
one. We can represent a point in the plane z=0 as p = (x, y, 0) in
3-D, or as p = (x, y) in 2-D subspace corresponding to the plane.
Contd
Slide 8
In OpenGL, internal representation for both 2-D and 3-D points
is same. Hence a 3-D point is represented by a triplet: regardless
of in what coordinate system p is represented. Vertex ( rather than
point): A vertex is a location in space (in Computer Graphics a
2-D, 3-D, 4-D spaces are used). Vertices are used to define the
atomic geometric objects that are recognized by graphics system.
The simplest geometric object is a point in space, which is
specified by a single vertex. Two vertices define a line segment.
Three vertices can determine either a triangle or a circle. Four
vertices determine a quadrilateral, and so on. Contd
Slide 9
OpenGL has multiple forms for many functions. The variety of
forms allows the user to select the one best suited for the
problem. General for of the vertex function is glVertex* where the
* can be interpreted as either two or three characters of the form
nt or ntv, where -n signifies the number of dimensions (2, 3, or
4); -t denotes the data type, such as integer (i), float (f), or
double (d); -v, if present, indicates the variables are specified
through a pointer to an array, rather than through an argument
list. Regardless of which form a user chooses, the underlying
representation is the same. Multiple Forms of functions
Slide 10
GLfloat and GLint, are used rather than the C types, such as
float and int. These types are defined in the header files and
usually in the obvious way-for example, #define GLfloat float
However, use of the OpenGL types allows additional flexibility for
implementations for example, suppose the floats are to be changed
to doubles without altering existing application programs. Basic
OpenGL types
Slide 11
Returning to the vertex function, if the user wants to work in
2-D with integers, then the form glVertex2i(GLint xi, GLint yi) is
appropriate glVertex3f(GLfloat x, GLfloat y, GLfloat z) specifies a
position in 3-D space using floating-point numbers. If an array is
used to store the information for a 3-D vertex, GLfloat vertex[3]
then glVertex3fv(vertex) can be used. Contd..
Slide 12
Different numbers of vertices are required depending on the
object. Any number of vertices can be grouped using the functions
glBegin and glEnd. The argument of glBegin specifies the geometric
type that the vertices define. Hence, a line segment can be
specified by glBegin(GL_LINES); glVertex2f(xl,yl);
glVertex2f(x2,y2); glEnd(); Geometric primitives
Slide 13
Same data can be used to define a pair of points, by using the
form, glBegin(GL_POINTS); glVertex2f(xl,yl); glVertex2f(x2,y2);
glEnd(); Contd
Slide 14
Suppose that all points are to be generated within a 500 x 500
square whose lower left-hand corner is at (0,0) - a convenient, but
easily altered, choice. How to represent geometric data in program?
A two-element array for 2-D points is used: typedef GLfloat
point2[2]; A function called display, is created to generate 5000
points each time it is called. Assume that an array of triangle
vertices triangle[3] is defined in display as an array of point2.
Heart of Sierpinski gasket program
Slide 15
void display(void) { point2 triangle[3] = {{0.0, 0.0}, {250.0,
500.0}, {500.0, 0.0}}; /* an arbitrary triangle */ static point2 p
= {75.0, 50.0}; /* or set to any desired initial point */ int j, k;
int rand(); /* standard random- number generator*/ for(k=0;k