C++ Commande Z 2017


SUBMITTED BY: karimdzdz

DATE: May 23, 2017, 9:31 a.m.

FORMAT: C++

SIZE: 2.8 kB

HITS: 1319

  1. void RendererOpenGL::test() {
  2. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  3. glClear(GL_COLOR_BUFFER_BIT);
  4. Texture* debugt = Locator::getLibraryTexture()->get("debug");
  5. Texture* errort = Locator::getLibraryTexture()->get("error");
  6. Shader* debugs = Locator::getLibraryShader()->get("basic-new");
  7. debugt->bind();
  8. debugs->Use();
  9. drawQuadFull(500, 200, 1, 100, 100, debugs, debugt);
  10. drawQuadFull(550, 250, 2, 100, 100, debugs, debugt);
  11. drawQuadFull(200, 200, 2, 100, 100, debugs, debugt);
  12. drawQuadFull(250, 250, 1, 100, 100, debugs, debugt);
  13. //Still draws them in order of calling
  14. }
  15. //Just passes the correct texture coordinates so it will draw the whole texture
  16. void RendererOpenGL::drawQuadFull(int x, int y, int z, int width, int height, Shader* shader, Texture* texture, float rotation) {
  17. drawQuad(x, y, z, width, height, shader, texture, fullQuadDraw_, rotation);
  18. }
  19. void RendererOpenGL::drawQuad(int x, int y, int z, int width, int height, Shader* shader, Texture* texture, float tc[8], float rotation) {
  20. glm::mat4 model;
  21. // Translate into place
  22. model = glm::translate(model, glm::vec3(x, y, z));
  23. // Fix non square rotation
  24. float diff = 0.0f;
  25. float rot = rotation;
  26. model = glm::translate(model, glm::vec3(width / 2, height / 2, 0.0f));
  27. model = glm::rotate(model, glm::radians(rot), glm::vec3(0.0f, 0.0f, 1.0f));
  28. model = glm::translate(model, glm::vec3(((-width / 2)), (-height / 2), 0.0f));
  29. model = glm::scale(model, glm::vec3(width, height, 1.0f));
  30. glUniformMatrix4fv(glGetUniformLocation(shader->Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
  31. glUniformMatrix4fv(glGetUniformLocation(shader->Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection_));
  32. glUniform2fv(glGetUniformLocation(shader->Program, "textureCoordinates"), 4, tc);
  33. glUniform4f(glGetUniformLocation(shader->Program, "color"), 1.0f, 1.0f, 1.0f, 1.0f);
  34. glUniform1i(glGetUniformLocation(shader->Program, "fragTexture"), 0);
  35. glBindBuffer(GL_ARRAY_BUFFER, quadVBO_);
  36. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 12, vertices_, GL_STATIC_DRAW);
  37. // Position attribute
  38. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
  39. glEnableVertexAttribArray(0);
  40. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  41. }
  42. ///////////////////////// Vertex Shader
  43. #version 330 core
  44. layout (location = 0) in vec3 position;
  45. out vec4 fColor;
  46. out vec2 fCoord;
  47. uniform mat4 model;
  48. uniform mat4 projection;
  49. uniform vec4 color;
  50. uniform vec2 textureCoordinates[4];
  51. void main()
  52. {
  53. gl_Position = projection * model * vec4(position, 1.0f);
  54. fColor = color;
  55. fCoord = vec2(1 - textureCoordinates[gl_VertexID].x, 1 - textureCoordinates[gl_VertexID].y);
  56. }

comments powered by Disqus