This program displays a 'tumbling' gold pyramid, lit from the front by a single point-source light, to demonstrate the capabilities of the Direct3D lighting engine and the ease with which it can be used from BBC BASIC for Windows. It requires DirectX™ 8.0 or later to be installed on your PC. Updated to include specular reflections.
| Download LIGHTING.BBC | Run LIGHTING.EXE |
|---|
REM. Program to demonstrate use of Direct3D lighting from BBC BASIC for Windows
MODE 8
DIM pVB%(0), nv%(0), vf%(0), vl%(0), l%(0), m%(0), t%(0), y(0), p(0), r(0), X(0), Y(0), Z(0), eye(2), at(2), l%(0) 103, m%(0) 67, n(2)
INSTALL @lib$+"D3DLIB"
ON CLOSE PROCcleanup : OSCLI "DELETE MATERIAL.B3D" : QUIT
ON ERROR PROCcleanup : SYS "MessageBox", @hwnd%, REPORT$, 0, 48 : QUIT
REM. Initialise Direct3D and return a pointer to the IDirect3DDevice8 object:
pDevice% = FN_initd3d(@hwnd%, 2, 1)
IF pDevice% = 0 ERROR 100, "Cannot create Direct3D device"
SYS!(!pDevice%+200), pDevice%, 29, 1 : REM Enable specular
REM. Create 3D object with surface normals:
F% = OPENOUT"MATERIAL.B3D"
BPUT #F%,18 : BPUT#F%,0 : BPUT #F%,0 : BPUT#F%,0
BPUT #F%,&12 : BPUT #F%,0 : BPUT#F%,24 : BPUT#F%,0
FOR V% = 0 TO 5
READ x1, y1, z1, x2, y2, z2, x3, y3, z3
a = x2 - x3
b = y2 - y3
c = z2 - z3
d = x1 - x3
e = y1 - y3
f = z1 - z3
n(0) = b*f-c*e
n(1) = c*d-a*f
n(2) = a*e-b*d
n() /= MOD(n())
PROC4(x1) : PROC4(y1) : PROC4(z1) : PROC4(n(0)) : PROC4(n(1)) : PROC4(n(2))
PROC4(x2) : PROC4(y2) : PROC4(z2) : PROC4(n(0)) : PROC4(n(1)) : PROC4(n(2))
PROC4(x3) : PROC4(y3) : PROC4(z3) : PROC4(n(0)) : PROC4(n(1)) : PROC4(n(2))
NEXT
CLOSE #F%
REM. Load 3D object:
pVB%(0) = FN_load3d(pDevice%, "MATERIAL.B3D", nv%(0), vf%(0), vl%(0))
REM. Point-source light:
l%(0)!0 = 1 : REM. point source
l%(0)!4 = FN_f4(1) : REM. diffuse colour RGB
l%(0)!8 = FN_f4(1)
l%(0)!12 = FN_f4(1)
l%(0)!20 = FN_f4(1) : REM. specular colour RGB
l%(0)!24 = FN_f4(1)
l%(0)!28 = FN_f4(1)
l%(0)!52 = FN_f4(0) : REM. position XYZ
l%(0)!56 = FN_f4(0)
l%(0)!60 = FN_f4(-4)
l%(0)!76 = FN_f4(10): REM. range
l%(0)!84 = FN_f4(1) : REM. attenuation (constant)
REM. Gold-coloured material:
m%(0)!0 = FN_f4(1) : REM. diffuse colour RGB
m%(0)!4 = FN_f4(0.7)
m%(0)!8 = FN_f4(0)
m%(0)!32 = FN_f4(1) : REM. specular colour RGB
m%(0)!36 = FN_f4(1)
m%(0)!40 = FN_f4(1)
m%(0)!64 = FN_f4(32): REM. specular 'power'
REM. Render the tumbling object:
eye() = 0, 0, -9
at() = 0, 0, 0
REPEAT
y() = TIME/200
r() = TIME/80
PROC_render(pDevice%, &7F7F7F, 1, l%(), 1, m%(), t%(), pVB%(), nv%(), vf%(), vl%(), y(), p(), r(), X(), Y(), Z(), eye(), at(), PI/6, 1.25, 1, 1000)
UNTIL INKEY(1)=0
END
DEF PROCcleanup
pVB%(0) += 0 : IF pVB%(0) PROC_release(pVB%(0))
pDevice% += 0 : IF pDevice% PROC_release(pDevice%)
ENDPROC
DEF PROC4(a) : LOCAL A% : A%=FN_f4(a) : BPUT #F%,A% : BPUT #F%,A%>>8 : BPUT#F%,A%>>16 : BPUT#F%,A%>>24 : ENDPROC
REM. Pyramid (6 triangles):
DATA -1, -1, 1, -1, -1, -1, 0, .414, 0
DATA 1, -1, 1, -1, -1, 1, 0, .414, 0
DATA 1, -1, -1, 1, -1, 1, 0, .414, 0
DATA -1, -1, -1, 1, -1, -1, 0, .414, 0
DATA 1, -1, -1, -1, -1, -1, -1, -1, 1
DATA -1, -1, 1, 1, -1, 1, 1, -1, -1