Image Processing. Geometry Processing. Reading: (Not really covered in our text. See Sects 18.1, 18.2.) Overview: Display

Similar documents
CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Introduction to Computer Graphics with OpenGL/GLUT

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

RECITATION - 1. Ceng477 Fall

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Rendering. Part 1 An introduction to OpenGL

Computer Graphics 1 Computer Graphics 1

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Computer Graphics. Bing-Yu Chen National Taiwan University

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

VR-programming tools (procedural) More VRML later in this course! (declarative)

Programming using OpenGL: A first Introduction

Computer Graphics. OpenGL

Lecture 3 Advanced Computer Graphics (CS & SE )

Computer Graphics Course 2005

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Lecture 4 of 41. Lab 1a: OpenGL Basics

Drawing Primitives. OpenGL basics

Lectures OpenGL Introduction

Lecture 2 2D transformations Introduction to OpenGL

OpenGL/GLUT Intro. Week 1, Fri Jan 12

COMPUTER GRAPHICS LAB # 3

Exercise 1 Introduction to OpenGL

OpenGL refresher. Advanced Computer Graphics 2012

Introduction to OpenGL. CSCI 4229/5229 Computer Graphics Fall 2012

Computer Graphics, Chapt 08

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

CS 4204 Computer Graphics

Computer graphics MN1

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Computer Graphics. Downloaded from: LectureNotes 1 CSC-254

2. OpenGL -I. 2.1 What is OpenGL? Things OpenGL can do: -23-

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

CMSC 427 Computer Graphics 1

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

COMP 371/4 Computer Graphics Week 1

Basic Graphics Programming

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

Precept 2 Aleksey Boyko February 18, 2011

Computer graphic -- Programming with OpenGL 2

Computer Graphics Primitive Attributes

Computer Graphics. Chapter 3 Computer Graphics Software

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Graphics and Visualization

Input and Interaction

Basic Graphics Programming

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

CSC 8470 Computer Graphics. What is Computer Graphics?

Computer Graphics Introduction to OpenGL

Computer graphic -- Programming with OpenGL I

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Normalized Device Coordinate System (NDC) World Coordinate System. Example Coordinate Systems. Device Coordinate System

World Coordinate System

Abel J. P. Gomes LAB. 1. INTRODUCTION TO OpenGL

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Announcements OpenGL. Computer Graphics. Spring CS4815

Programming of Graphics

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

to OpenGL Introduction Pipeline Graphics pipeline OpenGL pipeline OpenGL syntax Modeling Arrays Conclusion 1 Introduction Introduction to OpenGL

Class of Algorithms. Visible Surface Determination. Back Face Culling Test. Back Face Culling: Object Space v. Back Face Culling: Object Space.

Discrete Techniques. 11 th Week, Define a buffer by its spatial resolution (n m) and its depth (or precision) k, the number of

Overview of Graphics Systems Hearn & Baker Chapter 2. Some slides are taken from Robert Thomsons notes.

Programming with OpenGL Part 1: Background

Graphics Pipeline & APIs

OpenGL and GLUT based library for Image Processing (GLIP)

CS 432 Interactive Computer Graphics

OpenGL. Toolkits.

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

Graphics Pipeline & APIs

Lecture 2. Determinants. Ax = 0. a 11 x 1 + a 12 x a 1n x n = 0 a 21 x 1 + a 22 x a 2n x n = 0

GLUT. What is OpenGL? Introduction to OpenGL and GLUT

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

Computer Graphics (Basic OpenGL)

An Interactive Introduction to OpenGL Programming

Interaction. CSCI 480 Computer Graphics Lecture 3

Lecture 4. Interaction / Graphical Devices. CS 354 Computer Graphics Sunday, January 20, 13

2 Transformations and Homogeneous Coordinates

Introduction to OpenGL

Comp 410/510 Computer Graphics Spring Input & Interaction

Programming with OpenGL Part 1: Background

3D computer graphics: geometric modeling of objects in the computer and rendering them

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Assignment 1. Simple Graphics program using OpenGL

OpenGL Introduction Computer Graphics and Visualization

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4620/8626 Computer Graphics Spring 2014 Homework Set 1 Suggested Answers

Transcription:

CMSC 427: Chapter 2 Graphics Libraries and OpenGL Reading: (Not really covered in our text. See Sects 18.1, 18.2.) Overview: Graphics Libraries OpenGL and its Structure Drawing Primitives in OpenGL GLUT and Event-Driven Programming Buffers and raster operations Chapter 2, Slide 1 The Graphics Pipeline Geometry Processing Model (description) i Retrieve (database) Transform (eye space) Clip (viewing volume) Light (for each spectral component) Image Processing Scan (primitives iti to pixels) Visibility (at each pixel) Texture (texture map) Composite (masks, other images) Frame Buffer Display (CRT/LCD/DMD) Chapter 2, Slide 2 1

Types Graphics Libraries Retained-Mode Libraries (e.g., Java3d, Ogre) Data is copied to library s internal data-structures. Library s copy is sent down each frame. Library can perform global optimization. Less suitable for time-varying datasets. Functionally analogous to program compilation. Immediate-Mode Libraries (e.g., OpenGL, DirectX) Application supplies all the primitives for display every frame. Library can optimize only per-primitive (local optimizations). Only the application can perform global optimizations. More suitable for scenes where changes happen every frame. Functionally analogous to program interpretation. Chapter 2, Slide 3 Types Graphics Libraries Retained-Mode Libraries (e.g., Java3d, Ogre) Data is copied to library s internal data-structures. Library s copy is sent down each frame. Library can perform global optimization. Less suitable for time-varying datasets. Functionally analogous to program compilation. Immediate-Mode Libraries (e.g., OpenGL, DirectX) Application supplies all the primitives for display every frame. Library can optimize only per-primitive (local optimizations). Only the application can perform global optimizations. More suitable for scenes where changes happen every frame. Functionally analogous to program interpretation. Chapter 2, Slide 4 2

Overview OpenGL Architecture OpenGL Objects and Attributes Event-Driven Programming GLUT OpenGL/GLUT Sample Program OpenGL Buffers OpenGL Pixel Operations Chapter 2, Slide 5 OpenGL: http://www.opengl.org Standard: Most widely-used/supported 2D/3D graphics API Windows NT/95/98/00, UNIX, Linux, MacOS, OS/2, Python, Bindings for C, C++, Java, Fortran, Ada ATI, HP/Compaq, E&S,IBM, Intel, Intergraph, NVIDIA, Microsoft, SGI Independent of hardware, OS, window system. Immediate-mode (mostly): Design philosophy is immediate-mode, with a few retained-mode structures. Appearance attributes have per-frame state. Windowing not included: Does not include commands for windowing tasks or user interaction. (This is what Glut is for.) Chapter 2, Slide 6 3

API Hierarchy OpenGL works with different operating systems and windowing systems by interfacing with the operating system and windowspecific display libraries: Images from www.opengl.org/about/architecture.html GLU (OpenGL Utilities): provides transformation matrix utilities, common objects (sphere, cylinder), implicit it primitives iti (quadrics), parametric primitives (NURBS), utilities for image manipulation Chapter 2, Slide 7 OpenGL Architecture Images from www.opengl.org/about/architecture.html Other Features: Supports imaging as well as 2D/3D geometry operations Accepts information on transformation, lighting, g texturing, blending, anti-aliasing, etc. Can return information about all state variables (including queries to frame buffer and texture memory) Chapter 2, Slide 8 4

Overview OpenGL Architecture OpenGL Objects and Attributes Event-Driven Programming GLUT OpenGL/GLUT Sample Program OpenGL Buffers OpenGL Pixel Operations Chapter 2, Slide 9 OpenGL Naming Conventions Functions: begin with gl Constants: begin with GL_ Types: begin with GL OpenGL Data Types Suffix OpenGL Datatype C/C++ Datatype b s i GLbyte GLshort GLint signed char short int ub us GLubyte GLushort unsigned char unsigned short ui f d GLuint GLfloat GLdouble unsigned int float double Chapter 2, Slide 10 5

Specifying Vertices Vertex Arguments: All objects in OpenGL are constructed from convex polygons, which are represented by their vertex coordinates. The argument type is specified by the suffix to the OpenGL function name: func_name dim type ( argument list ) Examples: 2D point in GLint (int) coordinates: glvertex2i (200, -150); 3D point in GLfloat (float) coordinates: glvertex3f (200.3f, -150f, 40.75f); Vector (array) Arguments: Add suffix v to the function name 3D point in GLdouble (double) coordinates given as a vector: GLdouble pt[3] = { 200.3, -150, 40.75 }; glvertex3dv (pt); Chapter 2, Slide 11 OpenGL Objects OpenGL supports a number of different complex objects. Each object is specified by enumerating the vertices that define the object. General Form: glbegin ( object type ); glvertex ( ); glvertex ( ); glvertex ( ); glend ( ); // don t forget this! Note: There are a number of other attributes that can be placed within the glbegin glend pair. These affect things like color and shading properties. We will discuss these later. Chapter 2, Slide 12 6

Isolated Points GL_POINTS: Draws a set of isolated points. glbegin ( GL_POINTS ); (0,1) (1,1) 1) glvertex2i ( 0, 0 ); glvertex2i ( 0, 1 ); glvertex2i ( 1, 0 ); glvertex2i ( 1, 1 ); glend ( ); (0,0) (1,0) Chapter 2, Slide 13 Line Loop (Polyline) GL_LINE_LOOP: Draws a closed polygonal line (segments joined end to end). glbegin ( GL_LINE_LOOP ); glvertex2i ( 0, 0 ); glvertex2i ( 0, 1 ); glvertex2i ( 1, 1 ); glvertex2i ( 1, 0 ); glend ( ); Variants: GL_LINE_STRIP: Polygonal line, but not closed off to form a loop. GL_LINES: A sequence of line segments, not connected to each other. Chapter 2, Slide 14 7

Polygon GL_POLYGON: Draws a filled convex polygon. glbegin ( GL_POLYGON ); glvertex2i( 0, 0 ); glvertex2i( 0, 1 ); glvertex2i( 1, 1 ); glvertex2i( 1, 0 ); glend( ); Note: OpenGL assumes that all polygons are convex, meaning that all interior angles are at most 180 degrees. OpenGL is largely silent about errors, so be careful. Chapter 2, Slide 15 Triangles GL_TRIANGLES: Draws a series of filled triangles. Each sequence of three vertices defines a separate triangle. glbegin ( GL_TRIANGLES ); a=(0,1) c=(1,1) 1) glvertex2i ( 0, 1 ); glvertex2i ( 0, 0 ); glvertex2i ( 1, 1 ); // abc glvertex2i ( 1, 1 ); glvertex2i ( 0, 0 ); glvertex2i ( 1, 0 ); // cbd glvertex2i ( 1, 0 ); b=(0,0) d=(1,0) e=(2,0) glvertex2i ( 2, 0 ); These lines will not appear in final image. glvertex2i ( 1, 1 ); // dec glend ( ); Note: It is a good idea enumerate all vertices in a consistent order, say, counterclockwise. We will discuss why later. Chapter 2, Slide 16 8

Triangle Strip GL_TRIANGLE_STRIP: Draws a series of triangles by joining the next vertex to the previous two. glbegin ( GL_TRIANGLE_STRIP ); glvertex2i ( 0, 1 ); // a glvertex2i ( 0, 0 ); // b glvertex2i ( 1, 1 ); // c abc glvertex2i ( 1, 0 ); // d bcd glvertex2i ( 2, 0 ); // e cde glend ( ); a=(0,1) c=(1,1) 1) b=(0,0) d=(1,0) e=(2,0) Orientation: Note that orientations appear to alternate (abc is CCW, bcd is CW, and cde is CCW). OpenGL automatically reverses the orientation of every other triangle, so they all will be consistent with the first. In this case, all will be CCW. Chapter 2, Slide 17 In-Class Exercise Draw the shape on the right using a single triangle strip, with triangles drawn in counter- clockwise orientation. c b a f h d g e Drawn the shape on the right using a small number of triangle strips. c b a j i (What is the smallest number you can find?) d e f h g Ans 1: (cdbeafhg). Ans 2: Three (abjhi)(ghfde)(cdbh) Chapter 2, Slide 18 9

Attributes Attributes: Affect the manner in which objects are drawn. These can be placed within each glbegin glend pair. Once set, they affect subsequent objects, until changed again. Point: Point size: glpointsize ( 2.0 ); Point color: glcolor3f ( 0.0, 0.0, 1.0 ); (Sets RGB color components: Red, Green, Blue.) Line: Line width: gllinewidth ( 2.0 ); Line color: glcolor3f ( 0.0, 0.0, 1.0 ); Face: Front and/or back: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK. Face color: glcolor3f ( 0.0, 0.0, 1.0 ); Chapter 2, Slide 19 Overview OpenGL Architecture OpenGL Objects and Attributes Event-Driven Programming GLUT OpenGL/GLUT Sample Program OpenGL Buffers OpenGL Pixel Operations Chapter 2, Slide 20 10

Event-Driven Computing Typical (noninteractive) Program: Read Data. Process Data. Output Results. Event-Driven Computing: (System s perspective) Check whether an event has occurred. if (an event has occurred) call event_handler(event_type). Repeat above. Event-Driven Di Computing: (Programmer s perspective) Register event-handler pairs. (For each event call a function that performs handles this event and returns.) Pass control to the operating system. Chapter 2, Slide 21 Event Queue Operating System/Window Management System: Copies all handled events to an Event Queue. Let EQ be the event queue (first-in, in first-out queue). New event(e): if (e is handled) { append e to the end of EQ } Processing Events: while (true) { if (EQ is not empty) { call event_handler(eq.front()); remove front event from EQ; } } Chapter 2, Slide 22 11

Overview OpenGL Architecture OpenGL Objects and Attributes Event-Driven Programming GLUT OpenGL/GLUT Sample Program OpenGL Buffers OpenGL Pixel Operations Chapter 2, Slide 23 OpenGL and GLUT OpenGL is window-system independent Makes it portable. Can be targeted for different platforms: PCs, game consoles, interactive TV set-top boxes. Independent of: operating system windows system display size and display properties (only assumes a raster-device). But: OpenGL still needs to interface with windowing system. For instance: X-windows, Microsoft s s windows system GLUT (to the rescue): GLUT (GL Utility Toolkit) is a windowsystem independent programming interface for OpenGL. Chapter 2, Slide 24 12

GLUT: OpenGL Utility Toolkit GLUT: is a simple programming interface to the windows system. is largely window-system independent. supports only pop-up menus (no pull-down menus). maintains its own event loop. accepts registration of callback functions from user programs. has its own (limited) set of fonts. Some useful sites for GLUT: Mark Kilgard s GLUT page: Full online documentation. http://www.opengl.org/documentation/specs/glut/spec3/spec3.html Nate Robins GLUT for Win32: Has precompiled binaries for Microsoft Windows and installation instructions! http://www.xmission.com/~nate/glut.html Chapter 2, Slide 25 GLUT Initialization glutinit ( int* argc, char** argv ) Initialize GLUT library. Must be called first. Recognizes and processes GLUT-specific command-line arguments. glutinitdisplaymode ( unsigned int mode ) Specifies a number of options affecting general operation connected by boolean or : E.g. GLUT_RGBA GLUT_DEPTH GLUT_DOUBLE, etc Examples of Modes: GLUT_RGB : Use RGB color (most common) GLUT_RGBA: Use RGB + A color (we ll discuss ss this later) GLUT_DEPTH: Use a depth buffer for hidden surface removal GLUT_DOUBLE: Do double-buffering (recommended for smooth animation). Chapter 2, Slide 26 13

GLUT Window Manipulation glutinitwindowsize ( int width, int height ) Specifies the desired window size. (No guarantees that you will get this.) glutinitwindowposition ( int x, int y ) Requests location (in pixels) of the upper left corner of the window. Note that (0,0) is the upper left corner of the display. glutcreatewindow ( char* window_name ) Request that the window be created. (This initializes the request. The window is not created immediately.) glutreshapewindow ( int width, int height ) Resize the display window using the parameters. Alternately, t l you can run in full-screen mode: glutfullscreen ( ) Resize window to full screen. (See also glutentergamemode ( )). glutentergamemode ( ) Same as glutfullscreen, but with further speed enhancements. Chapter 2, Slide 27 GLUT Main Event Loop glutmainloop ( ) Starts the GLUT event processing loop. Never returns (except through callbacks). Calls registered function callbacks (user-defined event handlers) as appropriate. Should be called at most once. Before calling this, be sure to register your events (or this will be a very uninteresting program). Chapter 2, Slide 28 14

Sample Program (Part I) int main ( int argc, char** argv ) { // program arguments glutinit( &argc, argv ); // initialize glut and gl // double buffering and RGB glutinitdisplaymode l ( GLUT_DOUBLE GLUT_RGB ); glutinitwindowsize ( 400, 300 ); // initial window size glutinitwindowposition ( 0, 0 ); // initial window position glutcreatewindow ( argv[0] ); // create window initialize callbacks here (described below) } myinit ( ); // your own initializations glutmainloop ( ); // turn control over to glut return 0; // (make the compiler happy) This is never executed! Chapter 2, Slide 29 GLUT Window Management glutswapbuffers ( ) Swaps front and back buffers. This is part of a process called double-buffering (more on this later). glutpostredisplay ( ) Mark the current window as needing to be redrawn. GLUT only redraws the window when you request it. Next iteration of the glutmainloop will refresh the window using the display-function callback. To achieve a continuous animation, call this function repeatedly (e.g., inside a timer loop or idle loop that wakes up every 1/30 second). Chapter 2, Slide 30 15

GLUT Callback Registration glutdisplayfunc (void (*func) ( ) ) Call the given function whenever the window needs to be redrawn. For example: When window is first created. When the window is revealed because an overlapping window is removed. When the program explicitly calls glutpostredisplay( ). glutreshapefunc (void (*func) (int width, int height) ) Call the given function whenever the window is resized. This is always called when the window is first created. Many opengl commands require that the graphics window exists. This is useful as a signal that the window (requested in glutcreatewindow()) actually exists. Chapter 2, Slide 31 GLUT Callback Registration glutkeyboardfunc (void (*func) (unsigned char key, int x, int y)) Call the given function when a keyboard key is hit. The (x, y) arguments indicate where the mouse was when key was pressed. See also glutspecialfunc( ) for shift/control keys. glutmousefunc (void (*func) (int button, int state, int x, int y)) Call the given function when a mouse button is hit or released. Button argument: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON. State: GLUT_UP, GLUT_DOWN. Position (x, y): ) Relative to upper left corner. (We ll see an example below) Chapter 2, Slide 32 16

GLUT Callback Registration (cont) glutmotionfunc (void (*func) (int x, int y) ) Mouse motion while button pressed. (x, y) is current mouse position. glutpassivemotionfunc (void (*func) (int width, int height) ) Mouse motion without button press. (Warning: This can generate lots of events!) glutidlefunc (void (*func) ( ) ) Called whenever no other events are on the event queue. Passing NULL disables this. gluttimerfunc (unsigned int msecs, void (*func) (int value), value) ) Callback every msecs milliseconds (or more): Best effort. The value parameter is used for setting multiple alarms. Function func called with the specified value parameter. Chapter 2, Slide 33 Sample Program (Part II) int main ( int argc, char** argv ) { // program arguments (given in Part I) glutdisplayfunc ( mydraw ); // set up callbacks glutreshapefunc ( myreshape ); glutmousefunc ( mymouse ); glutkeyboardfunc ( mykeyboard ); gluttimerfunc (20, mytimeout, 0 ); // (see below) (given in Part I) } Chapter 2, Slide 34 17

Sample Program (Part III) void mydraw ( ) { // called to display window //...insert your drawing code here... } void myreshape ( int w, int h ) { // called if reshaped windowwidth = w; // save new window size windowheight = h; //...may need to update the projection... glutpostredisplay ( ); // request window redisplay } void mytimeout ( int id ) { // called if timer event //...advance the state of animation incrementally... glutpostredisplay ( ); // request redisplay gluttimerfunc (20, mytimeout, 0); // request next timer event } Chapter 2, Slide 35 Sample Program (Part IV) void mymouse ( int b, int s, int x, int y ) { // called if mouse click switch (b) { // b indicates button case GLUT_LEFT_BUTTON: if ( s == GLUT_DOWN ) // left button pressed else if ( s == GLUT_UP ) // left button released break; //... // other button events } } void mykeyboard ( unsigned char c, int x, int y ) { // keyboard key hit switch (c) { // c is the key that is hit case 'q': // 'q' means quit exit(0); break; //... // other keyboard events } } Chapter 2, Slide 36 18

Sample Program (Part V) void mydisplay ( ) { // display function glclear ( GL_COLOR_BUFFER_BIT ); // clear the window } glcolor3f ( 1.0, 10 00 0.0, 00 0.0 ); glbegin ( GL_POLYGON ); glvertex2f ( 0.90, 0.50 ); glvertex2f ( 0.50, 0.90 ); glvertex2f ( 0.10, 0.50 ); glvertex2f ( 0.50, 0.10 ); glend ( ); glcolor3f ( 0.0, 0.0, 1.0 ); glrectf ( 0.25, 0.25, 0.75, 0.75 ); glutswapbuffers ( ); // set color to red // draw a diamond // set color to blue // draw a rectangle // swap buffers (make visible) Chapter 2, Slide 37 DEMO Chapter 2, Slide 38 19

Overview OpenGL Architecture OpenGL Objects and Attributes Event-Driven Programming GLUT OpenGL/GLUT Sample Program OpenGL Buffers OpenGL Pixel Operations Chapter 2, Slide 39 Pixel Buffers and Operations Pixel Buffers: OpenGL maintains from one to many pixel buffers. These buffers store different types of information and have different functions. We will discuss: Buffer concepts: bitmaps, pixmaps, depth. OpenGL Buffers: color-, depth-, accumulation-, and stencil buffers. Transfer: reading and writing pixel buffers. Imaging g Operations: user operations, bitblt, blend. Chapter 2, Slide 40 20

Bitmaps and Pixmaps Pixel: A picture element. Bitmap: A 2D array of Pixel Pixmap single-bit pixels (0/1 or (depth = 8) black/white). Pixmap: Stack of bitmaps. The number of bits per pixel is called its depth. To represent full RGB color, it is sufficient to have 24-bit depth, 8 bits each for red, green, and blue. Bitmap Chapter 2, Slide 41 Bitmaps and Pixmaps Bitmap (depth = 1) Pixmap (depth = 8) Pixmap (depth = 24) Chapter 2, Slide 42 21

OpenGL Buffers Color Buffer: Stores image color information. RGB: Red, green, blue RGB or RGBA: Alpha-channel used for blending operations, such as transparency. Depth Buffer: Stores distance to object pixel. Used for hidden surface removal the closest pixel survives. Also called the Z-buffer (z-coordinate stores distance). Accumulation Buffer: Used for composing and blending images. Useful for achieving affects such as motion blur. Stencil Buffer: Useful for masking operations. Chapter 2, Slide 43 Color Buffer: Indexed Color Indexed Color: Rather than storing full 24-bits per pixel, a small number (say 256) of representative colors are stored in a color-map table, and each pixel stores an index to this table. Less storage: 8-bits per pixel versus 24. Somewhat slower: Color lookup cost. Limited: Fewer simultaneous colors (typically 256). Color mixing: Such as dithering needed to approximate colors. Rarely used: Since memory is cheap. (Still seen in.gif image files.) Chapter 2, Slide 44 22

Color Buffer: Direct Color Direct Colors: Full 24-bit color is stored in each pixel. More storage: 24-bits per pixel. Faster: No lookups required. General: Can represent more colors. More flexible: Easier to perform interpolations, etc. Common: Used on virtually all modern systems. Typically stored in RGB format. (Easy for devices, not for humans.) Can also include -value for transparency and blending. Chapter 2, Slide 45 Other Buffers Stencil Buffer: Restrict drawing using a stencil (mask). Much like cardboard stencils used in art. Accumulation Buffer: Can hold RGBA and allow multiple overlapping drawings to be blended to form final image. Useful for antialiasing, soft shadows, motion blur, Double buffering: Using two buffers to achieve smooth animation transitions. Drawing is done to the back buffer. Front buffer is displayed. Swapping between buffers (glutswapbuffers) is instantaneous. Chapter 2, Slide 46 23

Buffers Selection in GLUT void glutinitdisplaymode ( unsigned int mode ): where mode is the bitwise OR of GLUT display mode bit masks: GLUT_RGBA: Select an RGBA (direct) color. (Default) GLUT_RGB: (Same as GLUT_RGBA) GLUT_INDEX: Select indexed color. (Rarely used) GLUT_SINGLE: Use single buffering. (Default) GLUT_DOUBLE: Use double buffering. GLUT_ACCUM: Allocate space for accumulation buffer. GLUT_ALPHA: Allocate space for color blending. GLUT_DEPTH: Allocate space for depth buffer. GLUT_STENCIL: Allocate space for stencil buffer. Example: glutinitdisplaymode ( GLUT_RGBA GLUT_DOUBLE GLUT_DEPTH ); Chapter 2, Slide 47 Overview OpenGL Architecture OpenGL Objects and Attributes Event-Driven Programming GLUT OpenGL/GLUT Sample Program OpenGL Buffers OpenGL Pixel Operations Chapter 2, Slide 48 24

Reading and Writing Buffers glreadpixels ( x, y, width, height, format, type, *pixels ) glrasterpos2i ( x, y ) gldrawpixels ( width, height, format, type, *pixels ) glcopypixels ( x, y, width, height, format, type, buffer ) where: format: GL_RGB, GL_RGBA, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_COLOR_INDEX, GL_DEPTH_COMPONENT, type: GL_UNSIGNED_BYTE, _ GL_UNSIGNED_SHORT, _ GL_FLOAT, GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_SHORT_5_6_5, buffer: GL_COLOR, GL_DEPTH, GL_STENCIL Chapter 2, Slide 49 Pixel Operations User Defined: draw, erase. Bitblt (Bit Block Transfer) ) and Raster Ops: hardware supported. works on rectangular regions of the buffer. clear, set, move, and, or, xor. Blending: dissolve, composite, accumulate. Chapter 2, Slide 50 25

Chroma Keying + = Chapter 2, Slide 51 Summary Summary: Graphics Libraries OpenGL and its Structure Drawing Primitives in OpenGL GLUT and Event-Driven Programming Buffers and raster operations What s Next? Geometry for Graphics Chapter 2, Slide 52 26