410 likes | 816 Vues
Graphics pipeline Coordinate systems Graphics software Specific-purpose graphics packages Computer-Graphics Application Programming Interface (CG API) OpenGL OpenGL example and testing. Lecture 3 Graphics Pipeline and Graphics Software. CG demo. Answer Questions.
E N D
Graphics pipeline • Coordinate systems • Graphics software • Specific-purpose graphics packages • Computer-Graphics Application Programming Interface (CG API) • OpenGL • OpenGL example and testing Lecture 3 Graphics Pipeline and Graphics Software CG demo CP411 Computer Graphics
Answer Questions • What are the four major parts of a typical raster graphics system? • A raster system has a resolution of 1280 by 1024. If the color depth is 24 bits per pixel. What size frame buffer (in bytes) is needed for this system? • How long would it take to load a 1280 by 1024 frame buffer with 16 bits per pixel if 64*106 bits can be transferred per second? • A raster system has a resolution of 1280 by 1024. How many pixels could be accessed per second by a display controller that refreshes the screen at a rate of 60 frames per second? CP411 Computer Graphics
Transform Illuminate Transform Clip Project Rasterize Model and view Rendering Frame buffer Display Rendering 3D Scenes CP411 Computer Graphics
Rendering Steps Scene graphObject geometry ModelingTransforms LightingCalculations ViewingTransform Clipping ProjectionTransform Mapping to Display CP411 Computer Graphics
Graphics pipeline CP411 Computer Graphics
Graphics pipeline • Scenes are composed of models in two or three-dimensional space • Models are composed of primitives (such as vertex, line, triangle, polygons) supported by the rendering system • Models are entered by hands or created by programs. • The image is drawn on monitor, printed on laser printer, or written to a raster in memory or a file. Rendering is the conversion from a scene to an image • Graphics pipeline is the finer steps of conversion from model to scene and to image CP411 Computer Graphics
Coordinate systems in the pipeline • Modeling Coordinate System (MCS), local coordinate or master coordinate • World Coordinate System (WCS) • Viewer Coordinate System (VCS) • Normalized Device Coordinate System (NDCS) • Device Coordinate System or equivalently the Screen Coordinate System (DCS or SCS) CP411 Computer Graphics
Transformations • Modeling transforms (models to world) • position, scale, rotation • Viewing transforms (world to viewer’s world) • Position of camera (eye). • Projection transforms (viewer’s world to viewer’s plane) • Project the objects into a plane • Display transformation (view plane to display) • Mapping the view portion to display window CP411 Computer Graphics
Transformations CP411 Computer Graphics Fall 2006 Wilfrid Laurier University
Projection • Projection transforms primitives in 3D to 2D • Parallel projection • Perspective project CP411 Computer Graphics
Illumination and Shading • Lighting (derived information) • The color and intensity of primitives may be added in the rendering process according to the light source and reflection of features of the objects • Often, determine the color intensity of vertices of a primitive • Determine the color intensity of each pixel of a primitive • Using the vertex color or a texture and shading models CP411 Computer Graphics
Culling and Clipping • Culling is to remove hidden primitives • No need to render primitives covered by other primitive, so they may be removed and save computing time • Clipping • To remove or modify primitives if they are not within the view portion CP411 Computer Graphics
Primitives • Models are composed of/converted to geometric primitives. • Typical rendering primitives directly supported in hardware: • Points (single pixels) • Line segments • Polygons (perhaps only convex polygons or triangles). • Modeling primitives also include • Piecewise polynomial (spline) curves • Piecewise polynomial (spline) surfaces • Implicit surfaces (quadrics, bobbies, etc) CP411 Computer Graphics
Rendering Primitives • Algorithms are used to implement primitives. Such algorithms are called scan conversion algorithms. • Scan conversion algorithms are implemented in either software or hardware. • Hardware implementation is usually in GPU. • Software implementation can ran in CPU, or GPU if it is programmable. CP411 Computer Graphics
Graphics algorithms • Basic algorithms • Transformation: Convert representations of models/primitives from one coordinate system to another. • Culling and clipping: Remove primitives and parts of primitives that are not visible on the display. • Rasterization: Convert a projected screen-space primitive to a set of pixels. • Advanced algorithms: • Picking: Select a 3D object by clicking an input device over a pixel location. • Shading and Illumination: Simulate the interaction of light with a scene. • Animation: Simulate movement by rendering a sequence of frames. CP411 Computer Graphics
Graphics software • A graphics software is a package consisting of implementation of the graphics algorithms • Special purpose packages • For non-programmers • Generate pictures, graphs, in some application area • Use menus and button and input device to create • No need to worry about the graphic procedure • Examples • General programming graphics packages • Provide library of graphics functions that can be used in a programming language through API calls • Graphics functions include straight lines, polygons, sphere, and some other simple geometric shapes CP411 Computer Graphics
CG API standard specification • A standard CG API specification by an institute that followed by CG hardware and software venders for their products • Example: OpenGL is CG API specification • It has C/C++ implementation, hardware implementation, and Java implementation • Direct3D is another standard by Microsoft CP411 Computer Graphics
OpenGL Introduction to OpenGL OpenGL related library OpenGL program structure OpenGL routines for drawing primitives Work with I/O devices OpenGL installation and configuration CP411 Computer Graphics Lecture 4 Introduction to OpenGL
Figure 22-3 The OpenGL fixed-function pipeline. CP411 Computer Graphics Wilfrid Laurier University
1. Introduction to OpenGL • What is OpenGL (Open Graphics Library) ? Provide a standard specification defining a cross-platform API for writing applications that produce 3D computer graphics (and 2D computer graphics as well). • Mid-level, device-independent, portable graphics subroutine package • Developed primarily by SGI in early of 1990’s. • Does not include low-level I/O management • Basis for higher-level libraries/toolkits, GLU, GLUT • OpenGL provides a standard implementation for graphics pipeline and scan conversion algorithms for fundamental primitives. CP411 Computer Graphics Lecture 4 Introduction to OpenGL
A brief history of OpenGL • OpenGL 1.0 • released in 1992, by OpenGL architectural review board (OpenGL ARB), by Mark Segal and Kurt Akeley • OpenGL 2.0 • Released on September 7, 2004. Support Shader language GLSL • OpenGL 3.0 • Released on July 11, 2008. • OpenGL 4.0 • Released on March 11, 2010 • Current OpenGL 4.2 • Released on 8 August 201Check more on textbook Appendix C or Wiki CP411 Computer Graphics Lecture 4 Introduction to OpenGL
OpenGL Design Goals • SGI’s design goals for OpenGL: • High-performance (hardware-accelerated) graphics API • Some hardware independence • Natural, terse API with some built-in extensibility • To add new functions to adapt to new hardware features • Why has OpenGL become a CG standard? • It doesn’t try to do too much • Only renders the image, doesn’t manage windows, etc. • No high-level animation, modeling, sound (!), etc. • It does enough • Useful rendering effects + high performance • It was promoted by SGI (and Microsoft), is now promoted/supported by NVIDIA, ATI, etc. • It doesn’t change every year • Work with Network environment • Client: ran programs and issue OpenGL drawing command • Server: receive commands and perform the drawing CP411 Computer Graphics Lecture 4 Introduction to OpenGL
OpenGL • OpenGL does the rendering pipeline: • Transform geometry (object ->world ->eye->device-> frame buffer) • Calculate surface lighting • Apply perspective projection (eye ->screen) • Clip • Perform visible-surface processing • Scan conversion/rasterization of primitives • Core functions/routines, for transformation, and rasteriziation for primitives for 2D/3D graphics, and many others. • GL functions have a prefix gl CP411 Computer Graphics Lecture 4 Introduction to OpenGL
2. Related Libraries • OpenGL utility library (GLU) • It consists of a number of functions that use the base OpenGL library to provide higher-level drawing routines from the more primitive routines that OpenGL provides. • It is usually distributed with the base OpenGL package • Functions in GLU are basically built up by routines from GL • Features including • Setting up viewing and projection • Describing complex objects with line and polygon approximaiton • Display quadrics and B-splines using approximaition • It also provides additional primitives for use in OpenGL applications, including spheres, cylinders and disks • Generally in more human-friendly terms than the routines presented by OpenGL. CP411 Computer Graphics Lecture 4 Introduction to OpenGL
GLUT • OpenGL Utility Toolkit (GLUT) was written by Mark J. Kilgard • A library of utilities for OpenGL programs, which primarily perform system-level I/O with the host operating system. • Functions performed include window definition, window control, and monitoring of keyboard and mouse input: including setting up windows, window size, position, keyboard, mouse, etc. • Functions with prefix: glut, see glut reference • Features • Routines for drawing a number of geometric primitives (both in solid and wireframe mode), including cubes, spheres, and the teapot • GLUT has some limited support for creating pop-up menus • Note that there special extension for each platform • GLX for X window system • Apple GL (AGL) • WGL (Windows-to-OpenGL) CP411 Computer Graphics Lecture 4 Introduction to OpenGL
GLUI • GLUI is a GLUT-based C++ user interface library(was written by Paul Rademacher) • It provides controls such as buttons, checkboxes, radio buttons, and spinners to OpenGL applications • It is window- and operating system independent, relying on GLUT to handle all system-dependent issues, such as window and mouse management • GLUI can be installed by updating from Dev-C++ CP411 Computer Graphics Lecture 4 Introduction to OpenGL
OpenGL: Conventions • Functions in OpenGL start with gl • Most functions just gl(e.g., glColor()) • Functions starting with glu are utility functions (e.g., gluLookAt()) • Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c) See OpenGL example, textbook chapter 3, page 46. CP411 Computer Graphics Lecture 4 Introduction to OpenGL
#include <GL/glut.h> // (or others, depending on the system in use) void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glMatrixMode (GL_PROJECTION); // Set projection parameters. gluOrtho2D (0.0, 200.0, 0.0, 150.0); } void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); // Clear display window. glColor3f (0.0, 0.0, 1.0); // Set line segment color to red. glBegin (GL_LINES); glVertex2i (180, 15); // Specify line-segment geometry. glVertex2i (10, 145); glEnd ( ); glFlush ( ); // Process all OpenGL routines as quickly as possible. } CP411 Computer Graphics Wilfrid Laurier University
int main (intargc, char** argv) { glutInit (&argc, argv); // Initialize GLUT. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glutInitWindowPosition (50, 100); // Set top-left display-window position. glutInitWindowSize (400, 300); // Set display-window width and height. glutCreateWindow ("An Example OpenGL Program"); // Create display window. init ( ); // Execute initialization procedure. glutDisplayFunc (lineSegment); // Send graphics to display window. glutMainLoop ( ); // Display everything and wait. } CP411 Computer Graphics Wilfrid Laurier University
3. OpenGL Program Structure Basic program structure: C/C++ with function call to OpenGL functions • Header • Global variables and data structures for controls and modeling • Self-defined functions (C/C++, gl, glu, glui, function calls are involved) • Main function (glut functions calls are involved) • Header files#include <windows.h>#include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <GL/glui.h> // if GLUI is installed • If glut.h is included, no need to include gl.h and glu.h • Libraries links (on Windows platform) for the project-lopengl32 -lglu32 -lglut32 -lwinmm • May include other C/C++ headers, such as stdio.h, stdlib.h, math.h, etc. CP411 Computer Graphics Lecture 4 Introduction to OpenGL
main() function • Main() includes glut functions • GLUT initialization • Create a window and attach OpenGL to it • Set camera parameters Setup lighting, if any 4. Register callback functions • Key press, mouse movement, screen resize 5. Main rendering loop • OpenGL controls execution from this point forward CP411 Computer Graphics Lecture 4 Introduction to OpenGL
1. GLUT initializationglutInit(&argc, argv); • argc and argv potentially used by glutInit and preserved glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE ); • Red, green, blue, and alpha (transparency), double buffer, and depth buffered glutInitWindowSize(400, 300); // default 400 by 300 glutInitWindowPosition(50, 100); 2. Create a window glutCreateWindow(“An example of OpenGL grogram”); CP411 Computer Graphics Lecture 4 Introduction to OpenGL
3. Define viewport, project, matrixmod (defined in init() ) glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glMatrixMode (GL_PROJECTION); // Set projection parameters. gluOrtho2D (0.0, 200.0, 0.0, 150.0); • More glViewport(0, 0, width, height); • Viewport is the portion of the window on the project plane you want to render CP411 Computer Graphics Lecture 4 Introduction to OpenGL
5. Register callback fucntionsexecuted upon interrupt caused by user or by OpenGL main loop glutDisplayFunc(lineSegment); lineSegment() does the rendering, send graphics to display window, redisplay • More register funcitons glutReshapeFunc(resize_scene); resize_screen() is executed upon startup and upon window resize glutKeyboardFunc(key_press); key_press() is executed when a key is pressed glutMouseFunc(handle_mouse_click); handle_mouse_click() is executed when a mouse button pressed glutMotionFunc(mouse_motion); mouse_motion() is executed when key clicked and mouse moved 6. Initiate main rendering loop glutMainLoop(); Your program never regains explicit control CP411 Computer Graphics Lecture 4 Introduction to OpenGL
Error handling #include <stdio.h> GLenumerrorCheck () { GLenum code; const GLubyte *string; code = glGetError (); if (code != GL_NO_ERROR) { string = gluErrorString (code); fprintf( stderr, "OpenGL error: %s\n", string ); } return code; } CP411 Computer Graphics Fall 2006 Wilfrid Laurier University
4. OpenGL routines to draw primitives void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); // Clear display window. glColor3f (0.0, 0.0, 1.0);// Set line segment color to red. glBegin (GL_LINES); glVertex2i (180, 15); // Specify line-segment geometry. glVertex2i (10, 145); glEnd ( ); glFlush ( ); // Clean buffers, make the processing routines fast } CP411 Computer Graphics Lecture 4 Introduction to OpenGL
Specifying Geometry • Simple case first: object vertices already in world coordinates • Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleglBegin (GL_POLYGON); glVertex2i (80, 10); glVertex2i (30, 100); glVertex2i (120, 50); glEnd ( ); • Usage: glBegin(geomtype) where geomtype is: points, lines, polygons, triangles, quadrilaterals, etc... CP411 Computer Graphics Lecture 4 Introduction to OpenGL
v2 v4 v0 v5 v1 v3 OpenGL: Triangle Strips • An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glVertex3fv(v5); glEnd(); • triangle 0 is v0, v1, v2 • triangle 1 is v2, v1, v3 • triangle 2 is v2, v3, v4 • triangle 3 is v4, v3, v5 (again, not v3, v4, v5) CP411 Computer Graphics Lecture 4 Introduction to OpenGL
Function names indicate argument type and number • Functions ending with f take floats • Functions ending with i take ints • Functions ending with b take bytes • Functions ending with ub take unsigned bytes • Functions that end with v take an array. • Examples glColor3f() takes 3 floats glColor4fv()takes an array of 4 floats CP411 Computer Graphics Lecture 4 Introduction to OpenGL
5. Working with I/O Devices • Key board. See example • Mouse. See example • Menu and submenu: See example CP411 Computer Graphics Lecture 4 Introduction to OpenGL