Realtime vertex lighting?

Blitz3D Forums/Blitz3D Programming/Realtime vertex lighting?

Isaac P(Posted 2004) [#1]
Is it possible in blitz?

And if so could someone suggest some code reference.. either in blitz or other.

I contacted a maker of one of my favourite games atm and wanted to try implementing the same lighting.

Here is the response i got:

Lighting is done by controlling the RGB value for each polygon. You have to compare
each vertice of each poly to every light source and increase the RGB an amount
based on the distance from that light source.

You can also use the reverse of that system where you set all RGBs to max and then
darken them where there are no light sources.



I looked at the code archives and this seems exactly what robs maths lights example does..


Bot Builder(Posted 2004) [#2]
Um, yeah. Sounds like a dx point light to me. otherwise, you could use your own code.


Isaac P(Posted 2004) [#3]
Any ideas on this?

The game im talking about is www.xenimus.com It has a free trial if u wish to see what i mean.

I want to implement this code in my dungeon questing game


slave_of_time(Posted 2004) [#4]
hmmm...something like that?

EntityFX Mesh,2
light=createpivot()


Function UpdateLight(mesh,light)

surf_nr = CountSurfaces(mesh)
For i = 1 To surf_nr
surf = GetSurface(mesh,i)
vert_nr = CountVertices(surf)-1
For z = 0 To vert_nr

;Code...

VertexColor surf,z,red,grn,blu
Next
Next

End Function


jhocking(Posted 2004) [#5]
"each vertice of each poly"

The word is VERTEX! Sorry, that's a hangup of mine.

I'm not sure what you're asking for here. I must be reading your post wrong because to me it sounds like you are describing a regular ole point light ie. CreateLight(2)


Isaac P(Posted 2004) [#6]
I know thats why i used vertex in the title of the post... It was the creator of the game that sent an email that i included in my post, so it is indeed him who made the mistake.

A point light is exactly what i want to use.. but i know for a fact there will be times when i will need more than 8 lights. So i was wondering the implementation of such a system, using vertex colours.

Thanks for your help all of you... Im still unsure as to which system Ill use, A directx lights based system which dynaically creates and culls lights.. or this vertex colour system.

Will have to knock up some speed tests to check


Miracle(Posted 2004) [#7]
Also:

http://www.blitzbasic.com/b3ddocs/command.php?name=LightMesh&ref=3d_cat

:D


Neochrome(Posted 2004) [#8]
Does Lightmesh slow things down? i mean is, DX only lets you use 8 at anyone time, does LightMesh have a limit?


Miracle(Posted 2004) [#9]
I haven't tested it for speed, but I understand there's no limit. Of course, you have to do a LightMesh on every mesh that's affected by a "light", so that could slow things down a little and make for some complicated code if you have several objects, especially moving ones.


Neochrome(Posted 2004) [#10]
i dont realy understand LightMesh, either its not explained very well or more likely i just simply dont understand, has anyone here got away to explain it in more, (idiot proof?) hehe


JoshK(Posted 2004) [#11]
Use a function to color all vertices black, then use lightmesh. Only do it before a render.

For even faster lighting, just make all the vertices one uniform color. This should be an option in your game menu.


Isaac P(Posted 2004) [#12]
Halo you got any ideas for having maybe 10-15 fake lights in the seen merely lighting verts.. its for a top down dungeon crawl game where i want torches to illuminate the dungeons


JoshK(Posted 2004) [#13]
Yeah, first do a distance check, and don't light the mesh if it is out of range.

Second, just calculate the distance between the light and mesh for the light intensity. Don't check distance for every vertex.

Finally, take the crossproduct of the normal of the vertex and the vector between the mesh and the light, and multiply that by the distance intensity from step 2 to get the color for each vertex.

This is the method I am using in my engine, and it's very fast.


Picklesworth(Posted 2004) [#14]
hey, thanks for that halo. I think I'll try this soon so I can play about with proper math stuff.


Isaac P(Posted 2004) [#15]
Thanks halo... will have a try to implement what you said, sounds very technical but if i manage to understand what everything is I'll throw together a demo and see how fast it is.

Thankyou :)


JoshK(Posted 2004) [#16]
This has a recursive and a smooth lighting option. The smooth lighting will keep your vertices from harshly flashing from one color to another. This function doesn't account for range, right now:

Function LightMesh_(mesh,r,g,b,range#=0,x#=0,y#=0,z#=0,smooth=0,rec=0)
If EntityClass(mesh)="Mesh"
Normalize x,y,z
dx#=VectorX()
dy#=VectorY()
dz#=VectorZ()
For s=1 To CountSurfaces(mesh)
surf=GetSurface(mesh,s)
For v=0 To CountVertices(surf)-1
intensity#=DotProduct(dx,dy,dz,VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v))
If intensity>0
If intensity>1 intensity=1
vr=VertexRed(surf,v)
vg=VertexGreen(surf,v)
vb=VertexBlue(surf,v)
If smooth
If vr<r
vr=vr+smooth
If vr>r*intensity vr=r*intensity
EndIf
If vg<g
vg=vg+smooth
If vg>g*intensity vg=g*intensity
EndIf
If vb<b
vb=vb+smooth
If vb>b*intensity vb=b*intensity
EndIf
If vr<AMBIENTRED vr=AMBIENTRED
If vg<AMBIENTGREEN vg=AMBIENTGREEN
If vb<AMBIENTBLUE vb=AMBIENTBLUE
Else
vr=vr+r*intensity
vg=vg+g*intensity
vb=vb+b*intensity
If vr>255 vr=255
If vg>255 vg=255
If vb>255 vb=255
EndIf
VertexTexCoords surf,v,vr/255.0,vg/255.0,0,1
VertexColor surf,v,vr,vg,vb,vb/255.0
EndIf
Next
Next
EndIf
If rec
For c=1 To CountChildren(mesh)
TFormVector x,y,z,mesh,GetChild(mesh,c)
LightMesh_(GetChild(mesh,c),r,g,b,range#,TFormedX(),TFormedY(),TFormedZ(),smooth,1)
Next
EndIf
End Function


JoshK(Posted 2004) [#17]
And then use this to darken it each lighting cycle. I would use a darken smooth value of about 20, and a liting smooth value of like 40. So this function would be like "MeshColor mesh,-20,-20,-20,true,true":

Function MeshColor(mesh,r,g,b,change=False,rec=0)
If EntityClass(mesh)="Mesh"
For s=1 To CountSurfaces(mesh)
surf=GetSurface(mesh,s)
For v=0 To CountVertices(surf)-1
If change
vr=VertexRed(surf,v)+r
vg=VertexGreen(surf,v)+g
vb=VertexBlue(surf,v)+b
If vr<AMBIENTRED vr=AMBIENTRED
If vg<AMBIENTGREEN vg=AMBIENTGREEN
If vb<AMBIENTBLUE vb=AMBIENTBLUE
If vr>255 vr=255
If vg>255 vg=255
If vb>255 vb=255
VertexColor surf,v,vr,vg,vb
;VertexTexCoords surf,v,(vr+r)/255.0,(vg+g)/255.0,0,1
;VertexColor surf,v,vr,vg,vb,(vb+b)/255.0
Else
VertexColor surf,v,r,g,b
EndIf
Next
Next
EndIf
If rec
For c=1 To CountChildren(mesh)
MeshColor GetChild(mesh,c),r,g,b,change,True
Next
EndIf
End Function


Isaac P(Posted 2004) [#18]
Thanks halo its as if you knew of my headache .. :)

one quick question

Function LightMesh_(mesh,r,g,b,range#=0,x#=0,y#=0,z#=0,smooth=0,rec=0)

x,y,z is for the position of the light right? what is range for? and rec?