Interactive Computer Graphics Graphics Programming

Slides:



Advertisements
Similar presentations
Chapter 2: Graphics Programming
Advertisements

Pemrograman OpenGL Dasar
Informationsteknologi Monday, October 29, 2007Computer Graphics - Class 21 Today’s class Graphics programming Color.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
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,
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Graphics Programming Chapter 2. CS 480/680 2Chapter 2 -- Graphics Programming Introduction: Introduction: Our approach is programming oriented. Our approach.
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. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
Computer Graphics Chapter 6 Andreas Savva. 2 Interactive Graphics Graphics provides one of the most natural means of communicating with a computer. Interactive.
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.
1 Chapter 2 Graphics Programming. 2 Using OpenGL in Visual C++ – 1/3 Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib and glu32.lib.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
Graphics: Conceptual Model
What are Computer Graphics Basically anything that is on you Monitor – This includes the text that you will see Text isn’t Advanced Graphics But…. Understanding.
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
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 4: Color and Attributes Isaac Gang University.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 15 Creating 3D Models.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
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.
Graphics Programming. Graphics Functions We can think of the graphics system as a black box whose inputs are function calls from an application program;
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Chapter (2)1 Computer Graphics - Chapter 2 Graphics Programming Objectives are to learn about: An example: The Sierpinski Gasket The OpenGL API Primitives.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
The Human Visual System vs The Pinhole camera
Graphics Programming Chun-Yuan Lin CG 2018/5/15.
Course code:10CS65 | Computer Graphics and Visualization
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
Programming with OpenGL Part 3: Three Dimensions
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Graphics Programming (I)
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 4: Color and Attributes
Drawing in the plane 455.
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
OpenGL program.
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 3: Three Dimensions
Programming with OpenGL Part 4: Color and Attributes
Programming with OpenGL Part 2: Complete Programs
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:

Interactive Computer Graphics Graphics Programming James Gain and Edwin Blake Department of Computer Science University of Cape Town July 2002 jgain@cs.uct.ac.za Collaborative Visual Computing Laboratory

Interactive Computer Graphics Contents Map of the Lecture Introduce the OpenGL API Primitives Attributes Viewing Control Create a sample 2-D program, which can be easily extended to 3-D 18/01/2019 Interactive Computer Graphics Contents

Case Study: The Sierpinski Gasket Recursive example from Fractal Geometry used to illustrate OpenGL Algorithm: Pick a random point P0 inside a triangle Select a vertex V at random Find the point P1 halfway between V and P0 Display P1 Let P0 = P1 Return to step 2 Each point has (x,y) coordinates in a suitable coordinatge system OpenGL is 3-D but can easily implement 2-D as a subset 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents A First Pass for(k = 0; k < 5000; k++) { // pick a random vertex 0-2 j = rand()%3; // compute new point px = (px + vx[j])/2.0; py = (py + vy[j])/2.0; // display new point glBegin(GL_POINTS); glVertex2f(px, py); glEnd(); } glFlush(); px, py, vx[ ], vy[ ] initialized before the loop OpenGL often uses #defines (GL_POINTS) to aid readability glVertex* has many forms * = nt or ntv, where n is dimension (2, 3, 4), t is type (i, f, d) and presence of v indicates that arguments are passed as an array glFlush() forces immediate display 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Outstanding Issues In what colour are we drawing? Where on the screen does our image appear? How large will the image be? How do we create a demarcated area of the screen (a window) for our image? How much of the 2-D coordinate system will appear on the screen? How long will the image remain on the screen? 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents The OpenGL API Need the OpenGL API to resolve outstanding issues A black box between user programs and I/O devices Classes of functionality: Primitives (points, line segments, polygons) Attributes (colour, pattern, type face) Viewing (synthetic camera control) Transformation (rotation, translation and scaling of objects) Input (deal with a variety of input devices) Control (manage the windowing environment) 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Coordinate Systems Early graphics systems restricted users to the units of the display Device independent graphics frees the programmer from details of the input and output devices API handles coordinate mapping From world (problem) coordinates, usually floating point To device (screen) coordinates, usually integers Display device units make little sense if talking about galaxies or atoms 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents The OpenGL Interface Native OpenGL functions prefixed by “gl” Graphics Utility Library (GLU) contains code for common objects (e.g. spheres) GL Utility Toolkit (GLUT) provides window system management #include <GL/glut.h> inherits glut.h, gl.h and glu.h 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Primitives There is debate, similar to CISC vs. RISC, over how many primitives to support OpenGL takes the middle ground Primitives specified by vertex calls (glVertex*) bracketed by glBegin(type) and glEnd() Line-based primitives: Line segments (GL_LINES), Polylines (GL_LINE_STRIP) 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Polygons Polygons are objects with a line loop border and an interior Polygons must be simple (no crossing edges), convex (the line segment between two interior points does not leave the polygon) and flat (planar) Triangles always obey these properties and are often better supported in hardware 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Polygon Types Polygons (GL_POLYGON) successive vertices define line segments, last vertex connects to first Triangles and Quadrilaterals (GL_TRIANGLES, GL_QUADS) successive groups of 3 or 4 interpreted as triangles or quads Strips and Fans (GL_TRIANGLE_STRIP, GL_QUAD_STRIP, GL_TRIANGLE_FAN) joined triangles or quads that share vertices 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Text Stroke text (e.g. postscript fonts): Constructed from closed curves and line segments Can be manipulated (scaled, rotated) like any other graphical primitive and still retain detail Takes up significant memory and processing Raster text Characters defined as bit blocks and placed in the frame buffer with a bit block transfer (bitblt) Does not scale or rotate well OpenGL text glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c); Character with ASCII code c is placed at the current raster pos glRasterPos* changes the raster position Bitblt is often implemented as a single efficient operation Raster Scaling requires pixel duplication Raster Rotation moves pixels from their grid reference points 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Curved Objects Computer Graphics requires smooth complex objects (e.g. Geri’s face) Mathematical Definition: Define an object mathematically with supporting functions for graphical operations (transformation, intersection, rendering, etc.) Example: a circle can be represented by its centre and a radius Advanced OpenGL supports some mathematical objects Tesselation: Approximating a curved surface by a mesh of convex polygons (often triangles) Example: a circle can be approximated by a regular polygon with n sides Rendering with polygon scan conversion ultimately requires tesselation Polynomial curves and surfaces will be covered later As the number of circle sides increases the approximation quality improves Non PSC can use the mathematical definitions more directly 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Attributes An attribute is any property that determines how a primitive will be rendered Immediate Mode graphics: Primitives are not retained. Instead they are displayed and then discarded Attributes are retained. Always uses the last retained attribute for that primitive Attributes of Primitives Point (size; colour) Line Segment (colour; thickness; type - solid, dashed, dotted) Polygon (colour; fill - solid, pattern, empty) Text (direction; size; font; style - bold, italic) Example: glPointSize(2.0); Examples of attributes, not the complete list 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Colour Computer colour models are a course approximations of the human visual system RGB Colour Model: Colour is a combination of Red, Green and Blue primitives in the range 0-1 Conceptually separate frame buffers for each red, green and blue channel Typically in 24 bit colour each channel is allocated 1 byte ≈ 16M colours OpenGL RGBA: A = alpha channel, which encodes transparency (1) / opacity (0) glClearColor(1.0, 1.0, 1.0, 0.0); clears the window to solid white glColor4f(1.0, 0.0, 0.0, 0.5); sets the colour to half transparent red A more complete discussion of colour (physiology and colour models) will be given later 18/01/2019 Interactive Computer Graphics Contents

Viewing The synthetic camera model separates the specification of the objects from the camera 2-D Viewing: Display a rectangular portion (clipping rectangle) of the 2-D coordinate plane The size and position of the window on screen are independent of the clipping rectangle A special case of 3-D orthographic projection Before Clipping After Clipping 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Orthographic View Points projected onto the viewing plane (z = 0): (x,y,z)  (x,y,0) Primitives outside the view volume are cliiped OpenGL: void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) Default: glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 2-D alternative: void gluOrtho2D(GLdouble left, Gldouble right, GLdouble bottom, Gldouble top) same as glOrtho with near = -1.0 and far = 1.0 Unlike a real camera, points behind the viewing plane (but inside the viewing volume) are displayed 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Matrix Modes Computer Graphics relies on concatenating transformations. This is achieved by multiplying matrices OpenGL stores Projection (viewing transformation) and ModelView (object transformation) matrices as part of its state Changing matrices: glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 500.0, 0.0, 500.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); Sets up a 500x500 clipping rectangle with lower left at (0,0) Always return to a consistent matrix mode Transformations covered in the next lecture 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Control Functions Need to interact with the windowing environment to display graphics. Our approach: keep complexity to a minimum Pixel positions in a window are measured in integer window coordinates Rendering origin at lower left but mouse origin at upper left OpenGL initialization: glutInit(int * argcp, char ** argv); intitiate windows interaction - pass command line arguments glutCreateWindow(char * title); create a window with title in the title bar glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); specify RGB colour, hidden surface removal and double buffering glutInitWindowSize(480, 640); a 480x640 window glutInitWindowPosition(0, 0); at top left of screen 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Aspect Ratio Problems Aspect ratio is the ratio of a rectangle’s width to its height Distortion if the aspect ratio of the viewing rectangle (glOrtho) does not match the window (from glutInitWindowSize) Viewport: A rectangular area of the display window used to solve aspect ratio distortion void glViewport(GLint x, GLint y, GLsizei w, GLsizei h) Lower left (x,y); upper right (x+w, y+h) Complicated by window resizing 18/01/2019 Interactive Computer Graphics Contents

OpenGL Program Structure display(): A function which contains all the rendering calls Executed during window init, window moves and window uncovering glutDisplayFunc(display); sets up the callback function for redisplay myinit(): Set the OpenGL state variables associated with viewing and attributes Contains parameters that are set only once, independantly of display 18/01/2019 Interactive Computer Graphics Contents

At Last: the Gasket Program I void myinit() { // attributes glClearColor(1.0, 1.0, 1.0, 0.0); // white background glColor3f(0.0, 0.0, 0.0); // draw in black // set up viewing glMatrixMode(GL_PROJECTION); gluLoadIdentity(); glOrtho(0.0, 500.0, 0.0, 500.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); } 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents Gasket Program II void display() { typedef Glfloat point2[2]; point2 v[3] = {{0.0,0.0},{250.0, 500.0}, {500.0, 0.0}}; int i, j, k; int rand(); point2 p = {75.0, 50.0} \\ random point in triangle glClear(GL_COLOR_BUFFER_BIT); for(k = 0; k < 5000; k++) { j = rand()%3; // compute new point p[0] = (p[0] + v[j][0])/2.0; p[1] = (p[1] + v[j][1])/2.0; // display new point glBegin(GL_POINTS); glVertex2fv(p); glEnd(); } glFlush(); 18/01/2019 Interactive Computer Graphics Contents

Interactive Computer Graphics Contents 3-D Sierpinski Gasket Replace: Triangle with Tetrahedron 2-D points with 3-D points Colour the vertices to help with visualization // compute new point p[0] = (p[0] + v[j][0]) / 2.0; p[1] = (p[1] + v[j][1]) / 2.0; p[2] = (p[2] + v[j][2]) / 2.0 // display new point glBegin(GL_POINTS); glColor3f(p[0]/250.0, p[1]/250.0, p[2]/250.0); glVertex3fv(p); glEnd(); 18/01/2019 Interactive Computer Graphics Contents