page8.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <font face="verdana">
  2. <table border="1" width="100%" bordercolor="#FFFFFF">
  3. <tr>
  4. <td width="61%" bgcolor="#6097C9"><font face="verdana">
  5. <h1 align="left"><font color="#FFFFFF">&nbsp;Vertexes</font></h1>
  6. <p align="left"><b>&nbsp;Aim</b>: To edit a meshes vertexes in Real-time<br>
  7. &nbsp;<b>Skill level</b>: Intermediate<br>
  8. &nbsp;<b>Files Needed</b>: vertex.bb / plane.3ds</font>
  9. <p>&nbsp;</td>
  10. <td width="5%">&nbsp;</td>
  11. <td width="34%">
  12. <p align="center"><font face="verdana">
  13. <img src="10c.jpg" alt="Movement" width="167" height="127" align="right">
  14. </font>
  15. </p>
  16. </td>
  17. </tr>
  18. </table>
  19. <p>
  20. If you've already read the section on Meshes beforehand then hopefully you'll
  21. know most of what we will be doing already in this tutorial, if not - go away
  22. and read it first !.</p>
  23. <p>
  24. let's recap:</p>
  25. <ul>
  26. <li>Each <b>MESH</b> (a 3d object), is made up of SURFACES.<br>
  27. </li>
  28. <li>Each <b>SURFACE</b> has a BRUSH.<br>
  29. </li>
  30. <li>Each <b>BRUSH</b> can be assigned 8 different texturemaps.<br>
  31. (which can be overlaid on each other to create new effects)<br>
  32. </li>
  33. <li>Each SURFACE is made up of TRIANGLES.<br>
  34. </li>
  35. <li>Each <b>TRIANGLE</b> is made up of 3 VERTEXES.</li>
  36. </ul>
  37. <p>So, armed with that info - you should know what makes a 3d object tick !.
  38. Lets take a flat square as an example, it is made up of 4 vertexes and 2
  39. triangles. What we are planning of doing is to take 2 of those vertexes and
  40. change their coordinates.<br>
  41. </p>
  42. <p>Infact as mentioned in the Introduction to Meshes, we can even change the
  43. colour of the vertexes in realtime too. Run the example - what you can hopefully
  44. see is a square object (which is slowly spinning on the Z plane), being pulled
  45. out of shape in 2 corners - while every-so-often the colours change.</p>
  46. <p>It's a very easy effect to create, I wont go into great detail about how/why
  47. the program works - but here's a quick rundown if your interested:</p>
  48. <p>We setup the variable 'COUNTER', which does exactly that.. to be used as a
  49. counter. Every time the program runs through its main loop, it is incremented.
  50. Based on what value the counter is equal to, corresponds to what direction we
  51. should pull the vertexes. If the counter reaches 1000 then change the colour of
  52. each vertex to a random selection, before resetting the counter value.</p>
  53. <p>Let's take a look:</p>
  54. <table border="1" width="86%" bordercolor="#FFFFFF">
  55. <tr>
  56. <td width="14%">&nbsp;</td>
  57. <td width="72%" bordercolor="#000000" bgcolor="#C0C0C0">&nbsp;<br>
  58. &nbsp;
  59. </font>
  60. <font face="arial">Graphics3D 800,600<br>
  61. <br>
  62. &nbsp;&nbsp; SetBuffer BackBuffer()<br>
  63. <br>
  64. &nbsp;&nbsp; camera=CreateCamera()<br>
  65. &nbsp;&nbsp; CameraViewport camera,0,0,800,600<br>
  66. <br>
  67. &nbsp;&nbsp; light=CreateLight()<br>
  68. <br>
  69. &nbsp;&nbsp; plane=LoadMesh(&quot;plane.3ds&quot;)<br>
  70. &nbsp;&nbsp; PositionEntity plane,0,0,25<br>
  71. &nbsp;&nbsp; EntityFX plane,2<br>
  72. <br>
  73. &nbsp;&nbsp; surface=GetSurface(plane,CountSurfaces(plane))<br>
  74. <br>
  75. &nbsp;&nbsp; VertexColor surface,0,255,0,0<br>
  76. &nbsp;&nbsp; VertexColor surface,1,0,255,0<br>
  77. &nbsp;&nbsp; VertexColor surface,2,0,0,255<br>
  78. &nbsp;&nbsp; VertexColor surface,3,255,0,255<br>
  79. <br>
  80. &nbsp;&nbsp; While Not KeyHit(1)<br>
  81. <br>
  82. &nbsp;&nbsp; TurnEntity plane,0,0,.3<br>
  83. <br>
  84. &nbsp;&nbsp; counter=counter+1<br>
  85. <br>
  86. &nbsp;&nbsp; If counter&lt;500 Then&nbsp;<br>
  87. &nbsp;&nbsp; x1#=-.01<br>
  88. &nbsp;&nbsp; y1#=-.01<br>
  89. &nbsp;&nbsp; x2#=+.01<br>
  90. &nbsp;&nbsp; EndIf<br>
  91. <br>
  92. &nbsp;&nbsp; If counter>499 Then<br>
  93. &nbsp;&nbsp; x1#=+.01<br>
  94. &nbsp;&nbsp; y1#=+.01<br>
  95. &nbsp;&nbsp; x2#=-.01<br>
  96. &nbsp;&nbsp; EndIf<br>
  97. <br>
  98. &nbsp;&nbsp; xx#=VertexX(surface,0)<br>
  99. &nbsp;&nbsp; yy#=VertexY(surface,0)<br>
  100. &nbsp;&nbsp; zz#=VertexZ(surface,0)<br>
  101. <br>
  102. &nbsp;&nbsp; VertexCoords surface,0,xx+x1,yy+y1,zz<br>
  103. <br>
  104. &nbsp;&nbsp; xx#=VertexX(surface,2)<br>
  105. &nbsp;&nbsp; yy#=VertexY(surface,2)<br>
  106. &nbsp;&nbsp; zz#=VertexZ(surface,2)<br>
  107. <br>
  108. &nbsp;&nbsp; VertexCoords surface,2,xx+x2,yy+y1,zz<br>
  109. <br>
  110. &nbsp;&nbsp; If counter=1000 Then<br>
  111. &nbsp;&nbsp; counter=0<br>
  112. &nbsp;&nbsp; VertexColor surface,0,Rnd#(0,255),Rnd#(0,255),Rnd#(0,255)<br>
  113. &nbsp;&nbsp; VertexColor surface,1,Rnd#(0,255),Rnd#(0,255),Rnd#(0,255)<br>
  114. &nbsp;&nbsp; VertexColor surface,2,Rnd#(0,255),Rnd#(0,255),Rnd#(0,255)<br>
  115. &nbsp;&nbsp; VertexColor surface,3,Rnd#(0,255),Rnd#(0,255),Rnd#(0,255)<br>
  116. &nbsp;&nbsp; EndIf<br>
  117. <br>
  118. &nbsp;&nbsp; UpdateWorld<br>
  119. &nbsp;&nbsp; RenderWorld<br>
  120. <br>
  121. &nbsp;&nbsp; Text 350,500,"Vertex Control"<br>
  122. <br>
  123. &nbsp;&nbsp; Flip<br>
  124. <br>
  125. &nbsp;&nbsp; Wend<br>
  126. &nbsp;&nbsp; End<br><br></font>
  127. </td>
  128. <td width="14%">&nbsp;</td>
  129. </tr>
  130. </table>
  131. <p>
  132. So how do we get at the vertexes of the object ?</p>
  133. <p>
  134. Well for starters we load the object with the LoadMesh command, the object we
  135. are loading is of course called Plane.3ds.</p>
  136. <table border="0" width="100%" bgcolor="#6097C9">
  137. <tr>
  138. <td width="100%">
  139. <b><font face="Arial">EntityFX plane,2</font></b></td>
  140. </tr>
  141. </table>
  142. <font face="verdana">
  143. <p>Now here's a new command we haven't seen before !, this command is really
  144. more of mode switch than anything else. But setting values we can access the
  145. entity in different ways. the mode value '2' is to able vertex colouring on the
  146. whole entity, by default this is turned off.</p>
  147. <p>Here's those mode settings:</p>
  148. </font>
  149. <table border="0" width="100%" bgcolor="#FFFF00">
  150. <tr>
  151. <td width="100%">
  152. <font face="Arial"><i>1 = Full-Bright<br>
  153. 2 = Use Vertex Colours<br>
  154. 4 = Flatshading<br>
  155. 8 = Disable Fog</i></font></td>
  156. </tr>
  157. </table>
  158. <font face="verdana">
  159. <p>There is another command very similar to EntitiyFX called BRUSHFX. This uses
  160. the same mode settings, but instead of changing the whole entity will work on a
  161. single brush. (remember a mesh has surfaces, with brushes applied to them)</p>
  162. </font>
  163. <table border="0" width="100%" bgcolor="#6097C9">
  164. <tr>
  165. <td width="100%">
  166. <b><font face="Arial">surface=GetSurface(plane,CountSurfaces(plane))</font></b></td>
  167. </tr>
  168. </table>
  169. <font face="verdana">
  170. <p>
  171. In order to get at the vertexes we must first unlock them, we do this by
  172. creating a pointer variable that holds the memory address to the surfaces on the
  173. mesh.</p>
  174. <p>
  175. Calm down !, we don't have to get our hands dirty calling with lots of nasty math's
  176. - instead we just use the GETSURFACE command, which likes us to pass firstly the
  177. mesh name - and secondly the amount of surfaces it has. As you can see I've
  178. cheated and used the COUNTSURFACES command to do this for me.</p>
  179. </font>
  180. <table border="0" width="100%" bgcolor="#6097C9">
  181. <tr>
  182. <td width="100%">
  183. <font face="Arial"><b>VertexColor surface,0,255,0,0<br>
  184. VertexColor surface,1,0,255,0<br>
  185. VertexColor surface,2,0,0,255<br>
  186. VertexColor surface,3,255,0,255</b></font>
  187. </td>
  188. </tr>
  189. </table>
  190. <font face="verdana">
  191. <p>Before going into the main loop, I've set the colour of each vertex to a
  192. different colour. This gives us a nice rainbow effect !. As you can see we pass
  193. the pointer variable SURFACE to the VERTEXCOLOR command, as well as the vertex
  194. number (0-3, since our object only has 4 points) - followed by the colour values
  195. for the Red, Green and Blue shades. (must be in the range of 0 (Dark) through to
  196. 255 (Light))</p>
  197. </font>
  198. <table border="0" width="100%" bgcolor="#6097C9">
  199. <tr>
  200. <td width="100%">
  201. <font face="Arial"><b>xx#=VertexX(surface,0)<br>
  202. yy#=VertexY(surface,0)<br>
  203. zz#=VertexZ(surface,0)</b></font>
  204. </td>
  205. </tr>
  206. </table>
  207. <font face="verdana">
  208. <p>Since I want the coordinates of the mesh to change all the time, I cant set
  209. it with a value that doesn't change. Every update I've got to get the current
  210. coordinates and slightly update them (by adding an offset to the X and Y coords).</p>
  211. <p>I do this by firstly, getting the current X,Y and Z vertex coords - using the
  212. various get vertex commands.</p>
  213. <p>VertexX(surface,0) - gives us access to the X coordinate of the object
  214. surface, at vertex 0.</p>
  215. <p>Just as, VertexY(surface,99) - would give us access to the Y coodinate of
  216. vertex 99 !!!.</p>
  217. <table border="0" width="100%" bgcolor="#6097C9">
  218. <tr>
  219. <td width="100%">
  220. <font face="Arial"><b>VertexCoords surface,0,xx+x1,yy+y1,zz</b></font>
  221. </td>
  222. </tr>
  223. </table>
  224. <p>
  225. As you've probably worked out by now, this is the main instruction for changing
  226. the actual Vertex positions. It needs to be called with the Surface pointer
  227. value, followed by the new values for the X, Y and Z positions.</p>
  228. <p>
  229. And that's all there is to it !!</p>
  230. <p>
  231. But why would you want to change the coordinates ?</p>
  232. <p>
  233. All games will alter their objects, its just a case of working out how, and
  234. where they do it. Imagine you've just written a driving simulation.. wouldn't it
  235. be nice when you crash the car to reflect the damage ?. Perhaps crumple that
  236. fender.. or crack that window.</p>
  237. <p>
  238. Just like a certain other car game currently in the charts, they use exactly the
  239. same method. You gotta hand it to B3D - You want it.. it's there, now go and use it wisely !.</p>
  240. <p align="center"><font color="#808080"><b>More Tutorials to follow..</b><br>
  241. Tutorials written by Paul Gerfen<br>
  242. (c) 2001GameCodingUK
  243. </font>