Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright 2016 twisterge.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24.  
  25. /*
  26. * File: SpriteBatch.cpp
  27. * Author: twisterge
  28. *
  29. * Created on November 30, 2016, 10:39 AM
  30. */
  31.  
  32. #include <vector>
  33.  
  34. #include "SpriteBatch.h"
  35.  
  36. SpriteBatch::SpriteBatch(int width, int height) {
  37. glGenBuffers(1, &m_vbo);
  38. glGenVertexArrays(1, &m_vao);
  39.  
  40. glBindVertexArray(m_vao);
  41. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  42. glBufferData(GL_ARRAY_BUFFER, SB_BUFFER_SIZE, nullptr, GL_DYNAMIC_DRAW);
  43.  
  44. glEnableVertexAttribArray(0);
  45. glEnableVertexAttribArray(1);
  46. glEnableVertexAttribArray(2);
  47. glVertexAttribPointer(0, 3, GL_FLOAT, false, SB_VERTEX_SIZE, (void*) 0);
  48. glVertexAttribPointer(1, 2, GL_FLOAT, false, SB_VERTEX_SIZE, (void*)12);
  49. glVertexAttribPointer(2, 4, GL_FLOAT, true, SB_VERTEX_SIZE, (void*)20);
  50.  
  51. glBindVertexArray(0);
  52.  
  53. Str vss =
  54. "#version 120\n"
  55. "attribute vec3 position;\n"
  56. "attribute vec2 uv;\n"
  57. "attribute vec4 color;\n"
  58. "uniform mat4 projection;\n"
  59. "void main() {\n"
  60. " gl_Position = projection * vec4(position, 1.0);\n"
  61. "}";
  62.  
  63. Str fss =
  64. "#version 120\n"
  65. "void main() {\n"
  66. " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
  67. "}";
  68.  
  69. m_default = new Shader(vss, fss);
  70.  
  71. model.identity();
  72. projection = Matrix4f::createOrtho(0, width, height, 0, -1, 1);
  73.  
  74. m_drawing = false;
  75. }
  76.  
  77. void SpriteBatch::Begin() {
  78. if (!m_drawing) {
  79. m_drawing = true;
  80.  
  81. for (auto it = m_sprites.begin(); it != m_sprites.end(); ++it) {
  82. delete *it;
  83. }
  84.  
  85. for (auto it = m_batches.begin(); it != m_batches.end(); ++it) {
  86. delete *it;
  87. }
  88.  
  89. m_sprites.clear();
  90. m_batches.clear();
  91. }
  92. }
  93.  
  94. void SpriteBatch::End() {
  95. if (m_drawing) {
  96. if (m_sprites.size() == 0) {
  97. m_drawing = false;
  98. return;
  99. }
  100.  
  101. std::vector<Vertex> verts;
  102. verts.resize(m_sprites.size() * 6);
  103.  
  104. uint offset = 0, i = 0;
  105.  
  106. for (auto it = m_sprites.begin(); it != m_sprites.end(); ++it) {
  107. Sprite* spr = *it;
  108. RenderBatch* b = new RenderBatch();
  109. b->depth = spr->depth;
  110. b->diffuse = spr->diffuse;
  111. b->normal = spr->normal;
  112. b->vertex_count = 6;
  113. b->offset = offset;
  114. m_batches.push_back(b);
  115.  
  116. verts[i++] = spr->v1;
  117. verts[i++] = spr->v4;
  118. verts[i++] = spr->v3;
  119. verts[i++] = spr->v3;
  120. verts[i++] = spr->v2;
  121. verts[i++] = spr->v1;
  122.  
  123. offset += 6;
  124. }
  125.  
  126. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  127. glBufferSubData(GL_ARRAY_BUFFER, 0, verts.size() * SB_VERTEX_SIZE, verts.data());
  128. glBindBuffer(GL_ARRAY_BUFFER, 0);
  129. m_drawing = false;
  130. }
  131. }
  132.  
  133. void SpriteBatch::Render() {
  134. glBindVertexArray(m_vao);
  135.  
  136. m_default->Bind();
  137. m_default->SetMatrix4("projection", projection);
  138.  
  139. for (auto it = m_batches.begin(); it != m_batches.end(); ++it) {
  140. RenderBatch* b = *it;
  141.  
  142. if (b->diffuse != nullptr) {
  143. b->diffuse->Bind(0);
  144. }
  145. if (b->normal != nullptr) {
  146. b->normal->Bind(1);
  147. }
  148. if (b->depth != nullptr) {
  149. b->depth->Bind(2);
  150. }
  151.  
  152. glDrawArrays(GL_TRIANGLES, b->offset, b->vertex_count);
  153.  
  154. glBindTexture(GL_TEXTURE_2D, 0);
  155. }
  156.  
  157. m_default->Unbind();
  158.  
  159. glBindVertexArray(0);
  160. }
  161.  
  162. void SpriteBatch::Draw(Vector2f position, Texture* diffuse) {
  163. Sprite* spr = new Sprite();
  164. spr->diffuse = diffuse;
  165. spr->v1 = {
  166. Vector3f(0, 0, 0),
  167. Vector2f(0, 0),
  168. Vector4f(1, 1, 1, 1)
  169. };
  170. spr->v2 = {
  171. Vector3f(128, 0, 0),
  172. Vector2f(1, 0),
  173. Vector4f(1, 1, 1, 1)
  174. };
  175. spr->v3 = {
  176. Vector3f(128, 128, 0),
  177. Vector2f(1, 1),
  178. Vector4f(1, 1, 1, 1)
  179. };
  180. spr->v4 = {
  181. Vector3f(0, 128, 0),
  182. Vector2f(0, 1),
  183. Vector4f(1, 1, 1, 1)
  184. };
  185. m_sprites.push_back(spr);
  186. }
  187.  
  188. SpriteBatch::~SpriteBatch() {
  189. if (m_vbo) {
  190. glDeleteBuffers(1, &m_vbo);
  191. }
  192. if (m_vao) {
  193. glDeleteVertexArrays(1, &m_vao);
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement