hi,

some notes about texels lighting shading ( "lightmapping" "shadowmapping" ) :

so i am trying to create my own texels lighting shading system, so while i learn and experiment, i am going to post some notes, for those who are interested in this...

i will (try to) keep it simple to make it understandable ok ?

a mesh has one or several surfaces,

in a surface there are vertices and triangles

each vertex has a position, and a normal (vector pointing outwards), and UV coordinates

each triangle has 3 vertices

you can color a surface in different ways, here we will use the texels (of a texture)

a texel area (a part of the texture) can be applied on a surface area (a part of the surface, on the triangles) by defining what we call UVs

U,V are values between 0.0 and 1.0 representing the x,y position of a vertex on a texture

example :

we have a square made of 4 vertices, 2 triangles, with :

vertex0 position = -0.5x, 0.0y, +0.5z

vertex1 position = +0.5x, 0.0y, +0.5z

vertex2 position = -0.5x, 0.0y, -0.5z

vertex3 position = +0.5x, 0.0y, -0.5z

so the size of the square is 1.0x1.0unit (1.0width 0.0height 1.0depth)

for the colors, we have a texture of 128x128 texels

the colors area is only 100x100texels from 0x0y to 99x99y

we want to color the square with the colors area, vertex0 at top left, vertex1 at top right, vertex2 at bottom left, vertex3 at bottom right (of the colors area)

let's calculate the UVs (for coordset 0) :

V0U = 0.0/128 | V0V = 0.0/128

V1U = 100.0/128 | V1V = 0.0/128

V2U = 0.0/128 | V2V = 100.0/128

V3U = 100.0/128 | V3V = 100.0/128

why 100.0/128 and not 99.0/128, you may ask ? because UV is the position of a vertex on the texture, not of a texel.

so the vertices must be around the 100x100 texels area...

easy enough ? ok let's complicate a little bit

so we can now apply a texture on a surface, good (you don't need to do that in code, there are tools to help you (like Fragmotion), but it can be useful to understand what it is and how it works.)

but in Blitz3d (and others 3d engines) there are 2 UVs coordsets, why ?

VertexTexCoords(surface,vertexindex,U,V,W,coordset) (disregard the "W", not used in Blitz3d)

TextureCoords(texture,coordset)

well usually the UVs coordset 0 is used to apply colors on a surface (="diffusemap") and the UVs coordset 1 is used to apply lighting shading (using texels) on a surface (="lightmap" or "shadowmap")

now what can be confusing, is that, the shape is the exact same, the structure is the same (vertices and triangles), but the UVs of the coordset 0 and the UVs of the coordset 1 can be (and usually are) different, why ?

well, the first reason is because you use tools and you have no idea what you are doing, and the exporter is often buggy, so at the end it is quite messy...

to be serious, usually we need more details (so a smaller texel size, so more texels in the texture) when coloring the surface, and we don't need as much details (so a bigger texel size, so less texels in the texture) when lighting shading the surface.

example :

previously we had a square which measured 1.0x1.0unit (1.0width 0.0height 1.0depth)

and we applied a colors area of 100x100texels

so in this case the texel size will be approximately 0.01u (=1.0/100)

now for lighting / shading using texels (="lightmaping" / "shadowmapping"), we don't need as much details, so we could a texel size of 0.1u

so for a square of 1.0x1.0, we would only need a 10x10texels area.

but it would be nice enough for lighting / shading and shadows, and also faster to calculate how each texel must be lighted shaded (colored with lightsource color, considering the distance lightsource->texel, and considering the angle between lightvector and texelnormal)

for the lighting shading, we have a texture of 16x16 texels

the lighting shading area is only 10x10texels from 0x0y to 9x9y

let's calculate the UVs (for coordset 1) :

V0U = 0.0/16 | V0V = 0.0/16

V1U = 10.0/16 | V1V = 0.0/16

V2U = 0.0/16 | V2V = 10.0/16

V3U = 10.0/16 | V3V = 10.0/16

easy enough ?

take a look at a demonstration, in code :

https://www.syntaxbomb.com/index.php/topic,6298.0.html