ACSC330 - Computer Graphics
Chapter 2 – Graphics Programming
Approach to Computer Graphics programming oriented
2D as a special case of the 3D graphics
Sierpinski gasket problem
Using OpenGL for API
broad enough for PHICS, GKS, Java-3D & Direct X
Sierpinski gasket (an object that can be defined recursively and randomly)
A vertex – a location in space, rather than a point
A point in space is specified by a single vertex. Two vertices define a line segment. Three vertices can determine either a triangle or a circle. Four a quadrilateral etc.
Vertex function glVertex*
*, two or three characters of the form nt or ntv
n, number of dimensions (2, 3, or 4)
t, denotes data type, integer(i), float(f), double(d)
v, (if present) the variable are specified through a pointer to an array, rather than through
an argument list
Basic OpenGL types, GLfloat, Glint
#define Glfloat float
e.g. glVertex2i(GLint xi, GLint yi), 2D with integers
glVertex3f(GLfloat x, GLfloat y, GLfloat z), 3D using floating points
GLfloat vertex93)
glVertex3fv(vertex), if an array is used, 3D
Group as many vertices as necessary
glBegin e.g. glBegin (GL_LINES);
. glVertex2f(x1, y1);
. glVertex2f(x2, y2);
. glEnd();
glEnd
Coordinate Systems
-
Device-independent Graphics – in any coordinate system
-
World-coordinate systems (user’s coordinate system) or application or problem coordinate system
-
Physical-device coordinates or device coordinates
Raster coordinates or screen coordinates (integer type): distinction in OpenGL
World coordinates must be mapped into device coordinates (the graphics system is responsible for this task).
The OpenGL API
Simple but powerful
Graphics system
as a black box
Function calls Output
Data Input
Graphics Functions
-
The primitive functions (low-level objects)
e.g. points, line segments, polygons, pixels, text and various types of curves and surfaces
2. Primitives – ‘what’
Attributes – ‘how’ (the way the primitives appear)
Attribute functions (e.g. colour, pattern, typeface)
-
Description of the synthetic camera
Viewing functions (specify various views)
-
A set of transformation functions
(rotation, scaling and translation – matrix transformations)
5. A set of input functions (deal with devices such as keyboards, mice and data tablets)
-
Control functions enable us to communicate with the window system (for a multiprocessing multiwindow environment)
-
Inquiry functions
GLUT (GL Utility Toolkit)
-
Geometric primitives (points-GL_POINTS, line segments-GL_LINES, polygons, curves and surfaces)
-
Raster primitives (arrays of pixels) – lack geometric properties
Polygons – a series of connected lines to be displayed correctly: it must be simple, convex and flat.
Convex – all points on any line segment are inside the object
Ex. Device a test for the convexity of a 2D polygon
GL_POLYGON, GL_TRIANGLES, GL-QUADS, GLTRIANGLE_STRIP, GL_QUAD_STRIP, GL_TRIANGLE_FAN
Text (various fonts)
Stroke text – constructed as other graphic primitives use vertices to define line segments or curves that outline each character (transformations may applied – retaining its detail and appearance).
Raster text – simple and fast – Characters are defined as rectangles of bits – bit blocks (bit-block-transfer (bitblt) operation). Replicate or duplicate pixels to increase the size of raster characters (larger characters of blocky appearance).
glutBitmapCharacter(GLUT_BITMAP_8BY_13, c)
c is the number of the ASCII character
Curved Objects
-
Can approximate a curved surface by a mesh of convex polygons – a tessellation
e.g. for a circle use a regular polygon of n sides.
for a sphere use a regular polyhedron
-
or, use the mathematical definitions of curved objects and then build graphics functions to implement those objects (e.g. quadratic surfaces, parametric polynomial curves and surfaces)
In OpenGL:
GLU (the utility library) – for a collection of approximations to common curved surfaces.
Attributes
attribute – any property that determines how a geometric primitive is to be rendered.
(e.g. colour, thickness of a line, the pattern used to fill a polygon)
Colour
most interesting aspect
Three colour theory
Additive colour model (three primary colours mixed); red, green and blue - RGB
Colour gamut – range of colours
glIndexi(element);
Viewing
which objects should appear on the screen
default viewing conditions exist
viewing rectangle of clipping rectangle (objects inside the rectangle are in the image; objects outside are clipped out and are not displayed)
orthographic view – orthographic projection (‘sees’ only those objects in the volume specified by viewing volume). Unlike a real camera, the orthographic projection can include objects behind the camera.
void glOrtho
Matrix Modes
model view and projection matrices
Control Functions
Interaction with the Window System
Window or screen window – a rectangular area of the display, the screen of a CRT
In OpenGL, window coordinates are 3D, whereas screen coordinates are 2D – measured in pixels
Many windows can be displayed on the CRT screen
Origin: OpenGl – bottom left; windowing information e.g. mouse position – top left conversion is required from one coordinate system to the other
Aspect Ratio and Viewports
aspect ratio of a rectangle is the ration of the rectangle’s width to its height
distortion – a consequence of the default mode of operation, in which the entire clipping rectangle is mapped to the display window.
Viewport – a rectangular area of the display window. By default, it is the entire window, but it can be set to any smaller size in pixels – void glVIEWport
Program Structure
The Gasket Program
Polygons and Recursion
The 3D Gasket
Hidden Surface Removal – visible-surface algorithms or hidden-surface-removal algorithms
Share with your friends: |