I have a 3D texture for the terrain I am trying to render, and i want to use a certain layer of the texture (ie, grass, rock, ice) depending on the slope of the terrain (or normal). Flat areas should use grass.
I think TexGenAttrib.MWorldNormal would do it for me, but i need to only set the W texture space, and not set U and V. In other words, I want to copy just use the Z component of the normal to the W texture coordinate.
I think there are two choices available to you: you could write a custom shader to generate your texture coordinates (requires learning Cg, and a card that supports shaders). Or you could write Python code to generate them using the GeomVertexWriter class (works anywhere).
But you can’t use TexGenAttrib.MWorldNormal to generate just one component of an existing texture coordinate, and leave the other two the same.
I am using self.tex2 = loader.load3DTexture(MYDIR + ‘models/s_#.png’) for the texture.
One more thing I noticed. I am using 3ds max to export models into egg files. And when examining the texcoords from these egg files, there is no Z (or W) coordinate information. I know I am generating 3D coords from 3ds max, but it looks like the information isnt saved in the egg file.
Ah, so your geometry doesn’t have space to store the W component, and it is getting dropped on the floor.
I guess the real problem is that the 3DSMax converter is not converting the 3-D texture coordinates (this doesn’t surprise me). So one answer is, of course, to fix the converter–the source code is right there for you.
The next answer is to hack the egg file produced by the converter by hand, and change lines like this:
{ 0.25 0.5 }
To this:
{ 0.25 0.5 0 }
The third answer is to load the unhacked egg file, then get a pointer to the GeomVertexData like you are doing now; then create a new GeomVertexData that has the same format, with a 3-d texture coordinate instead; and copy the vertices from the original to the new GeomVertexData. Then you can store this new GeomVertexData back into the node, and you can manipulate it as you are doing now.
That doesn’t add a third component to the data. That only sets the third component, if it happens to be there. If the texcoord only has two components, though, it will quietly ignore the third component.
In order to actually add a third component, you will need to create a new GeomVertexFormat, whose ‘texcoord’ column is a 3-component value. Since there are no default formats with a 3-component texture coordinate, you’ll have to build it up by hand, with code like this:
The reason is that the value returned by getData3f() is a const value. You’re not supposed to be able to modify it directly, as you are doing in the line “tex.setZ(.5)”. Unfortunately, Python doesn’t have a concept of a const structure, so it lets you modify it anyway, even though you might be modifying static memory or writing off the end of an array somewhere.
The correct, safe way to do this is the following:
This will make a copy of the data first, which you can then safely modify. The reason that GeomVertexReader doesn’t return a copy by default is that it is a low-level class that must be optimized for speed. In C++, it is safe, because C++ does support the idea of a const structure.