Textured cube
In the previous tutorial you’ve learned how to create a cube in truevision3d. There’s just something that isn’t cool about the cube. It’s white, just plain white. Therefore we’re going to add a texture to it. Now what is a texture? A texture is a picture that you can apply to a 3D object. For example, if you have a book laying on a table, you’ll want to have a picture of a cover of a book to apply to the book, otherwise it won’t look like a book.

What do we need?
We’re going to extend our previous sample, so we’ll have the cube already.
- We need something to load textures with. Textures should be loaded into TV, because directX doesn’t use regular bitmap objects. That’s where the TVTextureFactory comes in. The texture factory can make textures out of bitmaps, jpg’s etc. So we’ll need the texture factory to load our pictures.
- We need to keep track of the texture, because we first want to load the texture and then we’ll apply it to the surface of the cube. Whenever you load a texture with the texture factory, it returns an integer. That’s the ID of the texture. That’s why we’ll keep that stored in the integer IDDiffuse.
- Of course we also need a texture of some sort. In this case we’re using a *.dds file. This file format is a native format of directX. You can make those textures with the photoshop plugin of nVidia (developer.nvidia.com). The texture is a logo of truevision.
How do we set things up?
We’re extending the previous tutorial, so we just need to load the texture and apply it to the surface.
This will instantiate the texture factory properly. There are a couple of things that you can set up, but it’s good by default.
- The loading of the texture
Since the texture is in the common folder, we’ll going to have to point the texture factory to it. Depending on your language, this’ll look about the same as this:
The first parameter is the string path of the texture. In this case, the common folder is a couple levels down, that’s why you see a lot of ..\.
The second parameter is a string that gives the texture a name. If you don’t want to keep track of your texture ID’s, you can find them by this name.
The third and fourth parameter define the width and height of the texture. It will resize the texture to that size. So if you tell the texture factory to load the logo with a width and height of 16, then the texture will get loaded as a 16×16 pixel texture. This will look terrible, but it saves memory.
The fifth parameter defines which color should be used for the alpha layer. What’s alpha anyway? To simplify it, it’s the transparency of a pixel. So if you have an alpha of 0.5 on a pixel that’s red, and an alpha of 0.5 on a pixel that’s green painted right on top of it, it’ll make the pixel have an alpha of 1 and a yellow color on your screen. Anyway, there are a couple options for this. You can use black, or white or a couple other colors, but you can also use the alpha layer that is specified in the texture itself. That’s by using CONST_TV_COLORKEY.TV_USE_ALPHA_CHANNEL. We don’t want to have any alpha values for this texture, so we’re going to use TV_COLORKEY_NO. That’ll load the texture without alpha values.
The last parameter is pretty useful. If you specified a height and width and you set the last parameter to false, it’ll take the first pixels of the texture only. So lets say you have a height and width of 16 and you set the last parameter to false, it’ll take the top left 16 pixels only. If you set the parameter true, then it’ll reduce the whole texture to 16 pixels width and height.
- Applying the texture to the cube
This step is really easy, we just call
This will tell the TVMesh that it has a texture ID that’s equal to IDDiffuse. This isn’t all you can do though. I explained in the previous tutorial that you can make the box have multiple groups. If the box has 6 groups, you can apply a different texture to every face of the box. You can do that by using
where iGroup is between 0 and 5. This will apply the texture to all faces separately. Of course you would need to have other values for IDDiffuse as well, since you would have the same image on every face of the box if you didn’t use other textures.
How do we render this?
This step is exactly the same as in the previous tutorial.
Things you can add/change yourself
- Try making your own texture to apply to the cube. Don’t make it too big, since that will require a longer loading time. Also make sure it’s a power of two texture, because a lot of older computers don’t support a non power of two texture. So you’re best of using 512×512 I think.
- Try doing as i suggested by having each face of the cube have a different texture. Avoid using the same integer for the textures, so make 5 other integers called IDDiffuse2, IDDiffuse3 etc.