1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Part 3: Three Dimensions.

Slides:



Advertisements
Similar presentations
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
Advertisements

Draw a Simple Object. Example 1/4 #include “ glut.h ” void display(); void reshape(GLsizei w, GLsizei h); void main(int argc, char** argv){ glutInit(&argc,
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,
OpenGL and Projections
1 CSC461 Lecture 7: 3D Programming in OpenGL Objectives: Develop 2D and 3D examples -- Sierpinski gasket: a fractal Develop 2D and 3D examples -- Sierpinski.
Sierpinski Gasket Program
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Geometric Primitives Used in Open GL Polygons Polygon is : Flat shape with three or more straight sides. It could be either : convex or concave.
Demetriou/Loizidou – ACSC330 – Chapter 2 Graphics Programming Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science Frederick Institute.
Computer Graphics CS 385 January 31, Fractals Some definitions Object which is self-similar at all scales. Regardless of scale the same level of.
Graphics Programming Chapter 2. CS 480/680 2Chapter 2 -- Graphics Programming Introduction: Introduction: Our approach is programming oriented. Our approach.
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.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
DREAM PLAN IDEA IMPLEMENTATION Introduction to Computer Graphics Dr. Kourosh Kiani
1Computer Graphics Programming with OpenGL Three Dimensions – 2 Lecture 7 John Shearer Culture Lab – space 2
1 Programming with OpenGL Part 3: Three Dimensions Yuanfeng Zhou Shandong University.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1Computer Graphics Programming with OpenGL Three Dimensions Lecture 06 John Shearer Culture Lab – space 2
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 Bryson Payne CSCI 3600 NGCSU.
CS 480/680 Computer Graphics Programming with Open GL Part 6: Three Dimensions Dr. Frederick C Harris, Jr. Fall 2011.
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 OpenGL Transformations.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
Color spaces. Draw Buffers Color models. Mathmatical Protocols for defining colors with numbers  RGB Red, Green, Blue  HSV Hue, Saturation, Value(Brightness)‏
Geometric Primitives Used in Open GL Drawing Triangles glBegin(GL_TRIANGELS); glVertex2i(p0); glVertex2i(p1); glVertex2i(p2); glVertex2i(p3); glVertex2i(p4);
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
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 6: Three Dimensions Ed Angel Professor.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
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.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics -practical- Lecture 6. (visual c++) open gl library To use open GL with VC++ we add these files:- 1)Glut.h C:\program files\ Microsoft.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
CS5500 Computer Graphics March 2, 2006.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
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
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Introduction to Computer Graphics with WebGL
גרפיקה ממוחשבת: מבוא ל-OpenGL
Lighting and Shading Lab 8:.
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
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
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Part 3: Three Dimensions

2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Objectives Develop a more sophisticated three- dimensional example ­Sierpinski gasket: a fractal Introduce hidden-surface removal

3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Three-dimensional Applications In OpenGL, 2D applications are a special case of 3D graphics Going from 2D to 3D ­Use glVertex3f() ­Have to worry about the order in which polygons are drawn or use hidden-surface removal

4 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Sierpinski Gasket (2D) Start with a triangle Connect bisectors of sides and remove central triangle Repeat

5 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Example Five subdivisions

6 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 The gasket as a fractal Consider the filled area (black) and the perimeter (the length of all the lines around the filled triangles) As we continue subdividing ­the area goes to zero ­but the perimeter goes to infinity This is not an ordinary geometric object ­It is neither two- nor three-dimensional ­It is a fractal (fractional dimension) object

7 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket2d code // // Program: gasket2d.c // Purpose: To display 2D Sierpenski gasket // Author: John Gauch // Date: September 2008 // #include // // Init function for OpenGL // void init() { glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); }

8 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket2d code // // Function to draw 2D Sierpinski gasket // void gasket(int n, float ax, float ay, float bx, float by, float cx, float cy) { // Draw single trangle if (n <= 0) { glVertex2f(ax, ay); glVertex2f(bx, by); glVertex2f(cx, cy); }

9 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket2d code // Handle recursive case else { float dx = (ax+bx)/2; float dy = (ay+by)/2; float ex = (bx+cx)/2; float ey = (by+cy)/2; float fx = (cx+ax)/2; float fy = (cy+ay)/2; gasket(n-1, ax, ay, dx, dy, fx, fy); gasket(n-1, bx, by, ex, ey, dx, dy); gasket(n-1, cx, cy, fx, fy, ex, ey); }

10 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket2d code // // Draw callback for OpenGL // void draw() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); gasket(4, 0,0.5, -0.5,-0.5, 0.5,-0.5); glEnd(); glFlush(); }

11 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket2d code // // Main program // int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(250, 250); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("Gasket2D"); glutDisplayFunc(draw); init(); glutMainLoop(); return 0; }

12 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Efficiency Note By having the glBegin and glEnd in the display callback rather than in the function triangle and using GL_TRIANGLES rather than GL_POLYGON in glBegin, we call glBegin and glEnd only once for the entire gasket rather than once for each triangle

13 Angel: Interactive Computer Graphics 5E © Addison-Wesley D Gasket We can subdivide each of the four faces Appears as if we remove a solid tetrahedron from the center leaving four smaller tetrahedra

14 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Example after 5 iterations

15 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket3d code // // Program: gasket3d.c // Purpose: To display 3D Sierpenski gasket // Author: John Gauch // Date: September 2008 // #include // // Init function for OpenGL // void init() { glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glEnable(GL_DEPTH_TEST); }

16 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket3d code // // Function to draw 3D Sierpinski gasket // void gasket(int n, float ax, float ay, float az, float bx, float by, float bz, float cx, float cy, float cz) { // Draw single trangle if (n <= 0) { glVertex3f(ax, ay, az); glVertex3f(bx, by, bz); glVertex3f(cx, cy, cz); }

17 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket3d code // Handle recursive case else { float dx = (ax+bx)/2; float dy = (ay+by)/2; float dz = (az+bz)/2; float ex = (bx+cx)/2; float ey = (by+cy)/2; float ez = (bz+cz)/2; float fx = (cx+ax)/2; float fy = (cy+ay)/2; float fz = (cz+az)/2; gasket(n-1, ax, ay, az, dx, dy, dz, fx, fy, fz); gasket(n-1, bx, by, bz, ex, ey, ez, dx, dy, dz); gasket(n-1, cx, cy, cz, fx, fy, fz, ex, ey, ez); }

18 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket3d code // // Draw callback for OpenGL // void draw() { float ax = 0.0; float ay = 0.5; float az = -0.5; float bx = -0.43; float by = -0.25; float bz = -0.5; float cx = 0.43; float cy = -0.25; float cz = -0.5; float dx = 0.0; float dy = 0.0; float dz = 0.0;

19 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket3d code glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); glColor3f(0.5, 0.5, 0.5); gasket(3, ax, ay, az, bx, by, bz, cx, cy, cz); glColor3f(1.0, 0.0, 0.0); gasket(3, dx, dy, dz, ax, ay, az, bx, by, bz); glColor3f(0.0, 1.0, 0.0); gasket(3, dx, dy, dz, bx, by, bz, cx, cy, cz); glColor3f(0.0, 0.0, 1.0); gasket(3, dx, dy, dz, cx, cy, cz, ax, ay, az); glEnd(); glFlush(); }

20 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Gasket3d code // // Main program // int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(250, 250); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH); glutCreateWindow("Gasket3D"); glutDisplayFunc(draw); init(); glutMainLoop(); return 0; }

21 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Almost Correct Because the triangles are drawn in the order they are defined in the program, the front triangles are not always rendered in front of triangles behind them get this want this

22 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Hidden-Surface Removal We want to see only those surfaces in front of other surfaces OpenGL uses a hidden-surface method called the z-buffer algorithm that saves depth information as objects are rendered so that only the front objects appear in the image

23 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Using the z-buffer algorithm The algorithm uses an extra buffer, the z-buffer, to store depth information as geometry travels down the pipeline It must be ­Requested in main.c glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH) ­Enabled in init.c glEnable(GL_DEPTH_TEST) ­Cleared in the display callback glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

24 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Surface vs Volume Subdvision In our example, we divided the surface of each face We could also divide the volume using the same midpoints The midpoints define four smaller tetrahedrons, one for each vertex Keeping only these tetrahedrons removes a volume in the middle See text for code

25 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Volume Subdivision