Vertex Index Buffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /******************************************************************************
  2. Use 'VI' vertex index buffer for drawing simple 2D or 3D buffered graphics.
  3. /******************************************************************************/
  4. #if EE_PRIVATE
  5. enum VI_TYPE : Byte // VertexIndex vertex type
  6. {
  7. VI_NONE , // none
  8. VI_2D_FLAT , // pos
  9. VI_2D_COL , // pos+color
  10. VI_2D_TEX , // pos+tex
  11. VI_2D_TEX_COL , // pos+tex+color
  12. VI_2D_TEX2 , // pos+tex*2
  13. VI_2D_FONT , // pos+tex+shade
  14. VI_3D_FLAT , // pos
  15. VI_3D_COL , // pos+color
  16. VI_3D_TEX , // pos+tex
  17. VI_3D_TEX_COL , // pos+tex+color
  18. VI_3D_BILB , // pos+color+tex+size
  19. VI_3D_BILB_ANIM, // pos+color+tex+size+frame
  20. VI_3D_LASER , // pos+nrm
  21. VI_3D_SIMPLE , // pos+nrm+tex+color
  22. VI_3D_STANDARD , // pos+nrm+tan+bin+tex0+tex1+color
  23. VI_3D_FULL , // pos+nrm+tan+bin+hlp+tex+blend+matrix
  24. };
  25. enum VI_FLAG // VertexIndex flags
  26. {
  27. VI_LINE =0x1, // if drawing lines
  28. VI_STRIP =0x2, // if drawing strip
  29. VI_QUAD_IND=0x4, // if drawing quad using index buffer
  30. VI_SP_COL =0x8, // use ShaderParam color modifiers
  31. };
  32. enum VI_USER_FLAG // VertexIndex user flags
  33. {
  34. VI_CULL =1<<0, // perform triangle culling
  35. VI_ALPHA_TEST =1<<1, // perform alpha testing
  36. VI_FOG =1<<2, // make use of Fog
  37. VI_CUSTOM_TEX_WRAP =1<<3, // custom texture wrapping was specified
  38. VI_CUSTOM_DEPTH_WRITE=1<<4, // custom depth write was specified
  39. };
  40. /******************************************************************************/
  41. struct Vtx2DFlat
  42. {
  43. Vec2 pos;
  44. };
  45. struct Vtx2DCol
  46. {
  47. Vec2 pos;
  48. Color color;
  49. };
  50. struct Vtx2DTex2
  51. {
  52. Vec2 pos, tex[2];
  53. };
  54. struct Vtx2DFont
  55. {
  56. Vec2 pos, tex;
  57. Flt shade;
  58. };
  59. struct Vtx3DFlat
  60. {
  61. Vec pos;
  62. };
  63. struct Vtx3DCol
  64. {
  65. Vec pos;
  66. Color color;
  67. };
  68. struct Vtx3DBilb
  69. {
  70. Vec pos;
  71. #if GPU_HALF_SUPPORTED
  72. VecH4 vel_angle;
  73. #else
  74. Vec4 vel_angle;
  75. #endif
  76. VecB4 tex;
  77. Flt size;
  78. Color color;
  79. };
  80. struct Vtx3DBilbAnim : Vtx3DBilb
  81. {
  82. Flt frame;
  83. };
  84. struct Vtx3DLaser
  85. {
  86. Vec pos, nrm;
  87. };
  88. struct Vtx3DFull
  89. {
  90. Vec pos, hlp, nrm;
  91. Vec4 tan; // tan.xyz=tangent, tan.w=binormal direction (should be -1 or +1)
  92. Vec2 tex0, tex1, tex2;
  93. Color color;
  94. VecB4 material, matrix, blend;
  95. Flt size;
  96. void setNrmTan(C Vec &nrm, C Vec &tan ) {T.nrm=nrm; T.tan.set(tan, +1 );} // set normal tangent binormal from 'normal' 'tangent' , this method is faster, use it when you don't use mirroring in uv mapping or you don't use binormal in the shader at all
  97. void setNrmTan(C Vec &nrm, C Vec &tan, C Vec &bin) {T.nrm=nrm; T.tan.set(tan, (Dot(Cross(nrm, tan), bin)>0) ? +1 : -1);} // set normal tangent binormal from 'normal' 'tangent' 'binormal'
  98. };
  99. #endif
  100. struct Vtx2DTex
  101. {
  102. Vec2 pos, tex;
  103. };
  104. struct Vtx2DTexCol
  105. {
  106. Vec2 pos, tex;
  107. Color color;
  108. };
  109. struct Vtx3DTex
  110. {
  111. Vec pos;
  112. Vec2 tex;
  113. };
  114. struct Vtx3DTexCol
  115. {
  116. Vec pos;
  117. Vec2 tex;
  118. Color color;
  119. };
  120. struct Vtx3DSimple
  121. {
  122. Vec pos, nrm;
  123. Vec2 tex;
  124. Color color;
  125. };
  126. struct Vtx3DStandard
  127. {
  128. Vec pos, nrm;
  129. Vec4 tan; // tan.xyz=tangent, tan.w=binormal direction (should be -1 or +1)
  130. Vec2 tex;
  131. Color color;
  132. void setNrmTan(C Vec &nrm, C Vec &tan ) {T.nrm=nrm; T.tan.set(tan, +1 );} // set normal tangent binormal from 'normal' 'tangent' , this method is faster, use it when you don't use mirroring in uv mapping or you don't use binormal in the shader at all
  133. void setNrmTan(C Vec &nrm, C Vec &tan, C Vec &bin) {T.nrm=nrm; T.tan.set(tan, (Dot(Cross(nrm, tan), bin)>0) ? +1 : -1);} // set normal tangent binormal from 'normal' 'tangent' 'binormal'
  134. };
  135. /******************************************************************************/
  136. struct VtxIndBuf // Vertex Index Buffer - used for buffered drawing
  137. {
  138. // operations
  139. static void shader ( Shader *shader); // set custom shader
  140. static void image (C Image *image ); // set texture
  141. static void color (C Color &color ); // set color
  142. static void color2 (C Color &color ); // set color2
  143. static void cull (Bool cull ); // set face culling
  144. static void depthWrite(Bool on ); // set depth writing (this will affect only drawing that normally uses depth buffer)
  145. static void alphaTest (Bool on ); // set alpha testing (currently this is supported only for drawing faces using 'Vtx3DTex' and 'Vtx3DTexCol' as parameters)
  146. static void fog (Bool on ); // set fog usage (currently this is supported only for drawing faces using 'Vtx3DTex' and 'Vtx3DTexCol' as parameters), if this is enabled then any current 'Fog' will affect the face colors
  147. static void clamp ( ); // set texture addressing to (X:clamp, Y:clamp)
  148. static void wrap ( ); // set texture addressing to (X:wrap , Y:wrap )
  149. static void wrapX ( ); // set texture addressing to (X:wrap , Y:clamp)
  150. static void wrapY ( ); // set texture addressing to (X:clamp, Y:wrap )
  151. static void flush ( ); // flush queued data, calling this method is optional as 'end' method will automatically call it, you can call this method during drawing before changing some shader parameters
  152. static void end ( ); // flush and finish drawing, this must be called after each series of drawing
  153. // draw dots
  154. static void dot( C Vec2 &pos, Flt r=0.007f); // this uses 'VI.color'
  155. static void dot( C Vec &pos, Flt r=0.007f); // this uses 'VI.color'
  156. static void dot(C Color &color, C Vec2 &pos, Flt r=0.007f);
  157. static void dot(C Color &color, C Vec &pos, Flt r=0.007f);
  158. // draw rectangles
  159. static void rect( C Rect &rect); // this uses 'VI.color'
  160. static void rect(C Color &color, C Rect &rect);
  161. // draw non-filled rectangles (lines only)
  162. static void rectL( C Rect &rect); // this uses 'VI.color'
  163. static void rectL(C Color &color, C Rect &rect);
  164. // draw shaded rectangles
  165. static void rectShadedX(C Color &color0, C Color &color1, C Rect &rect); // shaded horizontally
  166. static void rectShadedY(C Color &color0, C Color &color1, C Rect &rect); // shaded vertically
  167. // draw lines
  168. static void line( C Vec2 &a, C Vec2 &b ); // this uses 'VI.color'
  169. static void line( C Vec2 &a, C Vec2 &b, Flt width); // this uses 'VI.color'
  170. static void line( C Vec &a, C Vec &b ); // this uses 'VI.color'
  171. static void line(C Color &color, C Vec2 &a, C Vec2 &b );
  172. static void line(C Color &color, C Vec &a, C Vec &b );
  173. static void line(C Color &col_a, C Color &col_b, C Vec2 &a, C Vec2 &b );
  174. static void line(C Color &col_a, C Color &col_b, C Vec &a, C Vec &b );
  175. // draw triangles
  176. static void tri( C Tri2 &tri ); // this uses 'VI.color'
  177. static void tri( C Tri &tri ); // this uses 'VI.color'
  178. static void tri( C Vec2 &a, C Vec2 &b, C Vec2 &c); // this uses 'VI.color'
  179. static void tri( C Vec &a, C Vec &b, C Vec &c); // this uses 'VI.color'
  180. static void tri(C Color &color, C Vec2 &a, C Vec2 &b, C Vec2 &c);
  181. static void tri(C Color &color, C Vec &a, C Vec &b, C Vec &c);
  182. static void tri(C Color &col_a, C Color &col_b, C Color &col_c, C Vec2 &a, C Vec2 &b, C Vec2 &c);
  183. static void tri(C Color &col_a, C Color &col_b, C Color &col_c, C Vec &a, C Vec &b, C Vec &c);
  184. // draw quads
  185. static void quad( C Quad2 &quad ); // this uses 'VI.color'
  186. static void quad( C Quad &quad ); // this uses 'VI.color'
  187. static void quad(C Color &color, C Quad2 &quad );
  188. static void quad(C Color &color, C Quad &quad );
  189. static void quad( C Vec2 &a, C Vec2 &b, C Vec2 &c, C Vec2 &d); // this uses 'VI.color'
  190. static void quad( C Vec &a, C Vec &b, C Vec &c, C Vec &d); // this uses 'VI.color'
  191. static void quad(C Color &color, C Vec2 &a, C Vec2 &b, C Vec2 &c, C Vec2 &d);
  192. static void quad(C Color &color, C Vec &a, C Vec &b, C Vec &c, C Vec &d);
  193. static void quad(C Color &col0 , C Color &col1, C Vec2 &a, C Vec2 &b, C Vec2 &c, C Vec2 &d);
  194. static void quad(C Color &col0 , C Color &col1, C Vec &a, C Vec &b, C Vec &c, C Vec &d);
  195. // draw image
  196. static void image (C Rect &screen_rect ); // this uses 'VI.color' and 'VI.color2' as "FinalColor = Image*color + color2"
  197. static void imagePart(C Rect &screen_rect, C Rect &tex_rect); // this uses 'VI.color' and 'VI.color2' as "FinalColor = Image*color + color2"
  198. #if EE_PRIVATE
  199. static void font (C Rect &screen_rect, C Rect &tex_rect);
  200. #endif
  201. // draw faces
  202. static void face(C Vtx2DTex &a, C Vtx2DTex &b, C Vtx2DTex &c);
  203. static void face(C Vtx2DTex &a, C Vtx2DTex &b, C Vtx2DTex &c, C Vtx2DTex &d);
  204. static void face(C Vtx2DTexCol &a, C Vtx2DTexCol &b, C Vtx2DTexCol &c);
  205. static void face(C Vtx2DTexCol &a, C Vtx2DTexCol &b, C Vtx2DTexCol &c, C Vtx2DTexCol &d);
  206. static void face(C Vtx3DTex &a, C Vtx3DTex &b, C Vtx3DTex &c);
  207. static void face(C Vtx3DTex &a, C Vtx3DTex &b, C Vtx3DTex &c, C Vtx3DTex &d);
  208. static void face(C Vtx3DTexCol &a, C Vtx3DTexCol &b, C Vtx3DTexCol &c);
  209. static void face(C Vtx3DTexCol &a, C Vtx3DTexCol &b, C Vtx3DTexCol &c, C Vtx3DTexCol &d);
  210. static void face(C Vtx3DSimple &a, C Vtx3DSimple &b, C Vtx3DSimple &c);
  211. static void face(C Vtx3DSimple &a, C Vtx3DSimple &b, C Vtx3DSimple &c, C Vtx3DSimple &d);
  212. static void face(C Vtx3DStandard &a, C Vtx3DStandard &b, C Vtx3DStandard &c);
  213. static void face(C Vtx3DStandard &a, C Vtx3DStandard &b, C Vtx3DStandard &c, C Vtx3DStandard &d);
  214. #if EE_PRIVATE
  215. static void face(C Vtx3DFull &a, C Vtx3DFull &b, C Vtx3DFull &c);
  216. static void face(C Vtx3DFull &a, C Vtx3DFull &b, C Vtx3DFull &c, C Vtx3DFull &d);
  217. void del ();
  218. void create ();
  219. void lost ();
  220. void reset ();
  221. static void clear ();
  222. static void setType (VI_TYPE vtx_type, UInt flag=0);
  223. static void setFirst(VI_TYPE vtx_type, UInt flag=0);
  224. // lock / unlock
  225. static Bool lockVtx(); static void unlockVtx();
  226. //static Bool lockInd(); static void unlockInd();
  227. // draw
  228. static void flushIndexed(IndBuf &ib, Int ind_num);
  229. // add
  230. static Ptr addVtx(Int vtxs);
  231. #endif
  232. #if !EE_PRIVATE
  233. private:
  234. #endif
  235. Bool _quad_ind, _depth_write;
  236. Byte _vtx_type, _flag, _user_flag;
  237. Int _mem_max,
  238. _vtx_queued, _vtx_drawing, _vtx_drawing_raw,
  239. _ind_div, _ind_sub;
  240. Color _color;
  241. C Image *_image;
  242. Shader *_shader;
  243. Byte *_vtx_buf;
  244. VtxFormat _vf2D_flat, _vf2D_col, _vf2D_tex, _vf2D_tex_col, _vf2D_tex2, _vf2D_font, _vf3D_flat, _vf3D_col, _vf3D_tex, _vf3D_tex_col, _vf3D_bilb, _vf3D_bilb_anim, _vf3D_laser, _vf3D_cloth, _vf3D_simple, _vf3D_standard, _vf3D_full;
  245. VtxBuf _vb;
  246. #if EE_PRIVATE
  247. GPU_API(D3DPRIMITIVETYPE, D3D11_PRIMITIVE_TOPOLOGY, UInt) _prim_type;
  248. #else
  249. UInt _prim_type;
  250. #endif
  251. VtxIndBuf();
  252. }extern
  253. VI; // Buffered Drawing
  254. /******************************************************************************/