Fundamentals of Computer Graphics Part 1

Slides:



Advertisements
Similar presentations
Chapter 2: Graphics Programming
Advertisements

Fundamentals of Computer Graphics Part 1
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
1 CSC461 Lecture 7: 3D Programming in OpenGL Objectives: Develop 2D and 3D examples -- Sierpinski gasket: a fractal Develop 2D and 3D examples -- Sierpinski.
InteractionHofstra University1 Graphics Programming Input and Interaction.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Models and Architectures Ed Angel Professor of Computer Science, Electrical and Computer.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
Fundamentals of Computer Graphics Part 3 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Lecture 3 OpenGL.
CSC 461: Lecture 3 1 CSC461 Lecture 3: Models and Architectures  Objectives –Learn the basic design of a graphics system –Introduce pipeline architecture.
CS 480/680 Computer Graphics Image Formation Dr. Frederick C Harris, Jr.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Computer Graphics I, Fall 2010 Programming with OpenGL Part 3: Three Dimensions.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
1 Angel: Interactive Computer Graphics5E © Addison- Wesley 2009 Image Formation Fundamental imaging notions Fundamental imaging notions Physical basis.
Graphics: Conceptual Model
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science,
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
Chapter 1 Graphics Systems and Models Models and Architectures.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Unit-4 Geometric Objects and Transformations- I
The Human Visual System vs The Pinhole camera
- Introduction - Graphics Pipeline
Computer Graphics Lecture 33
Course code:10CS65 | Computer Graphics and Visualization
Programming with OpenGL Part 2: Complete Programs
Course code: 10CS65 | Computer Graphics and Visualization
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
Programming with OpenGL Part 3: Three Dimensions
OpenGL API 2D Graphic Primitives
Models and Architectures
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Models and Architectures
Models and Architectures
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
Fundamentals of Computer Graphics Part 3
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Models and Architectures
Interactive Computer Graphics Graphics Programming
Input and Interaction Ed Angel
University of New Mexico
Input and Interaction Ed Angel
Models and Architectures
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 6: Three Dimensions
Programming with OpenGL Part 3: Three Dimensions
Programming with OpenGL Part 2: Complete Programs
University of New Mexico
Programming with OpenGL Part 2: Complete Programs
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

Fundamentals of Computer Graphics Part 1 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Fundamentals of Computer Graphics Graphics Systems Five major elements - processor, memory, frame buffer, output devices, input devices Fundamentals of Computer Graphics

Pixels and Frame Buffer Most graphics systems are pixel based – need of rasterization or scan conversion; pixel = picture element 8 bits deep frame – 256 colors; 24 or 32 bits for RGB colors picture detail Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Output Devices The cathode-ray tube CRT Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Output Devices RGB – shadow mask Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Output Devices Refresh rate: 50 – 85 Hz, for stereovision 120Hz (2 x60 Hz) Mode: interlaced versus non-interlaced Masks: DELTA versus INLINE LCD Displays – raster based Raster devices sequential access - plotters etc. direct access – displays etc. mixed – inkjet printers – printed sequentially, accessed directly Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Images – Physical versus Synthetic Computer graphics generates pictures with the aim of: to create realistic images to create images very close to “traditional” imaging methods Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Objects and Viewers Two basic entities (one object seen from two different positions) : object(s) – exists in space independent of any image-formation process or viewer viewer(s) Fundamentals of Computer Graphics

Objects, Viewers & Camera Camera system object and viewer exist in E3 image is formed in the Human Visual system (HSV) – on the retina In the film plane if a camera is used Object(s) & Viewer(s) in E3 Pictures in E2 Transformation from E3 to E2 projection Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Lights & Images Others attributes: light sources position monochromatic / color if not used scene would be very dark and flat shadows and reflections - very important for realistic perception geometric optics used for light modeling Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Colours Light is a form of electromagnetic radiation Visible spectrum 350 – 780 nm Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Ray Tracing Ray tracing building an imaging model by following light from a source a ray is a semi-infinite line that emanates from a point and “travels” to infinity in a particular direction portion of these infinite rays contributes to the image on the film plane of the camera surfaces: diffusing reflecting refracting Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Ray Tracing A different approach must be used: for each pixel intensity must be computed all contributions must be taken into account a ray is “followed” in the opposite direction, when intersect a surface it is split into two rays contribution from light sources and reflection from other resources are counted Fundamentals of Computer Graphics

Human Visual System - HVS rods and cones (tyčinky a čípky) excited by electromagnetic energy in the range 350-780 nm sizes of rods and cones determines the resolution of HVS – our visual acuity the sensors in the human eye do not react uniformly to the light energy at different wavelengths Fundamentals of Computer Graphics

Human Visual System - HVS Courtesy of http://www.webvision.med.utah.edu/into.html Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Human Visual System different HVS response for single frequency light – red/green/blue relative brightness response at different frequencies this curve is known as Commision Internationale de L’Eclairage (CIE) standard observer curve the curve matches the sensitivity of the monochromatic sensors used in black&white films and video camera most sensitive to GREEN colors Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Human Visual System three different cones in HVS blue, green & yellow – often reported as red for compatibility with camera & film Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Pinhole Camera Box with a small hole film plane z = - d !!!yp,-d Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Pinhole Camera point (xp,yp,-d) – projection of the point (x,y,z) angle of view or field of the camera – angle  ideal camera – infinite depth of field Fundamentals of Computer Graphics

Synthetic Camera Model computer-generated image based on an optical system – Synthetic Camera Model viewer behind the camera can move the back of the camera – change of the distance d i.e. additional flexibility objects and viewer specifications are independent – different functions within a graphics library Imaging system Fundamentals of Computer Graphics

Synthetic Camera Model a – situation with a camera b – mathematical model – image plane moved in front of the camera center of projection – center of the lens projection plane – film plane (průmětna) Fundamentals of Computer Graphics

Synthetic Camera Model Imaging with the Synthetic Camera Model film plane position in a camera projected scene to the projection plane Fundamentals of Computer Graphics

Synthetic Camera Model Not all objects can be seen limit due to viewing angle Solution: Clipping rectangle or clipping window placed inn front of the camera ad b shows the case when the clipping rectangle is shifted aside – only part of the the scene is projected Fundamentals of Computer Graphics

Programmer’s Interface Numerous ways for user interaction with a graphics system using input devices - pads, mouse, keyboards etc. different orientation of coordinate systems canvas versus OpenGL etc. Fundamentals of Computer Graphics

Application Programmer’s Interface API functionality should match the conceptual model Synthetic Camera Model used for APIs like OpenGL, PHIGS, Direct 3D, Java3D, VRML etc. Functionality needed in the API to specify: Objects Viewers Light sources Material properties Fundamentals of Computer Graphics

Application Programmer’s Interface Objects are defined by points or vertices, line segments, polygons etc. to represent complex objects API primitives are displayed rapidly on the hardware usual API primitives: points line segments polygons text Fundamentals of Computer Graphics

Application Programmer’s Interface OpenGL defines primitives through list of vertices – triangular polygon is drawn by: glBegin(GL_POLYGON); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glEnd( ); attribute GL_POLYGON actually defines the primitive to be drawn – others GL_LINE_STRIP - draws a strip – n+1 points define n triangles GL_POINTS – draws only points Fundamentals of Computer Graphics

Application Programmer’s Interface Some APIs : work with frame buffer – read/write pixel level provides curves & surfaces / approximated by a series of simpler primitives OpenGL provides frame buffer, curves and surfaces Fundamentals of Computer Graphics

Application Programmer’s Interface Camera specification in APIs: position – usually center of lens orientation – camera coordinate system in center of lens camera can rotate around those three axis focal length of lens determines the size of the image on the film actually viewing angle film plane - camera has a height and a width Fundamentals of Computer Graphics

Application Programmer’s Interface Two coordinate systems are used: world coordinates, where the object is defined camera coordinates, where the image is to be produced Transformation for conversion between coordinate systems or gluLookAt(cam_x, cam_y,cam_z, look_at_x, look_at_y, look_at_z,…) glPerspective( field_of_view) Lights – location, strength, color, directionality Material – properties are attributes of objects Observed visual properties of objects are given by material and light properties Fundamentals of Computer Graphics

Modeling - Rendering Paradigm In many applications the modeling is separated from production of an image – rendering (CAD systems, animations etc.) In this case the modeling SW/HW might be different from the renderer the connection between both parts can be simple or highly complex using distributed environments Fundamentals of Computer Graphics

Graphics Architectures Early graphics systems – CRT had just basic capability to generate line segments connecting two points vector based with refreshing – length of line segments limited light pen often used for manipulation systems with memory CRT – the whole picture redrawn if changed Fundamentals of Computer Graphics

Graphics Architectures Display processors standard architecture with capabilities to display primitives composition made at the host memory – display list – contains primitives to be displayed. Fundamentals of Computer Graphics

Pipeline Architectures VLSI circuits enabled major advances in graphics architectures simple arithmetic pipeline a + b * c when addition of (b * c) and a is performing new b * c is computed in parallel – pipelining enabled significant speed up similar approach can be used for processing of geometric primitives as well Fundamentals of Computer Graphics

Pipeline Architectures There are 4 major steps in the geometric pipeline: transformations – like scaling, rotations, translation, mirroring, sheering etc. clipping – removal of those parts that are out of the viewing field projection rasterization homogeneous coordinates and matrix operations geometric transformations are used Fundamentals of Computer Graphics

Clipping, Projection & Rasterization Clipping is used to remove those parts of the world that cannot be seen. Objects representation is “kept” in 3D as long as possible. After transformation and clipping must be projected to 2D somehow projected objects or their parts must be displayed – and therefore rasterized. All those steps are performed on your graphics cards in haerware nowadays. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Conclusion Consider a standard camera with 36x24 mm film and having a zoom 26 – 140mm. How the viewing angle or viewing field is defined. what is the difference in viewing between cases a) and b)? answer question from Chapter 1 Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Exercise No.1 The aim of the first experiment is: to read data of a complex geometric model defined by the TRI format (routine for reading will be given) to store it in the data structure to display the model as a set of triangles as wire-model (without shading, visibility) to explore other possibilities of drawing visible parts only by with constant shading by setting some attributes to explore a possibility to create your own data model Fundamentals of Computer Graphics

Exercise No.1 – TRI format # (data originally from powerflip, not avalon) # Object name : --- The canonical cow --- # Number of triangles : 5804 # Number of vertices : 2905 [Vertices] 0.0 0.0 0.0 0.151632 -0.043319 -0.08824 x,y,z coordinates of the j-th vertex 0.163424 -0.033934 -0.08411 0.163118 -0.053632 -0.080509 Fundamentals of Computer Graphics

Exercise No.1 – TRI format [Triangles] 1 2 3 vertex indices forming the i-th triangle 2 4 5 5 4 6 ……….. [Triangles' Normals] 0.442 -0.167 -0.881 i-th normal vector for the i-th triangle 0.595 -0.088 -0.798 0.735 -0.093 -0.671 ………………………………………….. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 2 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Fundamentals of Computer Graphics Graphics programming OpenGL API will be used for development principles presented are valid also for Java3D (some modifications needed), Graphical Kernel System- GKS, Programmer’s Hierarchical Graphics Interface – PHIGS functionality sufficient for writing sophisticated 2D programs without users interaction example of a simple 3D application Fundamentals of Computer Graphics

Sierpinski Gasket Three vertices are given (x1, y1), (x2, y2), (x3, y3) pick up a random point inside the triangle select one of the three vertices at random find the point halfway between initial point and the randomly selected vertex display this new point by putting some sort of marker, such as a small circle, at its location replace the initial point with this new point return to step 2 Typical example of “random access” Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Sierpinski Gasket Possible form of your program might be: main ( ) { initialize_the_system (); for (some_number_of_points ) pt = generate_a_point(); display_the_point(pt); } cleanup() Final program in OpenGL will almost that simple, but slightly different Fundamentals of Computer Graphics

Pen Plotter Model Two basic functions for drawing: moveto(x , y) – pen up lineto(x , y) – pen down moveto (0,0); lineto(1,0);lineto(1,1);lineto(0,1);lineto(0,0); { draws a rectangle } moveto(0,1); lineto(0.5,1.866); lineto(1.5,1.866); lineto(1.5,0.866); lineto(1,0);moveto(0,0); lineto(1.5,1.866); { draws a cube using oblique projection } Typical example of “sequential access” Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Pen Plotter Model This approach is used for printing industry PostScript page description HPGL language control of printers, typesetters etc. restriction to “2D world” only. BUT our interest is 3D API drawing area (pad) infinite 2D area correspond to z = 0 a point p = (x,y) the plane is represented as p = (x,y,0) in 3D often term vertex is used instead of point a vertex is a location in the space Fundamentals of Computer Graphics

Fundamentals of Computer Graphics GL specifications Multiple forms for functions Vertex functions – general forms glVertex[nt|ntv] where: n signifies number of dimensions (2,3,4) t denotes data type t {i,f,d,b,s,ub,us,ui} i – integer (32 bits), f – float (32 bits), d – double (64 bits) b – signed char (8), s – short (16), u – unsigned &{b,s,i} v indicates that variables are specified through the pointer to an array Fundamentals of Computer Graphics

Fundamentals of Computer Graphics GL specifications OpenGL types GLfloat, GLint – instead of float, int (integer) data types used in C or Pascal Examples: #define GLfloat float simple change of data types glVertex2i(GLint xi,GLint iy) position specification in 2D glVertex3f(GLfloat x, GLfloat y, GLfloat z) position specification in 3D Fundamentals of Computer Graphics

Fundamentals of Computer Graphics GL specifications To store 3D vertex using an array GLfloat vertex[3] can be used as glVertex3fv(vertex) using vertices variety of objects is defined. The can be “grouped” together using glBegin(argument); .............. glEnd( ); argument – specifies geometric type that is specified by vertices Fundamentals of Computer Graphics

Fundamentals of Computer Graphics GL specifications glBegin(GL_LINES); glVertex2f(x1,y1); glVertex2f(x2,y2); glEnd( ); /* specifies a line segment */ glBegin(GL_POINTS); glEnd( ); /* specifies two points */ Others possible arguments will be specified latter Consider a drawing area 500x500, lower left hand corner (0,0) and Sierpinski gasket to be drawn Fundamentals of Computer Graphics

Sierpinski Gasket & GL specifications typedef GLfloat point2 [2]; /* defines a point as two dimensional array */ void display(void); { /* an arbitrary triangle */ point2 vertices[3]= { {0.0,0.0}, {250.500}, {500.0,0.0}}; static point2 p = {75.0, 50.0}; /* set to any initial point */ int j,k; int rand( ); {* standard random number generator */ for (k=0;k<5000;k++) { j = rand ( )%3; /* pick a random vertex from 0, 1, 2 */ p[0] = (p[0]+ vertices[ j ][0])/2; p[1] = (p[1]+ vertices[ j ][1])/2; /* display the new point */ glBegin(GL_POINTS); glVertex2fv(p); glEnd(); } glFlush(); /* points are to be displayed as soon as possible */ Fundamentals of Computer Graphics

Sierpinski Gasket & GL specifications It is necessary: to write the CORE program to specify color of drawing where on the scene the picture will appear how large image will be how to create an area of the screen for our image – window how much of our infinite pad will appear on the screen how long the image will remain on the screen Those are important issues! – will be solved latter Image generated by the program Fundamentals of Computer Graphics

Coordinate System User works in: physical coordinates device (DC)/raster coordinates - canvas style - integer values world coordinates (WC) with transformation definition to device coordinates – float values Window-Viewport transformation Advanced mapping: normalized coordinate system (NDC) introduction WCNDC & NDC WCi Image generated by the program Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL API Graphics package as a BLACK BOX Functionality can be grouped to: primitive functions – low level primitives (points, pixels etc.) primitives API – what should be displayed attribute functions are – how should be displayed –lines, points, polygons etc. Synthetic camera must describe how to create an image – viewing functions (camera properties – lens, viewing field, several cameras, stereoscopic views etc.) Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL API geometric transformation – scaling, rotation, translation, sheering etc. and use of transformation matrix. This forms the main advantage as the geometric engine can be used for substantial speed up. input functions – allow interaction with graphics primitives or objects; functions for communication with a keyboard, mouse, data tablets etc. control functions – initialization, errors handling etc. in multiprocessor multi-window & network environment Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL Interface OpenGL function names begin with gl and are stored in library referred as GL Libraries: GLU - graphics utility library – uses only GL primitives but contains code for common objects like spheres etc. – available in all OpenGL implementations GLUT – GL Utility Toolkit / provides minimum functionality to be expected from any modern windowing system others libraries are called from OpenGL libraries – but indirectly, hidden from the user. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL Interface Picture shows the organization of the libraries for the X-Windows, similarly for Microsoft Windows Strings like GL_FILL, GL_POINTS are defined in header ( .h) files – in most implementation #include <GL/glut.h> or #include <glut.h> includes glut.h, gl.h, glu.h Fundamentals of Computer Graphics

OpenGL Primitives & Attributes should be minimal & orthogonal (capability that cannot be obtained from others) more advanced to offer complex functions to a user – GLU line segments and polylines can form the edges of objects glBegin(type) glVertex*(...); ............. glEnd ( ); Fundamentals of Computer Graphics

OpenGL - Polygons Polygons have borders and interior Can be displayed rapidly and approximate curved surfaces Must be simple, convex and flat Simple and non-simple polygon Filled objects Methods of displaying polygons Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL - Polygons Convex polygon – all points on the line segment between any two points inside of the object, are inside of the polygon Flat polygon – polygon can lie on a plane in 3D in general Tests on planarity, simplicity, flatness are not performed Convexity in 2D Convexity in 3D Fundamentals of Computer Graphics

OpenGL – Polygons Types several OpenGL types were defined GL_* polygon edge itself has no width if polygon edges are to be drawn and polygon filled – it must be drawn twice with diffrent modes Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL – Text Graphical output in applications requires text output Fonts – Times, Helvetica, Computer Modern etc. Forms – stroke vs. raster text stroke – constructed as other graphics primitives – points etc. – the standard PostScript fonts defined by polynomial curves – easy to enlarge raster – fast; using bit-block-transfer (bitblt) operations Fundamentals of Computer Graphics

OpenGL – Curved Objects Curved primitives (like circles, spheres, curves and surfaces etc.) are not directly supported – must be approximated by a mesh polygons – a tessellation – at the rendering stage or within the user program GLU can be used to approximate common curved surfaces Advanced OpenGL functions can be used to work with parametric curves and surfaces Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL – Attributes Distinction between what the type of a primitive is how the primitive is displayed A red solid line and green dashed line are the the same geometric type, but displayed differently due to attributes Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL – Color Color is one of the most interesting aspects of both human perception and computer graphics Light is a part of electromagnetic spectrum 350 – 780 nm Color C = rR + gG + bB r,g,b are tristimulus values assumption of three-color theory if two colors produce the same tristimulus values then they are visually indistinguishable Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL – Color Color space can be represented as RGB cube Gray levels are represented by the Black-White line segment Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL – Color Additive vs. subtractive color system RGB vs. CMY (K) color systems Range of colors that can be produced on the given system is called color gamut Fundamentals of Computer Graphics

Fundamentals of Computer Graphics OpenGL – RGB Color Additive RGB primary color system is used 8 bits/pixel for each channel R,G,B 224 possible colors 1024 x 1024 output resolution 3MB RAM for graphics purposes RGBA system (Alpha channel) – for transparency/opacity glColor3f(1.0, 0.0, 0.0) glClearColor(1.0, 1.0, 1.0, 1.0) – screen solid and white Fundamentals of Computer Graphics

OpenGL – RGB Color – Indexed Color Color palette enables to change in the whole image Limit of the frame buffer to k bits per pixel 2k –1 colors can be displayed from a palette of 23m of colors table of 2k x 3m is to be specified for k=m=8 – a common configuration – 256 colors out of 16M. 256 entries constitute the user’s color palette Fundamentals of Computer Graphics

OpenGL – RGB Color – Indexed Color In color-index mode – present color is selected by glIndexi(element) – selects particular color out of the table System can have just one common table or multiple tables for different windows – GLUT allows that glutSetColor(int color, GLfloat red, GLfloat blue, GLfloat green) - we will work directly with RGB colors (not indexed) in the next Fundamentals of Computer Graphics

Setting of Color Attributes For the Sierpinski gasket program we need to set: clear color glClearColor(1.0, 1.0, 1.0, 1.0); rendering color for points – set to RED glColor3f(1.0, 0.0, 0.0); set point size – let us use 2 pixels size glPointSize(2.0); /* size is in pixels not in [mm] */ These attributes will be valid for all the time if not changed explicitly. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Viewing In the synthetic camera concept the object specification is completely independent from the camera specification lights properties and positions used material properties as well We used some default settings for those expecting that objects are placed “in the right position”, sufficiently far from the camera etc. We will concentrate on 2D viewing, orthographic projection, matrix mode application Fundamentals of Computer Graphics

Fundamentals of Computer Graphics 2D Viewing Objects before and after 2D clipping Clipping rectangle or viewing rectangle Objects outside are clipped out and are not displayed partially inside – only parts inside are displayed Fundamentals of Computer Graphics

2D Viewing – Orthographic View Viewing rectangle is at z = 0 with corners (-1.0,-1.0) & (1.0, 1.0) Implicit viewing volume in OpenGL is 2 x 2 x 2 cube – center in the origin Fundamentals of Computer Graphics

2D Viewing – Orthographic View void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) /* near=-1, far=1*/ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics 2D Viewing - Matrix Mode Pipeline of the graphics system utilizes matrices multiplications for geometric transformations. Two the most important matrices: model-view projection Setting matrix mode ( it is the status definition of the system): glMatrixMode(GL_PROJECTION); /* change to PROJECTION status */ glLoadIdentity( ); /* sets the matrix to the IDENTITY matrix */ gluOrtho2D(0.0, 500.0, 0.0, 500.0); /* sets window to 500 x 500 image */ glMatrixMode(GL_MODELVIEW); /* return back to the MODEL-VIEW state – highly recommended */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Control Functions Minimal interaction between the program and the particular OS must be used (X-Windows - UNIX, Windows) GLUT library provides minimal & simple interface between API and particular OS (full functionality for interaction between a program and OS is not a part of this course) Programs using GLUT interface should run under multiple window system. Some details will be in the following parts Fundamentals of Computer Graphics

Interaction with the Window System Window or screen window – rectangular area of a display Window system – refers to X-Windows or MS Windows The origin – not always lower-left corner – OpenGL style (some have orientation: top to bottom, left to right as TV sets or position information returned from input devices like mouse) glutInit(int *argcp, char *argv) – enables to pass command-line arguments as in the standard C main function glutCreateWindow(char *title) – creates a window with the name specified in the string title with default values (size, position etc.) Fundamentals of Computer Graphics

Interaction with the Window System BEFORE creating the window GLUT functions can be used to specify properties: glutDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); /* parameters or-ed and stored in the argument to glutInitDisplayMode */ glutWindowSize(480, 640); glutWindowPosition(0 , 0); specifies the windows 480 x 640 in the top-left corner of the display GLUT_RGB x GLUT_INDEX – type of color system to be used GLUT_DEPTH – a depth buffer for hidden-line removal GLUT_DOUBLE x GLUT_SINGLE – double x single buffering Implicit options: RGB color, no hidden-line removal, single buffer Fundamentals of Computer Graphics

Aspect Ratio and Viewports Aspect ratio – the ratio of the rectangle’s width and height If different in glOrtho and glutInitWindowSize – undesirable side effects Caused by the independence of object, viewing parameters and workstation window specifications Concept of a VIEWPORT Fundamentals of Computer Graphics

Aspect Ratio and Viewports void glViewport(GLint x, GLint y, GLsizei w, GLsizei h) The viewport is part of the state – when changed between rendering objects or redisplay – different window-viewport transformations used to make the scene. Fundamentals of Computer Graphics

Functions main, display & myinit Program = simple initialization code + sequence with OpenGL Problems: generic to all graphics systems interaction with underlying window system in immediate-mode graphics a primitive is rendered to the screen immediately. The application ends and the screen is cleaned before anything can be seen - solution sleep(enough_time) better solution void glutMainLoop( void ); execution causes the program to begin an event-processing loop – until program is not terminated through the “kill” key Fundamentals of Computer Graphics

Functions main, display & myinit Graphics are sent to the scene through a function called the display callback that is specified through the GLUT function as void glutDisplayFunction (void (*func)(void)) func is the function name that will be called whenever the windowing system determines that the OpenGL windows needs to be redisplayed (created, moved, resized, made visible etc.) This function is called also when the window is created For non-interactive use the code can be specified in this function Fundamentals of Computer Graphics

Functions main, display & myinit #include <GL/glut.h> /* compiler should include gl.h & glu.h */ void main (int argc, char **argv) /* the main function */ { glutInit(&argc,argv); glutDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500,500); glutInitWindowPosition(0,0); glutCreateWindow(“simple OpenGL example”); glutDisplayFunc(display); myinit( ); glutMainLoop( ); } /* should work for all non-interactive programs */ Fundamentals of Computer Graphics

Sierpinski Gasket Program void myinit (void) { /* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing */ glMatrixMode(GL_PROJECTION); gluLoadIdentity ( ); gluOrtho2D(0.0, 500.0,0.0, 500.0); glMatrixMode(GL_MODELVIEW); } Fundamentals of Computer Graphics

Functions main, display & myinit void display(void) { typedef GLfloat point2 [2]; /* defines a point as 2D array */ point2 vertices[3]= { {0.0,0.0}, {250.500}, {500.0,0.0}}; /*triangle*/ int i, j, k; int rand( ); {* standard random number generator */ point2 p = {75.0, 50.0}; /* set to any initial point */ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ for (k=0; k<5000; k++) { j = rand ( )%3; /* pick a random vertex from 0, 1, 2 */ /* compute a point halfway between vertex and old point */ p[0] = (p[0]+vertices[ j ][0])/2; p[1] = (p[1]+vertices[ j ][1])/2; /* display the new point */ glBegin(GL_points); glVertex2fv(p); glEnd( ); } glFlush(); /* points are to be displayed as soon as possible */ Fundamentals of Computer Graphics

Sierpinski Gasket Program - triangles A similar approach – a triangle is split to triangles recursively original triangle given by the array point2 v[3]; void triangle (point2 a, point2 b, point2 c) { glBegin(GL_TRIANGLES); glVertex2fv(a); glVertex2fv(b); glVertex2fv(c); glEnd( ); } ------ mid-points computations ------------- for (j=0; j<2; j++) m[0][j]=(v[0][j]+ v[1][j])/2.0; for (j=0; j<2; j++) m[1][j]=(v[0][j]+ v[2][j])/2.0; for (j=0; j<2; j++) m[2][j]=(v[1][j]+ v[2][j])/2.0; Fundamentals of Computer Graphics

Sierpinski Gasket Program - triangles Modification for recursive triangles void display(void) { glClear(GL_COLOR_BUFFER_BIT); divide_triangle(v[0], v[1], v[2], n); /* n determines No of subdivision steps */ glFlush( ); } Note: there is no convenient way ho to pass variables to OpenGL functions and callback other than through global parameters Fundamentals of Computer Graphics

Sierpinski Gasket Program - triangles void divide_triangle(point2 a, point2 b, point2 c, int k) { point2 ab, ac, bc; int j; if ( k>0) { /* compute midpoints */ for (j=0; j<2; j++) ab[j]=(a[j]+ b[j])/2.0; for (j=0; j<2; j++) ac[j]=(a[j]+ c[j])/2.0; for (j=0; j<2; j++) bc[j]=(b[j]+ c[j])/2.0; /* subdivide all but inner triangle */ divide_triangle(a, ab, ac, k-1); divide_triangle(c, ac, bc, k-1); divide_triangle(b, bc, ab, k-1); } else triangle(a,b,c); /*draw a triangle at end of recursion */ } Fundamentals of Computer Graphics

3D Sierpinski Gasket Program Now we expand 2D to 3D case data type definition: typedef GLfloat point3[3]; int n; /* global variable */ tetrahedron specifications: point3 vertices[4] = { {0.0,0.0,0.0}, {250.0,500.0,100.0}, {500.0,250.0,250.0}, {250.0,100.0,250.0}}; point3 p= {250.0,50.0,250.0}; function glPoints3fv is used to define points different colors used for points to distinguish them drawing polygons – visibility should be solved Fundamentals of Computer Graphics

3D Sierpinski Gasket Program void triangle (point3 a, point3 b, point3 c) { point3 norm; cross(a, b, c, norm); /* computes the normal vector of the triangle */ glBegin(GL_POLYGON); glNormal3fv(a); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); glEnd( ); } /* normal computation - using cross product */ Fundamentals of Computer Graphics

3D Sierpinski Gasket Program void divide_triangle(point3 a, point3 b, point3 c, int k); { point3 v1, v2, v3; int j; if ( k>0) { /* compute midpoints */ for (j=0; j<3; j++) v1[ j]=(a[ j]+ b[ j])/2.0; for (j=0; j<3; j++) v2[ j]=(a[ j]+ c[ j])/2.0; for (j=0; j<3; j++) v3[ j]=(b[ j]+ c[ j])/2.0; divide_triangle(a, v1, v2, k-1); divide_triangle(c, v2, v3, k-1); divide_triangle(b, v3, v1, k-1); } else triangle(a,b,c); /*draw a triangle at end of recursion */ } Fundamentals of Computer Graphics

3D Sierpinski Gasket Program void tetrahedron (int m); { /* apply triangle subdivision to faces of tetrahedron */ glColor3f(1.0, 0.0, 0.0); divide_triangle (v[0], v[1], v[2], m); glColor3f(0.0, 1.0, 0.0); divide_triangle (v[3], v[2], v[1], m); glColor3f(0.0, 0.0, 1.0); divide_triangle (v[0], v[3], v[1], m); glColor3f(0.0, 0.0, 0.0); divide_triangle (v[0], v[2], v[3], m); } Fundamentals of Computer Graphics

3D Sierpinski Gasket Program void display ( ); { glClear(GL_COLOR_BUFFER_BIT | GLDEPTH_BUFFER_BIT); glLoadIdentity (); tetrahedron(n); glFlush ( ); } void myReshape(int w, int h) { glViewport (0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity (); if (w<=h) glOrtho(-2.0,2.0, (GLfloat)h/(GLfloat)w, (GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho(-2.0,2.0, (GLfloat)w/(GLfloat)h, (GLfloat)w/(GLfloat)h, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glutPostRedisplay ( ); Fundamentals of Computer Graphics

3D Sierpinski Gasket Program void main ( int argc, char **argv); { n = atoi(argv[1]); /* parameter on the command line */ /* n is a global variable */ glutInit ( int argc, char **argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500,500); glutCreateWindow (“3D Gasket”); glutReshapeFunc(myReshape); glutDisplayFunc(display); glEnable(GL_DEPTH_TEST); glClearColor (1.0, 1.0, 1.0, 1.0); glutMainLoop( ); } Fundamentals of Computer Graphics

3D Sierpinski Gasket Program Triangles are drawn in order of generation (given by the algorithm) not according to their geometric position Hidden surface removal is enabled by setting glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); and enable that by (using z-buffer algorithm) glEnable(GL_DEPTH_TEST); Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Conclusion The structure of the OpenGL API and interaction with OS have been explained you learnt how to write non-interactive complex OpenGL programs using non-trivial features, especially how to: redraw the windows if any change use window-viewport transformation handle orthographic projection use OpenGL in recursive environment draw primitives that are filled and solve the visibility of objects Some features will be explored in the following parts in detail Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Conclusion Read the book Study examples in the Appendices in detail Pass exercises at the end of Chapter 2 and try to answer all questions Learn how to compute a normal of a triangle using the cross-product Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 3 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Fundamentals of Computer Graphics Input & Interaction Till now – no interaction Introduction of devices for interaction how devices “appear” in your program client-server network & client-server graphics development of a painting program First attempt: - 37 years! Project Sketchpad – Sutherland Different approach will be taken – we will use API, but OpenGL does not support it directly – due to portability of the renderer, interaction with OS etc. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Input Devices Two possible ways to see input devices: as a physical device – keyboard, mouse, trackball, etc. as a logical device – from a programmer perspective – with specified functionality, in graphics more complex the separation of physical and logical levels enable us to make programs more flexible, independent from the actual physical device Fundamentals of Computer Graphics

Physical Input Devices pointing device – allows to indicate position & send signals/interrupts to the computer – relative/absolute positioning keyboard device – almost physical keyboard – returns character codes to a program Fundamentals of Computer Graphics

Physical Input Devices Absolute positioning: data tablets light pen joystick – variable-sensitivity device & haptic device spaceball – up-down, left-right, front-back & 3 independent twists Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Logical Input Devices Some APIs (PHIGS, GKS, Direct xx) supports 6 classes of logical input devices – OpenGL does not take this approach String – logical device providing ASCII strings – keyboard Locator – provides a position in world coordinates – usually implemented via pointing device – mouse, trackball. OpenGL provides similar but conversion from screen coordinates to world coordinates must be made by a user Pick – returns identifier of an object – in OpenGL process called selection can be used to accomplish picking Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Logical Input Devices Choice – allows the user to select on of a discrete number of options – in OpenGL various widgets provided by the window system can be used; widget is a graphical interactive device provided by window system or a toolkit (menu with n selections etc.) Dial – provides analog input to the user program – slidebars etc. Stroke – device returns an array of locations – different implementations – usually: mouse button down, transfer data to an array with different positions, release button – ends the transfer Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Input Devices & Modes Two entities: a measure process – is what the device returns to the user program (string from a keyboard) a device trigger – is a physical input on the device which user can signal the computer (return – end of the process) Modes: request – value is returned on a request sample mode – actual value is given (no buffering) request_locator ( device_id, &measure); /* usual form */ sample_locator (device_id, &measure); Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Input Devices & Modes Request versus Sample modes Generally sample & request modes are not sufficient for Human-Computer-Interface (HCI) Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Input Devices & Modes Event mode working in environment with multiple input devices – each has its own trigger and running measure process each time when the device is triggered – the event is generated, the measure with the process identifier is placed in an event queue. The program can examine the front event in the queue and decides what to do – this is used for GKS, PHIGS etc. Fundamentals of Computer Graphics

Input Devices & Modes - Callback another approach – association of a callback function with a specific type of event – mostly used in windowing system and client/server environments int main (...) { ....glutMouseFunc(mouse); glutDisplayFunc(...);..} ............ void mouse_callback_func(int button, int state, int x, int y) { if button ==GLUT_LEFT_BUTTON && state==GLUT_DOWN) drawSquare(x,y); /*users function*/ if button ==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) exit (); } /* window reshape principle */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Clients-Servers Our programs worked on single & isolated system so far, but it should also work in distributed computing and networks Distributed graphics, projection walls etc. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Display Lists Instructions are stored in a display memory – display file – display list Modes: - immediate – each element is processed and displayed - retained – objects are defined & stored in a display list on a server & redisplayed on the client request Early graphics systems - 1960 Display processor architecture Fundamentals of Computer Graphics

Definition & Execution of Display Lists #define BOX 1 glNewList(Box, GL_COMPILE); /* sends to the server*/ glBegin(GL_POLYGON); glColor3f(1.0, 1.0, 1.0); glVertex2f(-1.0, -1.0); glVertex2f( 1.0, -1.0); glVertex2f( 1.0, 1.0); glVertex2f(-1.0, 1.0); glEnd( ); glEndList ( ); /* GL_COMPILE_AND_EXECUTE – immediate display */ Drawing – execution glCallList(BOX); Fundamentals of Computer Graphics

Display Lists & Transformations If model-view or projection matrices changed between execution of the display list – the drawn model will appear at different positions glMatrixModel(GL_PROJECTION); for (i=1; i<5; i++); { glLoadIdentity( ); gluOrtho2D(-2.0*i, 2.0*i, -2.0*i, 2.0*i ); glCallList(BOX); } Fundamentals of Computer Graphics

Display Lists & Push/Pop ! Color is changed whenever the list is executed Execution of the display list changes the state and attributes in general – may cause unexpected effects it is possible and recommended to store them in the stack at the beginning of the display list specification glPushAttrib(GL_ALL_ATTRIB_BITS); glPushMatrix( ); at the end of the display list specification glPopAttrib( ); glPopMatrix ( ); Fundamentals of Computer Graphics

Characters, Fonts & Strings Study chapter 3.4.2 - 3.4.3 on your own Fundamentals of Computer Graphics

Programming Event Driven Input Pointing device: passive move event – mouse moved without pressing a button move event - mouse moved with a button pressed, or button released (some systems counts the pushing & releasing of a button as only a single event) glutMouseFunc(mouse_callback_func) /* registration */ void mouse_callback_func(int button, int state, int x, int y); { if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) exit ( ); } /* the left button pressed will be omitted no action will be taken as no corresponding action is specified */ Fundamentals of Computer Graphics

Programming Event Driven Input int main (int argc, char **argv); { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow(“square”); /* create a window with name square */ myinit( ); glutReshapeFunc(myReshape); /* generated if window size changed*/ glutMouseFunc(mouse); /* activated if status or position of a mouse changed */ glutDisplayFunc(display); /* GLUT call back requires it strictly – in case of no action */ /* void display ( ) { } must be specified – empty function */ glutMainLoop( ); } Fundamentals of Computer Graphics

Programming Event Driven Input Keyboards events: glutKeyBoardFunc ( keyboard); /* registration */ void keyboard (unsigned char key, int x, int y); { if (key == ‘q’ || key == ‘Q’) exit ( ); /* exits the program */ } Special functions: glutPostDisplay ( ) /* if window iconified redrawing is postoned */ Multiple Window management: id = glutCreateWindow(“second window”); /* int id*/ glutSetWindow (id) ; /* sets the window into which object will be rendered */ Fundamentals of Computer Graphics

Menus & Picking Study chapter 3.6 – 3.7 on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Menu Pop-up menus glutCreateMenu(demo_menu); glutAddmenuEntry(“quit”,1); glutAddmenuEntry(“increase square size”,2); glutAddmenuEntry(“decrease square size”,3); glutAttachmenu(GLUT_RIGHT_BUTTON); void demo_menu(int id); { if (id ==1) exit ( ); else if (id ==2) size = size * 2; else if (id ==3) size = size / 2; glutPostRedisplay( ); /* glutDisplayFunc is called-redisplay without menu */ } Fundamentals of Computer Graphics

Animation Study chapter 3.9 on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Animation For animation – double buffering glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); Buffers: front – content visible on the display back – where the rendering is made to the display function must be added glutSwapBuffers ( ); Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Conclusion Study on your own: Chapter 3.4.2 – 3, text, strings & character drawing Chapter 3.8 – simple Paint Program, Chapter 3.9 – Animating Interactive Program Chapter 3.10 – Design of Interactive Programs Make some experiments with display list construction and discuss efficiency of its use for complex objects Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 4 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Geometric Transformations Concentration on 3D graphics Affine & Euclidean vector spaces Homogeneous coordinates Formalities of vector spaces & matrix algebra – see App.B&C Goals: method for dealing with geometric objects – independent of a coordinate system coordinate free approach & homogeneous coordinates Fundamentals of Computer Graphics

Scalars, Points & Vectors Most geometric objects can be defined by a limited set of primitives, like scalars, points, vectors Different perspectives: mathematical programming geometric BUT ALL are important for understanding vector = directed (oriented) line segment vectors have no fixed position! Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Geometric View vectors have no fixed position had-to-tail rule – useful to express functionality C = A + B points & vectors – distinct geometric types! a given vector can be defined as from a fixed reference point (origin) to the given point p (dangerous vector repr.) Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Vector & Affine Spaces Vector (linear) space vector & scalars – addition &multiplication operations used to form a scalar field (scalars – real, complex numbers, rational functions – typical Ax=0, n-tuples etc.) Affine space – extension of vector space – the point is an object vector-point addition, point-point subtraction, geometric operations with points etc. Euclidean space – enables to measure distance, size Details see in the App.B & C Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Representation Mathematicians – scalars, points, vectors etc. – they are distinguished by symbols and fonts (bold, capital, italic etc.) Computer scientists – Abstract Data Types – ADT – set of operations on data; operations independent from the actual physical realization/implementation Data abstraction – fundamental to Computer Science but causes difficulties in a code understanding What is the meaning of a sequence in C++ : q = p + a * v; Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Representation What is the meaning of a sequence in C++ : q = p + a * v; can be determined if the definition is known: vector u, v; point p, q; scalar a, b; and their actual meaning must be vector type as a vector ! OpenGL is not object oriented so far Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Geometric ADT & Lines Symbols: , ,  - scalars P, Q, R – points u, v, w – vectors Typical geometrical operations: |  v| = |  | | v | v = P – Q => P = v + Q ( P – Q )+ ( Q – R ) = P – R P() = P0 +  d (a line in an affine space – param.form) Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Affine Sums new point P can be defined as P = Q +  v Point R v = R – Q and P = Q + (R –Q)= R + (1- )Q P = 1 R+  2 Q where 1 +  2 = 1 Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Convexity A convex object is one for which any point lying on the line segment connecting any two points in the object is also in the object P = 1 R+  2 Q & 1+ 2 = 1 More general form P = 1P1+2P2 +... +nPn where 1+2 +... +n= 1 & i  0 , i = 1, 2, ....,n Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Planes Let P, Q, R are points defining a plane in an affine space S() =  P + (1- )Q , 0   1 T() =  S + (1 - ) R , 0   1 using a substitution T(,) =  [ P + (1- )Q ] + ( 1 - ) R , 0   1 & 0   1 T(,) = P +  (1 -  )( Q – P ) + (1 - ) (R – P) Plane given by a point P0 and vectors u, v T(,) = P0 +  u +  v & 0  ,   1 Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Planes A triangle is defined as (a plane has no limits for ,  ) T(,) = P0 +  u +  v & 0 ,   1 If a point P lies in the plane then P - P0 =  u +  v Let w = u x v (cross product) The vector w is orthogonal to the plane and the equation wT (P – P0) = 0 (criterion for a test: point in the plane) vector w is called a normal vector and symbol n is often used Fundamentals of Computer Graphics

Three Dimensional Primitives Full range of graphics primitives cannot be supported by graphics systems – some are approximated most graphics systems optimized for procession points and polygons, polygons in E3 are not planar – tessellation is required or made by the system itself Constructive Solid Geometry (CSG) – objects build using set operations like union, intersection, difference etc. Fundamentals of Computer Graphics

Coordinate Systems and Frames A vector w is defined as w = 1v1 + 2v2 + 3v3 1, 2, 3 are components of w with respect to the basis vectors v1 ,v2 ,v3 (! toto jsou bázové vektory) vectors v forms coordinate system in vector space points representation needs to “fix” the origin – reference point and basis vectors are required - frame Fundamentals of Computer Graphics

Coordinate Systems and Frames Within a given frame every vector can be written uniquely as w = 1v1 + 2v2 + 3v3 just as in a vector space. v1 = [ 1, 0 , 0]T v2 = [ 0, 1 , 0]T v3 = [ 0, 0 , 1]T Every point can be written uniquely as w = P0 + 1v1 + 2v2 + 3v3 Fundamentals of Computer Graphics

Changes of Coordinate Systems Suppose that {v1 , v2 , v3 } & {u1 , u2 , u3 } are two basis vectors. Therefore 9 scalar components exist { ij} such as u = M v a vector w with respect to v = [v1 , v2 , v3 ]T a = [1 , 2 , 3 ]T w = aT v a vector w with respect to v = [u1 , u2 , u3 ]T b = [ß1 , ß2 , ß3 ]T w = bT u w = bT u = bT M v = aT v bT M = aT  a = MT b Fundamentals of Computer Graphics

Changes of Coordinate Systems The origin unchanged - rotation, scaling representation u = M v Simple translation or a change of a frame cannot be represented in this way Study change of representation – chapter 4.3.2 on your own Fundamentals of Computer Graphics

Homogeneous coordinates A point P located at (x,y,z) is represented using a 3D frame by P0, v1, v2, v3 as p = [ x , y , z ]T therefore P = P0 + x v1+ y v2 + z v3 and point P can be determined as P = 1v1 + 2v2 + 3v3 + P0 P = [1 , 2 , 3 , 1] [v1 , v2 , v3 , P0 ]T Every point can be written uniquely as w = P0 + 1v1 + 2v2 + 3v3 Fundamentals of Computer Graphics

Homogeneous Coordinates Suppose that {v1 , v2 , v3 , P0 }&{u1 , u2 , u3 , Q0 } are two basis vectors. Therefore 16 scalar components exist { ij} such as u = M v a vector w with respect to v = [v1 , v2 , v3 , P0 ]T  = [1 , 2 , 3 , 1]T w = aT v a vector w with respect to v = [u1 , u2 , u3 , Q0]T  = [ß1 , ß2 , ß3 , 1]T w = bT u w = bT u = bT M v = aT v bT M = aT  a = MT b Study Change in Frames and Frames&ADT Chapters 4.3.4.-5. on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Frames in OpenGL Two frames – camera & world frames Consider the camera frame fixed model-view matrix converts the homogeneous coordinate representations of points and vectors to their representations in the camera frame the model-view matrix is part of the state of the system – there is always a camera frame and a present-world frame (how to define new frames – next chapters) three basis vectors correspond to up, y, z -directions, Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Frames in OpenGL Default settings: Camera & World frames the same Objects must moved away from camera or Camera must be moved away from objects If camera frame fixed & model-view positioning world frame to camera frame then model-view matrix A is defined as ( d- distance): Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Frames in OpenGL Moves points (x,y,z) in the world frame to (x,y,z,-d) in the camera frame This concept is better than the repositioning objects position by changing their vertices Setting the Model-View matrix by sending an array of 16 elements to glLoadMatrix User working in the world coordinates positions the objects as before Fundamentals of Computer Graphics

Modeling a Colored Cube Problem: Draw a rotating cube. Tasks to be done: modeling converting to the camera frame clipping projection hidden surfaces removal rasterization display of the object Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Modeling of a Cube Cube as 6 polygons – facets typedef GLfloat point3[3]; point3 vertices[8] = { {x,y,z},...{x,y,z}}; /* definition of the cube vertices */ ...... glBegin(GL_POLYGON); glVertex3fv(vertices[0]); glVertex3fv(vertices[3]); glVertex3fv(vertices[2]); glVertex3fv(vertices[1]); glEnd ( ); /* facet drawn */ outward facing - normal has right hand rule orientation Fundamentals of Computer Graphics

Data Structures for Object Represenation Advantages of the data structure: separation of topology and geometry geometry stored in the vertex list hg Fundamentals of Computer Graphics

Data Structures for Object Represenation typedef GLfloat point3[3]; point3 vertices [8] = {{x,y,z}, ... ,{x,y,z}}; /* vertices x,y,z coordinates def. */ GLfloat Colors[8][3] = {{r,g,b}, .... , {r,g,b}}; /* color defs. */ void quad(int a, int b, int c, int d) { glBegin(GL_QUADS); glColor3fv(colors[a]);glVertex3fv(vertices[a]); glColor3fv(colors[b]);glVertex3fv(vertices[b]); glColor3fv(colors[c]);glVertex3fv(vertices[c]); glColor3fv(colors[d]);glVertex3fv(vertices[d]); glEnd ( ); } Fundamentals of Computer Graphics

Data Structures for Object Representation color is specified - graphics system must decide how to fill bilinear interpolation C01() = (1- )C0+ C1 C23() = (1- )C2+ C3 for the given value  C01() = C4 , C23() = C5 C45() = (1- )C0+  C5 scan-line interpolation OpenGL provides this for others values that are assigned on the vertex-to-vertex basis void colorcube( );/*draws a cube*/ { quad(0,3,2,1); quad(2,3,7,6); quad(0,4,7,3); quad(1,2,6,5); quad(4,5,6,7); quad(0,1,5,4); } Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Vertex Arrays glBegin, glEnd, glColor, glVertex – high overhead Vertex arrays – a glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); Arrays are the same as before GLfloat vertices [ ] = {.........}; GLfloat colors [ ] = {.........}; /* specification where the arrays are */ glVertexPointer(3,GL_FLOAT,0,vertices); glColorPointer(3,GL_FLOAT,0,colors); /* 3D vector, type, continuous (packed), pointer to the array */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Vertex Arrays The facets must be specified GLubyte cubeIndices [24] = {0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4}; /* facets are formed by 4 vertices */ Options how to draw: glDrawElements(type,n,format,pointer); -------------- SOLUTION ------------ for ( i=0; i<6; i++) /* n number of elements */ glDrawElements(GL_POLYGON,4, GL_UNSIGNED_BYTE, &cubeIndicis[4*i]); or with a single call glDrawElements(GL_QUADS,24, GL_UNSIGNED_BYTE, &cubeIndices); Fundamentals of Computer Graphics

Affine Transformations Chapter 4.5 – study on your own Fundamentals of Computer Graphics

Rotation, translation and Scaling P’ = P + d Rotation x =  cos  y =  sin  x’ =  cos (+) y’ =  sin (+) Fundamentals of Computer Graphics

Rotation about a fixed point For rotation – implicit point origin 2D – simple 3D –complicated Transformation rigid-body non-rigid-body reflections Fundamentals of Computer Graphics

Transformation in Homogeneous Coordinates Translation Scaling Inversion operations: T-1 = T ( -x , -y , -z ) S-1 = S ( 1/x , 1/ y , 1/ z ) Fundamentals of Computer Graphics

Transformation in Homogeneous Coordinates Rotation in x-y plane Rotation in y-z plane Rotation in z-x plane R-1 () = R ( - ) = RT () Fundamentals of Computer Graphics

Transformations Concatenation well known strategy q = C B A p interpretation q = C (B (A p) ) Transformation q = M p Fundamentals of Computer Graphics

Rotation About a Fixed Point Rotation about the fixed point M = T(pf ) Rz(  ) T(- pf ) Fundamentals of Computer Graphics

Rotation About a Fixed Point Rotation about the fixed point M = T(pf ) Rz(  ) T(- pf ) Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Rotations in E3  Rxy Ryz  Cube can be rotated about all x, y, z axis In our case the transformation matrix is defined M = Rzx Ryz Rxy = Ry Rx Rz Rzx Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Rotations in E3 Transformation is defined by the instance transformation M M = T R S (order is substantial!) Each occurrence of an object in the scene is an instance of the object’s prototype To obtain proper size, location, orientation – instance transformation to the prototype is to be applied Fundamentals of Computer Graphics

Rotations About an Arbitrary Axis Given: points p1 , p2 and rotation angle  objects to be rotated Define vectors u = p1 - p2 and v = u / |u| - normalized v = [ x , y , z ]T x2 + y2 + z2 = 1 – directional cosines cos( x ) = x , cos( y ) = y , cos( z ) = z cos2( x ) + cos2( y ) + cos2 ( z ) = 1  only two directions angles are independent !! Fundamentals of Computer Graphics

Rotations About an Arbitrary Axis Transformation R = Rx(-x) Ry(-y) Rz() Ry(y) Rx(x) Fundamentals of Computer Graphics

Rotations About an Arbitrary Axis Object is moved to the origin Rotation about x axis Fundamentals of Computer Graphics

Rotations About an Arbitrary Axis Object is moved to the origin Rotation about y axis note the “-” position Complete transformation M = T(p0) Rx(-x) Ry(-y) Rz() Ry(y) Rx(x) T(- p0) Fundamentals of Computer Graphics

OpenGL Transformation Matrices Three matrices as a part of the state in OpenGL Only Model-View will be used CMT – current transformation matrix – can be changed by OpenGL functions – 4 x 4 size Supported operations: translation, scaling & rotation – last two with the fixed point in the origin C  I initialization C  CT translation C  CS scaling C  CR rotation Fundamentals of Computer Graphics

OpenGL Transformation Matrices Most systems allow to set directly or load or post-multiply the CMT with an arbitrary matrix M , scaling & rotation – last two with the fixed point in the origin C  M loading C  CM post-multiplication Fundamentals of Computer Graphics

OpenGL Transformation Matrices OpenGL model-view (GL_MODELVIEW) and projection (GL_PROJECTION) matrices (actually their product) are applied to ALL primitives – we should consider them as one CMT matrix – can be manipulated individually using glMatrixMode function glLoadMatrixf(pointer_to_matrix); /* vector of 16 position – column first order */ Fundamentals of Computer Graphics

OpenGL Transformation Matrices glLoadIdentity ( ); /* loads identity matrix */ glRotatef(angle, vx, vy, vz); /* f – float used */ /* specifies general rotation angle in degrees, v – specifies the vector – fixed point P0 is the origin */ glTranslatef(dx, dy, dz); /* translation */ glScalef ( sx, sy, sz); /* scaling */ Fundamentals of Computer Graphics

Rotation about a Fixed Point glMatrixMode(GL_MODELVIEW); glLoadIdentity ( ); glTranslatef(4.0, 5.0, 6.0); glRotatef(45.0, 1.0, 2.0, 3.0); glTranslatef(-4.0, -5.0, -6.0); /* rotates objects about the vector (1.0, 2.0, 3.0) with the angle 45° */ NOTE: we need not to form the rotation matrices as shown recently – try to make it on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Transformation Order The sequence specified recently: C  I, initialization C  CT (4.0, 5.0, 6.0), translation C  CR (45.0, 1.0, 2.0, 3.0), rotation C  CT (-4.0, -5.0, -6.0), translation back in each step the CTM matrix is post-multiplied forming new CTM matrix C = T (4.0, 5.0, 6.0) R(45.0, 1.0, 2.0, 3.0) T (-4.0, -5.0, -6.0) Each vertex specified after the model-view matrix has been specified will be multiplied p’ = C p Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Spinning of the Cube Three callback functions: glutDisplayFunc(display); glutIdleFunc(spincube); glutMouseFunc(mouse); void display(void); /* visibility made by HW – GL_DEPTH.... */ {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity ( ); glRotatef(theta[0];1.0, 0.0, 0.0); glRotatef(theta[1];0.0, 1.0, 0.0); glRotatef(theta[2];0.0, 0.0, 1.0); colorcube ( ); glutSwapBuffers ( ); /* swaps the buffers – drawing to the display */ } /* theta vector – global variable */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Spinning of the Cube void spincube ( ); { theta[axis] +=2.0; if (theta[axis] >360.0) theta[axis] -=360.0; glutPostRedisplay ( ); } void mouse (int btn, int state, int x, int y); { if (btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis=0; if (btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis=1; if (btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis=2; } void mykey(char key, int mouse x, int mouse y); { if (key==‘q’ | | key==‘Q’ ) exit ( ); } /* simple termination */ Fundamentals of Computer Graphics

Loading, Pushing & Popping glLoadMatrixf(myarray); /* 4 x 4 matrix of floats -column first order from a vector */ glMultMatrixf(myarray); /* multiplies the current matrix by user specified matrix */ Sequence example GLfloat myarray [16]; for ( i=0; i<3; i++) for ( j=0; j<3; j++) myarray[4*j+i] = m[i][j]; Fundamentals of Computer Graphics

Loading, Pushing & Popping Sometimes it is reasonable to return the transformations back after they have been applied to some objects. Instead of re-computation the stack mechanism can be utilized glPushMatrix ( ); /* local transformation specifications */ glTranslatef ( .....); ................ /* DRAW OBJECTS HERE */ ................ /* recover recent state */ glPopMatrix ( ); Fundamentals of Computer Graphics

Interfaces to 3D Applications Study the Chapters 4.10 - 12 on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Conclusion Chapter 4 You have learnt in this chapter: how transformations are defined how can you use them how to construct quite complicated transformations Mention, please, that you are now capable to write quite complicated program with graphics output and input Next time we will learn how to represent different viewing principles, projections etc. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 5 Viewing prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Classical & Computer Viewing Center of projection (COP) – center of the camera lenses origin of the camera frame Direction of Projection (DOP) – viewing from infinity Projections planar geometric projections non-planar projections Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Classical Views principal faces; architectural building-mostly orthogonal faces front, back, top, bottom, right, left faces Fundamentals of Computer Graphics

Orthographic Projections Multi-view orthographic projection (rovnoběžné promítání) 3 views & orthogonal preserves angles Fundamentals of Computer Graphics

Axonometric Projections shortening of distances Views: trimetric top side Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Oblique Projections Views: construction top side Oblique (kosoúhlá) projection – most general parallel views Fundamentals of Computer Graphics

Perspective Projections Vanishing point(s) (úběžník) one-point two-points three-points perspectives Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Camera positioning Vanishing point(s) (úběžník) one-point two-points three-points perspectives ! right-handed x left-handed coordinates glOrtho( ...., near,far) measured from the camera glTranslate (0.0, 0.0, -d); /* moves the camera in positive dir.of z Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Camera positioning We want to see objects from distance d and from x axis direction: glMatrixMode(GL_MODEL_VIEW); glLoadIdentity ( ); glTranslate (0.0, 0.0, -d); glRotate(-90.0, 0.0, 1.0, 0.0); Order: rotate move away from the origin Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Camera positioning Isometric view of a cube [-1,-1,-1] x [1,1,1] Order: rotate about y-axis rotate about x-axis move away from the origin corner [-1,1,1] to be transformed to [0,1,2]  35.26° glMatrixMode(GL_MODELVIEW); glLoadIdentity (); glTranslatef(0.0, 0.0, -d); /* 3-rd */ glRotatef(35.26, 1.0, 0.0, 0.0); /* 2-nd */ glRotatef(45.0, 0.0, 1.0, 0.0); /* 1-st */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Two viewing API’s Unsatisfactory camera specification Starting point – world frame – description of camera position and orientation; precise type of image – perspective or parallel defined separately – Projection Matrix specification VRP – View Reference Point – origin is implicit set_view_reference_point(x,y,z); VPN – View Plane Normal – orientation of projection plane – camera back set_view_plane_normal(nx, ny,nz); VUP – View-UP vector - specifies what direction is up from the camera’s perspective set_view_up(vup_x, vup_y, vup_z); Fundamentals of Computer Graphics

view-orientation matrix Two viewing API’s v vector is obtained by VUP vector projection on the view plane; v is orthogonal to normal n This orthogonal system is referred as viewing-coordinate system or u-v-n system with VRP added - desired camera frame The matrix that DOES the change of frames is the view-orientation matrix p = [x,y,z,1]T – view-reference point n = [nx, ny, nz,0]T – view-plane normal vup = [vupx, vupy, vupz,0]T – view-up vector Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Two viewing API’s New frame construction view-reference point as its origin view-plane normal as one coordinate direction two other directions u & v Default x, y, z axes become u, v, n now Model-view matrix V = T R ; v & n must be orthogonal nTv = 0 v is a projection of vup into the plane formed by n & vup – it must be a linear combination of these two vectors v =  n + vup If the length of v is ignored,  = 1 can be set and  = - pTn / nTn v = p – ( pTn / nTn ) n u = v x n Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Two viewing API’s Vectors u, v, n can be normalized independently to u’, v’, n’ Matrix M is a rotation matrix that orients u’, v’, n’ system with respect to the original system We want inversion matrix R R = M-1 = MT Finally the model-view matrix V = T R For our isometric example p = (3/3) [-d,d,d,1]T n = [-1,1,1,0]T vup = [0,1,0,0]T Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Look-At Function VRP, VPN & VUP specifies camera position Straightforward method: e – camera position (called eye-point) a – position to look at (called at point) vpn = e – a gluLookAt(eye_x, eye_y, eye_z, at_x, at_y, at_z ) ; Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Others Viewing APIs For some applications others viewing transformations are needed flight simulation applications – roll, pitch, yaw angles are specified relative to the center of mass distance is counted from the center of mass of the vehicle astronomy etc. requires polar or spherical coordinates elevation, azimuth camera can rotate – twist angle Fundamentals of Computer Graphics

Perspective Projections Camera is pointing in the negative z-direction , d < 0 x / z = xp / d xp = x / (z /d) yp = y / (z /d) division by z describes non-uniform foreshortening Perspective transformation preserves lines, but it is not affine it is irreversible Fundamentals of Computer Graphics

Perspective Projections for w  0 - point represented as p = [ wx , wy , wz , w]T Usually w = 1 p = [ x , y , z , 1]T q = M p q = [ x , y , z , z/d]T q’ = [ x / (z/d) , y / (z/d) , d , 1]T = [ xp , yp , zp , 1]T Perspective transformation can be represented by 4 x 4 matrix - perspective division must be part of the pipeline Fundamentals of Computer Graphics

Orthogonal Projections Orthogonal or orthographic projection is a special case After projection: xp = x yp = y zp = 0 Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Projections in OpenGL We haven’t taken properties of the camera so far angle of view view volume frustrum – truncated pyramid objects not within the view volume are said to be clipped out Fundamentals of Computer Graphics

Perspective viewing in OpenGL Typical sequence: glMatrixMode (GL_PROJECTION); glLoadIdentity ( ); glFrustrum(xmin, xmax, ymin, ymax, near, far); near & far distances must be positive and measured from the COP be careful about the signs ! Fundamentals of Computer Graphics

Perspective viewing in OpenGL Typical sequence: glMatrixMode (GL_PROJECTION); glLoadIdentity ( ); gluPerspective (fovy, aspect, near, far); fovy – view angle in y-axis aspect – aspect ratio width/height Fundamentals of Computer Graphics

Parallel viewing in OpenGL Typical sequence: glMatrixMode (GL_PROJECTION); glLoadIdentity ( ); glOrtho (xmin, xmax, ymin, ymax, near, far); /* restriction far > near */ Fundamentals of Computer Graphics

Hidden Surface Removal Algorithms object space image space z-buffer – requires depth or z-buffer Typical sequence: glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glEnable(GL_DEPTH_TEST); Clear the buffer before new rendering glClear(GL_DEPTH_BUFFER_BIT); /* study example in chapter 5.6 */ Fundamentals of Computer Graphics

Projection Normalization Projection Normalization – converts all projections into orthogonal projections by first distorting objects – result after projection is the same perspective view orthographic projection of distorted objects Fundamentals of Computer Graphics

Orthogonal-Projection Matrices glOrtho defines mapping to the standard volume - canonical volume Operations: translate to the center scaling Projection matrix P = S T Fundamentals of Computer Graphics

Orthogonal-Projection Matrices P = S T Study Oblique projection on your own – chapter 5.7.3. Fundamentals of Computer Graphics

Perspective-Projection Matrices Perspective normalization – canonical pyramid x = z y = z (speeds-up pyramidal clipping) near plane z = zmin far plane z = zmax values are negative and therefore zmax > zmin Fundamentals of Computer Graphics

Perspective-Projection Matrices Consider a matrix N p = [ x, y, z, 1]T q = [ x’, y’, z’, w’]T q = N p x’ = x y’ = y z’ = z + w’ = -z after dividing x’’ = - x /d y’’ = -y /d z’’ = -( +  / z) w’’= 1 if orthographic projection is applied along to z axis p’ = MorthN p = [ x, y, 0, -z ]T Fundamentals of Computer Graphics

Perspective-Projection Matrices Pyramidal sides x = z y = z are transformed to x = 1 y = 1 front plane z = zmin to back plane z = zmax to If then z = zmin is mapped to z’’ = -1 z = zmax is mapped to z’’ = +1 Fundamentals of Computer Graphics

Perspective-Projection Matrices Matrix N transforms the viewing frustrum to a right parallelpiped and an orthogonal projection in the transformed volume yields to the same image as does perspective projection. N is called perspective normalization matrix Study a non-symmetric frustrum transformations, shadows Chap.5.9. on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Conclusion - Chapter5 You have learnt mathematical background and API for projections – parallel, oblique and perspective Try to find a solution for: define transformations needed for flight simulator as a composition of existing ones application of projections for a display walls (4 x 3 screens, using non-symmetric viewing frustrum) imagine a cube in perspective projection. Observer is in front of the object inside of the object what he will see, what you will get if you use geometric transformations and projection matrices and what OpenGL gives you? Discuss results! Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 6 Shading prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Shading – Light and Matter Our perception depends on: light directly – intensity, spectrum (color), position material of objects that “reflects” or “transmit” light, roughness, color of the surface Speed of computation – significant factor Fundamentals of Computer Graphics

Shading – Light and Matter Generally we do not need to compute all, but just those rays that contribute to the final image Methods: Global – ray tracing, radiosity – very slow Local – constant, Gouraud, Phong etc. – relatively fast Fundamentals of Computer Graphics

Shading – Light and Matter Interaction between light and materials can be classified as specular surfaces – ideal mirror diffuse surfaces – reflected light is ideally reflected to all directions uniformly translucent surfaces – allow some lights to penetrate the surface – refraction – glass, watter optical properties – Snell’s law Fundamentals of Computer Graphics

Shading – Light sources Light source – an object with a surface Each point (x,y,z) on the surface can emit light with characterization: direction of emission (,) intensity of energy emitted at each wavelength  illumination function I(x,y,z,,,) Basic light sources (sufficient for rendering the most simple scenes): ambient lighting point sources spotlights distance light Fundamentals of Computer Graphics

Shading – Light sources Light - an object with a surface Each point (x,y,z) on the surface can emit light with characterization: direction of emission (,) intensity of energy emitted at each wavelength  illumination function I(x,y,z,,,) Usually I = [ Ir , Ig , Ib ]T is handled as a scalar value Basic light sources - sufficient for rendering the most simple scenes: ambient lighting point sources spotlights distance light Fundamentals of Computer Graphics

Shading – Light sources Ambient light Ia- uniform light in the space (room etc.) Ideal point source – emits equally in all directions I(p0) Light received at a point p full shadow – umbra partial shadow – penumbra for non-point sources (d is distance) Fundamentals of Computer Graphics

Shading – Light sources Spotlights – very narrow angles of emission, if  = 180° -> point source distribution of light within the cone – usually cose() ; e determines how rapidly intensity drops off cos() = sT l s – vector that points from ps to a point s on a surface l – vector of the light direction Fundamentals of Computer Graphics

Shading – Distant light sources we replace location of light sources with their directions p0 = [ x , y , z , 0 ]T ( 0 is correct !) Phong Reflection Model I = Ia + Id + Is disadvantages linear model superposition Fundamentals of Computer Graphics

Ambient, Diffuse, Specular Reflections Ambient reflection 0  ka  1 Ia = ka La global ambient term or light Diffuse reflection characterized by rough surfaces perfectly diffuse surfaces – Lambertian surfaces Fundamentals of Computer Graphics

Ambient, Diffuse, Specular Reflections Lambert’s law: diffuse reflection 0  kd  1 if the influence of the distance is considered Fundamentals of Computer Graphics

Ambient, Diffuse, Specular Reflections Specular Reflection: 0  ks  1 as  ideal specular reflection   <100 , 500> metallic surfaces Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Polygonal Shading How to display surfaces with shading? Flat (constant) shading glShadeModel(GL_FLAT); Mach bands Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Polygonal Shading Interpolative and Gouraud shading Gouraud shading glShadeModel(GL_SMOOTH); normal in a vertex average normal vector intensity computation for a vertex intensity & color interpolation for a scan-line Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Polygonal Shading Phong shading normal in a vertex interpolation of a normal normal interpolation along the scan-line intensity computation Phong shading is almost always done off-line Fundamentals of Computer Graphics

Light Sources in OpenGL Study 6.6 – 6.9 on your own Fundamentals of Computer Graphics

Global Rendering – Ray tracing global versus local lightings models Fundamentals of Computer Graphics

Global Rendering – Ray tracing Algorithm complexity: O(M2 N 2k) M – resolution of a screen N – number of objects k – number of levels of the tree Typical program: POV Ray – available free Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 7 Implementation of a Renderer prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Implementation of a Render Four major tasks: modeling geometric processing rasterization display Fundamentals of Computer Graphics

prof.ing.Václav Skala, CSc. Fundamentals of Computer Graphics Part 8 Hierarchical&Object oriented Graphics prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Fundamentals of Computer Graphics Symbols&Instances Extension to transformations – Chapter 4 GLU&GLUT provide objects built on the top of OpenGL basic primitives NONHIERARCHICAL APPROACH primitives or objects -> SYMBOLS world is a collection of symbols APIs distinguish model frame or model coordinates – where the model is defined world frame Transformation from model frame to world frame is needed Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Symbols&Instances M = T R S In OpenGL appropriate transformation set up from the model frame (frame of symbols) to the world frame apply it to the MODEL-VIEW matrix BEFORE EXECUTING the code Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Symbols&Instances glMatrixMode(GL_MODELVIEW); /* M = T R S */ glLoadIdentity ( ); glTranslatef (....); glRotatef (...); glScalef(..); glutSolidCylinder (.....) /* or other symbol */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Symbols&Instances equivalent of a model in the form of the table contains flat information – no information on the actual structure how to represent complex structures? each part has its own model frame coordinate system how to manipulate with substructures Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Hierarchical Models A car - chassis & 4 wheels main ( ); { float s = ...;/*speed*/ float d[3]= {...}; /* direction */ draw_right_front_wheel (s,d); draw_left_front_wheel (s,d); draw_right_rear_wheel (s,d); draw_left_rear_wheel (s,d); draw_chassis (s,d); }/* WE DO NOT WANT THIS*/ two frames of animation Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Hierarchical Models Description - as a graph nodes & edges Tree – a directed graph root node parent node child nodes terminal nodes – leafs Direct Acyclic Graph (DAG) store a position of each wheel Trees & DAGs – hierarchical methods expressing the relational-ships tree structure of a car Fundamentals of Computer Graphics

Fundamentals of Computer Graphics A Robot Arm A robot arm – three parts each has 3 degrees of freedom – described by joint angle between components display ( ) { glRotatef(theta, 0.0., 1.0., 0.0); base( ); glTranslatef(0.0, h1, 0.0); glRotatef(phi, 0.0., 0.0., 1.0); lower_arm ( ); glTranslatef(0.0, h2, 0.0); glRotatef(psi, 0.0., 0.0., 1.0); upper_arm( ); }/* arm positioning independent to individual parts details */ Fundamentals of Computer Graphics

Fundamentals of Computer Graphics A Robot Arm If information stored in nodes (not in edges) – each node must store at least: pointer to a function that draws the object represented by the node matrix that positions, scales and orients this node relative to the node’s parent (including children) pointer to children of the node Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Trees and Traversal All matrices are incremental and any traversal algorithm can be used (depth-first or breadth-first); - we will traverse left to right & depth first Explicit traversal explicitly in the code, using stacks to store required matrices and attributes Recursive – code is simpler, storage of matrices&attributes done implicitly Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Trees and Traversal All matrices are incremental and any traversal algorithm can be used (depth-first or breadth-first); - we will traverse left to right & depth first Explicit traversal explicitly in the code, using stacks to store required matrices and attributes Recursive – code is simpler, storage of matrices&attributes done implicitly Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Stack Based Traversal figure( ) { glPushMatrix( ); torso ( ); glTranslatef (..); glRotate3 (..); head ( ); glPopMatrix ( ); /* restore state for the torso */ glPushMatrix( ); /* save the state for torso*/ glTranslatef (..); glRotate3 (..); left_upper_leg ( ); glTranslatef (..); glRotate3 (..); left_lower_leg ( ); /* incremental change */ glPopMatrix ( ); /* recent state recovery */ /* ----*/ glPushMatrix( ); .......... } Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Tree Data Structures typedef struct treenode { GLfloat m[16]; void (*f) ( ); struct treenode *sibling; struct treenode *child; } treenode; .... treenode torso_node, head_node, ....; Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Tree Data Structures glLoadIdentity ( ); glRotatef(theta[0],0.0, 1.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, torso_node.m); /* matrix elements copied to the m of the node */ /* the torso node has no sibling; leftmost child is the head node */ /* rest of the code for the torso node*/ torso_node.f = torso; torso_node.sibling = NULL; torso_node.child = &head_node; Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Tree Data Structures /* for the upper-arm node */ glLoadIdentity ( ); glTranslatef(-(TORSO_RADIUS+UPPER_ARM_RADIUS), 0.9*TORSO_HEIGHT, 0.0) glRotatef(theta[3], 1.0, 0.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, lua_node.m); /* matrix elements copied to the m of the node */ lua_node.f = left_upper_arm; lua_node.sibling = &rua_node; lua_node.child = &lla_node; Fundamentals of Computer Graphics

Tree Data Structures – Recursive Solution /* assumption MODEL-VIEW state */ void traverse (treenode* root); { if (root==NULL) return; glPushMatrix( ); glMultMatrixf(root->m); root->f( ); if (root->child!=NULL) traverse(root->child); glPopMatrix ( ); if (root->sibling!=NULL) traverse(root->sibling); } /* traversal method is independent from the particular tree !! */ Fundamentals of Computer Graphics

Tree Data Structures – Recursive Solution generic display callback function void display (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity ( ); traverse ( &torso_node); glutSwapBuffers( ); } Read p.346-347 to analyze the whole program including the interaction, read chapters 8.6 – 8.8 on your own Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Other Tree Structures DAG structures provide tools for describing scenes Constructive Solid Geometry – CSG trees uses solids objects to create the required object how to make an object with a hole? BSP trees – work wit surface Quadtrees & Octrees – discrete technique & space subdivision Fundamentals of Computer Graphics

Constructive Solid Geometry CSG – set operations union A  B intersection A  B difference A - B complement - B Describe intersection of 3 orthogonal cylinders simple symbolic manipulations difficult to evaluate and render Fundamentals of Computer Graphics

Constructive Solid Geometry plane A separates polygons to two groups => BINARY SPATIAL-PARTITIONING TREE standard tree techniques can be used useful for visibility solution Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Quadtrees & Octrees BSP trees do not have “a orientation” in space – application can be costly Quadtrees – hierarchical space subdivision technique - often used for image data compression - pixels Octree – extension to 3D applications volume representation - voxels Fundamentals of Computer Graphics

Hierarchical & Object oriented Graphics In this chapter you have learnt: fundamentals how very complex structures can be created and manipulated how the hierarchical structures can be used for real applications what could be a structure of database for geometric objects Have a look at: the SPIDER project at http://herakles.zcu.cz - hierarchical modeling & animation F-rep project – HyperFun language for implicit modeling – practical use of CSG trees Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 9 Discrete Techniques prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Fundamentals of Computer Graphics Discrete techniques Polygons, lines and others primitives transformed, rasterized and displayed API enables major mapping texture mapping uses pattern to be put on a surface of an object bump mapping – smooth surface is distorted to get variation of the surface environmental mapping (reflection maps) – enables ray-tracing like output Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Discrete techniques All three techniques rely on the map being stored as one-, two- or three-dimensional digital image (!!Texture mapping on a graphics card is limited!!) Causes ANTIALIASING errors buffer – a memory block with spatial resolution n x m and with k bits bitplane – single plane n x m with 1 bit only pixel – picture element Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Texture Mapping Regular patterns are mapped to an object’s surface Two dimensional texture T(s,t) s, t – texture coordinates stored in texture memory as n x m array of texture elements - texels Texture map associate a unique point of T with each point on a geometric object – mapped to screen coordinates Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Texture Mapping Difficulties: mapping from texture to geometric coordinates 2D texture defined over a rectangular region in the texture space -> mapping to 3D region can be quite complex rendering based on pixel-to-pixel approach – the inverse mapping from screen coordinates to texture coordinates is needed because of shading – mapping areas-to-areas and not point-to-point is required -> antialising problems, moire patterns etc. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Texture Mapping pixel (xs, ys) – corresponds to (x, y, z) on “curved” object problems with finding inverse mapping difficulties with mapping Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Linear Mapping Most curved surfaces represented parametrically as p(u,v) = [x(u,v) , y(u,v) , z(u,v)]T a point in the texture map T(s,t) is to be mapped to a point on the surface p(u,v) by a linear map u = as + bt + c v = ds + et + f (if ae  bd mapping is invertible) mapping is easy to use it does not respect the curvature of the object Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Linear Mapping Linear mapping is defined as: standard window-viewport approach. Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Texture Mapping Two part mapping - steps map the texture a simple 3D intermediate surface – cylinder cube, sphere etc. the surface containing the mapped texture is mapped to the surface being rendered. Can be applied in geometric or parametric coordinates Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Texture Mapping Suppose a cylinder x = r cos (2u) y = r sin (2u) z = v / h and u,v  (0,1) – then s = u & t = v we are able to map the texture WITHOUT distorting its shape for a sphere the Mercator projection can be used y = r sin (2u) cos (2v) z = r sin (2u) sin (2v) Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Texture Mapping The second step is to map the texture values on the intermediate object to the desired surface Figure shows THREE possible strategies: texture value is projected on the surface in the normal direction inverse solution from the surface texture element is to be find according to the normal if the center of the object is known – intersection with intermediate surface is computed and texture value assigned Fundamentals of Computer Graphics

Texture Mapping in OpenGL OpenGL – many mapping options including 1D, 2D & 3D textures GLubyte my_texels [512][512]; /* generated somehow */ glTexImage2D(GL_TEXTURE, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); /* 0 & 3 – level & components, 0 – border, format – see latter*/ glEnable(GL_TEXTURE_2D); /* enables texture mapping */ Fundamentals of Computer Graphics

Texture Mapping in OpenGL 2D textures specification glTexImage2D(GL_TEXTURE, level, components, width, height, border, format, type, array); components (1 – 4 ) number of components RGBA to be affected with the map level & border – parameters for fine control Fundamentals of Computer Graphics

Texture Mapping in OpenGL glBegin(GL_QUAD); glTexCoord2f(0.0, 0.0); glVertex2f(x1, y1, z1); glTexCoord2f(1.0, 0.0); glVertex2f(x2, y2, z2); glTexCoord2f(0.0, 1.0); glVertex2f(x3, y3, z3); glTexCoord2f(1.0, 1.0); glVertex2f(x4, y4, z4); glEnd ( ); range of s, t changed to <0,0.5> Fundamentals of Computer Graphics

Texture Mapping in OpenGL STOP HERE!!! Fundamentals of Computer Graphics

Fundamentals of Computer Graphics Part 10 Curves & Surfaces prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive Computer Graphics – A Top Down Approach with OpenGL, Addison Wesley, 2001

Fundamentals of Computer Graphics Not AVAILABLE Please, read the appropriate chapter on your own Fundamentals of Computer Graphics