+ All Categories
Home > Documents > Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input...

Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input...

Date post: 04-Apr-2018
Category:
Upload: nguyenngoc
View: 215 times
Download: 2 times
Share this document with a friend
34
GRAPHICS PROGRAMMING
Transcript
Page 1: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

GRAPHICS

PROGRAMMING

Page 2: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

Content

The Sierpinski Gasket

The Opengl Application Programming Interface

- Graphics Functions

- Graphics pipeline

- Coordinates System

-The OpenGL Interface

Primitives and attributes

Text

Colors

Page 3: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 4: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 5: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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 .

Page 6: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 7: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 8: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 9: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 10: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 11: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 12: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 13: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 14: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 15: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 16: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

Coordinates Systems

The object coordinate system

The World coordinate system

The Screen coordinate system

Page 17: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 18: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

Library organization

Page 19: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 20: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 21: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 23: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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

Page 24: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 25: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

Opengl Pipeline

Page 26: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 27: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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 .

Page 28: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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:

Page 29: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

Polygon in Opengl

The only OpenGL polygons that OpenGL supports are

triangles.

Triangle types

Page 30: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

Polygon in Opengl

Triangle strip and triangle fan

Page 31: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 32: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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)

Page 33: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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.

Page 34: Graphics Programming - … · Display q by putting some sort of marker, ... measurements from input devices, such as the mouse and ... operations that determine whether a primitive

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


Recommended