
Panda3D 1.7 Game Developer's Cookbook
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Who this book is for
All prices
More details
Other editions
Additional editions

Content
- Intro
- Panda3D 1.7 Game Developer's Cookbook
- Table of Contents
- Panda3D 1.7 Game Developer's Cookbook
- Credits
- About the Author
- About the Reviewers
- www.PacktPub.com
- Support files, eBooks, discount offers, and more
- Why Subscribe?
- Free Access for Packt account holders
- Preface
- What this book covers
- What you need for this book
- Who this book is for
- Conventions
- Reader feedback
- Customer support
- Downloading the example code
- Errata
- Piracy
- Questions
- 1. Setting Up Panda3D and Configuring Development Tools
- Introduction
- Downloading and configuring NetBeans to work with Panda3D
- Getting ready
- How to do it...
- How it works...
- There's more...
- Configuring Visual Studio 2008 to work with Panda3D
- Getting ready
- How to do it...
- How it works...
- There's more...
- Understanding Panda3D's runtime configuration options
- How to do it...
- How it works...
- There's more...
- Listing all configuration variables
- Loading a specific configuration file
- Embedding configuration data in Python code
- Setting up the game structure
- Getting ready
- How to do it...
- How it works...
- Building Panda3D from source code
- Getting ready
- How to do it...
- How it works...
- There's more...
- 2. Creating and Building Scenes
- Introduction
- Getting ready
- How to do it...
- How it works...
- Loading terrain
- Getting ready
- How to do it...
- How it works...
- There's more...
- Block size
- Near and far thresholds
- Loading and attaching sounds to objects
- Getting ready
- How to do it...
- How it works...
- There's more...
- Creating a scene using C++
- Getting ready
- How to do it...
- How it works...
- Adding an additional camera
- Getting ready
- How to do it...
- How it works...
- Inspecting and modifying the scene
- Getting ready
- How to do it...
- How it works...
- Modifying the scene graph
- Getting ready
- How to do it...
- How it works...
- There's more...
- Position
- Rotation
- Scale
- Moving objects based on time
- Getting ready
- How to do it...
- How it works...
- There's more...
- Controlling actions using intervals
- Getting ready
- How to do it...
- How it works...
- There's more...
- Lerp intervals
- Lerp function interval
- Interpolation easing
- Sequences and Parallels
- Wait interval
- Making animations fit to intervals
- Getting ready
- How to do it...
- How it works...
- There's more...
- Making objects follow a predefined path
- Getting ready
- How to do it...
- How it works...
- Making the camera smoothly follow an object
- Getting ready
- How to do it...
- How it works...
- There's more...
- Generating geometry at runtime
- Getting ready
- How to do it...
- How it works...
- There's more...
- Built in vertex formats
- Custom vertex formats
- Primitive types
- See also
- Loading data asynchronously
- Getting ready
- How to do it...
- How it works...
- 3. Controlling the Renderer
- Introduction
- Changing a model's render attributes
- Getting ready
- How to do it...
- How it works...
- Adding an alpha mask to a texture
- Getting ready
- How to do it...
- How it works...
- Creating a splitscreen mode
- Getting ready
- How to do it...
- How it works...
- Controlling the rendering order
- Getting ready
- How to do it...
- How it works...
- There's more...
- Cull bin types
- Default cull bins
- Adding a cull bin at runtime
- Adding a cull bin using the configuration file
- Using multiple displays
- Getting ready
- How to do it...
- How it works...
- 4. Scene Effects and Shaders
- Introduction
- Adding lights and shadows
- Getting ready
- How to do it...
- How it works...
- There's more...
- Using light ramps
- Getting ready
- How to do it...
- How it works...
- Creating particle effects
- Getting ready
- How to do it...
- How it works...
- There's more...
- Particle Factories
- Particle Renderers
- Particle Emitters
- Animating textures
- Getting ready
- How to do it...
- How it works...
- Adding ribbon trails to an object
- Getting ready
- How to do it...
- How it works...
- There's more
- Creating a flashlight effect
- Getting ready
- How to do it...
- How it works...
- Making objects reflect the scene
- Getting ready
- How to do it...
- How it works...
- Adding a custom shader generator
- Getting ready
- How to do it...
- How it works...
- There's more...
- Applying a custom Cg shader
- Getting ready
- How to do it...
- How it works...
- 5. Post-Processing and Screen Space Effects
- Introduction
- Adding built-in post-processing effects
- Getting ready
- How to do it...
- How it works...
- There's more...
- Building custom effects
- Getting ready
- How to do it...
- How it works...
- There's more...
- Adding a scanline and vignette effect
- Getting ready
- How to do it...
- How it works...
- Adding a color grading effect
- Getting ready
- How to do it...
- How it works...
- Adding a depth of field effect
- Getting ready
- How to do it...
- How it works...
- Building a deferred rendering pipeline
- Getting ready
- How to do it...
- How it works...
- There's more...
- 6. 2D Elements and User Interfaces
- Introduction
- Rendering text to the screen
- Getting ready
- How to do it...
- How it works...
- Rendering images to the 2D layer
- Getting ready
- How to do it...
- How it works...
- Playing a movie file
- Getting ready
- How to do it...
- How it works...
- Creating an interactive user interface
- Getting ready
- How to do it...
- How it works...
- There's more...
- More controls
- More parameters
- Making the user interface data-driven using XML
- Getting ready
- How to do it...
- How it works...
- There's more...
- 7. Application Control
- Introduction
- Toggling window and fullscreen modes
- Getting ready
- How to do it...
- How it works...
- Controlling game state
- Getting ready
- How to do it...
- How it works...
- Decoupling modules using events
- Getting ready
- How to do it...
- How it works...
- Handling events more elegantly
- Getting ready
- How to do it...
- How it works...
- There's more...
- Managing recurring tasks
- Getting ready
- How to do it...
- How it works...
- 8. Collision Detection and Physics
- Introduction
- Using the built-in collision detection system
- Getting ready
- How to do it...
- How it works...
- There's more...
- Collision Shapes
- Collision Handlers
- Using the built-in physics system
- Getting ready
- How to do it...
- How it works...
- Using the ODE physics engine
- Getting ready
- How to do it...
- How it works...
- Using the PhysX physics engine
- Getting ready
- How to do it...
- How it works...
- Integrating the Bullet physics engine
- Getting ready
- How to do it...
- How it works...
- 9. Networking
- Introduction
- Downloading a file from a server
- Getting ready
- How to do it...
- How it works...
- Using assets hosted on a server
- Getting ready
- How to do it...
- How it works...
- Sending high scores to a server
- Getting ready
- How to do it...
- How it works...
- Establishing a network connection
- Getting ready
- How to do it...
- How it works...
- Sending and receiving custom datagrams
- Getting ready
- How to do it...
- How it works...
- Synchronizing object state between server and client
- Getting ready
- How to do it...
- How it works...
- 10. Debugging and Performance
- Introduction
- Debugging Python code
- Getting ready
- How to do it...
- How it works...
- Debugging C++ code
- Getting ready
- How to do it...
- How it works...
- Using the PStats tool for finding performance bottlenecks
- Getting ready
- How to do it...
- How it works...
- Improving performance by flattening scenes
- Getting ready
- How to do it...
- How it works...
- Implementing performance critical code in C++
- Getting ready
- How to do it...
- How it works...
- 11. Input Handling
- Introduction
- Handling keyboard and mouse input
- Getting ready
- How to do it...
- How it works...
- There's more...
- Implementing an abstraction layer for supporting multiple input methods
- Getting ready
- How to do it...
- How it works...
- Handling input from an Xbox 360 controller
- Getting ready
- How to do it...
- How it works...
- Recording and simulating user input
- Getting ready
- How to do it...
- How it works...
- Reading audio data from a microphone
- Getting ready
- How to do it...
- How it works...
- Reading video data from a webcam
- Getting ready
- How to do it...
- How it works...
- There's more...
- Reading input data from a network
- Getting ready
- How to do it...
- How it works...
- 12. Packaging and Distribution
- Introduction
- Packing assets into multifiles
- Getting ready
- How to do it...
- How it works...
- There's more.
- Updating a subfile
- Extracting a subfile
- Encrypting subfiles
- Creating multifiles on the command line
- Creating a redistributable game package
- Getting ready
- How to do it...
- How it works...
- Advanced package creation and hosting
- Getting ready
- How to do it...
- How it works...
- There's more...
- Working with modules
- Creating patches
- Embedding a game into a website
- Getting ready
- How to do it...
- How it works...
- Using website and plugin interoperability
- Getting ready
- How to do it...
- How it works...
- 13. Connecting Panda3D with Content Creation Tools
- Introduction
- Setting up the Blender export plugin
- Getting ready
- How to do it...
- How it works...
- Exporting models from Blender
- Getting ready
- How to do it...
- How it works...
- Generating model files programmatically
- Getting ready
- How to do it...
- How it works...
- Using the "Pview" tool to preview models
- How to do it...
- How it works...
- Compressing and converting model files using pzip and egg2bam
- Getting ready
- How to do it...
- How it works...
- Index
Generating geometry at runtime
In some cases, Panda3D's model loading capabilities might not be enough for your needs. Maybe you want to procedurally generate new geometry at runtime or maybe you decided to drop the native file model file format of Panda3D in favor of your own custom data file layout. For all these cases where you need to glue together vertices by hand in order to form a model, the engine provides an API that you will learn about in this recipe.
Getting ready
As a prerequisite to the following steps, please create a new project as described in the recipe Setting up the game structure. This recipe can be found in the Chapter 1.
You will also need a texture image. Preferably it should be rectangular and in the best case be in a 2n format (64x64, 128x128, 256x256, and so on.). This recipe will use a crate texture in PNG format.
Lastly, add a directory called textures to your project and be sure it is in Panda3D's search path. This works analogous to what you did for the models and sounds directories.
How to do it...
Follow these steps to learn how to create geometry on the fly:
- Copy your texture image to the
texturesdirectory. Name itcrate.png. - Open
Application.py. Insert the following code: from direct.showbase.ShowBase import ShowBase from panda3d.core import * vertices = [Vec3(1, 0, 1), Vec3(-1, 0, 1), Vec3(-1, 0, -1), Vec3(1, 0, -1)] texcoords = [Vec2(1, 1), Vec2(0, 1), Vec2(0, 0), Vec2(1, 0)] class Application(ShowBase): def __init__(self): ShowBase.__init__(self) format = GeomVertexFormat.getV3t2() geomData = GeomVertexData("box", format, Geom.UHStatic) vertexWriter = GeomVertexWriter(geomData, "vertex") uvWriter = GeomVertexWriter(geomData, "texcoord") for pos, tex in zip(vertices, texcoords): vertexWriter.addData3f(pos) uvWriter.addData2f(tex) triangles = GeomTriangles(Geom.UHStatic) triangles.addVertices(0, 1, 2) triangles.closePrimitive() triangles.addVertices(2, 3, 0) triangles.closePrimitive() geom = Geom(geomData) geom.addPrimitive(triangles) node = GeomNode("box") node.addGeom(geom) box = render.attachNewNode(node) texture = loader.loadTexture("crate.png") box.setTexture(texture) self.cam.setPos(0, -5, 0) - Start the program. A quad with your texture on it should be rendered to the Panda3D window:
How it works...
Let's take a look at what this code is doing! We begin by creating a format descriptor for one of Panda3D's built in vertex data layouts. There are several of these layouts, which also can be completely customized, which describe what kind of data will be stored for each point in space that forms the mesh. In this particular case, we are using the getV3t2() method to get a descriptor for a vertex layout that stores the vertex position in space using three floating point values and the texture coordinate using two float values.
We then move on to create a GeomVertexData object, which uses the format we just requested. We also pass the Geom.UHStatic flag, which signals the underlying rendering systems that the vertex data will not change, which allows them to enable some optimizations. Additionally, we create two GeomVertexWriter objects-one for writing to the "vertex" channel, which stores the positions of the points that form the mesh, and the other one for writing to the "texcoord" channel of the point data we are adding to geomData in the loop that follows.
What we have so far is a cloud of seemingly random points-at least to the engine. To correct this issue, we need to connect the points to form primitives, which in this case are triangles. We create a new instance of GeomTriangles, using the Geom.UHStatic flag again to hint that the primitives will not be changed after they are defined. Then we create two triangles by passing indices to the proper points in the vertices list. After each triangle, we need to call the closePrimitive() method to mark the primitive as complete and start a new one.
At this point we have a collection of points in space, stored in a GeomVertexData object and a GeomTriangles primitive that holds the information necessary to connect the dots and form a mesh. To get the model to the screen, we need to create a new Geom, and add the point data and the triangle primitives. Because a model can in fact consist of multiple Geom objects, which also can't be added directly to the scene graph, we add it to a GeomNode. Finally, we attach the GeomNode to the scene graph, load and apply the texture and set the camera a bit back to be able to see our creation.
There's more...
There's a lot more to say about Panda3D's procedural geometry feature than what you just saw, so take your time and keep on reading to discover what else you can do to generate geometry at runtime.
Built in vertex formats
You already saw the built in GeomVertexFormat.getV3t2() format, but there are several more ready to use formats available:
getV3(): Vertices store position onlygetV3c4(): Vertex position and a RGBA color valuegetV3c4t2(): Position, color, and texture coordinatesgetV3n3(): Position and normal vectorgetV3n3c4(): Position, normal, and RGBA colorgetV3n3c4t2(): This is the most extensive format. Contains position, normal, color, and texture coordinatesgetV3n3t2(): Position, normal vector, and texture coordinates.
There's also a packed color format, which you can use by replacing c4 in the previous methods with cp. In this format, colors are stored into one 32-bit integer value. The best way to define color values for this format is in hexadecimal, because it lets you easily recognize the RGBA components of the color. For example, the value 0xFF0000FF is full red.
Custom vertex formats
Apart from the built-in vertex formats, Panda3D allows you to be much more flexible by defining your own custom formats. The key parts to this are the GeomVertexArrayFormat and GeomVertexFormat classes, which are used in the following way:
In the beginning, you need to describe your vertex array data layout by adding columns. The first parameter is the channel that Panda3D is going to use the data for. Very commonly used channels are "vertex", "color", "normal", "texcoord", "tangent", and "binormal".
The second and third parameters are the number of components and data type the channel is using. In this sample, the normal data is composed out of three 32-bit floating point values. Legal values for the third parameter include NTFloat32, NTUint*, where * is one of 8, 16, or 32, describing an unsigned integer of the according bit width as well as NTPackedDcba and NTPackedDabc, used for packed index and color data.
The third parameter influences how the data is going to be treated internally-for example, if and how it will be transformed if a matrix is applied to the data in the column. Possible values include:
CPoint: Point data in 3D space, most often used for the "vertex" channel.CVector: A vector giving a direction in space. Use this for normals, tangents, and binormals.CTexcoord: The data in the column contains the coordinates of texture sample points.CColor: The data is to be treated as color values.CIndex: The column contains table indices.COther: Arbitrary data values.
Points and texture coordinates are transformed as points, resulting in new positions if they are involved in a matrix multiplication. Vectors of course are following different rules when being transformed, because they describe directions, not positions! It would go too far to go into the details here, but lots of material on vector math and linear algebra are freely available on Wikipedia and other websites.
Primitive types
Panda3D supports all the standard primitive types commonly known in computer graphics: Triangles, triangle strips, triangle fans, lines, line strips, and points. The according classes used to describe these primitives are GeomTriangles,...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.