SpriteBatch.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "Base.h"
  2. #include "SpriteBatch.h"
  3. #include "Game.h"
  4. // Default size of a newly created sprite batch
  5. #define SPRITE_BATCH_DEFAULT_SIZE 128
  6. // Factor to grow a sprite batch by when its size is exceeded
  7. #define SPRITE_BATCH_GROW_FACTOR 2.0f
  8. // Macro for adding a sprite to the batch
  9. #define SPRITE_ADD_VERTEX(vtx, vx, vy, vz, vu, vv, vr, vg, vb, va) \
  10. vtx.x = vx; vtx.y = vy; vtx.z = vz; \
  11. vtx.u = vu; vtx.v = vv; \
  12. vtx.r = vr; vtx.g = vg; vtx.b = vb; vtx.a = va
  13. // Default sprite vertex shader
  14. #define SPRITE_VSH \
  15. "uniform mat4 u_projectionMatrix;\n" \
  16. "attribute vec3 a_position;\n" \
  17. "attribute vec2 a_texCoord;\n" \
  18. "attribute vec4 a_color;\n" \
  19. "varying vec2 v_texCoord;\n" \
  20. "varying vec4 v_color;\n" \
  21. "void main()\n" \
  22. "{\n" \
  23. "gl_Position = u_projectionMatrix * vec4(a_position, 1);\n" \
  24. "v_texCoord = a_texCoord;\n" \
  25. "v_color = a_color;\n" \
  26. "}\n"
  27. // Default sprite fragment shader
  28. #define SPRITE_FSH \
  29. "#ifdef OPENGL_ES\n" \
  30. "precision highp float;\n" \
  31. "#endif\n" \
  32. "varying vec2 v_texCoord;\n" \
  33. "varying vec4 v_color;\n" \
  34. "uniform sampler2D u_texture;\n" \
  35. "void main()\n" \
  36. "{\n" \
  37. "gl_FragColor = v_color * texture2D(u_texture, v_texCoord);\n" \
  38. "}\n"
  39. namespace gameplay
  40. {
  41. static Effect* __spriteEffect = NULL;
  42. SpriteBatch::SpriteBatch()
  43. : _batch(NULL), _textureWidthRatio(0.0f), _textureHeightRatio(0.0f)
  44. {
  45. }
  46. SpriteBatch::SpriteBatch(const SpriteBatch& copy)
  47. {
  48. // hiddden
  49. }
  50. SpriteBatch::~SpriteBatch()
  51. {
  52. SAFE_DELETE(_batch);
  53. if (!_customEffect)
  54. {
  55. if (__spriteEffect->getRefCount() == 1)
  56. {
  57. __spriteEffect->release();
  58. __spriteEffect = NULL;
  59. }
  60. else
  61. {
  62. __spriteEffect->release();
  63. }
  64. }
  65. }
  66. SpriteBatch* SpriteBatch::create(const char* texturePath, Effect* effect, unsigned int initialCapacity)
  67. {
  68. Texture* texture = Texture::create(texturePath);
  69. SpriteBatch* batch = SpriteBatch::create(texture);
  70. SAFE_RELEASE(texture);
  71. return batch;
  72. }
  73. SpriteBatch* SpriteBatch::create(Texture* texture, Effect* effect, unsigned int initialCapacity)
  74. {
  75. GP_ASSERT(texture != NULL);
  76. bool customEffect = (effect != NULL);
  77. if (!customEffect)
  78. {
  79. // Create our static sprite effect.
  80. if (__spriteEffect == NULL)
  81. {
  82. __spriteEffect = Effect::createFromSource(SPRITE_VSH, SPRITE_FSH);
  83. if (__spriteEffect == NULL)
  84. {
  85. GP_ERROR("Unable to load sprite effect.");
  86. return NULL;
  87. }
  88. effect = __spriteEffect;
  89. }
  90. else
  91. {
  92. effect = __spriteEffect;
  93. __spriteEffect->addRef();
  94. }
  95. }
  96. // Search for the first sampler uniform in the effect.
  97. Uniform* samplerUniform = NULL;
  98. for (unsigned int i = 0, count = effect->getUniformCount(); i < count; ++i)
  99. {
  100. Uniform* uniform = effect->getUniform(i);
  101. if (uniform && uniform->getType() == GL_SAMPLER_2D)
  102. {
  103. samplerUniform = uniform;
  104. break;
  105. }
  106. }
  107. if (!samplerUniform)
  108. {
  109. GP_ERROR("No uniform of type GL_SAMPLER_2D found in sprite effect.");
  110. SAFE_RELEASE(effect);
  111. return NULL;
  112. }
  113. // Wrap the effect in a material
  114. Material* material = Material::create(effect); // +ref effect
  115. // Set initial material state
  116. material->getStateBlock()->setBlend(true);
  117. material->getStateBlock()->setBlendSrc(RenderState::BLEND_SRC_ALPHA);
  118. material->getStateBlock()->setBlendDst(RenderState::BLEND_ONE_MINUS_SRC_ALPHA);
  119. // Bind the texture to the material as a sampler
  120. Texture::Sampler* sampler = Texture::Sampler::create(texture); // +ref texture
  121. material->getParameter(samplerUniform->getName())->setValue(sampler);
  122. SAFE_RELEASE(sampler);
  123. // Define the vertex format for the batch
  124. VertexFormat::Element vertexElements[] =
  125. {
  126. VertexFormat::Element(VertexFormat::POSITION, 3),
  127. VertexFormat::Element(VertexFormat::TEXCOORD0, 2),
  128. VertexFormat::Element(VertexFormat::COLOR, 4)
  129. };
  130. VertexFormat vertexFormat(vertexElements, 3);
  131. // Create the mesh batch
  132. MeshBatch* meshBatch = MeshBatch::create(vertexFormat, Mesh::TRIANGLE_STRIP, material, true, initialCapacity > 0 ? initialCapacity : SPRITE_BATCH_DEFAULT_SIZE);
  133. material->release(); // don't call SAFE_RELEASE since material is used below
  134. // Create the batch
  135. SpriteBatch* batch = new SpriteBatch();
  136. batch->_customEffect = customEffect;
  137. batch->_batch = meshBatch;
  138. batch->_textureWidthRatio = 1.0f / (float)texture->getWidth();
  139. batch->_textureHeightRatio = 1.0f / (float)texture->getHeight();
  140. // Bind an ortho projection to the material by default (user can override with setProjectionMatrix)
  141. Game* game = Game::getInstance();
  142. Matrix::createOrthographicOffCenter(0, game->getWidth(), game->getHeight(), 0, 0, 1, &batch->_projectionMatrix);
  143. material->getParameter("u_projectionMatrix")->bindValue(batch, &SpriteBatch::getProjectionMatrix);
  144. return batch;
  145. }
  146. void SpriteBatch::begin()
  147. {
  148. _batch->begin();
  149. }
  150. void SpriteBatch::draw(const Rectangle& dst, const Rectangle& src, const Vector4& color)
  151. {
  152. // Calculate uvs.
  153. float u1 = _textureWidthRatio * src.x;
  154. float v1 = 1.0f - _textureHeightRatio * src.y;
  155. float u2 = u1 + _textureWidthRatio * src.width;
  156. float v2 = v1 - _textureHeightRatio * src.height;
  157. draw(dst.x, dst.y, dst.width, dst.height, u1, v1, u2, v2, color);
  158. }
  159. void SpriteBatch::draw(const Vector3& dst, const Rectangle& src, const Vector2& scale, const Vector4& color)
  160. {
  161. // Calculate uvs.
  162. float u1 = _textureWidthRatio * src.x;
  163. float v1 = 1.0f - _textureHeightRatio * src.y;
  164. float u2 = u1 + _textureWidthRatio * src.width;
  165. float v2 = v1 - _textureHeightRatio * src.height;
  166. draw(dst.x, dst.y, dst.z, scale.x, scale.y, u2, v2, u1, v1, color);
  167. }
  168. void SpriteBatch::draw(const Vector3& dst, const Rectangle& src, const Vector2& scale, const Vector4& color,
  169. const Vector2& rotationPoint, float rotationAngle)
  170. {
  171. // Calculate uvs.
  172. float u1 = _textureWidthRatio * src.x;
  173. float v1 = 1.0f - _textureHeightRatio * src.y;
  174. float u2 = u1 + _textureWidthRatio * src.width;
  175. float v2 = v1 - _textureHeightRatio * src.height;
  176. draw(dst, scale.x, scale.y, u1, v1, u2, v2, color, rotationPoint, rotationAngle);
  177. }
  178. void SpriteBatch::draw(const Vector3& dst, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color,
  179. const Vector2& rotationPoint, float rotationAngle, bool positionIsCenter)
  180. {
  181. draw(dst.x, dst.y, dst.z, width, height, u1, v1, u2, v2, color, rotationPoint, rotationAngle, positionIsCenter);
  182. }
  183. void SpriteBatch::draw(float x, float y, float z, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color,
  184. const Vector2& rotationPoint, float rotationAngle, bool positionIsCenter)
  185. {
  186. // Treat the given position as the center if the user specified it as such.
  187. if (positionIsCenter)
  188. {
  189. x -= 0.5f * width;
  190. y -= 0.5f * height;
  191. }
  192. // Expand the destination position by scale into 4 points.
  193. float x2 = x + width;
  194. float y2 = y + height;
  195. Vector2 upLeft(x, y);
  196. Vector2 upRight(x2, y);
  197. Vector2 downLeft(x, y2);
  198. Vector2 downRight(x2, y2);
  199. // Rotate points around rotationAxis by rotationAngle.
  200. Vector2 pivotPoint(rotationPoint);
  201. pivotPoint.x *= width;
  202. pivotPoint.y *= height;
  203. pivotPoint.x += x;
  204. pivotPoint.y += y;
  205. upLeft.rotate(pivotPoint, rotationAngle);
  206. upRight.rotate(pivotPoint, rotationAngle);
  207. downLeft.rotate(pivotPoint, rotationAngle);
  208. downRight.rotate(pivotPoint, rotationAngle);
  209. // Write sprite vertex data.
  210. static SpriteVertex v[4];
  211. SPRITE_ADD_VERTEX(v[0], downLeft.x, downLeft.y, z, u1, v1, color.x, color.y, color.z, color.w);
  212. SPRITE_ADD_VERTEX(v[1], upLeft.x, upLeft.y, z, u1, v2, color.x, color.y, color.z, color.w);
  213. SPRITE_ADD_VERTEX(v[2], downRight.x, downRight.y, z, u2, v1, color.x, color.y, color.z, color.w);
  214. SPRITE_ADD_VERTEX(v[3], upRight.x, upRight.y, z, u2, v2, color.x, color.y, color.z, color.w);
  215. static unsigned short indices[4] = { 0, 1, 2, 3 };
  216. _batch->add(v, 4, indices, 4);
  217. }
  218. void SpriteBatch::draw(const Vector3& position, const Vector3& right, const Vector3& forward, float width, float height,
  219. float u1, float v1, float u2, float v2, const Vector4& color, const Vector2& rotationPoint, float rotationAngle)
  220. {
  221. // Calculate the vertex positions.
  222. Vector3 tRight(right);
  223. tRight *= width * 0.5f;
  224. Vector3 tForward(forward);
  225. tForward *= height * 0.5f;
  226. Vector3 p0 = position;
  227. p0 -= tRight;
  228. p0 -= tForward;
  229. Vector3 p1 = position;
  230. p1 += tRight;
  231. p1 -= tForward;
  232. tForward = forward;
  233. tForward *= height;
  234. Vector3 p2 = p0;
  235. p2 += tForward;
  236. Vector3 p3 = p1;
  237. p3 += tForward;
  238. // Calculate the rotation point.
  239. Vector3 rp = p0;
  240. tRight = right;
  241. tRight *= width * rotationPoint.x;
  242. tForward *= rotationPoint.y;
  243. rp += tRight;
  244. rp += tForward;
  245. // Rotate all points the specified amount about the given point (about the up vector).
  246. static Vector3 u;
  247. Vector3::cross(right, forward, &u);
  248. static Matrix rotation;
  249. Matrix::createRotation(u, rotationAngle, &rotation);
  250. p0 -= rp;
  251. p0 *= rotation;
  252. p0 += rp;
  253. p1 -= rp;
  254. p1 *= rotation;
  255. p1 += rp;
  256. p2 -= rp;
  257. p2 *= rotation;
  258. p2 += rp;
  259. p3 -= rp;
  260. p3 *= rotation;
  261. p3 += rp;
  262. // Add the sprite vertex data to the batch.
  263. static SpriteVertex v[4];
  264. SPRITE_ADD_VERTEX(v[0], p0.x, p0.y, p0.z, u1, v1, color.x, color.y, color.z, color.w);
  265. SPRITE_ADD_VERTEX(v[1], p1.x, p1.y, p1.z, u2, v1, color.x, color.y, color.z, color.w);
  266. SPRITE_ADD_VERTEX(v[2], p2.x, p2.y, p2.z, u1, v2, color.x, color.y, color.z, color.w);
  267. SPRITE_ADD_VERTEX(v[3], p3.x, p3.y, p3.z, u2, v2, color.x, color.y, color.z, color.w);
  268. static const unsigned short indices[4] = { 0, 1, 2, 3 };
  269. _batch->add(v, 4, const_cast<unsigned short*>(indices), 4);
  270. }
  271. void SpriteBatch::draw(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color)
  272. {
  273. draw(x, y, 0, width, height, u1, v1, u2, v2, color);
  274. }
  275. void SpriteBatch::draw(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, const Rectangle& clip)
  276. {
  277. // Only draw if at least part of the sprite is within the clip region.
  278. if (clipSprite(clip, x, y, width, height, u1, v1, u2, v2))
  279. draw(x, y, 0, width, height, u1, v1, u2, v2, color);
  280. }
  281. void SpriteBatch::addSprite(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, SpriteBatch::SpriteVertex* vertices)
  282. {
  283. GP_ASSERT(vertices);
  284. const float x2 = x + width;
  285. const float y2 = y + height;
  286. SPRITE_ADD_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w);
  287. SPRITE_ADD_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w);
  288. SPRITE_ADD_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w);
  289. SPRITE_ADD_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w);
  290. }
  291. void SpriteBatch::addSprite(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, const Rectangle& clip, SpriteBatch::SpriteVertex* vertices)
  292. {
  293. GP_ASSERT(vertices);
  294. // Only add a sprite if at least part of the sprite is within the clip region.
  295. if (clipSprite(clip, x, y, width, height, u1, v1, u2, v2))
  296. {
  297. const float x2 = x + width;
  298. const float y2 = y + height;
  299. SPRITE_ADD_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w);
  300. SPRITE_ADD_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w);
  301. SPRITE_ADD_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w);
  302. SPRITE_ADD_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w);
  303. }
  304. }
  305. void SpriteBatch::draw(SpriteBatch::SpriteVertex* vertices, unsigned int vertexCount, unsigned short* indices, unsigned int indexCount)
  306. {
  307. GP_ASSERT(vertices);
  308. GP_ASSERT(indices);
  309. _batch->add(vertices, vertexCount, indices, indexCount);
  310. }
  311. void SpriteBatch::draw(float x, float y, float z, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, bool positionIsCenter)
  312. {
  313. // Treat the given position as the center if the user specified it as such.
  314. if (positionIsCenter)
  315. {
  316. x -= 0.5f * width;
  317. y -= 0.5f * height;
  318. }
  319. // Write sprite vertex data.
  320. const float x2 = x + width;
  321. const float y2 = y + height;
  322. static SpriteVertex v[4];
  323. SPRITE_ADD_VERTEX(v[0], x, y, z, u1, v1, color.x, color.y, color.z, color.w);
  324. SPRITE_ADD_VERTEX(v[1], x, y2, z, u1, v2, color.x, color.y, color.z, color.w);
  325. SPRITE_ADD_VERTEX(v[2], x2, y, z, u2, v1, color.x, color.y, color.z, color.w);
  326. SPRITE_ADD_VERTEX(v[3], x2, y2, z, u2, v2, color.x, color.y, color.z, color.w);
  327. static unsigned short indices[4] = { 0, 1, 2, 3 };
  328. _batch->add(v, 4, indices, 4);
  329. }
  330. void SpriteBatch::end()
  331. {
  332. // Finish and draw the batch
  333. _batch->end();
  334. _batch->draw();
  335. }
  336. RenderState::StateBlock* SpriteBatch::getStateBlock() const
  337. {
  338. return _batch->getMaterial()->getStateBlock();
  339. }
  340. Material* SpriteBatch::getMaterial() const
  341. {
  342. return _batch->getMaterial();
  343. }
  344. void SpriteBatch::setProjectionMatrix(const Matrix& matrix)
  345. {
  346. _projectionMatrix = matrix;
  347. }
  348. const Matrix& SpriteBatch::getProjectionMatrix() const
  349. {
  350. return _projectionMatrix;
  351. }
  352. bool SpriteBatch::clipSprite(const Rectangle& clip, float& x, float& y, float& width, float& height, float& u1, float& v1, float& u2, float& v2)
  353. {
  354. // Clip the rectangle given by { x, y, width, height } into clip.
  355. // We need to scale the uvs accordingly as we do this.
  356. // First check to see if we need to draw at all.
  357. if (x + width < clip.x || x > clip.x + clip.width ||
  358. y + height < clip.y || y > clip.y + clip.height)
  359. {
  360. return false;
  361. }
  362. const float uvWidth = u2 - u1;
  363. const float uvHeight = v2 - v1;
  364. // Moving x to the right.
  365. if (x < clip.x)
  366. {
  367. const float percent = (clip.x - x) / width;
  368. const float dx = clip.x - x;
  369. x = clip.x;
  370. width -= dx;
  371. u1 += uvWidth * percent;
  372. }
  373. // Moving y down.
  374. if (y < clip.y)
  375. {
  376. const float percent = (clip.y - y) / height;
  377. const float dy = clip.y - y;
  378. y = clip.y;
  379. height -= dy;
  380. v1 += uvHeight * percent;
  381. }
  382. // Moving width to the left.
  383. const float clipX2 = clip.x + clip.width;
  384. float x2 = x + width;
  385. if (x2 > clipX2)
  386. {
  387. const float percent = (x2 - clipX2) / width;
  388. width = clipX2 - x;
  389. u2 -= uvWidth * percent;
  390. }
  391. // Moving height up.
  392. const float clipY2 = clip.y + clip.height;
  393. float y2 = y + height;
  394. if (y2 > clipY2)
  395. {
  396. const float percent = (y2 - clipY2) / height;
  397. height = clipY2 - y;
  398. v2 -= uvHeight * percent;
  399. }
  400. return true;
  401. }
  402. }