Loading the Grassy Scenery

The Scene Graph

Panda3D contains a data structure called the Scene Graph. The Scene Graph is a tree containing all objects that need to be rendered. At the root of the tree is an object named render. Nothing is rendered until it is first inserted into the Scene Graph.

You can get the NodePath of render by calling window->get_render().

To install the grassy scenery model into the Scene Graph, we use the method reparent_to(). This sets the parent of the model, thereby giving it a place in the Scene Graph. Doing so makes the model visible in the scene.

Finally, we adjust the position and scale of the model. In this particular case, the environment model is a little too large and somewhat offset for our purposes. The set_scale() and set_pos() procedures rescale and center the model.

Panda3D uses the “geographical” coordinate system where position (-8, 42, 0) means map coordinates (8, 42) and height 0. If you are used to OpenGL/Direct3D coordinates, then hold up your right hand in the classical position with thumb as X, fingers as Y, and palm as Z facing toward you; then tilt backward until your hand is level with the fingers pointing away and palm facing up. Moving “forward” in Panda3D is a positive change in Y coordinate.

The Program

Update the Code

With Panda3D running properly, it is now possible to load some grassy scenery. Update your code as follows:

 1#include "pandaFramework.h"
 2#include "pandaSystem.h"
 3
 4int main(int argc, char *argv[]) {
 5  // Load the window and set its title.
 6  PandaFramework framework;
 7  framework.open_framework(argc, argv);
 8  framework.set_window_title("My Panda3D Window");
 9  WindowFramework *window = framework.open_window();
10
11  // Load the environment model.
12  NodePath scene = window->load_model(framework.get_models(), "models/environment");
13  // Reparent the model to render.
14  scene.reparent_to(window->get_render());
15  // Apply scale and position transforms to the model.
16  scene.set_scale(0.25f, 0.25f, 0.25f);
17  scene.set_pos(-8, 42, 0);
18
19  // Run Panda's main loop until the user closes the window.
20  framework.main_loop();
21  return 0;
22}

The WindowFramework procedure window->load_model() loads the specified file, in this case the environment.egg file in the models folder. The return value is an object of the NodePath class, effectively a pointer to the model. Note that Panda Filename Syntax uses the forward-slash, even under Windows.

Run the Program

Go ahead and run the program. You should see this:

../../../_images/tutorial1.jpg

The rock and tree appear to be hovering. The camera is slightly below ground, and back-face culling is making the ground invisible to us. If we reposition the camera, the terrain will look better.