VideoDriverGL.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "VideoDriverGL.h"
  2. #include "NativeTextureGLES.h"
  3. #include "oxgl.h"
  4. #include "math/Color.h"
  5. #include "utils/stringUtils.h"
  6. namespace oxygine
  7. {
  8. VideoDriverGL::VideoDriverGL(): _prevFBO(0), _batches(0), _triangles(0),
  9. _traceStats(true)
  10. {
  11. }
  12. void VideoDriverGL::getStats(Stats& s) const
  13. {
  14. s.batches = _batches;
  15. s.triangles = _triangles;
  16. }
  17. unsigned int VideoDriverGL::getPT(IVideoDriver::PRIMITIVE_TYPE pt)
  18. {
  19. switch (pt)
  20. {
  21. case PT_POINTS:
  22. return GL_POINTS;
  23. case PT_LINES:
  24. return GL_LINES;
  25. case PT_LINE_LOOP:
  26. return GL_LINE_LOOP;
  27. case PT_LINE_STRIP:
  28. return PT_LINE_STRIP;
  29. case PT_TRIANGLES:
  30. return GL_TRIANGLES;
  31. case PT_TRIANGLE_STRIP:
  32. return GL_TRIANGLE_STRIP;
  33. case PT_TRIANGLE_FAN:
  34. return GL_TRIANGLE_FAN;
  35. }
  36. OX_ASSERT(!"unknown primitive type");
  37. return PT_POINTS;
  38. }
  39. unsigned int VideoDriverGL::getBT(IVideoDriver::BLEND_TYPE pt)
  40. {
  41. switch (pt)
  42. {
  43. case BT_ZERO:
  44. return GL_ZERO;
  45. case BT_ONE:
  46. return GL_ONE;
  47. case BT_SRC_COLOR:
  48. return GL_SRC_COLOR;
  49. case BT_ONE_MINUS_SRC_COLOR:
  50. return GL_ONE_MINUS_SRC_COLOR;
  51. case BT_SRC_ALPHA:
  52. return GL_SRC_ALPHA;
  53. case BT_ONE_MINUS_SRC_ALPHA:
  54. return GL_ONE_MINUS_SRC_ALPHA;
  55. case BT_DST_COLOR:
  56. return GL_DST_COLOR;
  57. case BT_DST_ALPHA:
  58. return GL_DST_ALPHA;
  59. case BT_ONE_MINUS_DST_ALPHA:
  60. return GL_ONE_MINUS_DST_ALPHA;
  61. case BT_ONE_MINUS_DST_COLOR:
  62. return GL_ONE_MINUS_DST_COLOR;
  63. }
  64. OX_ASSERT(!"unknown blend");
  65. return GL_ONE;
  66. }
  67. void VideoDriverGL::_debugAddPrimitives(IVideoDriver::PRIMITIVE_TYPE pt, int num)
  68. {
  69. if (!_traceStats)
  70. return;
  71. switch (pt)
  72. {
  73. case PT_TRIANGLE_STRIP:
  74. _triangles += num - 2;
  75. break;
  76. case PT_TRIANGLES:
  77. _triangles += num / 3;
  78. break;
  79. }
  80. _batches++;
  81. }
  82. bool VideoDriverGL::getScissorRect(Rect& r) const
  83. {
  84. GLboolean scrTest = glIsEnabled(GL_SCISSOR_TEST);
  85. GLint box[4];
  86. glGetIntegerv(GL_SCISSOR_BOX, box);
  87. r = Rect(box[0], box[1], box[2], box[3]);
  88. CHECKGL();
  89. return scrTest ? true : false;
  90. }
  91. const VertexDeclarationGL* VideoDriverGL::getVertexDeclaration(bvertex_format fmt) const
  92. {
  93. return _vdeclarations.get(fmt);
  94. }
  95. void VideoDriverGL::getViewport(Rect& r) const
  96. {
  97. GLint vp[4];
  98. glGetIntegerv(GL_VIEWPORT, vp);
  99. r = Rect(vp[0], vp[1], vp[2], vp[3]);;
  100. //log::messageln("vp %d %d %d %d", vp[0], vp[1], vp[2], vp[3]);
  101. CHECKGL();
  102. }
  103. void VideoDriverGL::setScissorRect(const Rect* rect)
  104. {
  105. if (rect)
  106. {
  107. glEnable(GL_SCISSOR_TEST);
  108. glScissor(rect->getX(), rect->getY(), rect->getWidth(), rect->getHeight());
  109. }
  110. else
  111. glDisable(GL_SCISSOR_TEST);
  112. CHECKGL();
  113. }
  114. void VideoDriverGL::setRenderTarget(spNativeTexture rt)
  115. {
  116. if (!rt)
  117. {
  118. oxglBindFramebuffer(GL_FRAMEBUFFER, _prevFBO);
  119. CHECKGL();
  120. return;
  121. }
  122. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_prevFBO);
  123. NativeTextureGLES* gl = safeCast<NativeTextureGLES*>(rt.get());
  124. oxglBindFramebuffer(GL_FRAMEBUFFER, gl->getFboID());
  125. CHECKGL();
  126. }
  127. void VideoDriverGL::_begin(const Rect& viewport, const Color* clearColor)
  128. {
  129. // log::messageln("begin %d %d %d %d", viewport.pos.x, viewport.pos.y, viewport.size.x, viewport.size.y);
  130. glViewport(viewport.getX(), viewport.getY(), viewport.getWidth(), viewport.getHeight());
  131. glDisable(GL_SCISSOR_TEST);
  132. if (clearColor)
  133. {
  134. glClearColor(clearColor->getRedF(), clearColor->getGreenF(), clearColor->getBlueF(), clearColor->getAlphaF());
  135. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  136. }
  137. else
  138. {
  139. glClear(GL_DEPTH_BUFFER_BIT);
  140. }
  141. CHECKGL();
  142. }
  143. void VideoDriverGL::setBlendFunc(BLEND_TYPE src, BLEND_TYPE dest)
  144. {
  145. glBlendFunc(getBT(src), getBT(dest));
  146. CHECKGL();
  147. }
  148. void VideoDriverGL::setState(STATE state, unsigned int value)
  149. {
  150. switch (state)
  151. {
  152. case STATE_BLEND:
  153. if (value)
  154. glEnable(GL_BLEND);
  155. else
  156. glDisable(GL_BLEND);
  157. break;
  158. default:
  159. OX_ASSERT(!"unknown state");
  160. }
  161. CHECKGL();
  162. }
  163. /*
  164. void VertexDeclarationGL::init(bvertex_format fmt)
  165. {
  166. bformat = fmt;
  167. int i = 0;
  168. int offset = 0;
  169. VertextElement *dest = elements;
  170. if (fmt & VERTEX_POSITION)
  171. {
  172. strcpy(dest->name, "position");
  173. dest->elemType = GL_FLOAT;
  174. dest->size = 3;
  175. dest->offset = offset;
  176. dest->index = i;
  177. dest->normalized = false;
  178. offset += sizeof(float) * 3;
  179. ++i;
  180. ++dest;
  181. }
  182. if (fmt & VERTEX_COLOR)
  183. {
  184. strcpy(dest->name, "color");
  185. dest->elemType = GL_UNSIGNED_BYTE;
  186. dest->size = 4;
  187. dest->offset = offset;
  188. dest->index = i;
  189. dest->normalized = true;
  190. offset += 4;
  191. ++i;
  192. ++dest;
  193. }
  194. int numTexCoords = numTextureCoordinates(fmt);
  195. for (int j = 0; j < numTexCoords; ++j )
  196. {
  197. int coordSize = texCoordSize(j, fmt);
  198. if (j == 0)
  199. strcpy(dest->name, "uv");
  200. else
  201. safe_sprintf(dest->name, "uv%d", j + 1);
  202. dest->elemType = GL_FLOAT;
  203. dest->size = coordSize;
  204. dest->offset = offset;
  205. dest->index = i;
  206. dest->normalized = true;
  207. offset += sizeof(float) * coordSize;
  208. ++i;
  209. ++dest;
  210. }
  211. int userSize = userDataSize(fmt);
  212. if (userSize > 0)
  213. {
  214. dest->elemType = GL_FLOAT;
  215. dest->size = userSize;
  216. dest->offset = offset;
  217. dest->index = i;
  218. dest->normalized = false;
  219. offset += sizeof(float) * userSize;
  220. ++i;
  221. ++dest;
  222. }
  223. numElements = i;
  224. size = offset;
  225. }
  226. VertexDeclarationGL declarations[8];
  227. const VertexDeclarationGL *VertexDeclarationGL::getDeclaration(bvertex_format bformat)
  228. {
  229. for (int i = 0; i < 8; ++i)
  230. {
  231. VertexDeclarationGL &decl = declarations[i];
  232. if (decl.bformat == 0)
  233. {
  234. decl.init(bformat);
  235. }
  236. if (decl.bformat == bformat)
  237. return &decl;
  238. }
  239. OX_ASSERT(0);
  240. return 0;
  241. }
  242. */
  243. }