home *** CD-ROM | disk | FTP | other *** search
/ DarkBasic Professional / DarkBasicPro.iso / data1.cab / Lang_Files_(English) / Help / examples / 3dmaths / 3dmaths1-example.dba < prev    next >
Encoding:
Text File  |  2004-09-22  |  6.2 KB  |  184 lines

  1. rem 3DMaths Showcase
  2. rem A COMPLETE FVF, USAGE and
  3. rem TYPE list at end of program
  4.  
  5. ObjectNumber=1
  6. VertexShaderNumber=1
  7.  
  8. rem Constants for vertex shader
  9. #constant CSCALE          1
  10. #constant CTRANSLATION    2
  11. #constant CXROTATE        3
  12. #constant CYROTATE        4
  13. #constant CZROTATE        5
  14. #constant CROTATION       6
  15. #constant CWORLD          7
  16. #constant CVIEW           8
  17. #constant CPROJECTION     9
  18. #constant CTEMP           10
  19. #constant CLIGHT          11
  20. #constant CDEGTORAD       ( 3.14 / 180.0 )
  21.  
  22. rem Provide vertex shader with stream count
  23. set vertex shader streamcount VertexShaderNumber, 4
  24.  
  25. rem Provide vertex shader with stream
  26. VSDT_FLOAT2=0x01 : VSDT_FLOAT3=0x02 : VSDT_D3DCOLOR=0x04
  27. VSDE_POSITION=0 : VSDE_NORMAL=3 : VSDE_DIFFUSE=10 : VSDE_TEXCOORD0=5
  28. set vertex shader stream VertexShaderNumber, 1, VSDE_POSITION, VSDT_FLOAT3
  29. set vertex shader stream VertexShaderNumber, 2, VSDE_NORMAL, VSDT_FLOAT3
  30. set vertex shader stream VertexShaderNumber, 3, VSDE_DIFFUSE, VSDT_D3DCOLOR
  31. set vertex shader stream VertexShaderNumber, 4, VSDE_TEXCOORD0, VSDT_FLOAT2
  32.  
  33. rem Create a vertex shader from a text file
  34. create vertex shader from file VertexShaderNumber, "vshader.vsh"
  35.  
  36. rem Make simple object
  37. make object sphere ObjectNumber,100
  38.  
  39. rem Convert object to correct mesh format
  40. FVF_XYZ=0x002 : FVF_NORMAL=0x010 : FVF_DIFFUSE=0x040 : FVF_TEX1=0x100
  41. FVF_FINAL=FVF_XYZ || FVF_NORMAL || FVF_DIFFUSE || FVF_TEX1
  42. convert object fvf ObjectNumber,FVF_FINAL
  43.  
  44. rem Check if vertex shader was created
  45. if vertex shader exist(VertexShaderNumber)=1
  46.  
  47.  rem Apply vertex shader to object
  48.  set vertex shader on ObjectNumber, VertexShaderNumber
  49.  
  50.  rem Create required matrices
  51.  r=make matrix4(CVIEW)
  52.  r=make matrix4(CPROJECTION)
  53.  r=make matrix4(CSCALE)
  54.  r=make matrix4(CTRANSLATION)
  55.  r=make matrix4(CXROTATE)
  56.  r=make matrix4(CYROTATE)
  57.  r=make matrix4(CZROTATE)
  58.  r=make matrix4(CROTATION)
  59.  r=make matrix4(CWORLD)
  60.  r=make matrix4(CTEMP)
  61.  
  62.  rem required light vector
  63.  r=make vector4(CLIGHT)
  64.  
  65.  rem setup camera and light vector position
  66.  sync on : position camera 0,0,-500
  67.  set vector4 CLIGHT,50,20,-200,0
  68.  
  69.  rem Simple loop
  70.  while mouseclick()=0
  71.  
  72.   rem get view and projection matrices
  73.   view matrix4 CVIEW
  74.   projection matrix4 CPROJECTION
  75.  
  76.   rem setup a scale matrix
  77.   scale matrix4 CSCALE, 2.0, 2.0, 2.0
  78.  
  79.   rem now setup the position
  80.   translate matrix4 CTRANSLATION, 0.0, 10.0, 0.0
  81.  
  82.   rem setup rotation
  83.   rotate x matrix4 CXROTATE, 0.0 * CDEGTORAD
  84.   rotate y matrix4 CYROTATE, 0.0 * CDEGTORAD
  85.   rotate z matrix4 CZROTATE, 45.0 * CDEGTORAD
  86.  
  87.   rem multiply all 3 rotation matrices together
  88.   multiply matrix4 CROTATION, CXROTATE, CYROTATE
  89.   multiply matrix4 CROTATION, CROTATION, CZROTATE
  90.  
  91.   rem multiply all final matrices
  92.   multiply matrix4 CTEMP,  CROTATION, CSCALE
  93.   multiply matrix4 CWORLD, CTEMP,     CSCALE
  94.   multiply matrix4 CWORLD, CWORLD,    CVIEW
  95.   multiply matrix4 CWORLD, CWORLD,    CPROJECTION
  96.  
  97.   rem transpose the matrix
  98.   transpose matrix4 CWORLD, CWORLD
  99.  
  100.   rem we send the light vector3 to the vertex shader
  101.   normalize vector3 CLIGHT, CLIGHT
  102.   set vertex shader vector VertexShaderNumber, 4, CLIGHT, 4
  103.  
  104.   rem this is an important part รป we send the world matrix to the vertex shader
  105.   set vertex shader matrix VertexShaderNumber, 0, CWORLD, 4
  106.  
  107.   rem camera look
  108.   rotate camera camera angle x()+mousemovey(),camera angle y()+mousemovex(),0
  109.  
  110.   rem update screen
  111.   sync
  112.  
  113.  rem end loop
  114.  endwhile
  115.  
  116.  rem Deactivate vertex shader of the object
  117.  set vertex shader off ObjectNumber
  118.  
  119.  rem Delete a vertex shader
  120.  delete vertex shader VertexShaderNumber
  121.  
  122. endif
  123.  
  124. rem fvf
  125. `D3DFVF_RESERVED0        0x001
  126. `D3DFVF_POSITION_MASK    0x00E
  127. `D3DFVF_XYZ              0x002
  128. `D3DFVF_XYZRHW           0x004
  129. `D3DFVF_XYZB1            0x006
  130. `D3DFVF_XYZB2            0x008
  131. `D3DFVF_XYZB3            0x00a
  132. `D3DFVF_XYZB4            0x00c
  133. `D3DFVF_XYZB5            0x00e
  134. `D3DFVF_NORMAL           0x010
  135. `D3DFVF_PSIZE            0x020
  136. `D3DFVF_DIFFUSE          0x040
  137. `D3DFVF_SPECULAR         0x080
  138. `D3DFVF_TEX0             0x000
  139. `D3DFVF_TEX1             0x100
  140. `D3DFVF_TEX2             0x200
  141. `D3DFVF_TEX3             0x300
  142. `D3DFVF_TEX4             0x400
  143. `D3DFVF_TEX5             0x500
  144. `D3DFVF_TEX6             0x600
  145. `D3DFVF_TEX7             0x700
  146. `D3DFVF_TEX8             0x800
  147. `D3DFVF_LASTBETA_UBYTE4  0x1000
  148.  
  149. rem usages
  150. `D3DDECLUSAGE_POSITION = 0,
  151. `D3DDECLUSAGE_BLENDWEIGHT = 1,
  152. `D3DDECLUSAGE_BLENDINDICES = 2,
  153. `D3DDECLUSAGE_NORMAL = 3,
  154. `D3DDECLUSAGE_PSIZE = 4,
  155. `D3DDECLUSAGE_TEXCOORD = 5,
  156. `D3DDECLUSAGE_TANGENT = 6,
  157. `D3DDECLUSAGE_BINORMAL = 7,
  158. `D3DDECLUSAGE_TESSFACTOR = 8,
  159. `D3DDECLUSAGE_POSITIONT = 9,
  160. `D3DDECLUSAGE_COLOR = 10,
  161. `D3DDECLUSAGE_FOG = 11,
  162. `D3DDECLUSAGE_DEPTH = 12,
  163. `D3DDECLUSAGE_SAMPLE = 13
  164.  
  165. rem type values
  166. `D3DDECLTYPE_FLOAT1    =  0,  // 1D float expanded to (value, 0., 0., 1.)
  167. `D3DDECLTYPE_FLOAT2    =  1,  // 2D float expanded to (value, value, 0., 1.)
  168. `D3DDECLTYPE_FLOAT3    =  2,  // 3D float expanded to (value, value, value, 1.)
  169. `D3DDECLTYPE_FLOAT4    =  3,  // 4D float
  170. `D3DDECLTYPE_D3DCOLOR  =  4,  // 4D packed unsigned bytes mapped to 0. to 1. range
  171. `D3DDECLTYPE_UBYTE4    =  5,  // 4D unsigned byte
  172. `D3DDECLTYPE_SHORT2    =  6,  // 2D signed short expanded to (value, value, 0., 1.)
  173. `D3DDECLTYPE_SHORT4    =  7,  // 4D signed short
  174. `D3DDECLTYPE_UBYTE4N   =  8,  // Each of 4 bytes is normalized by dividing to 255.0
  175. `D3DDECLTYPE_SHORT2N   =  9,  // 2D signed short normalized (v[0]/32767.0,v[1]/32767.0,0,1)
  176. `D3DDECLTYPE_SHORT4N   = 10,  // 4D signed short normalized (v[0]/32767.0,v[1]/32767.0,v[2]/32767.0,v[3]/32767.0)
  177. `D3DDECLTYPE_USHORT2N  = 11,  // 2D unsigned short normalized (v[0]/65535.0,v[1]/65535.0,0,1)
  178. `D3DDECLTYPE_USHORT4N  = 12,  // 4D unsigned short normalized (v[0]/65535.0,v[1]/65535.0,v[2]/65535.0,v[3]/65535.0)
  179. `D3DDECLTYPE_UDEC3     = 13,  // 3D unsigned 10 10 10 format expanded to (value, value, value, 1)
  180. `D3DDECLTYPE_DEC3N     = 14,  // 3D signed 10 10 10 format expanded to (v[0]/511.0, v[1]/511.0, v[2]/511.0, `1)
  181. `D3DDECLTYPE_FLOAT16_2 = 15,  // Two 16-bit floating point values, expanded to (value, value, 0, 1)
  182. `D3DDECLTYPE_FLOAT16_4 = 16,  // Four 16-bit floating point values
  183. `D3DDECLTYPE_UNUSED    = 17,  // When the type field in a decl is unused.
  184.