TGC Codebase Backup



UV Mapping To Bitmap by moqzart

15th Aug 2010 22:20
Summary

Draws the UV co-ordinates of vertices onto a bitmap.



Description

This short program maps out the UV co-ordinates of 3D objects onto a bitmap and saves the result. Some objects use only vertice data and others use indices, this code covers both. DB Pro generated cones and spheres produce odd results. I haven't worked out why, however I have routines for generating these particular shapes with good results.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    bmw=512
bmh=512
create bitmap 1, bmw, bmh
cls rgb(255,255,255)
ink 0,rgb(255,255,255)
set current bitmap 0

object = 1

`make object triangle object, 50,100,0, -50,100,0, 0,0,0    // uses vertice count
`make object plain object,100,100                           // uses vertice count
`make object cone object,100                                // uses indices count-produces an odd result
`make object cylinder object, 100                           // uses vertice count
`make object cube object, 100                               // uses indices count
`make object box object,100,110,120                         // uses indices count
`make object sphere object, 200                             // uses indices count-produces an odd result

repeat
	turn object right object,0.25
until mouseclick()=1
while mouseclick()=1
	turn object right object,0.25
endwhile
map_uvdata_to_bitmap(object,0,1)
set current bitmap 1
get image 1, 0, 0, bmw, bmh, 3
set current bitmap 0
texture object object,1
repeat
	turn object right object,0.25
until mouseclick()=1
while mouseclick()=1
	turn object right object,0.25
endwhile
f$ = "uvmap.bmp"
if file exist(f$) then delete file f$
save image f$, 1
delete bitmap 1
delete image 1
delete object object
end

function map_uvdata_to_bitmap(obj,limb,bm)
	lock vertexdata for limb obj, limb
	indices = get vertexdata index count()
	if indices > 0
		i = 0
	    repeat
			inc i
			v = get indexdata(i)
			u1# = get vertexdata u(v)
			v1# = get vertexdata v(v)
			inc i
			v = get indexdata(i)
			u2# = get vertexdata u(v)
			v2# = get vertexdata v(v)
			inc i
			v = get indexdata(i)
			u3# = get vertexdata u(v)
			v3# = get vertexdata v(v)
			map_polygon_to_bitmap(bm,0.0,0.0,bitmap width(bm),bitmap height(bm),u1#,v1#,u2#,v2#,u3#,v3#)
		until i >= indices
	else
	    count = get vertexdata vertex count()
		for v = 0 to count  step 3
			u1# = get vertexdata u(v)
			v1# = get vertexdata v(v)
			u2# = get vertexdata u(v+1)
			v2# = get vertexdata v(v+1)
			u3# = get vertexdata u(v+2)
			v3# = get vertexdata v(v+2)
			map_polygon_to_bitmap(bm,0.0,0.0,bitmap width(bm),bitmap height(bm),u1#,v1#,u2#,v2#,u3#,v3#)
		next v
	endif
	unlock vertexdata
endfunction

function map_polygon_to_bitmap(bm, xoff#, yoff#, w#, h#, x1#, y1#, x2#, y2#, x3#, y3#)
	set current bitmap bm
	line W# * x1# + xoff#, h# * y1# + yoff#, w# * x2# + xoff#, h# * y2# + yoff#
	line W# * x2# + xoff#, h# * y2# + yoff#, w# * x3# + xoff#, h# * y3# + yoff#
	line W# * x3# + xoff#, h# * y3# + yoff#, w# * x1# + xoff#, h# * y1# + yoff#
	set current bitmap 0
endfunction