Hi all,
this is the second part of the challenge of LEE BAMBER, updated.(texture shifting on a 3D object)
You will find the first part here:
http://forum.thegamecreators.com/?m=forum_view&t=200186&b=10
challenge two:
Rem Project: ShadowPlaneOnAnObject
Rem Created: 28/10/2005 16:33:27
rem Technique: A casting object provides the XYZ coordinates to place
rem a shadow using UV data manipulation of a transparent shadow mesh.
rem Because of the characteristics of DBC, ghost mode is used for a translucent shade.
rem So, tranparence level of the shadow is controlled by the ambient light specific to the object "shadow"
rem synchro
sync on : sync rate 30
` *=---------------------------------------------------------------------=*
`
`Init FUNCTIONS for vertexdata manipulation
dim sf_currentmemblockmesh(0)
`
` *=---------------------------------------------------------------------=*
rem Make white shadow texture(the color will be reversed in ghost mode)
cls rgb(0,0,0)
ink rgb(255,255,255),0
box 3,3,13,13
get image 1,0,0,16,16
rem Make the object
make object box 1,5,1,5
make mesh from object 1,1 : limbnb=0
inc limbnb : add limb 1,limbnb,1 : offset limb 1,1,3,0.25,4
inc limbnb : add limb 1,limbnb,1 : offset limb 1,2,-3,0.25,4
inc limbnb : add limb 1,limbnb,1 : offset limb 1,3,-3,0.25,-4
inc limbnb : add limb 1,limbnb,1 : offset limb 1,4,3,0.25,-4
`
`If the number of vertices is not divisible by 3, there is a display bug on the last polygon object.
`Adding an invisible polygon (without visible surface) to avoid this.
make object triangle 2,0,0,0,0,0,0,0,0,0
make mesh from object 2,2
inc limbnb : add limb 1,limbnb,2
delete mesh 2 : delete object 2
`
make mesh from object 1,1
delete object 1 : make object 1,1,0
color object 1,rgb(255,255,128)
rem Make the caster
make object box 2,1,1,1
position object 2,0,2.0,0
color object 2,rgb(255,0,0)
rem "Shadow" object(object=3, mesh=1, texture=1)
make object 3,1,1
set object texture 3,2,0
set object diffuse 3,0
`SET OBJECT object number, solid, opaque, polygon hidden, texture filtering
set object 3 , 1 , 0 , 1 , 1
`ghost mode 1: white and black are reversed
ghost object on 3,1
rem create memblock object, for vertex manipulation
if memblock exist(100) then delete memblock 100
make memblock from mesh 100,1
set current memblockmesh(100)
rem init camera
ink rgb(255,255,128),0
position camera 4,10,-10
point camera 1.5,0.8,0
autocam off
rem loop
do
`
rem move catser
if upkey()=1 then mov object(2,0.2)
if downkey()=1 then mov object(2,-0.2)
if leftkey()=1 then mov object left(2,0.2)
if rightkey()=1 then mov object right(2,0.2)
if ambientmp<255
if shiftkey()=1 then mov object up(2,0.2)
endif
if ambientmp>0
if controlkey()=1 then mov object down(2,0.2)
endif
if chall=1 and returnkey()=1
y#=wrapvalue(object angle y(2)+2)
rotate object 2,0,y#,0
endif
if chall=2 and returnkey()=1
x#=wrapvalue(object angle x(2)+1)
y#=wrapvalue(object angle y(2)+2)
z#=wrapvalue(object angle z(2)+3)
rotate object 2,x#,y#,z#
endif
`
rem update UV+tranparence level of the shadow
gosub update_uv_of_shadow_object
`
rem User prompt
set cursor 0,0
print "L E E S C H A L L E N G E T W O"
print
print "Use Arrow Keys to Move, SHIFT and CONTROL raise and lower the Caster Object"
if chall=0
print "Press SPACE to view the challenge"
if spacekey()=1
while spacekey()=1 : sync : endwhile
chall=1
endif
else
if chall=1
print "Use the RETURN Key to Y rotate the Caster Object"
print "Press SPACE for bonus challenge"
print
print "The challenge is to modify the Update UV subroutine so that the Y axis"
print "rotation of the caster object also rotates the shadow via the UV data!"
if spacekey()=1 then chall=2
else
print "Use the RETURN Key to rotate the Caster Object in all directions"
print "The bonus challenge is to modify the Update UV subroutine so that the full"
print "rotation of the caster object creates a realistic shadow using UV data only!"
print "The illustration in the corner shows the 'ideal' shadow, which you must"
print "approximate as closely as you can without changing the texture or shadow mesh"
endif
endif
`
rem bonus challenge
if chall=2
if object exist(10)=0
make object cube 10,3 : lock object on 10 : position object 10,9,-5,16
make object cube 11,3 : lock object on 11 : position object 11,9,-16,16
color object 10,rgb(255,0,0)
endif
rotate object 10,object angle x(10)+1,object angle y(10)+2,object angle z(10)+3
make mesh from object 11,10
delete object 11 : make object 11,11,0
lock object on 11 : position object 11,9,-16,16
color object 11,0
scale object 11,100,1,100
center text 545,270,"BONUS CHALLENGE"
endif
`
`refresh mesh
UPDATE OBJECT FROM MESH(3,1)
rem update
sync
loop
update_uv_of_shadow_object:
`
rem uses XYZ of target object
x#=object position x(2)
z#=object position z(2)
perc10#=object position y(2)-1.0
if perc10#<0.0 then perc10#=0.0
if perc10#>10.0 then perc10#=10.0
size#=3.0-((2.0/10.0)*perc10#)
`ratio of 0.65 avoids the shadow becomes completely invisible
ambientmp=(25.5*perc10#)*0.65
ambien=(ambientmp*2^16)+(ambientmp*2^8)+(ambientmp)
`
rem update UV data
polynum=get vertexdata vertex count()/3
for p=0 to polynum-1
`
rem which poly
vo=p*3
`
rem Get polygon vertices
x0#=get vertexdata position x(vo+0)
z0#=get vertexdata position z(vo+0)
x1#=get vertexdata position x(vo+1)
z1#=get vertexdata position z(vo+1)
x2#=get vertexdata position x(vo+2)
z2#=get vertexdata position z(vo+2)
`
rem Work out UV-coords from Polygon Vs XY-coords
u0# = (x0#-x#)*size#*0.25+0.5 : v0# = (z0#-z#)*size#*0.25+0.5
u1# = (x1#-x#)*size#*0.25+0.5 : v1# = (z1#-z#)*size#*0.25+0.5
u2# = (x2#-x#)*size#*0.25+0.5 : v2# = (z2#-z#)*size#*0.25+0.5
`
rem write UV DATA
set vertexdata uv(vo+0,u0#,v0#)
set vertexdata uv(vo+1,u1#,v1#)
set vertexdata uv(vo+2,u2#,v2#)
`and AMBIENT(tranparence level of the shadow)
set object ambient 3,ambien
`
rem Next polygon
next p
`
return
end
`FUNCTIONS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function set current memblockmesh(mem)
sf_currentmemblockmesh(0)=mem
endfunction
function GET VERTEXDATA VERTEX COUNT()
mem=sf_currentmemblockmesh(0)
result=memblock dword(mem,0)
endfunction result
function GET VERTEXDATA POSITION X(vertex_number)
mem=sf_currentmemblockmesh(0)
offset=32
result#=memblock float(mem,offset+(12*vertex_number))
endfunction result#
function GET VERTEXDATA POSITION Y(vertex_number)
mem=sf_currentmemblockmesh(0)
offset=36
result#=memblock float(mem,offset+(12*vertex_number))
endfunction result#
function GET VERTEXDATA POSITION Z(vertex_number)
mem=sf_currentmemblockmesh(0)
offset=40
result#=memblock float(mem,offset+(12*vertex_number))
endfunction result#
function SET VERTEXDATA UV(vertex_number, U#, V#)
mem=sf_currentmemblockmesh(0)
offsetU=memblock dword(mem,28)
offsetV=memblock dword(mem,28)+4
write memblock float mem,offsetU+(8*vertex_number),U#
write memblock float mem,offsetV+(8*vertex_number),V#
endfunction
function UPDATE OBJECT FROM MESH(obj,mesh)
mem=sf_currentmemblockmesh(0)
change mesh from memblock mesh,mem
change mesh obj,0,mesh
endfunction
function mov object(obj,v#)
x#=wrapvalue(object angle x(obj))
y#=wrapvalue(object angle y(obj))
z#=wrapvalue(object angle z(obj))
rotate object obj,0,0,0
move object obj,v#
rotate object obj,x#,y#,z#
endfunction
function mov object left(obj,v#)
x#=wrapvalue(object angle x(obj))
y#=wrapvalue(object angle y(obj))
z#=wrapvalue(object angle z(obj))
`
rotate object obj,0,270,0
move object obj,v#
rotate object obj,x#,y#,z#
endfunction
function mov object right(obj,v#)
x#=wrapvalue(object angle x(obj))
y#=wrapvalue(object angle y(obj))
z#=wrapvalue(object angle z(obj))
`
rotate object obj,0,90,0
move object obj,v#
rotate object obj,x#,y#,z#
endfunction
function mov object up(obj,v#)
x#=object angle x(obj)
y#=object angle y(obj)
z#=object angle z(obj)
`
rotate object obj,270,0,0
move object obj,v#
rotate object obj,x#,y#,z#
endfunction
function mov object down(obj,v#)
x#=object angle x(obj)
y#=object angle y(obj)
z#=object angle z(obj)
`
rotate object obj,90,0,0
move object obj,v#
rotate object obj,x#,y#,z#
endfunction
enjoy!
@+
DirectX 9.0c (February 2010)/ DBClassic v1.20