SVDRV Lab04: Introduction to Panda3D
Michele Svanera & Fabrizio Pedersoli
Department of Information Engineering University of Brescia
28 Apr 2014 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 objects texture mapping light source little animation . . . and for something more complex? . . . Animation? Interaction between objects?
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 3 Panda3D: a game engineI
What is it? Open-source (from 2002) game engine Library of subroutines for 3D rendering and game development Provides: Graphics Audio I/O Collision detection ... Set of C++ library with Python bindings [5] Jointly developed by Disney and Carnegie Mellon University
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 4 Panda3D: a game engineII
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 an SDK) is dedicated to the problem of tracking and isolating errors. Panda3D almost never crashes!
Michele Svanera & Fabrizio Pedersoli SVDRV 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 MAX High-quality Models can be animated by the engine . . . Drawing is still possible!
Michele Svanera & Fabrizio Pedersoli SVDRV 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”, by Christoph Lang
Panda3D Manual [14] Panda3D Forum [13]
Michele Svanera & Fabrizio Pedersoli SVDRV 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 GraphI
“Tree of things to render”:
More flexible than lists (as many other engine uses) and inheritance An object is not visible (and rendered) until it is inserted Objects are children of (super)class PandaNode ModelNode GeomNode LightNode ...... usually refer as general NodePath.
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 9 The Scene GraphII
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 actually performing an operation on the node to which it points
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 10 The Scene GraphIII
Panda3D generates bounding boxes for each node in the tree Number of methods dedicated to finding nodes and returning the NodePaths
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 11 The Scene GraphIV
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 12 The Scene GraphV
Example 1: Solar System
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 13 The Scene GraphVI
Example 2: What makes a man?
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 14 Dealing with nodesI
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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 15 Dealing with nodesII
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(); //Rotatesa model to face another object np.look_at(otherObject); //Color: floating point numbers from0 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 other packages)–these specify angle rotations in degrees. Michele Svanera & Fabrizio Pedersoli SVDRV 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 TaskI
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 of time) task->get_elapsed_time() //how long task running task->get_elapsed_frames() //number of elapsed frames ... Each task returns after it has finished processing the current frame: AsyncTask::DS_cont = call it again the next frame AsyncTask::DS_done = finished, don’t call it anymore AsyncTask::DS_again = perform it again, with same delay
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 18 TaskII
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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 19 TaskIII
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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 20 The Task Manager
All tasks are handled through the Task Manager object (list) TM can be initialized by PT(AsyncTaskManger) 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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 21 The Task Chain
Each TaskChain is a ordered list of tasks that are available to be 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 New task chain AsyncTaskManager *task_mgr = AsyncTaskManager::get_global_ptr(); AsyncTaskChain *chain = task_mgr-> make_task_chain("chain_name");
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 22 Exercise: Rotating CubeI
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 23 Exercise: Rotating CubeII
Michele Svanera & Fabrizio Pedersoli SVDRV 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 an actor) or both (better to pack in two .egg) Are created by exporting models from 3D modeling programs: Maya, Max, or Blender (Lab06)
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 26 Loading Actors and AnimationsI
Actor operations: Required Includes #include"auto_bind.h" 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
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 27 Loading Actors and AnimationsII
Bind the Model and the Animation // don’t usePT orCPT with AnimControlCollection AnimControlCollection anim_collection; // bind the animations to the model auto_bind(Actor.node(), anim_collection); Control the Animations // loop all animations anim_collection.loop_all(true); // loopa specific animation anim_collection.loop("panda_walk_character", true) // play an animation once anim_collection.play("panda_walk_character"); // pose: holda particular frame of the animation anim_collection.pose("panda_walk_character", 5);
Michele Svanera & Fabrizio Pedersoli SVDRV 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 AttributesI
After loading a model, you can alter its appearance by altering its attributes (such as color, texture, lighting, and . . . ) All the attributes of an object are called the object’s render state Setting an attribute on a node automatically applies it to all of 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 (e.g.node_path->set_fog()) that manages the creation of the attributes
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 30 Render AttributesII
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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 31 LightingI
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 (and how: Material) Two general categories of lights: Directional Light that comes straight from a particular lamp Point: single point in space and shining in all directions (like sun) Directional: infinite wave of light, always in the same direction (like sunlight) Spotlight: sophisticated kind of light; has both a point and a direction, and a field-of-view Nondirectional Light that maybe came from somewhere Ambient: uniformly distributed in the world
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 32 LightingII
// Point Lights #include"pointLight.h" PointerTo
// 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 illuminated Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 33 LightingIII
// 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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 34 Materials
Lights are used to express how much light is striking the model Materials are used to express how much of the light striking the model is reflected The Material consists of four values: Ambient Scattering How much of the nondirectional light is reflected 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 & Fabrizio Pedersoli SVDRV 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 heads up displays nodePath.set_depth_test(false);//Disable nodePath.set_depth_test(true);//Enable //Similar to glEnable(GL_DEPTH_TEST);
Michele Svanera & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 36 TexturingI
Process that sticks an image on to a surface Details of texture (ex. path, filtering or repeat-method) are saved into the egg file of the model (u, v) texture coordinates with u from 0 to 1 from left to right, 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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 37 TexturingII
#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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 38 Texturing - WrapModeI
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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 39 Texturing - WrapModeII
Texture::WM repeat The texture image repeats to infinity (default) Texture::WM clamp The last pixel of the texture image stretches out to infinity. When you apply a texture that is intended to exactly fill a polygon, you should usually set its wrap mode to clamp Texture::WM border color The color specified by texture->set_border_color() is used to fill the space Texture::WM mirror The texture image flips back-and-forth to infinity Texture::WM mirror once The texture image flips backwards, once, and then the ”border color” is used
Michele Svanera & Fabrizio Pedersoli SVDRV 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 controlI
Panda3D’s camera is considered a PandaNode: it can be manipulated as any other node. By default, panda runs a task that enables you to move the camera using the mouse This task will conflict with any code you write to move the camera, 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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 42 Camera controlII
//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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 43 Exercise
(Only Section 1 - Creation of the scene)
Michele Svanera & Fabrizio Pedersoli SVDRV 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 & Fabrizio Pedersoli SVDRV 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 & Fabrizio Pedersoli SVDRV 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 & Fabrizio Pedersoli SVDRV 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 & Fabrizio Pedersoli SVDRV 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 & Fabrizio Pedersoli SVDRV lab04: Introduction to Panda3D 50