FCG Lab04: Introduction to Panda3Dprojects.i-ctm.eu/sites/default/files/Lab_04_0.pdf · Where? .egg...
Transcript of FCG Lab04: Introduction to Panda3Dprojects.i-ctm.eu/sites/default/files/Lab_04_0.pdf · Where? .egg...
FCG Lab04: Introduction to Panda3D
Michele Svanera
Department of Information EngineeringUniversity of Brescia
02 May 2016
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
OpenGL limitations
Low-level library!
Speaks graphic cards “languages” (translator functionality)
Portable and very fast
System independent
Useful for implementing basic functionality
simple objectstexture mappinglight sourcelittle animation
. . . and for something more complex?
. . . Animation? Interaction between objects?
Michele Svanera FCG lab04: Introduction to Panda3D 3
Panda3D: a game engine I
What is it?
Open-source (since 2002) game engine
Library of subroutines for 3D rendering and game development
Provides:
GraphicsAudioI/OCollision detection. . .
Set of C++ library with Python bindings [5]
Jointly developed by Disney and Carnegie Mellon University
Michele Svanera FCG lab04: Introduction to Panda3D 4
Panda3D: a game engine II
Commercial and Open-source library. So 4 characteristics:
Power easy to implement complex scenes
Speed important frame-rates
Completeness contains tons of essential tools
scene graph browsing
performance monitoring
animation optimizers
Error tolerance all developers create bugs! Much code (it is anSDK) is dedicated to the problem of tracking andisolating errors. Panda3D almost never crashes!
Michele Svanera FCG lab04: Introduction to Panda3D 5
The Use of Models
Usually here we don’t draw stuff
Boring. . .
Tricky to do procedurally
Not trivial to design “realistic” models
Usually ready-made models are imported into panda
Models are drawn with proper modelling tools:
Blender (we will see it the next time), Maya, 3DS MAXHigh-quality
Models can be animated by the engine
. . . Drawing is still possible!
Michele Svanera FCG lab04: Introduction to Panda3D 6
Panda3D books
“Panda3D 1.6 Game Engine Beginner’s Guide”,by Dave Mathews
“Panda3D 1.7 Game Developer’s Cookbook”, byChristoph Lang
Panda3D Manual [14]
Panda3D Forum [13]
Michele Svanera FCG lab04: Introduction to Panda3D 7
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
The Scene Graph I
“Tree of things to render”:
More flexible than lists (as manyother engine uses) and inheritance
An object is not visible (andrendered) until it is inserted
Objects are children of (super)classPandaNode
ModelNode
GeomNode
LightNode
. . .
. . . usually refer as generalNodePath.
Michele Svanera FCG lab04: Introduction to Panda3D 9
The Scene Graph II
Something to know about the tree
The root of the tree is called render
Each node attributes are inherited by the children (ex.position)
Possibility to manipulate the tree, NodePath helper class
pointer to a node (in reality it’s something more... “handle”)
when you invoke a method of NodePath, you are actuallyperforming an operation on the node to which it points
Michele Svanera FCG lab04: Introduction to Panda3D 10
The Scene Graph III
Panda3D generates bounding boxes for each node in the tree
Number of methods dedicated to finding nodes and returningthe NodePaths
Michele Svanera FCG lab04: Introduction to Panda3D 11
The Scene Graph V
Example 1: Solar System
Michele Svanera FCG lab04: Introduction to Panda3D 13
The Scene Graph VI
Example 2: What makes a man?
Michele Svanera FCG lab04: Introduction to Panda3D 14
Dealing with nodes I
Loading models
NodePath np = window ->load_model(framework.
get_models (),
"path/to/models/model.egg"); //ready -made models
can be found under "models /"
Manipulating nodes
np.reparent_to(window ->get_render ()); // attach
this node under root
np.detach_node (); // remove node from tree
np.remove_node (); // delete node: releasing memory
Create empty nodes
NodePath dummyNode = window ->get_render ().
attach_new_node("Dummy_Node_Name");
myModel.reparent_to(dummyNode);
myOtherModel.reparent_to(dummyNode)
Michele Svanera FCG lab04: Introduction to Panda3D 15
Dealing with nodes II
Setting attributes:
np.set_pos(x, y, z);
np.set_hpr(Yaw , Pitch , Roll);
np.set_scale(s); // object ’s size; set_x () _y()
// Store current transform information
np.get_pos ();
// Rotates a model to face another object
np.look_at(otherObject);
// Color: floating point numbers from 0 to 1, 0
being black , 1 being white.
np.set_color(R, G, B, A);
// Temporarily prevent an object from being drawn
np.hide();
np.show();
Notes1
1An object’s rotation is usually described using Euler angles called Heading,Pitch, and Roll (sometimes called Yaw, Pitch, and Roll in otherpackages)–these specify angle rotations in degrees.
Michele Svanera FCG lab04: Introduction to Panda3D 16
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
Task I
Special functions (subroutines) called on each frame:
Similar in concept to threads
All tasks run cooperatively within the main thread
Defined by GenericAsyncTask class
The function receives one parameter: the task object
Each task carries information about itself (ex. amount oftime)
task->get_elapsed_time() //how long task runningtask->get_elapsed_frames() //number of elapsed frames. . .
Each task returns after it has finished processing the currentframe:
AsyncTask::DS_cont = call it again the next frameAsyncTask::DS_done = finished, don’t call it anymoreAsyncTask::DS_again = perform it again, with same delay
Michele Svanera FCG lab04: Introduction to Panda3D 18
Task II
Generic Task
AsyncTask :: DoneStatus your_task(GenericAsyncTask* task
, void* data) {
// Do your stuff here.
// ...
// ...
// Tell the task manager to continue this task the
next frame.
// You can also pass DS_done if this task should not
be run again.
return AsyncTask :: DS_cont;
}
Michele Svanera FCG lab04: Introduction to Panda3D 19
Task III
Task example
#include "asyncTaskManager.h"
// This task runs for two seconds , then prints done
AsyncTask :: DoneStatus example_task(GenericAsyncTask*
task , void* data){
if (task ->get_elapsed_time () < 2.0){
return AsyncTask :: DS_cont;
}
cout << "Done" << endl;
return AsyncTask :: DS_done;
}
Michele Svanera FCG lab04: Introduction to Panda3D 20
The Task Manager
All tasks are handled through the Task Manager object (list)
TM can be initialized by
PT(AsyncTaskManager) taskMgr =
AsyncTaskManager:get_global_ptr ();
Adding a new task can be done by
PT(GenericAsyncTask) task;
task = new GenericAsyncTask("taskName", &
function , (void*)NULL);
taskMgr ->add(task);
Tasks con also be removed by task->remove()
Michele Svanera FCG lab04: Introduction to Panda3D 21
The Task Chain
Each TaskChain is a ordered list of tasks that are available tobe executed
Add tasks to the TaskManager = adding in default Task Chain
TaskManager can maintains one or more task chains
You can create additional task chains as you see the need
Each task chain has the option of run in parallel with others
Normally, no reason to have more than the default task chain(unless threaded tasks)
New task chain
AsyncTaskManager *task_mgr =
AsyncTaskManager :: get_global_ptr ();
AsyncTaskChain *chain = task_mgr ->
make_task_chain("chain_name");
Michele Svanera FCG lab04: Introduction to Panda3D 22
Exercise: Rotating Cube I
Michele Svanera FCG lab04: Introduction to Panda3D 23
Exercise: Rotating Cube II
Michele Svanera FCG lab04: Introduction to Panda3D 24
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
Actors and Models
Two main classes for 3D geometry:
Model for nonanimated geometry (static)
Actor for animated (if it changes shape) geometry(dynamic)
Where? .egg file
Used to store Geometry
May contain Model, Actor, Animation (to be applied to anactor) or both (better to pack in two .egg)
Are created by exporting models from 3D modeling programs:Maya, Max, or Blender (Lab06)
Michele Svanera FCG lab04: Introduction to Panda3D 26
Loading Actors and Animations I
Actor operations:
Load the Actor Model
NodePath Actor = window ->load_model(window ->
get_render (), "models/panda -model");
Load the Animation
// the name of an animation is preceded in the .
egg file with <Bundle >
window ->load_model(Actor , "models/panda -walk4");
One-command animation
//bind models and animations + set it to loop
window ->loop_animations (0);
Michele Svanera FCG lab04: Introduction to Panda3D 27
Loading Actors and Animations II
Control the Animations
// loop all animations
anim_collection.loop_all(true);
// loop a specific animation
anim_collection.loop("panda_walk_character", true)
// play an animation once
anim_collection.play("panda_walk_character");
// pose: hold a particular frame of the animation
anim_collection.pose("panda_walk_character", 5);
Michele Svanera FCG lab04: Introduction to Panda3D 28
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
Render Attributes I
After loading a model, you can alter its appearance by alteringits attributes (such as color, texture, lighting, and . . . )
All the attributes of an object are called the object’s renderstate
Setting an attribute on a node automatically applies it to allof the children of that node
Create and assign attributes to a node:
node_path.node()->set_attrib(attributeObject);
In many cases, there is a convenience function on NodePath(ex. node_path->set_fog()) that manages the creation ofthe attributes
Michele Svanera FCG lab04: Introduction to Panda3D 30
Render Attributes II
Object’s render state:
AlphaTest Hides part of the model, based on the texture’s alpha channel
Antialias Controls fullscreen antialiasing and polygonedge antialiasing
Color Tints the model (only works if the model is not illuminated)
CullBin Controls the order in which Panda renders geometry
DepthTest Alters the way the Z-buffer affects the model
Fog Causes the model to be obscured by fog if it is far from camera
Light Causes the model to be illuminated by certain lights
Material Changes the way the model reflects light
ShadeModel Can cause the model to appear faceted instead of smooth
TexGen Causes system to synthesize texture coordinates for the model
TexMatrix Alters the existing texture coordinates
Texture Applies a texture map to the model
Transparency Causes the model to be partially transparent
Michele Svanera FCG lab04: Introduction to Panda3D 31
Lighting I
Lighting a scene consists of two steps:
1 Creating lights, and positioning them within the scene
2 Telling which objects should be illuminated by the lights (andhow: Material)
Two general categories of lights:
Directional Light that comes straight from a particular lamp
Point: single point in space and shining in alldirections (like sun)Directional: infinite wave of light, always in thesame direction (like sunlight)Spotlight: sophisticated kind of light; has both apoint and a direction, and a field-of-view
Nondirectional Light that maybe came from somewhere
Ambient: uniformly distributed in the worldMichele Svanera FCG lab04: Introduction to Panda3D 32
Lighting II
// Point Lights
#include "pointLight.h"
PointerTo <PointLight > plightSun = new PointLight(
"sun"); // default white
NodePath plightSun_p = window ->get_render ().
attach_new_node(plightSun);
plightSun_p.set_pos (500 ,500 ,500);
window ->get_render ().set_light(plightSun_p); //all
world is illuminated
// Directional light
#include "directionalLight.h"
PT(DirectionalLight) d_light;
d_light = new DirectionalLight("my d_light");
d_light ->set_color(LVecBase4f (0.8, 0.8, 0.5, 1));
NodePath dlnp = window ->get_render ().attach_new_node(
d_light);
dlnp.set_hpr (-30, -60, 0);
myObject.set_light(dlnp); //only myObject is
illuminatedMichele Svanera FCG lab04: Introduction to Panda3D 33
Lighting III
// Spotlights
#include "spotlight.h"
PT(Spotlight) s_light = new Spotlight("my s_light");
s_light ->set_color(LVecBase4f (0, 1, 0, 1));
NodePath slnp = window ->get_render ().attach_new_node(
s_light);
slnp.set_pos(0, 5, -1);
slnp.look_at(myObject);
myObject.set_light(slnp); //only myObject is
illuminated
// Ambient Lights
#include "ambientLight.h"
PT(AmbientLight) a_light = new AmbientLight("my
a_light");
a_light ->set_color(LVecBase4f (0.2, 0.2, 0.2, 1));
NodePath alnp = window ->get_render ().attach_new_node(
a_light);
window ->get_render ().set_light(alnp);
Michele Svanera FCG lab04: Introduction to Panda3D 34
Materials
Lights are used to express how much light is striking themodel
Materials are used to express how much of the light strikingthe model is reflected
The Material consists of four values:
Ambient Scattering How much of the nondirectional light isreflected
Diffuse Scattering How much of the directional light is scattered
Specular Reflection How much of the directional light is reflected
Emissive How much light the surface produces itself
Michele Svanera FCG lab04: Introduction to Panda3D 35
Depth Test and Depth Write
Hidden surface removal
By default, depth buffer is enabled
//En-Disables writing to the depth buffer. Useful for
transparent objects
nodePath.set_depth_write(false); // Disable
nodePath.set_depth_write(true); // Enable
// Similar to glutInitDisplayMode (... | GLUT_DEPTH);
//Do depth comparisons and update buffer. Often used
for rendering things like h e a d s u p displays
nodePath.set_depth_test(false); // Disable
nodePath.set_depth_test(true); // Enable
// Similar to glEnable(GL_DEPTH_TEST);
Michele Svanera FCG lab04: Introduction to Panda3D 36
Texturing I
Process that sticks an image on to a surface
Details of texture (ex. path, filtering or repeat-method) aresaved into the egg file of the model
(u, v) texture coordinates with u from 0 to 1 from left toright, and the v from 0 to 1 from bottom to top
u : from 0 to 1 around sphere
v : from 0 at the bottom to 1 at the top
Michele Svanera FCG lab04: Introduction to Panda3D 37
Texturing II
#include "texture.h"
#include "texturePool.h"
TexturePool *texture_pool = TexturePool ::
get_global_ptr ();
// In main
NodePath myNodePath = window ->load_model(framework.
get_models (), "Square");
Texture *tex = texture_pool ->load_texture(
"myTexture.jpg");
myNodePath.set_texture(tex);
//Square.egg model comes from [1].
Michele Svanera FCG lab04: Introduction to Panda3D 38
Texturing - WrapMode I
Texture coordinates can go outside the range (0 < u or u > 1)
Behavior managed by wrap mode
The wrap_mode is specified separately for u and v directions
texture ->set_wrap_u(wrap_mode);
texture ->set_wrap_w(wrap_mode);
wrap_mode options, starting from:
Michele Svanera FCG lab04: Introduction to Panda3D 39
Texturing - WrapMode II
Texture::WM repeat The texture image repeats to infinity(default)
Texture::WM clamp The last pixel of the texture imagestretches out to infinity. When you apply atexture that is intended to exactly fill apolygon, you should usually set its wrap modeto clamp
Texture::WM border color The color specified bytexture->set_border_color() is used to fillthe space
Texture::WM mirror The texture image flips back-and-forthto infinity
Texture::WM mirror once The texture image flips backwards,once, and then the ”border color” is used
Michele Svanera FCG lab04: Introduction to Panda3D 40
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
Camera control I
Panda3D’s camera is considered a PandaNode: it can bemanipulated as any other node.
By default, panda runs a task that enables you to move thecamera using the mouse
This task will conflict with any code you write to move thecamera, like: camera.set_pos, camera.look_at
// Mouse control camera
window ->setup_trackball ();
// Position of the mouse
if (mouseWatcher ->has_mouse ()){
if (window ->get_graphics_window ()){
int x = window ->get_graphics_window ()->
get_pointer (0).get_x ();
int y = window ->get_graphics_window ()->
get_pointer (0).get_y ();
}
}Michele Svanera FCG lab04: Introduction to Panda3D 42
Camera control II
// Enable keyboard
window ->enable_keyboard ();
//exit function
void sys_exit(const Event* eventPtr , void* dataPtr)
{
exit (0);
}
//in main
framework.define_key("escape", "sysExit", sys_exit , (
void)* NULL);
"escape", "space", "f"+"1-12" (e.g. "f1", ..."f12"),
"backspace", "insert", "home", "page_up", "num_lock",
"tab", "delete", "end", "page_down", "enter",
"arrow_left", "arrow_up", "arrow_down", "arrow_right",
"control", "alt"
Michele Svanera FCG lab04: Introduction to Panda3D 43
Exercise
(Only Section 1 - Creation of the scene)
Michele Svanera FCG lab04: Introduction to Panda3D 44
1 Introduction to Panda3D
2 Scene Graph
3 Task
4 Actors and Models
5 Render Attributes
6 Camera control
7 Install and References
Install and References
Installing Panda3D in Linux (with problems solutions) [12]
Installing Panda3D in Windows [10]
Installing Panda3D in Mac OSX [11]
Panda3D Manual: Main Page (C++ & Python) [14]
Panda3D Documentation and References [15]
Panda3D Manual: Cheat Sheets [9]
Bullet Collision Detection and Physics Library [2]
Panda3D C++ Example [7]
Free Model [4] and [1]
Sample program [8]:
C++: make [6] and run
python: python2.5 name.py or python2.7 name.py
Michele Svanera FCG lab04: Introduction to Panda3D 46
SDK contents
samples (or Examples) (only in python! See [7] for C++)
etc with configuration file Config.prc [3]
include with header files
models with already made objects
Michele Svanera FCG lab04: Introduction to Panda3D 47
Alice gallery for models.http://alice.org/pandagallery/.
Bullet collision detection and physics library.http://bulletphysics.org/Bullet/BulletFull/.
Configuring panda3d.https://www.panda3d.org/manual/index.php/
Configuring_Panda3D.
Free models.http://www.turbosquid.com/Search/Index.cfm?
keyword=&certification_id=1&media_typeid=2&sort_
column=A5&sort_order=asc.
Language binding.http://en.wikipedia.org/wiki/Language_binding.
Make file example.Michele Svanera FCG lab04: Introduction to Panda3D 48
https://dl.dropboxusercontent.com/u/44434712/
SVDRV_lab_2014/LAB04/Makefile_VM.zip.
Panda3d c++ example.https://github.com/drivird/drunken-octo-robot.
Sample example.https://dl.dropboxusercontent.com/u/44434712/
SVDRV_lab_2014/LAB04/ex-animation.zip.
Panda3D cheat-sheet.http:
//www.panda3d.org/manual/index.php/Cheat_Sheets.
Install on linux.http://www.panda3d.org/manual/index.php/
Installing_Panda3D_in_Linux.
Install on macosx.
Michele Svanera FCG lab04: Introduction to Panda3D 49
http://www.panda3d.org/manual/index.php/Getting_
Started_on_OSX.
Install on windows.http://www.panda3d.org/manual/index.php/
Installing_Panda3D_in_Windows.
Panda3D forum.http://www.panda3d.org/forums/index.php.
Panda3D manual.http://www.panda3d.org/manual/index.php/Main_Page.
Panda3D lib reference.http:
//www.panda3d.org/reference/1.8.1/cxx/annotated.
Michele Svanera FCG lab04: Introduction to Panda3D 50