Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 482 trang
THÔNG TIN TÀI LIỆU
Cấu trúc
Beginning Android 3D
Game Development
Contents at a Glance
Contents
About the Author
About the Technical
Reviewer
Acknowledgments
Introduction
Chapter 1: Let’s Meet the Android
Overview of Android
Overview of the Android SDK
Android Software Development Kit (SDK) Requirements
Android SDK Components Overview
Eclipse with Android Development Tools Plug-in
Android SDK Manager
Android Virtual Device
How to Set Up for Development
Android Development Tools Integrated Development Environment (IDE) Overview
Package Explorer
Source Code Area
Outline
Dalvik Debug Monitor Server (DDMS)
LogCat Window
Launching the SDK Manager and AVD Manager from Eclipse
Hands-on Example: Non–OpenGL ES Text “Hello World” Program
Creating a New Android Project
Running on an Android Emulator
Running on an Actual Android Device
The Main Source Code
The Graphical Layout
The Actual “Hello World” Data
Summary
Chapter 2: Java for Android
Overview of the Java Language
Java Comments
Java Basic Data Types
Arrays
Data Modifiers
Java Operators
Arithmetic Operators
Unary Operators
Conditional Operators
Bitwise and Bit Shift Operators
Java Flow Control Statements
Java Classes
Packages and Classes
Accessing Classes in Packages
Java Interfaces
Accessing Class Variables and Functions
Java Functions
Calling the Parent Function
The Basic Android Java Program Framework
Android Activity Life Cycle Overview
Key Activity Life Cycle Cases
Seeing the Activity Life Cycle in Action
The Basic Android Java OpenGL Framework
Basic Android OpenGL ES Framework for a Single-View OpenGL ES Application
The Custom GLSurfaceView
The Custom Renderer
Basic Android OpenGL ES Framework for a Multiple View OpenGL ES Application
The XML Layout File
The Activity Class and GLSurfaceView Class
Hands-on Example: A 3D OpenGL “Hello Droid” Example
Importing Project Examples into Eclipse
The MainActivity and MyGLSurfaceView Classes
The MyGLRenderer Class
Class Overview
Experimenting with “Hello Droid”
Summary
Chapter 3: 3D Math Review
Vectors and Vector Operations
What Is a Vector ?
Vector Representing Position
Vector Representing Direction
Vector Representing Rotation Axis
Vector Representing Force
Vectors Representing Local Axes
Our Vector Class
The Vector Magnitude
Vector Normalization
Vector Addition
Vector Multiplication
Vector Negation
The Right Triangle
Vector Dot Product
Vector Cross Product
Matrices and Matrix Operations
What Is a Matrix ?
Built-in Android Matrix Class
The Identity Matrix
Matrix Transpose
Matrix Multiplication
Matrix Inverse
Homogeneous Coordinates
Using Matrices to Move Objects
Using Matrices to Rotate Objects
Using Matrices to Scale Objects
Combining Matrices
Hands-on Example: Manipulating Objects in 3D Space
Building a 3D Object’s Model Matrix
Adding a Rotation to an Object
Moving an Object in 3D Space
Scaling an Object
Summary
Chapter 4: 3D Graphics Using OpenGL ES 2.0
Overview of OpenGL ES 2.0 on Android
General Overview of OpenGL Object Rendering
Specific Overview of the Rendering Procedure
Transforming the 3D Object’s Vertices
Building the Model Matrix
Building the View Matrix
Building the Projection Matrix
Setting the Viewport
Sending the Matrices and Lighting Information to the Vertex and Fragment Shaders
Rendering the Scene
Overview of the OpenGL ES 2.0 Shading Language
Basic Data Types
Vector Components
Operators and Expressions
Program Flow Control Statements
Storage Qualifiers
Reserved Variables
Built-in Functions
Overview of Vertex Shaders
A Complex Vertex Shader
Overview of Fragment or Pixel Shaders
Overview of the Shader Class
The Camera
The 3D Object Mesh
Mesh Vertex Data
The MeshEx Class
MeshEx Class Overview
MeshEx Class Constructor
MeshEx Class Error Debug Function
MeshEx Class Mesh Draw Function
Lighting
Overview of Lighting
The PointLight Class
Building the Normal Matrix
Lighting in the Vertex Shader
Ambient
Diffuse
Specular
Lighting in the Fragment Shader
Ambient Lighting
Diffuse Lighting
Specular Lighting
Final Fragment Color
Materials
The Material Class
Materials in the Fragment Shader
Textures
Texture Magnification and Minification
Texture Clamping and Repeating
The Texture Class
Textures in the Vertex Shader
Textures in the Fragment Shader
Summary
Chapter 5: Motion and Collision
Overview of Motion
Linear Velocity and Linear Acceleration
Newton’s Laws of Motion
Gravity
Angular Velocity and Angular Acceleration
Rotational Forces
The Physics Class
Hands-on Example: Linear Motion and Angular Motion Using Forces
Creating a Four-Sided Textured Cube
Modifying the Object3d Class
Modifying the MyGLRenderer Class
Overview of Collisions
Collision Detection
Modifying the MeshEx Class
Modifying the Object3d Class
Types of Collisions
Modifying the Physics Class
Calculating Collisions
Modifying the Physics Class
Hands-on Example: Collisions
Modifying the MyGLRenderer Class
Newton’s Law of Gravity
Drone Grid Case Study: Creating a Gravity Grid Using a Vertex Shader
Modifying the Physics Class
Modifying the MeshEx Class
The GravityGridEx Class
Creating the New Vertex Shader
Main() Function of Shader
Modifying the MyGLRenderer Class
Summary
Chapter 6: Game Environment
Overview of Sounds on Android
The Sound Class
Modifying the Object3d Class
Hands-on Example: Sounds
Modifying the MyGLRenderer Class
Overview of a Heads-Up Display
Overview of Our HUD
Creating the BillBoard Class
Creating the BillBoardFont Class
Modifying the Texture Class
Creating the BillBoardCharacterSet Class
Creating the HUDItem Class
Creating the HUD Class
Modifying the Object3d Class
Drone Grid Case Study: Creating the HUD
Modifying the MyGLRenderer Class
Overview of Persistent Data
Modifying the Orientation Class
Modifying the Physics Class
Modifying the Object3d Class
Hands-on Example: Saving Persistent Data
Modifying the MyGLRenderer Class
Modifying the MyGLSurfaceView Class
Modifying the MainActivity Class
Summary
Chapter 7: Drone Grid Case Study: Creating the Player
Creating the Player Graphic
Creating the Mesh Class
Modifying the Object3d Class
Modifying Other Classes That Use the Object3d Class
Creating the Pyramid Class
Creating the PowerPyramid Class
Creating the Player’s Viewpoint and Input
Modifying the MyGLRenderer Class
Modifying the MyGLSurfaceView Class
Creating Player Weapons and Ammunition
Creating the Explosions
Creating the PolyParticleEx Class
Creating the SphericalPolygonExplosion Class
Modifying the Object3d Class
Creating Game Object Statistics
Creating the Stats Class
Modifying the Object3d Class
Hands-on Example: Target Shooting!
Creating the Player
Creating the Player’s Weapon
Processing Collisions
Modifying the onDrawFrame() Function
Summary
Chapter 8: Drone Grid Case Study: Creating the Enemies
Creating Arena Objects
Overview of Artificial Intelligence
Creating the Tank Enemy
Creating the Tank Graphic
Creating the Tank State
Creating Vehicle Commands
Creating the Tank State to Process Commands
Creating the Vehicle Steering Class
Creating the Tank’s Patrol/Attack State
Creating the Tank Finite State Machine
Creating the Driver for the Tank
Modifying the Physics Class
Modifying the Object3d Class
Creating the Tank Class
Hands-on Example: Arena Objects and Tanks
Modifying the MyGLRenderer Class
Summary
Chapter 9: Drone Grid Case Study: The User Interface
Creating the Main Menu System
The MenuItem Class
The MainMenu Class
Creating the High Score Table
The HighScoreEntry Class
The HighScoreTable Class
Creating the High Score Entry System
Hands-on Example: Demonstrating the User Interface
Modifying the MyGLRenderer Class
Summary
Chapter 10: The Final Drone Grid Game
Organizing and Controlling Groups of Enemies
The ArenaObjectSet Class
The TankFleet Class
The GamePlayController Class
Saving and Loading the Game State
Modifying the MainActivity Class
Modifying the MyGLRenderer Class
Adding in the Game Over Game State
Hands-on Example: The Drone Grid Game
Modifying the MyGLRenderer Class
Summary
Chapter 11: The Android Native Development Kit (NDK)
NDK Overview
NDK System Requirements
Android Platform Compatibility
Installing the Android NDK
Ways to Use the Android NDK
Java Native Interface Overview
The Java Interface Pointer
Loading and Linking Native C/C++ Methods
Naming Native Functions
Native Function Parameters
C vs. C++ Native Function Formats
Native Types
Reference Types
JNI Signature Types
Calling Native Code from Java and Accessing Java Methods from Native Code
JNI Functions
Android JNI Makefile
Hands-on Example: “Hello World from JNI and Native Code ”
Modifying the MyGLRenderer Class
Hands-on Example: Adding Native Functions to the Drone Grid Game Case Study
Calculating Gravity in Native Code
Modifying the hello-jni.c File
Modifying the Physics Class
Rotating Objects from Native Code
Modifying the hello-jni.c File
Modifying the Physics Class
Modifying the MyGLRenderer Class
Calculating the Reaction Force for a Collision from Native Code
Modifying the hello-jni.c File
Modifying the Physics Class
Summary
Chapter 12: Publishing and Marketing Your Final Game
Creating the Final Distribution File
Testing the Distribution .apk File
List of Android Marketplaces and Policies
Google Play
Amazon Appstore for Android
Samsung Apps Store
Aptoide
Appitalism
GetJar
SlideMe
Soc.Io Mall
Your Own WebSite.Com
List of Android Ad Networks
AppFlood
Appwiz
LeadBolt
AppBucks
MobileCore
AdMob
StartApp
Other Ad Network and Marketing-Related Companies
List of Android Game Review Web Sites
List of Other Helpful Sites for Android Developers
Summary
Index
Nội dung
BeginningAndroid3DGameDevelopment COMPANION eBOOK Shelve in Mobile Computing User level: Beginning–Intermediate www.apress.com BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® B eginning Android3DGameDevelopment is a unique, examples-driven book for today’s Android and game app developers who want to learn how to build 3Dgame apps that run on the latest Android 5.0 (KitKat) platform using Java and OpenGL ES. After reading and using this book, you’ll be able to build your first 3DAndroidgame app for smartphones and tablets. This book offers an extensive 3Dgame app case study called Drone Grid with code that is modular and reusable to help you create your own games using advanced vertex and fragment shaders. Drone Grid is somewhat similar to the best-selling Geometry Wars game series, utilizing a gravity grid and colorful abstract graphics and particles. You’ll learn: • How to build 3Dgame apps using Android SDK, NDK, and OpenGL ES • Which Android SDK and NDK APIs are most useful for 3Dgamedevelopment • How to get a 3D Math Review for gamedevelopment • How to build 3D graphics using OpenGL ES • How to create various motion patterns • How to set up a game environment, including gravity grid, sounds, and heads-up display • How to create the characters, including the player and its enemies or targets • How to develop a rich menu system with high score table Robert Chin Companion eBook Available Chin BeginningAndroid3DGameDevelopment SOURCE CODE ONLINE Beginning 9781430 265474 54999 ISBN 978-1-4302-6547-4 For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. iii Contents at a Glance About the Author �������������������������������������������������������������������������������������������������������������� xvii About the Technical Reviewer ������������������������������������������������������������������������������������������� xix Acknowledgments �������������������������������������������������������������������������������������������������������������xxi Introduction ��������������������������������������������������������������������������������������������������������������������� xxiii Chapter 1: Let’s Meet the Android ■ �������������������������������������������������������������������������������������1 Chapter 2: Java for Android ■ ��������������������������������������������������������������������������������������������29 Chapter 3: 3D Math Review ■ ���������������������������������������������������������������������������������������������57 Chapter 4: 3D Graphics Using OpenGL ES 2�0 ■ ������������������������������������������������������������������81 Chapter 5: Motion and Collision ■ ������������������������������������������������������������������������������������135 Chapter 6: Game Environment ■ ���������������������������������������������������������������������������������������183 Chapter 7: Drone Grid Case Study: Creating the Player ■ �������������������������������������������������221 Chapter 8: Drone Grid Case Study: Creating the Enemies ■ ���������������������������������������������267 Chapter 9: Drone Grid Case Study: The User Interface ■ ��������������������������������������������������321 Chapter 10: The Final Drone Grid Game ■ �������������������������������������������������������������������������353 Chapter 11: The Android Native Development Kit (NDK) ■ �����������������������������������������������393 Chapter 12: Publishing and Marketing Your Final Game ■ �����������������������������������������������419 Index ���������������������������������������������������������������������������������������������������������������������������������453 xxiii Introduction This book is meant to be a quick-start guide to developing 3D games for the Android platform using Java and OpenGL ES 2.0. Development will utilize the Eclipse Integrated Development Environment (IDE) with AndroidDevelopment Tools (ADT) plug-ins installed. The goal is to cover key concepts and illustrate them, using concrete hands-on examples and case studies. A single book cannot cover every aspect of Androidgamedevelopment or Android software development in general. Thus, this book is not meant as a reference guide. The following is a summary of each chapter in this book. Chapter 1: “Let’s Meet the Android.” In this chapter, I provide an overview of Android, an overview of the Android SDK, instructions on how to set up your computer for Android development, and a hands-on example involving a simple “Hello World” program for those unfamiliar with Android. Chapter 2: “Java for Android.” In this chapter, I offer an overview of the Java language, the basic Android Java program framework, and information on the basic Java OpenGL ES framework. Chapter 3: “3D Math Review.” In this chapter, 3D math, vectors, matrices, and vector and matrix operations are discussed. Chapter 4: “3D Graphics Using OpenGL ES 2.0.” In this chapter, I provide an overview of OpenGL ES 2.0 on Android, 3D meshes, lighting, materials, textures, saving persistent data, and creating a gravity grid using vertex and fragment shaders. Chapter 5: “Motion and Collision.” In this chapter, collision and Newtonian mechanics are covered. Chapter 6: “Game Environment.” In this chapter, sounds and the heads-up display are discussed. xxiv Introduction Chapter 7: “Drone Grid Case Study: Creating the Player.” In this chapter, I explain how to create a player, including elements associated with a player within our Drone Grid game, such as weapons, ammunition and player’s HUD. Chapter 8: “Drone Grid Case Study: Creating the Enemies.” This chapter details how to create the enemies in our Drone Grid game. The enemies are arena objects and tanks. Arena objects are fairly simple in their behavior. Tanks are more complex enemy objects that will require the use of complex artificial intelligence, which I also cover. Chapter 9: “Drone Grid Case Study: The User Interface.” User interfaces for our Drone Grid game are discussed in this chapter, including the Main Menu System, the creation of the high score table, and the high score entry menu. Chapter 10: “The Final Drone Grid Game.” This chapter brings together everything from previous chapters into the final Drone Grid game. A final complete working game that integrates all the elements from previous chapters is presented. The final game will use elements discussed previously such as menus, heads up display, and enemy objects such as arena objects and tanks. Chapter 11: “The Android Native Development Kit (NDK).” This chapter covers the Android Native Development Kit and discusses the Java Native Interface (JNI) in addition. Chapter 12: “Publishing and Marketing Your Final Game.” This chapter discusses how to publish and market your final Android game. It includes a list of Android marketplaces from which you can upload your game distribution file, a list of numerous ad networks that support Android, and a list of game sites that review Android games. 1 Chapter 1 Let’s Meet the AndroidAndroid mobile phones dominate the mobile smartphone market, surpassing even Apple’s iPhone. There are hundreds of millions of mobile phones using the Android operating system in over 190 countries around the world. Every day, a million new users begin using their Android phones to surf the Web, to e-mail friends, and to download apps and games. In fact, in the Google Play Store alone, there are 1.5 billion downloads per month of Android games and applications. If you include other web sites that offer Android games and apps for sale, such as Amazon Appstore for Android, then the number is even higher. In this chapter, you will learn about the Android Software Development Kit (SDK). You will learn how to set up the Androiddevelopment environment. You will also learn about the major components of this environment, such as Eclipse. We then go through the creation and deployment of a simple “Hello World” program for Android, to both a virtual Android emulator program and also a real Android device. Overview of Android The Android operating system is a widely used operating system available on mobile phones and tablets. It is even used on a video game console called the Ouya. Android phones range from expensive phones that require a contract to inexpensive prepaid phones that do not require any contract. Developing programs for the Android platform does not require any developer’s fees, unlike Apple mobile devices, which require yearly fees in order to even be able to run your program on their devices. A good working prepaid no-contract Android phone that can develop 3D games using OpenGL ES 2.0 can be bought on Amazon.com for as little as $75–$100 with free shipping. Overview of the Android SDK This section discusses the Android SDK. Development system requirements and important individual pieces of the SDK, such as the SDK Manager, Android Virtual Device Manager, and the actual Android emulator will be covered. 2 CHAPTER 1: Let’s Meet the AndroidAndroid Software Development Kit (SDK) Requirements Androiddevelopment can be done on a Windows PC, Mac OS machine, or a Linux machine. The exact operating system requirements are as follows: Operating Systems: Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit) Mac OS X 10.5.8 or later (x86 only) Linux (tested on Ubuntu Linux, Lucid Lynx) GNU C Library (glibc) 2.7 or later is required. On Ubuntu Linux, version 8.04 or later is required. 64-bit distributions must be capable of running 32-bit applications. Developing Android programs also requires installation of the Java Development Kit. Java Development Kit requirements are JDK 6 or later and are located at www.oracle.com/technetwork/java/javase/ downloads/index.html. If you are using a Mac, then Java may already be installed. The Eclipse IDE program modified with the AndroidDevelopment Tools (ADT) plug-in forms the basis for the Androiddevelopment environment. The requirements for Eclipse are as follows: Eclipse 3.6.2 (Helios) or greater located at http://eclipse.org Eclipse JDT plug-in (included in most Eclipse IDE packages) AndroidDevelopment Tools (ADT) plug-in for Eclipse located at http://developer.android.com/tools/sdk/eclipse-adt.html Notes Eclipse 3.5 (Galileo) is no longer supported with the latest version of ADT. For the latest information on Androiddevelopment tools, go to http://developer.android.com/tools/index.html. Android SDK Components Overview The different components of the Android SDK are the Eclipse program, the Android SDK Manager, and the Android Virtual Device Manager and emulator. Let’s look at each in more detail. Eclipse with AndroidDevelopment Tools Plug-in The actual part of the Android SDK that you will spend most of your time dealing with is a program called Eclipse, which is customized specifically for use with Android through the ADT software plug-in. You will enter new code, create new classes, run programs on the Android emulator and on real devices from this program. On older, less capable computers, the emulator may run so slowly 3CHAPTER 1: Let’s Meet the Android that the best option would be running the program on an actual Android device. Because we are dealing with CPU-intensive 3D games in this book, you should use an actual Android device to run the example projects (see Figure 1-1). Figure 1-1. Eclipse with AndroidDevelopment Tools plug-ins Android SDK Manager The Android SDK Manager allows you to download new Android platform versions and tools through its interface. Current tools and platform versions that are installed are also displayed. For example, in Figure 1-2, the Android 2.2 platform has already been installed and is ready for use for development. This means that you can compile your source code to target this platform. 4 CHAPTER 1: Let’s Meet the AndroidAndroid Virtual Device The Android SDK also supports a virtual device emulator (see Figure 1-3). In many instances, you will be able to run your Android programs on a software emulator on your development system rather than an actual device. However, this works best for non-graphic intensive applications. Because this book deals with 3D games, we will not be using this software emulator but an actual Android device. The Android Virtual Device Manager allows you to create new virtual Android devices, edit existing Android devices, delete existing devices, and start up an existing virtual Android device. Figure 1-3 indicates that there is a valid virtual Android device named “Android22,” which emulates the 2.2 version of the Android operating system (API Level 8) and simulates the ARM CPU type. The 2.2 version of the Android operating system is important because it is the first version that supports OpenGL ES 2.0, which we will be using in this book to develop our 3D graphics. OpenGL is the graphics system that allows the programmer to create 3D graphics on the Android platform. It is designed to be hardware-independent. That is, OpenGL graphics commands are designed to be the same across many different hardware platforms, such as PC, Mac, Android, etc. The OpenGL 2.0 version of OpenGL is the first version of OpenGL that includes programmable vertex and fragment shaders. OpenGL ES is a subset of regular OpenGL and contains fewer features. Figure 1-2. The Android SDK Manager 5CHAPTER 1: Let’s Meet the Android Figure 1-4 depicts the actual emulator after it is launched. The emulator depicted is the one for version 2.2 of the Android operating system. Figure 1-3. The Android Virtual Device Manager [...]... . PROFESSIONALS ® B eginning Android 3D Game Development is a unique, examples-driven book for today’s Android and game app developers who want to learn how to build 3D game apps that run on the latest Android 5.0. useful for 3D game development • How to get a 3D Math Review for game development • How to build 3D graphics using OpenGL ES • How to create various motion patterns • How to set up a game environment,. Beginning Android 3D Game Development COMPANION eBOOK Shelve in Mobile Computing User level: Beginning Intermediate www.apress.com BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® B eginning Android