Panda3D Manual: Automatic Texture Coordinates
In addition to using texture coordinates that are built into the model, it is also possible to generate texture coordinates at runtime. Usually you would use this technique to achieve some particular effect, such as projective texturing or environment mapping, but sometimes you may simply want to apply a texture to a model that does not already have texture coordinates, and this is the only way to do that. The texture coordinates generated by this technique are generated on-the-fly, and are not stored within the model. When you turn off the generation mode, the texture coordinates cease to exist. Use the following NodePath method to enable automatic generation of texture coordinates: nodePath.setTexGen(TextureStage, texGenMode) The texGenMode parameter specifies how the texture coordinates are to be computed, and may be any of the following options. In the list below, "eye" means the coordinate space of the observing camera, and "world" means world coordinates, e.g. the coordinate space of render, the root of the scene graph.
Note that several of the above options generate 3-D texture coordinates: (u, v, w) instead of just (u, v). The third coordinate may be important if you have a 3-D texture or a cube map (described later), but if you just have an ordinary 2-D texture the extra coordinate is ignored. (However, even with a 2-D texture, you might apply a 3-D transform to the texture coordinates, which would bring the third coordinate back into the equation.) Also, note that almost all of these options have a very narrow purpose; you would generally use most of these only to perform the particular effect that they were designed for. This manual will discuss these special-purpose TexGen modes in later sections, as each effect is discussed; for now, you only need to understand that they exist, and not worry about exactly what they do. The mode that is most likely to have general utility is the first one:
For instance, the teapot.egg sample model that ships with Panda has no texture coordinates built in the model, so you cannot normally apply a texture to it. But you can enable automatic generation of texture coordinates and then apply a texture: teapot = loader.loadModel('teapot.egg') tex = loader.loadTexture('maps/color-grid.rgb') teapot.setTexGen(TextureStage.getDefault(), TexGenAttrib.MWorldPosition) teapot.setTexture(tex) And you end up with something like this:
You can use this in conjunction with a texture transform to further manipulate the texture coordinates. For instance, to rotate the texture 90 degrees, you could do something like this: teapot.setTexTransform(TextureStage.getDefault(), TransformState.makeHpr(VBase3(0, 90, 0)))
Finally, consider that the only two choices for the coordinate frame of the texture coordinate generation are "world" and "eye", for the root NodePath and the camera NodePath, respectively. But what if you want to generate the texture coordinates relative to some other node, say the teapot itself? The above images are all well and good for a teapot that happens to be situated at the origin, but suppose we want the teapot to remain the same when we move it somewhere else in the world? If you use only Panda3D provides the capability to generate texture coordinates in the
coordinate space of any arbitrary node you like. To do this, use
teapot.setTexGen(TextureStage.getDefault(), TexGenAttrib.MWorldPosition) teapot.setTexProjector(TextureStage.getDefault(), render, teapot); It may seem a little circuitous to convert the teapot vertices to world space to generate the texture coordinates, and then convert the texture coordinates back to teapot space again--after all, didn't they start out in teapot space? It would have saved a lot of effort just to keep them there! Why doesn't Panda just provide an That's a fair question, and Note that you only want to call © Carnegie Mellon University 2010 |