Guest User

Untitled

a guest
Jun 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. float texID = 0.0f;
  2. Uint color;
  3. };
  4.  
  5. enum
  6. {
  7. MAX_SPRITES = 60000,
  8. MAX_ELEMENTS = MAX_SPRITES * 6,
  9. MAX_TEXTURES = 32,
  10. SPRITE_SIZE = sizeof(Vertex) * 4,
  11. };
  12.  
  13. enum BUFFER_SIZE
  14. {
  15. VERTEX = SPRITE_SIZE * MAX_SPRITES,
  16. ELEMENT = MAX_ELEMENTS * sizeof(GLuint),
  17. };
  18.  
  19. enum SHADER_OUT
  20. {
  21. POSITION = 0,
  22. COLOR = 1,
  23. UV = 2,
  24. TEXTURE = 3
  25. };
  26.  
  27. void Renderer2D::Create()
  28. {
  29. vao = vao->Create();
  30. //create array buffer with dynamic draw
  31. vbo = vbo->Create(API::BUFFER::ARRAY, API::DRAW::DYNAMIC);
  32. //create element array buffer with static draw
  33. ebo = ebo->Create(API::BUFFER::ELEMENT, API::DRAW::STATIC);
  34.  
  35. vbo->AddData(BUFFER_SIZE::VERTEX, nullptr);
  36. ebo->AddData(BUFFER_SIZE::ELEMENT, setIndices());
  37.  
  38. vbo->AddAttribute(SHADER_OUT::POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, position));
  39. vbo->AddAttribute(SHADER_OUT::COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (const void*)offsetof(Vertex, color));
  40. vbo->AddAttribute(SHADER_OUT::UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, uv));
  41. vbo->AddAttribute(SHADER_OUT::TEXTURE, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, texID));
  42.  
  43. vao->UnbindVertexArray();
  44. }
  45.  
  46. void Renderer2D::Begin()
  47. {
  48. //map the buffer
  49. mappedVertex = vbo->getDataPointer<Vertex>();
  50. }
  51.  
  52. void Renderer2D::DrawString(const std::string& text, const Vector2& position, Uint color, const Font& font)
  53. {
  54. Texture* texture = font.getTexture();
  55. float textureSlot = FindTexture(texture);
  56.  
  57. const Vector2& scale = 1.0f;// font.getScale();
  58.  
  59. float x = position.x;
  60.  
  61. texture_font_t* ftFont = font.getFTFont();
  62.  
  63. for (Uint i = 0; i < text.length(); ++i)
  64. {
  65. texture_glyph_t* glyph = texture_font_get_glyph(ftFont, text.c_str() + i);
  66. if (glyph)
  67. {
  68. if (i > 0)
  69. {
  70. float kerning = texture_glyph_get_kerning(glyph, text.c_str() + i - 1);
  71. x += kerning / scale.x;
  72. }
  73.  
  74. float x0 = x + glyph->offset_x / scale.x,
  75. y0 = position.y + glyph->offset_y / scale.y,
  76. x1 = x0 + glyph->width / scale.x,
  77. y1 = y0 - glyph->height / scale.y,
  78.  
  79. u0 = glyph->s0,
  80. v0 = glyph->t0,
  81. u1 = glyph->s1,
  82. v1 = glyph->t1;
  83.  
  84. mappedVertex->position = Vector3(x0, y0, 0);
  85. mappedVertex->uv = Vector2(u0, v0);
  86. mappedVertex->texID = textureSlot;
  87. mappedVertex->color = color;
  88. mappedVertex++;
  89.  
  90. mappedVertex->position = Vector3(x0, y1, 0);
  91. mappedVertex->uv = Vector2(u0, v1);
  92. mappedVertex->texID = textureSlot;
  93. mappedVertex->color = color;
  94. mappedVertex++;
  95.  
  96. mappedVertex->position = Vector3(x1, y1, 0);
  97. mappedVertex->uv = Vector2(u1, v1);
  98. mappedVertex->texID = textureSlot;
  99. mappedVertex->color = color;
  100. mappedVertex++;
  101.  
  102. mappedVertex->position = Vector3(x1, y0, 0);
  103. mappedVertex->uv = Vector2(u1, v0);
  104. mappedVertex->texID = textureSlot;
  105. mappedVertex->color = color;
  106. mappedVertex++;
  107.  
  108. indexCount += 6;
  109.  
  110. x += glyph->advance_x;
  111. }
  112. }
  113. }
  114.  
  115. void Renderer2D::End()
  116. {
  117. //Unmap the buffer
  118. vbo->releaseDataPointer();
  119.  
  120. glEnable(GL_BLEND);
  121. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  122. }
  123.  
  124. void Renderer2D::Render()
  125. {
  126. End();
  127.  
  128. for (Uint i = 0; i < texture.size(); ++i)
  129. {
  130. //active texture 'i' and bind it
  131. texture[i]->BindTexture(i);
  132. }
  133.  
  134. vao->BindVertexArray();
  135. vao->DrawElements(API::ELEMENTS, indexCount);
  136. vao->UnbindVertexArray();
  137.  
  138. for (Uint i = 0; i < texture.size(); ++i)
  139. {
  140. //active texture 'i' and bind as a 0
  141. texture[i]->UnbindTexture(i);
  142. }
  143.  
  144. indexCount = 0;
  145. texture.clear();
  146.  
  147. //glDisable(GL_BLEND);
  148. }
  149.  
  150. Uint* Renderer2D::setIndices()
  151. {
  152. Uint* indices = new Uint[BUFFER_SIZE::ELEMENT],
  153. offset = 0;
  154. for (int i = 0; i < BUFFER_SIZE::ELEMENT; i += 6)
  155. {
  156. indices[i] = offset;
  157. indices[i + 1] = offset + 1;
  158. indices[i + 2] = offset + 2;
  159. indices[i + 3] = offset + 2;
  160. indices[i + 4] = offset + 3;
  161. indices[i + 5] = offset;
  162.  
  163. offset += 4;
  164. }
  165.  
  166. return indices;
  167. }
  168.  
  169. float Renderer2D::FindTexture(Texture* t)
  170. {
  171. float result = 0.0f;
  172. bool found = false;
  173. for (Uint i = 0; i < texture.size(); ++i)
  174. {
  175. if (texture[i] == t)
  176. {
  177. result = (float)(i + 1);
  178. found = true;
  179. break;
  180. }
  181. }
  182.  
  183. if (!found)
  184. {
  185. if (texture.size() >= MAX_TEXTURES)
  186. {
  187. Present();
  188. Begin();
  189. }
  190. texture.push_back(t);
  191. result = (float)(texture.size());
  192. }
  193.  
  194. return result;
  195. }
  196.  
  197. Font::Font(const std::string& name, const std::string& filename, float size)
  198. : name(name), filename(filename), size(size), texture(nullptr)
  199. {
  200. FTAtlas = ftgl::texture_atlas_new(512, 512, 1);
  201. FTFont = ftgl::texture_font_new_from_file(FTAtlas, size, filename.c_str());
  202.  
  203. TextureParameters parameters = { TextureFormat::LUMINANCE_ALPHA, TextureFilter::LINEAR, TextureFilter::LINEAR, TextureWrap::CLAMP_TO_EDGE };
  204. //Creates texture w = 512; h = 512; texImage data = nullptr;
  205. texture = texture->Create(512, 512, parameters);
  206.  
  207. //set texSubImage data to FTAtlas->Data
  208. texture->setData(FTAtlas->data);
  209. }
  210.  
  211. Texture* Font::getTexture() const
  212. {
  213. UpdateAtlas();
  214. return texture;
  215. }
  216.  
  217. void Font::UpdateAtlas() const
  218. {
  219. texture->setData(FTAtlas->data);
  220. }
  221.  
  222. int main(int argc, char* args[])
  223. {
  224. Window window;
  225. window.Create(1280, 720, "window");
  226.  
  227. Renderer renderer;
  228. renderer.Create();
  229.  
  230. //Shader stuff
  231. //Set 32 uniform texture samplers
  232. //Set mvp
  233.  
  234. //Font name, font path, font size
  235. FontManager::getInstance().AddFont(new Font("font", "font.ttf", 30));
  236.  
  237. Event event;
  238. bool quit = false;
  239.  
  240. while(!quit)
  241. {
  242. while(PollEvent(event))
  243. {
  244. if(event.type == QUIT)
  245. {
  246. quit = true;
  247. }
  248. }
  249.  
  250. renderere.Begin();
  251.  
  252. //Color is reversed alpha, blue, green, red
  253. //getFont() returns font which is named "font"
  254. renderer.DrawString("TEST", Vector2(0.0f, 0.0f), 0xff00ffff, FontManager::getInstance().getFont(font"));
  255.  
  256. renderer.Present();
  257.  
  258. window.Swap();
  259. }
  260.  
  261. //Free all if it is needed
  262. return 0;
  263. }
  264.  
  265. Texture::Texture(Uint width, Uint height, TextureParameters parameters, TextureTarget target)
  266. : filePath("")
  267. {
  268. w = width;
  269. h = height;
  270. textureParameters = parameters;
  271. textureTarget = target;
  272. texture = LoadFromFile();
  273. }
  274.  
  275. Texture* Texture::Create(uint width, Uint height, TextureParameters parameters, TextureTarget target /*Texture2D default*/)
  276. {
  277. return new Texture(width, height, parameters, target);
  278. }
  279.  
  280. Uint Texture::LoadFromFile()
  281. {
  282. Uint tex = 0;
  283. Uint format = textureParameters.format != TextureFormat::NONE ? ConvertFormatToOpenGL(textureParameters.format) : GL_RGBA;
  284. Uint target = ConvertTargetToOpenGL(textureTarget);
  285.  
  286. glCall(glGenTextures(1, &tex));
  287. glCall(glBindTexture(target, tex));
  288.  
  289. glCall(glTexImage2D(target, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, nullptr));
  290.  
  291. glCall(glTexParameteri(target, GL_TEXTURE_WRAP_S, ConvertWrapToOpenGL(textureParameters.wrap)));
  292. glCall(glTexParameteri(target, GL_TEXTURE_WRAP_T, ConvertWrapToOpenGL(textureParameters.wrap)));
  293. glCall(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, ConvertFilterToOpenGL(textureParameters.filterMAG)));
  294. glCall(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, ConvertFilterToOpenGL(textureParameters.filterMIN)));
  295.  
  296. glCall(glBindTexture(target, 0));
  297.  
  298. return tex;
  299. }
  300.  
  301. void Texture::setData(const void* pixels)
  302. {
  303. Uint format = textureParameters.format != TextureFormat::NONE ? ConvertFormatToOpenGL(textureParameters.format) : GL_RGBA;
  304. glCall(glBindTexture(ConvertTargetToOpenGL(textureTarget), texture));
  305. glCall(glTexSubImage2D(ConvertTargetToOpenGL(textureTarget), 0, 0, 0, w, h, format, GL_UNSIGNED_BYTE, pixels));
  306. }
  307.  
  308. Texture::Texture(Uint tex)
  309. {
  310. texture = tex;
  311. }
  312.  
  313. Texture* Texture::Create(Uint tex)
  314. {
  315. return new Texture(tex);
  316. }
  317.  
  318. Font::Font(const std::string& name, const std::string& filename, float size)
  319. : name(name), filename(filename), size(size), texture(nullptr)
  320. {
  321. FTAtlas = ftgl::texture_atlas_new(512, 512, 1);
  322. FTFont = ftgl::texture_font_new_from_file(FTAtlas, size, filename.c_str());
  323.  
  324. glCall(glGenTextures(1, &tex));
  325. glCall(glBindTexture(GL_TEXTURE_2D, tex));
  326.  
  327. glCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, FTAtlas->width, FTAtlas->height, 0, GL_RED, GL_UNSIGNED_BYTE, FTAtlas->data));
  328.  
  329. glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
  330. glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
  331. glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
  332. glCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
  333.  
  334. //Set created texture to texture class as if it was a texture class instance that it can be activated in renderer
  335. texture = texture->Create(tex);
  336.  
  337. glCall(glBindTexture(GL_TEXTURE_2D, 0));
  338. }
  339.  
  340. void Font::UpdateAtlas() const
  341. {
  342. glBindTexture(GL_TEXTURE_2D, tex);
  343. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512, GL_RED, GL_UNSIGNED_BYTE, FTAtlas->data);
  344. }
  345.  
  346. layout (location = 0) in vec2 position;
  347. layout (location = 1) in vec4 color;
  348. layout (location = 2) in vec2 uv;
  349. layout (location = 3) in float texID;
  350.  
  351. out vec2 fragmentUV;
  352. out vec4 fragmentColor;
  353. out vec2 fragmenPosition;
  354. out float fragmentTexID;
  355.  
  356. layout (location = 1) uniform mat4 mvp;
  357.  
  358. void main()
  359. {
  360. gl_Position = mvp * vec4(a_position, 0.0, 1.0);
  361.  
  362. fragmenPosition = position;
  363. fragmentColor = color;
  364. fragmentTexID= texID;
  365. fragmentUV = vec2(uv.x, 1 - uv.y);
  366. }
  367.  
  368. #version 330 core
  369.  
  370. out vec4 finalColor;
  371.  
  372. in vec2 fragmentUV;
  373. in vec4 fragmentColor;
  374. in vec2 fragmenPosition;
  375. in float fragmentTexID;
  376.  
  377. layout (binding = 0) uniform sampler2D textureSampler[32];
  378.  
  379. void main()
  380. {
  381. vec4 texColor = fragmentColor;
  382.  
  383. if (fragmentTexID > 0.0) {
  384. int texID = int(fragmentTexID - 0.5);
  385. texColor = texture(textureSampler[texID], fragmentUV);
  386. }
  387.  
  388. finalColor = texColor * fragmentColor;
  389. }
Add Comment
Please, Sign In to add comment