Fundamentals of Computer Graphics with Java, Opengl, and Jogl
Total Page:16
File Type:pdf, Size:1020Kb
Fundamentals of Computer Graphics With Java, OpenGL, and Jogl (Preliminary Partial Version, May 2010) David J. Eck Hobart and William Smith Colleges This is a PDF version of an on-line book that is available at http://math.hws.edu/graphicsnotes/. The PDF does not include source code files, but it does have external links to them, shown in blue. In addition, each section has a link to the on-line version. The PDF also has internal links, shown in red. These links can be used in Acrobat Reader and some other PDF reader programs. ii c 2010, David J. Eck David J. Eck ([email protected]) Department of Mathematics and Computer Science Hobart and William Smith Colleges Geneva, NY 14456 This book can be distributed in unmodified form with no restrictions. Modified versions can be made and distributed provided they are distributed under the same license as the original. More specifically: This work is licensed under the Creative Commons Attribution-Share Alike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. The web site for this book is: http://math.hws.edu/graphicsnotes Contents Preface iii 1 Java Graphics in 2D 1 1.1 Vector Graphics and Raster Graphics ........................ 1 1.2 Two-dimensional Graphics in Java .......................... 4 1.2.1 BufferedImages ................................. 5 1.2.2 Shapes and Graphics2D ............................ 6 1.3 Transformations and Modeling ............................ 12 1.3.1 Geometric Transforms ............................. 12 1.3.2 Hierarchical Modeling ............................. 21 2 Basics of OpenGL and Jogl 25 2.1 Basic OpenGL 2D Programs ............................. 25 2.1.1 A Basic Jogl App ................................ 26 2.1.2 Color ...................................... 28 2.1.3 Geometric Transforms and Animation .................... 29 2.1.4 Hierarchical Modeling ............................. 31 2.1.5 Introduction to Scene Graphs ......................... 33 2.1.6 Compiling and Running Jogl Apps ...................... 34 2.2 Into the Third Dimension ............................... 36 2.2.1 Coordinate Systems .............................. 37 2.2.2 Essential Settings ................................ 41 2.3 Drawing in 3D ..................................... 44 2.3.1 Geometric Modeling .............................. 44 2.3.2 Some Complex Shapes ............................. 46 2.3.3 Optimization and Display Lists ........................ 47 2.4 Normals and Textures ................................. 50 2.4.1 Introduction to Normal Vectors ........................ 51 2.4.2 Introduction to Textures ............................ 54 3 Geometry 60 3.1 Vectors, Matrices, and Homogeneous Coordinates .................. 60 3.1.1 Vector Operations ............................... 61 3.1.2 Matrices and Transformations ......................... 62 3.1.3 Homogeneous Coordinates ........................... 63 3.1.4 Vector Forms of OpenGL Commands ..................... 64 3.2 Primitives ........................................ 66 3.2.1 Points ...................................... 66 i CONTENTS ii 3.2.2 Lines ....................................... 67 3.2.3 Polygons .................................... 68 3.2.4 Triangles .................................... 71 3.2.5 Quadrilaterals ................................. 72 3.3 Polygonal Meshes .................................... 73 3.3.1 Indexed Face Sets ............................... 73 3.3.2 OBJ Files .................................... 76 3.3.3 Terraine and Grids ............................... 78 3.4 Drawing Primitives ................................... 80 3.4.1 Java’s Data Buffers ............................... 81 3.4.2 Drawing With Vertex Arrays ......................... 82 3.4.3 Vertex Buffer Objects ............................. 86 3.4.4 Drawing with Array Indices .......................... 89 3.5 Viewing and Projection ................................ 92 3.5.1 Perspective Projection ............................. 92 3.5.2 Orthographic Projection ............................ 93 3.5.3 The Viewing Transform ............................ 94 3.5.4 A Simple Avatar ................................ 96 3.5.5 Viewer Nodes in Scene Graphs ........................ 97 4 Light and Material 99 4.1 Vision and Color .................................... 99 4.2 OpenGL Materials ...................................102 4.2.1 Setting Material Properties ..........................102 4.2.2 Color Material .................................104 4.3 OpenGL Lighting ....................................105 4.3.1 Light Color and Position ............................105 4.3.2 Other Properties of Lights ...........................107 4.3.3 The Global Light Model ............................108 4.3.4 The Lighting Equation .............................109 4.4 Lights and Materials in Scenes ............................111 4.4.1 The Attribute Stack ..............................111 4.4.2 Lights in Scene Graphs ............................114 4.5 Textures .........................................115 4.5.1 Texture Targets .................................116 4.5.2 Mipmaps and Filtering .............................117 4.5.3 Texture Transformations ............................118 4.5.4 Creating Textures with OpenGL .......................120 4.5.5 Loading Data into a Texture .........................122 4.5.6 Texture Coordinate Generation ........................123 4.5.7 Texture Objects ................................125 5 Some Topics Not Covered 126 Appendix: Source Files 129 Preface These notes represent an attempt to develop a new computer graphics course at the advanced undergraduate level. The primary goal, as in any such course, is to cover the funda- mental concepts of computer graphics, and the concentration is on graphics in three dimensions. However, computer graphics has become a huge and complex field, and the typical textbook in the field covers much more material than can reasonably fit into a one-semester undergraduate course. Furthermore, the selection of topics can easily bury what should be an exciting and applicable subject under a pile of detailed algorithms and equations. These details are the basis of computer graphics, but they are to a large extent built into graphics systems. While practitioners should be aware of this basic material in a general way, there are other things that are more important for students on an introductory level to learn. These notes were written over the course of the Spring semester, 2010. More information can be found on the web page for the course at http://math.hws.edu/eck/cs424/. The notes cover computer graphics programming using Java. Jogl is used for three-dimensional graphics programming. Jogl is the Java API for OpenGL; OpenGL is a standard and widely used graphics API. While it is most commonly used with the C programming language, Jogl gives Java programmers access to all the features of OpenGL. The version of Jogl that was used in this course was 1.1.1a. A new version, Jogl 2, was under development as the course was being taught, but Jogl 2 is still listed as a “work in progress” in May 2010. (Unfortunately, it looks like Jogl 1.1.1a will not be upward compatible with Jogl 2, so code written for the older version will not automatically work with the new version. However, the changes that will be needed to adapt code from this book to the new version should not be large.) ∗∗ ∗ As often happens, not as much is covered in the notes as I had hoped, and even then, the writing gets a bit rushed near the end. A number of topics were covered in the course that did not make it into the notes. Some examples from those topics can be found in Chapter 5 (which is not a real chapter). In addition to OpenGL, the course covered two open-source graphics programs, GIMP briefly and Blender in a little more depth. Some of the labs for the course deal with these programs. Here are the topics covered in the four completed chapters of the book: • Chapter 1: Java Graphics Fundamentals in Two Dimensions. This chapter includes a short general discussion of graphics and the distinction between “painting” and “drawing.” It covers some features of the Graphics2D class, including in particular the use of geometric transforms for geometric modeling and animation. • Chapter 2: Overview of OpenGL and Jogl. This chapter introduces drawing with OpenGL in both two and three dimensions, with very basic color and lighting. Drawing in this chapter uses the OpenGL routines glBegin and glEnd. It shows how to use Jogl to write OpenGL applications and applets. It introduces the use of transforms and scene iii Preface iv graphs to do hierarchical modeling and animation. It introduces the topic of optimization of graphics performance by covering display lists. • Chapter 3: Geometric Modeling. This chapter concentrates on composing scenes in three dimensions out of geometric primitives, including the use of vector buffer objects and the OpenGL routines glDrawArrays and glDrawArrayElements. And it covers viewing, that is, the projection of a 3D scene down to a 2D picture. • Chapter 4: Color, Lighting, and Materials. This chapter discusses how to add color, light, and textures to a 3D scene, including the use of lights in scene graphs. Note that source code for all the examples in the book can be found in the source directory on-line or in the web site download. ∗∗ ∗ The web site and the PDF versions of this