@lidongheng
Hello,
I'm adding a code snippet that demonstrates what setting a normal does to the lighting on a matrix. The code itself may be too complex for a beginner to understand but the point is to show you visually how setting a normal will change the shading on a matrix.
You will see a 5x5 matrix with little blue lines going up to a yellow square.
The lines are showing the direction that the normals are pointing for each vertex on the matrix. The yellow box helps to show where the end of the normal is and is also where you can LEFT LICK to drag the line and change the normal.
The controls are:
ARROW KEYS to move the camera forward,backward, left and right
Hold the RIGHT MOUSE BUTTON and move the mouse to rotate the camera view left or right, up and down
Hold LEFT MOUSE BUTTON on or near a yellow square and move the mouse to change the normals direction.
When you change the normal, you should see the area around the normal get brighter or darker depending on which direction you move the line.
At the top of the screen you will be told which vertex you changed on the matrix and the values of the normal at that position and how SET MATRIX NORMAL was used.
The light source comes from the left and slightly above the matrix and shines to the right so you can see the effects of changing the normals more clearly.
This example works in both DBC 1.13 and DBC 1.20 - though 1.13 is noticeably darker. Again, the point of this demonstration is to show you what the normals do to change how a matrix responds to light.
remstart
==============================================================
= Title : observe matrix normal changes
= Author : latch
= Date : 01/21/2011
= Update :
= Version: .01
==============================================================
Comments
Using DBC 1.20, change the normals for a matrix and
observe the results real time. Lines will be drawn
showing the direction of the matrix normals. These
can be changed to change the matrix normals.
==============================================================
remend
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
autocam off
set display mode 800,600,32
sync on
sync rate 60
rem =============================================================
rem = MAIN
rem =============================================================
_main:
gosub _init
white=rgb(255,255,255)
do
ink white,0
n$=str$(normx)+","+str$(normz)
text 0,0,"Last Normal updated at "+n$
text 0,20,"The command sent was"
text 0,40,"SET MATRIX NORMAL "+str$(mat)+","+n$+","+str$(nx#)+","+str$(ny#)+","+str$(nz#)
gosub _draw_selection_boxes
gosub _move_camera
sync
loop
end
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
_init:
rem grid for matrix
ink rgb(255,255,255),0
box 1,1,62,62
get image 1,0,0,64,64
sync
rem matrix variables
mat=1
matx#=200.0
matz#=200.0
tilex=5
tilez=5
xsize#=matx#/tilex
zsize#=matz#/tilez
rem create matrix and make it bumpy
make matrix mat,matx#,matz#,tilex,tilez
randomize matrix mat,25
set matrix wireframe off mat
prepare matrix texture mat,1,1,1
rem create objects to position and control normals on matrix
obj=0
obj1=1000
for z=0 to tilez
for x=0 to tilex
inc obj
inc obj1
make object plain obj,1,1
make object plain obj1,1,1
hide object obj
hide object obj1
xpos#=x*xsize#
zpos#=z*zsize#
position object obj,xpos#,get matrix height(mat,x,z),zpos#
position object obj1,xpos#,get matrix height(mat,x,z)+10,zpos#
next x
next z
rem change lighting to more easily see the effects of the normals
gosub _lighting
rem start position for the camera
position camera 50,50,-50
xrotate camera 30
return
`================================================================
_draw_selection_boxes:
rem draw normal lines and selection boxes to adjust normals
for sb=1001 to obj1
rem only draw stuff that's in the view
if object in screen(sb)
x1=object screen x(sb-1000)
y1=object screen y(sb-1000)
x2=object screen x(sb)
y2=object screen y(sb)
x3=x2-10 : y3=y2-10
x4=x2+10 : y4=y2+10
ink rgb(0,255,255),0
line x1,y1,x2,y2
ink rgb(255,255,0),0
box x2-2,y2-2,x2+2,y2+2
rem check for mouse click
if mouseclick()=1
mx=mousex()
my=mousey()
if mouse_within(mx,my,x3,y3,x4,y4)
while mouseclick()=1
rem move the mouse to redraw the normal line - reposition top object
newx#=newxvalue(object position x(sb),wrapvalue(camera angle y()+90),mousemovex()/2.0)
newz#=newzvalue(object position z(sb),wrapvalue(camera angle y()+90),mousemovex()/2.0)
newy#=object position y(sb)-(mousemovey()/2.0)
position object sb,newx#,newy#,newz#
x2=object screen x(sb)
y2=object screen y(sb)
position mouse x2,y2
x3=x2-10 : y3=y2-10
x4=x2+10 : y4=y2+10
ink rgb(0,255,255),0
line x1,y1,x2,y2
ink rgb(255,255,0),0
box x2-2,y2-2,x2+2,y2+2
rem calculate the new matrix vertex normal - first get the matrix
rem normal x and z position
normx=object position x(sb-1000)/xsize#
normz=object position z(sb-1000)/zsize#
rem since the normal is a zero based vector, we have to adjust
rem for that
nx#=newx#-object position x(sb-1000)
nz#=newz#-object position z(sb-1000)
ny#=newy#-object position y(sb-1000)
rem get the magnitude of the vector so we can normalize the normal
mag#=sqrt(nx#^2+ny#^2+nz#^2)
nx#=nx#/mag#
ny#=ny#/mag#
nz#=nz#/mag#
rem now set the matrix normal
set matrix normal mat,normx,normz,nx#,ny#,nz#
update matrix mat
sync
endwhile
endif
endif
endif
next sb
return
`================================================================
_move_camera:
if mouseclick()=2
xang#=wrapvalue(camera angle x()+mousemovey())
yang#=wrapvalue(camera angle y()+mousemovex())
rotate camera xang#,yang#,0
endif
move camera (upkey()-downkey())
ncx#=newxvalue(camera position x(),wrapvalue(yang#+90),rightkey()-leftkey())
ncz#=newzvalue(camera position z(),wrapvalue(yang#+90),rightkey()-leftkey())
position camera ncx#,camera position y(),ncz#
return
`================================================================
_lighting:
color ambient light 0
set directional light 0,1,-.1,0
return
rem =============================================================
rem = FUNCTIONS
rem =============================================================
function mouse_within(mx,my,x1,y1,x2,y2)
if mx > x1
if mx < x2
if my > y1
if my < y2
exitfunction 1
endif
endif
endif
endif
endfunction 0
Enjoy your day.