Per vertex lighting
In real life, an object is not evenly lighted. Some spots are darker, some places are lighter. To achieve the same result in the 3D engine, we’ll make a light and a material for our cube.

What do we need?
- First of all we need the light engine of TV. This light engine can make us a light that will shine on the objects. This object is called TVLightEngine.
- Then we’ll need a material factory. A material defines how much light will be reflected from the object the material is applied to. It will also specify how much shininess there will be, and of course the possibilities are almost endless. The material factory of TV is called TVMaterialFactory.
-We will need 2 integers as well to keep track of the material ID and the light ID. This works in the same way as with textures.
How do we set things up?
- We first instantiate the material factory and the lightengine by calling these two lines:
Lights = new TVLightEngine();
- Lets make a material. This can get really complex, but we’ll keep it simple here. There are a lot of settings to be found in the material editor. If you want more info than i give, you should take a look at the material tutorial on the wiki: wiki truevision
We call
to create the material.
The first 4 parameters are the diffuse and specular parameters. The material’s diffuse parameters define how much light of the specific colors is being bounced of the surface. The specular parameters define the shiny color. In this it’s the same.
The fifth parameter defines the ambient amount. The ambient color is the color that is being shown when there’s no light shining on an object. In this case it’s a small number, so the cube won’t have a lot of color when there’s no light shining on it. Ambient is usually being used to simplify scenes. If there a lot of lights that shine and it doesn’t really make a difference if it’s not really accurate, you usually turn the ambient up a bit.
The sixth parameter defines the specular value. The specular parameter defines how much the object will shine. Lowering the specular value will decrease the shininess.
The last parameter will specify the name of the material. This is not necessary if you’re keeping track of the material ID’s yourself, but if you want to find it with the TVGlobals, it’s pretty handy.
- Now we need to make the light. In TV3D, it’s pretty easy making things look pretty with the lights. We’re calling:
This line has 7 parameters.
The first parameter is a TV_3DVECTOR. A TV_3DVECTOR has 3 properties: x, y and z. You can set the x, y and z when creating the object with the constructor. That’s why you see “new TV_3DVECTOR(5,5,-10)”. This will make a TV_3DVECTOR with the x value 5, y value 5 and z value -10. So the light will be at that position.
The next 3 parameters are the colors of the light (red, green, blue). These can have values from 0 to 1. So the light we’re making right now has every color max. That means the color will be white.
The fifth parameter is the radius of the point light. A point light is like a light bulb, but it this radius setting has nothing to do with the size of the light bulb. This radius specifies the point at which the light doesn’t influence the objects in the worlds anymore, so it won’t kill your processor while trying to figure out which light shines on what mesh.
The sixth parameter is the name of the light obviously. Again, this isn’t really necessary, but it’s handy anyway, because it makes your code better readable.
The last parameter is the specular level. This works the same as on the material, but now it specifies how much light there is for the shininess. This way you can reduce the shininess of every object at once.
- We’ll add a small sphere to indicate where the light is stationed, since you can’t see a light source (only the light that’s being reflected/bounced of a surface). See tutorial 1 about the how to do this.
- Let’s apply the material to the mesh.
does it for you. The first parameter is the ID of the material of course, and the second parameter specifies on which group the material is applied. -1 means it’ll apply the material to every group of the mesh.
- Now we need to tell the engine that there’s a light and we need to tell it what to do with it.
does the trick.
The first constant tells the engine that it should have normal/regular lighting. This means that it will calculate the distance/orientation of the model to the light by the vertexes only. This is why we call this way of doing stuff “Per vertex lighting”. If you don’t know what the whole vertex thing is about, or you don’t know what they are, have a look at this webpage: wiki. There are a couple other modes, I won’t explain them, since it’s pretty easy figuring out what they do yourself.
The second and third parameter are pretty handy, since those parameters can increase performance. They specify how many lights are affecting a mesh at maximum. The first one specifies the number of directional lights and the second one the point lights and spotlights.
How do we render this?
We just need to add the light mark sphere to the render loop, since we want it to show up. The rest of the code doesn’t need any changes.
Things you can add/change yourself
- Try playing around with the radius of the light. Try setting it to 10 for example. That’ll show the light even better sometimes.
- Try playing with the color of the light and the material. See what happens to the rendered cube.
- Also moving the light around the scene could be good to get an understanding of the coordinate/axis system in TV3D.