ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk...

37
ATTRIBUTES OF GRAPHİCS PRİMİTİVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk [email protected] http://www.ube.ege.edu.tr/~ozturk

Transcript of ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk...

Page 1: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

ATTRIBUTES OF GRAPHİCS PRİMİTİVES

CA 302 Computer Graphics and Visual Programming

Aydın Öztürk

[email protected]://www.ube.ege.edu.tr/~ozturk

Page 2: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

1. Introduction

Attribute parameter: A parameter that affects the way a pirimitive is displayed.

ColorSizeVisibility

...etc.

Page 3: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

2.Color

Red, Green, Blue Tri-stimulus theory. Additive system : Adding

ligths to blank points. RGB cube has 3 axis (0

to 1).

Green

Red

Blue

C

M

Y

Page 4: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

2.1 RGB Color Components

(01) ……………………………………………………….………. 21 colors

(00), (01), (10), (11) ………………………………… ………… 22 colors

(00000000),… 224 colors

Red Green Blue

Page 5: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

2.1 RGB Color Components

Green

Red

Blue

C

M

Y

Page 6: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

2.1 RGB Color Components

Color information can be stored in the frame buffer in two ways:

RGB Color codes can be stored directly in the frame buffer.RGB Color codes can be put into a seperate table and use

the pixel locations to store index values referencing the color table entries.

Page 7: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

2.1 RGB Color Components

196y

x

.

.

.

2081

.

.

.

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0

Red Green Blue

Page 8: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

2.2. Gray Scale

When an RGB color setting specifies an equal amount of red, gren and blue, the result is some shade of gray.

Values close to 0 for the color component produce dark grayValues close to 1 for the color component produce light gray

Page 9: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

3. OpenGL Color Functions

The following command states that a single buffer for the frame buffer is used and the RGB mode is swiched on.

glutInitDisplayMode (GLUT_SINGLE| GLUT RGB)

Page 10: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

3.1 The OpenGL RGB mode

We select the current color components with the function

glColor* (colorComponents);

glColor3f(0.0, 1.0, 1.0);

glColor3fv(colorArray);

Page 11: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

3.2 The OpenGL Color-Index Mode

Using this mode, we set the current color by specifying an index into a color table:

glIndex* (colorIndex);

glIndexi(196);

Page 12: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

3.2 The OpenGL Color-Index Mode

There is no function provided in the OpenGL library for loading values into a color look-up table,

However, there is a GLUT routine that interacts with a window system to set color specifications into a table at a given index position:

glutSetColor (index, red, green, blue)

Page 13: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

4. Point Attributes

Pixel size = 1

Page 14: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

5. Line Attributes

Line color, width & style :

glLineWidth ( width );

glLineStipple ( repeatFactor, pattern );

glEnable ( GL_LINE_STIPPLE );...

glDisable ( GL_LINE_STIPPLE );

Page 15: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

5. Line Attributes

Implementation of line width :

Page 16: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

5. Line Attributes

Smooth Lines

Page 17: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

6. Fill-Area Attributes

Page 18: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9 OpenGL Wire-Frame Methods

We can choose to show only polygon edges with the command

glPolygonMode (face, displayMode);

face: Designate which face of the polygon we want to show as edges: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

displayMode: GL_LINE, GL_POINT, GL_FILL (default)

Page 19: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

7. General Scan-Line Polygon-Fill Algorithm

Consider the following polygon:

How do we know whether a given pixel on the scanline is inside or outside the polygon?

A

B

C

D

E

F

Page 20: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

7.1 Polygon Rasterization

Intersections are sorted from left to right and the pixel positions including each intersection pair are set to the specified color.

Case 1: Even number of intersections

Page 21: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

7.1 Polygon Rasterization

Scan line y′ (Even number of intersections

Scan line y ((Odd number of intersections

1 2 1

1 2 1 1

Case 2: Odd number of intersections

Compare two edges sharing an intersecting vertex to see if they are on opposite sides of the scanline or not.

Page 22: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

7.2. Flood-Fill Algorithm

Set pixel to fill color value until bounds. An interior point (x, y) A boundary color A fill color

Boundary color

Interior point (x, y)

Fill color

Page 23: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

8. OpenGL Anti-aliasing

In RGB display modeglEnable(mode);

GL_POINT_SMOOTHGL_LINE_SMOOTHGL_POLYGON_SMOOTH

Page 24: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation

One of the most exciting things we can do on a graphics computer is draw pictures that move. An engineer: Tries to see all sides of a mechanical

part he is designing, A pilot learns how to fly an airplane using a simulation

etc.

Animation is an important part of computer graphics.

Page 25: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

In a movie theater, motion is achieved by taking a sequence of pictures and projecting them at 24 per second on the screen.

Each frame is moved into position behind the lens, the shutter is opened, and the frame is displayed.

The shutter is momentarily closed while the film is advanced to the next frame, then that frame is displayed, and so on.

Although we're watching 24 different frames each second, our brain blends them all into a smooth animation.

Page 26: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

Most modern projectors display each picture twice at a rate of 48 per second to reduce flickering.

Computer-graphics screens typically refresh (redraw the picture) approximately 60 to 76 times per second, and some even run at about 120 refreshes per second.

Clearly, 60 per second is smoother than 30, and 120 is marginally better than 60.

Refresh rates faster than 120, however, are beyond the point of diminishing returns, since the human eye is only so good.

Page 27: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

The key reason that motion picture projection works is that each frame is complete when it is displayed.

Suppose we try to do computer animation of a million-frame movie with a program like this:

open_window(); for (i = 0; i < 1000000; i++) {

clear_the_window();

draw_frame(i);

wait_until_a_24th_of_a_second_is_over(); }

Page 28: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

If we add the time it takes for our system to clear the screen and to draw a typical frame, this program gives more and more disturbing results depending on how close to 1/24 second it takes to clear and draw.

Suppose the drawing takes nearly a full 1/24 second. Items drawn first are visible for the full 1/24 second and present a solid image on the screen;

Items drawn toward the end are instantly cleared as the program starts on the next frame. The problem is that this program doesn't display completely drawn frames; instead, we watch the drawing as it happens.

Page 29: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

Most OpenGL implementations provide double-buffering - hardware or software that supplies two complete color buffers.

One is displayed while the other is being drawn. When the drawing of a frame is complete, the two

buffers are swapped, so the one that was being viewed is now used for drawing, and vice versa.

With double-buffering, every frame is shown only when the drawing is complete; the viewer never sees a partially drawn frame.

Page 30: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

A modified version of the preceding program that does display smoothly animated graphics might look like this:

open_window();

for (i = 0; i < 1000000; i++) {

clear_the_window();

draw_frame(i);

wait_until_a_24th_of_a_second_is_over();

swap_the_buffers();

}

Page 31: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

For some OpenGL implementations, in addition to simply swapping the viewable and drawable buffers, the swap_the_buffers() routine waits until the current screen refresh period is over so that the previous buffer is completely displayed.

This routine also allows the new buffer to be completely displayed, starting from the beginning.

Page 32: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

Assuming that our system refreshes the display 60 times per second, this means that the fastest frame rate we can achieve is 60 frames per second (fps), and if all our frames can be cleared and drawn in under 1/60 second, our animation will run smoothly at that rate.

Page 33: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

What often happens on such a system is that the frame is too complicated to draw in 1/60 second, so each frame is displayed more than once.

If, for example, it takes 1/45 second to draw a frame, we get 30 fps, and the graphics are idle for 1/30-1/45=1/90 second per frame.

Page 34: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

In addition, the video refresh rate is constant, which can have some unexpected performance consequences.

For example, with the 1/60 second per refresh monitor and a constant frame rate, we can run at 60 fps, 30 fps, 20 fps, 15 fps, 12 fps, and so on (60/1, 60/2, 60/3, 60/4, 60/5, ...).

That means that if you're writing an application and gradually adding features at first each feature we add has no effect on the overall performance - we still get 60 fps.

Then, all of a sudden, you add one new feature, and the system can't quite draw the whole thing in 1/60 of a second, so the animation slows from 60 fps to 30 fps because it misses the first possible buffer-swapping time.

Page 35: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

The structure of real animation programs does not differ too much from this description.

Usually, it is easier to redraw the entire buffer from scratch for each frame than to figure out which parts require redrawing.

This is especially true with applications such as three-dimensional flight simulators where a tiny change in the plane's orientation changes the position of everything outside the window.

Page 36: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

In most animations, the objects in a scene are simply redrawn with different transformations - the viewpoint of the viewer moves, or a car moves down the road a bit, or an object is rotated slightly.

If significant recomputation is required for non-drawing operations, the attainable frame rate often slows down.

Keep in mind, however, that the idle time after the swap_the_buffers() routine can often be used for such calculations.

Page 37: ATTRIBUTES OF GRAPHICS PRIMITIVES CA 302 Computer Graphics and Visual Programming Aydın Öztürk aydin.ozturk@ege.edu.tr ozturk.

9. Animation (contd.)

OpenGL doesn't have a swap_the_buffers() command because the feature might not be available on all hardware and, in any case, it's highly dependent on the window system.

For example, if we are using the GLUT library, we'll want to call this routine:

void glutSwapBuffers(void);