Problem with uvīs / Lightmapping

Blitz3D Forums/Blitz3D Programming/Problem with uvīs / Lightmapping

cywhale(Posted 2003) [#1]
Hello.

Inspired by many very nice Screenshots around here iīm writing a little lightmapper. Lightmapping itself seems to work, the big problem is the right uv mapping ov a simple texture map - maybe iīm kind of blind right now, testing some days...

The function reads prev. generated separate lightmap for a triangle out of the type tri\.. , puts the map into the texturemap (one for all tris then) and tryīs to generate the vertex uv-coords for the tri.

Putting the maps into the big texture works, generating the uvīs seems to work too (the red dots in screenshot mark the uvīs) - but the mapped cube (yes, it is a cube...:( ) looks absolutely texture-distorted...

Screeny:


The Function:

Function create_lightmap(meshobj,size#,lmsize#,set=0)
	
        ; getting ticount
	For sys.lmsystem=Each lmsystem
		tricount=sys\tris
	Next
	
        ;getting pixeldimension of texture
	mapw#=Float(tricount)*size+Float(tricount)*4
	maph#=Float(size+4)
	mapxstep#=Float(1/mapw)
	mapystep#=Float(1/maph)
	x#=0
	y#=0
	
	lm=CreateTexture(mapw,maph)
	SetBuffer TextureBuffer(lm) 
	
	For tri.triangle=Each triangle
		DebugLog "Tri: "+t+" X:"+x
		Color t*20,t*20,t*20
		Rect x,y,size,size,1 	
		;CopyRect(0,0,size,size,x,0,TextureBuffer(tri\lightmap),TextureBuffer(lm))
		;Color 255,255,255
		;Rect x,0,size,size,0
		For vert=0 To 2
			u#=mapxstep*x+(tri\pu[vert]*(size*mapxstep))
			v#=maxystep*y+(tri\pv[vert]*(size*mapystep))
			DebugLog "VERTEX "+tri\vert[vert]+" Texturcoord: "+u*mapw+" "+v*maph+" - "+tri\pu[vert]+" "+tri\pv[vert]
			VertexTexCoords(tri\surface,tri\vert[vert],u,v,1,set)
			Color 255,0,0
			marker(u*mapw,v*maph,3,t)
		Next

		DebugLog ""
		x=x+size+4
		t=t+1
	Next
	
	SetBuffer BackBuffer()
	TextureCoords lm,set
	EntityTexture(meshobj,lm,0,set)
	mapimage=CreateImage(mapw+20,maph+20)
	CopyRect(0,0,mapw+20,maph+20,0,0,TextureBuffer(lm),ImageBuffer(mapimage))
	Return(lm)
End Function


Function marker(x#,y#,s#,t)
	Oval x-(s/2),y-(s/2),s,s,1
	SeedRnd MilliSecs()
	d=Rand(5,0)
	;Text x+d,y+d,t
End Function



The Cube is a bb3d-Entity, 1 Surface.
Debuglog gives all values right ?????

Please, if anyone has an idea, please help me...

hopefully, cy...


cywhale(Posted 2003) [#2]
Sorry, got it - 5 minīs after posting:
			u#=(tri\pu[vert] * (size /lmsize) )+(x/lmsize)
			v#=(tri\pv[vert] * (size / lmsize) )+(y/lmsize)


works...