1. Trang chủ
  2. » Công Nghệ Thông Tin

opengl es 2 for android

330 1,1K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 330
Dung lượng 30,03 MB

Nội dung

www.it-ebooks.info www.it-ebooks.info Early praise for OpenGL ES 2.0 for Android Kevin’s book is stuffed full of useful code and explanations, to the point of being inspirational. The math/matrix stuff is the best I have ever read on the topic. If you already know Java well and want to break into 3D graphics, this book is perfect. ➤ John Horton HadronWebDesign.com and author of the Android math league app Math Legends Cuts out the academic fluff and teaches you what you need to become productive quickly. I wish I had access to such a clear, concise, and humorous book when I first learned OpenGL. ➤ Mark F. Guerra Developer, GLWallpaperService library for OpenGL An amazing introduction to Android graphics programming, covering all the topics that give headaches to OpenGL beginners with such extensive detail that you’ll be perfectly prepared to implement your own ideas. ➤ Carsten Haubold Maintainer, NeHe OpenGL tutorials I wish I had this book when I first started programming Android games. It’s well written and up-to-date. ➤ Owen Alanzo Hogarth President, Team Blubee, Inc. I am greatly impressed by this book and would gladly recommend it to any programming enthusiast or anyone who’s ever dreamed of making a game. ➤ Tibor Simic www.it-ebooks.info OpenGL ES 2 for Android A Quick-Start Guide Kevin Brothaler The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com . The Android robot is reproduced from work created and shared by Google and is used according to terms described in the Creative Commons 3.0 Attribution License ( http://creativecommons.org/licenses/by/3.0/us/legalcode ). The unit circle image in Figure 43, from http://en.wikipedia.org/wiki/File:Unit_circle.svg , is used according to the terms described in the Creative Commons Attribution-ShareAlike license, located at http://creativecommons.org/licenses/by-sa/3.0/legalcode . Day skybox and night skybox courtesy of Jockum Skoglund, also known as hipshot, hipshot@zfight.com, http://www.zfight.com . The image of the trace capture button is created and shared by the Android Open Source Project and is used according to terms described in the Creative Commons 2.5 Attribution License. Copyright © 2013 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-937785-34-5 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—June 2013 www.it-ebooks.info To Anne and my Oma You have brought so much joy and wonder into my life. Thank you for making it all possible. www.it-ebooks.info Contents Foreword . . . . . . . . . . . . . . xi Acknowledgments . . . . . . . . . . . xiii Welcome to OpenGL ES for Android! . . . . . . . xv 1. Getting Started . . . . . . . . . . . . 1 Installing the Tools 11.1 1.2 Creating Our First Program 4 1.3 Initializing OpenGL 6 1.4 Creating a Renderer Class 11 1.5 Using Static Imports 14 1.6 A Review 15 Part I — A Simple Game of Air Hockey 2. Defining Vertices and Shaders . . . . . . . . 19 Why Air Hockey? 192.1 2.2 Don’t Start from Scratch 21 2.3 Defining the Structure of Our Air Hockey Table 22 2.4 Making the Data Accessible to OpenGL 26 2.5 Introducing the OpenGL Pipeline 28 2.6 The OpenGL Color Model 34 2.7 A Review 35 3. Compiling Shaders and Drawing to the Screen . . . . 37 Loading Shaders 373.1 3.2 Compiling Shaders 39 3.3 Linking Shaders Together into an OpenGL Program 44 3.4 Making the Final Connections 46 3.5 Drawing to the Screen 51 www.it-ebooks.info 3.6 A Review 57 3.7 Exercises 58 4. Adding Color and Shade . . . . . . . . . . 59 Smooth Shading 604.1 4.2 Introducing Triangle Fans 61 4.3 Adding a New Color Attribute 63 4.4 Rendering with the New Color Attribute 71 4.5 A Review 75 4.6 Exercises 75 5. Adjusting to the Screen’s Aspect Ratio . . . . . . 77 We Have an Aspect Ratio Problem 785.1 5.2 Working with a Virtual Coordinate Space 80 5.3 Linear Algebra 101 83 5.4 Defining an Orthographic Projection 87 5.5 Adding an Orthographic Projection 89 5.6 A Review 94 5.7 Exercises 94 6. Entering the Third Dimension . . . . . . . . 95 The Art of 3D 956.1 6.2 Transforming a Coordinate from the Shader to the Screen 96 6.3 Adding the W Component to Create Perspective 99 6.4 Moving to a Perspective Projection 101 6.5 Defining a Perspective Projection 103 6.6 Creating a Projection Matrix in Our Code 105 6.7 Switching to a Projection Matrix 107 6.8 Adding Rotation 111 6.9 A Review 114 6.10 Exercises 114 7. Adding Detail with Textures . . . . . . . . 115 Understanding Textures 1167.1 7.2 Loading Textures into OpenGL 119 7.3 Creating a New Set of Shaders 126 7.4 Creating a New Class Structure for Our Vertex Data 127 7.5 Adding Classes for Our Shader Programs 133 7.6 Drawing Our Texture 137 7.7 A Review 139 7.8 Exercises 139 Contents • viii www.it-ebooks.info 8. Building Simple Objects . . . . . . . . . 141 Combining Triangle Strips and Triangle Fans 1428.1 8.2 Adding a Geometry Class 143 8.3 Adding an Object Builder 145 8.4 Updating Our Objects 153 8.5 Updating Shaders 155 8.6 Integrating Our Changes 156 8.7 A Review 161 8.8 Exercises 161 9. Adding Touch Feedback: Interacting with Our Air Hockey Game . . . . . . . . . . . 165 9.1 Adding Touch Support to Our Activity 165 9.2 Adding Intersection Tests 168 9.3 Moving Around an Object by Dragging 177 9.4 Adding Collision Detection 181 9.5 A Review and Wrap-Up 185 9.6 Exercises 186 Part II — Building a 3D World 10. Spicing Things Up with Particles . . . . . . . 191 Creating a Set of Shaders for a Simple Particle System 19310.1 10.2 Adding the Particle System 197 10.3 Drawing the Particle System 201 10.4 Spreading Out the Particles 204 10.5 Adding Gravity 206 10.6 Mixing the Particles with Additive Blending 208 10.7 Customizing the Appearance of Our Points 209 10.8 Drawing Each Point as a Sprite 211 10.9 A Review 214 10.10 Exercises 214 11. Adding a Skybox . . . . . . . . . . . 217 Creating a Skybox 21811.1 11.2 Loading a Cube Map into OpenGL 219 11.3 Creating a Cube 221 11.4 Adding a Skybox Shader Program 224 11.5 Adding the Skybox to Our Scene 227 11.6 Panning the Camera Around the Scene 228 Contents • ix www.it-ebooks.info 11.7 A Review 232 11.8 Exercises 232 12. Adding Terrain . . . . . . . . . . . . 233 Creating a Height Map 23312.1 12.2 Creating Vertex and Index Buffer Objects 234 12.3 Loading in the Height Map 237 12.4 Drawing the Height Map 241 12.5 Occluding Hidden Objects 245 12.6 A Review 250 12.7 Exercises 251 13. Lighting Up the World . . . . . . . . . . 253 Simulating the Effects of Light 25313.1 13.2 Implementing a Directional Light with Lambertian Reflectance 255 13.3 Adding Point Lights 264 13.4 A Review 272 13.5 Exercises 272 14. Creating a Live Wallpaper . . . . . . . . . 275 Implementing the Live Wallpaper Service 27514.1 14.2 Playing Nicely with the Rest of the System 283 14.3 A Review 286 14.4 Exercises 286 15. Taking the Next Step . . . . . . . . . . 289 15.1 Looking Toward Other Platforms 289 15.2 Learning About More Advanced Techniques 291 15.3 Sharing Your Artistic Vision with the World 295 A1. The Matrix Math Behind the Projections . . . . . 297 A1.1 The Math Behind Orthographic Projections 297 A1.2 The Math Behind Perspective Projections 300 A2. Debugging . . . . . . . . . . . . . 305 A2.1 Debugging with glGetError 305 A2.2 Using Tracer for OpenGL ES 306 A2.3 Pitfalls to Watch Out For 308 Bibliography . . . . . . . . . . . . 311 Index . . . . . . . . . . . . . . 313 Contents • x www.it-ebooks.info [...]... FirstOpenGLProject/src/com/firstopenglproject /android/ FirstOpenGLProjectRenderer.java package com.firstopenglproject .android; import import import import static static static static android .opengl. GLES20.GL_COLOR_BUFFER_BIT; android .opengl. GLES20.glClear; android .opengl. GLES20.glClearColor; android .opengl. GLES20.glViewport; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android .opengl. GLSurfaceView.Renderer;... to also visit Learn OpenGL ES, an OpenGL ES tutorial blog that I maintain.4 The following is a list of some great online resources maintained by the Khronos Group:5 • • • • • OpenGL ES 2. 0 API Quick Reference Card6 OpenGL ES 2. 0 Reference Pages7 OpenGL ES Shading Language (GLSL ES) Reference Pages8 The OpenGL ES Shading Language9 OpenGL ES Common Profile Specification Version 2. 0 .25 (Full Specification)10... http://www.learnopengles.com/ http://www.khronos.org/ http://www.khronos.org/opengles/sdk/docs/reference_cards /OpenGL- ES- 2_ 0-Reference-card.pdf http://www.khronos.org/opengles/sdk/docs/man/ http://www.khronos.org/opengles/sdk/docs/manglsl/ http://www.khronos.org/registry/gles/specs /2. 0/GLSL _ES_ Specification_1.0.17.pdf http://www.khronos.org/registry/gles/specs /2. 0 /es_ full_spec _2. 0 .25 .pdf http://www.khronos.org/registry/egl/... Build.MODEL.contains( "Android SDK built for x86"))); This code tests if the current device is an emulator build, and if it is, we assume that it supports OpenGL ES 2. 0 For this to actually work, we need to be sure that we’re running on an emulator image that has been configured for OpenGL ES 2. 0, as described in Configuring a New Emulator, on page 2 Configuring the Surface for OpenGL ES 2. 0 The next step... glClear(GL_COLOR_BUFFER_BIT); This makes a big difference when a significant amount of our code is working with OpenGL and other utilities Unfortunately, Eclipse doesn’t have great support for static imports To make things easier, I recommend that you select Window→Preferences and then select Java→Editor→Content Assist→Favorites and add the following types: • android .opengl. GLES20 • android .opengl. GLUtils • android .opengl. Matrix... well 2 3 http://developer .android. com/training/basics/firstapp/index.html http://developer .android. com/training/basics/activity-lifecycle/index.html www.it-ebooks.info report erratum • discuss Welcome to OpenGL ES for Android! • xviii Online Resources All of the resources for this book can be found at http://pragprog.com/book/kbogla, where you can find code samples and accompanying images and textures... bells and whistles we can ask for; and finally, the very young and not-yet-widely-available version 3.0, which adds new features on top of the 2. 0 standard While OpenGL ES has been in use for almost a decade, hands-on material for beginners is hard to come by, especially for version 2. 0, which introduced shaders, an esoteric topic for newcomers In addition to device- and platformspecific issues, this lack... doesn’t support OpenGL ES 2. 0? It’s possible to add a fallback renderer that supports OpenGL ES 1.0, but this situation is so rare these days that it may not be worth the effort According to the Android Developer Dashboard,7 only around 9 percent of devices are GL 1.1 only, and this number should keep shrinking as time goes on In Updating the Android Manifest and Excluding from Unsupported Devices,... ‘com.firstopenglproject .android package Press Alt - Shift - R to rename this to ‘com.airhockey .android 8 Open AndroidManifest.xml and change the package to ‘com.airhockey .android Also change android: name for the activity to ‘com.airhockey .android. AirHockeyActivity’ www.it-ebooks.info report erratum • discuss Chapter 2 Defining Vertices and Shaders • 22 9 Eclipse may have added import com.firstopenglproject .android. R; to the... You can look up the key bindings for your platform by selecting the key assistance from Help→Key Assist It doesn’t look like much at the moment: FirstOpenGLProject/src/com/firstopenglproject /android/ FirstOpenGLProjectActivity.java package com.firstopenglproject .android; import android. os.Bundle; import android. app.Activity; import android. view.Menu; public class FirstOpenGLProjectActivity extends Activity . Hockey? 1 92. 1 2. 2 Don’t Start from Scratch 21 2. 3 Defining the Structure of Our Air Hockey Table 22 2. 4 Making the Data Accessible to OpenGL 26 2. 5 Introducing the OpenGL Pipeline 28 2. 6 The OpenGL. Card 6 • OpenGL ES 2. 0 Reference Pages 7 • OpenGL ES Shading Language (GLSL ES) Reference Pages 8 • The OpenGL ES Shading Language 9 • OpenGL ES Common Profile Specification Version 2. 0 .25 (Full. Objects 23 4 12. 3 Loading in the Height Map 23 7 12. 4 Drawing the Height Map 24 1 12. 5 Occluding Hidden Objects 24 5 12. 6 A Review 25 0 12. 7 Exercises 25 1 13. Lighting Up the World . . . . . . . . . . 25 3 Simulating

Ngày đăng: 05/05/2014, 15:35

TỪ KHÓA LIÊN QUAN