Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include "Terrain.hpp"
  2. #include "../Util/Logger.hpp"
  3. #include <vector>
  4. using namespace Graphics;
  5.  
  6. Terrain::Terrain() : m_vbo(0),m_ibo(0)
  7. {
  8.     glGenBuffers(1, &m_vbo);
  9.     glGenBuffers(1, &m_ibo);
  10.     m_shader.LoadFromFile(Shader::VERTEX_SHADER, "shader/terrain.vs");
  11.     m_shader.LoadFromFile(Shader::FRAGMENT_SHADER, "shader/terrain.fs");
  12.     m_shader.Link();
  13.  
  14.     m_shader.AddUniform("MVP");
  15.     m_shader.AddAttribute("pos");
  16.     m_shader.AddAttribute("color");
  17. }
  18.  
  19. Terrain::~Terrain()
  20. {
  21.     glDeleteBuffers(1, &m_vbo);
  22.     glDeleteBuffers(1, &m_ibo);
  23. }
  24.  
  25. void Terrain::Render(const glm::mat4& mvp)
  26. {
  27.    
  28.     m_shader.Use();
  29.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  30.     glEnableVertexAttribArray(m_shader.Attribute("pos"));
  31.     glEnableVertexAttribArray(m_shader.Attribute("color"));
  32.  
  33.     glVertexAttribPointer(
  34.         m_shader.Attribute("pos"),
  35.         3,
  36.         GL_FLOAT,
  37.         GL_FALSE,
  38.         sizeof(glm::vec3),
  39.         (void*)offsetof(Vertex, pos)
  40.         );
  41.  
  42.     glVertexAttribPointer(
  43.         m_shader.Attribute("color"),
  44.         3,
  45.         GL_FLOAT,
  46.         GL_FALSE,
  47.         sizeof(glm::vec3),
  48.         (void*)offsetof(Vertex,color)
  49.         );
  50.  
  51.     glDisableVertexAttribArray(m_shader.Attribute("pos"));
  52.     glDisableVertexAttribArray(m_shader.Attribute("color"));
  53.  
  54.     glBindBuffer(GL_ARRAY_BUFFER, m_ibo);
  55.     glUniformMatrix4fv(m_shader.Uniform("MVP"), 1, false, &mvp[0][0]);
  56.     glDrawElements(
  57.         GL_TRIANGLES,    
  58.         m_indicecount,
  59.         GL_UNSIGNED_INT,
  60.         nullptr
  61.         );
  62.  
  63.     SAGEGLCheck();
  64.    
  65.     m_shader.UnUse();
  66. }
  67.  
  68. void Terrain::Create(const glm::ivec2& size)
  69. {
  70.     //Create vertices first
  71.     std::vector<Vertex> vertices;
  72.  
  73.     for (auto y = 0; y < size.y; ++y)
  74.     {
  75.         for (auto x = 0; x < size.x; ++x)
  76.         {
  77.             auto offset = y*size.x + x;
  78.             Vertex vert;
  79.             vert.pos = glm::vec3(x, y, 0.0f);
  80.             vert.color = glm::vec3(1.0f, 0.0f, 0.0f);
  81.             vertices.push_back(vert);
  82.         }
  83.     }
  84.  
  85.     m_vertexcount = vertices.size();
  86.     glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  87.     glBufferData(GL_ARRAY_BUFFER, m_vertexcount * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
  88.     SAGEGLCheck();
  89.  
  90.  
  91.     std::vector<uint16_t> indices;
  92.     for (auto y = 0; y < size.y-1; ++y)
  93.     {
  94.         for (auto x = 0; x < size.x-1; ++x)
  95.         {
  96.             //lower left triangle
  97.             indices.push_back((y + 1)*size.x + x);
  98.             indices.push_back(y*size.x + x);
  99.             indices.push_back(y*size.x + x+1);
  100.             //upper right triangle
  101.             indices.push_back((y + 1)*size.x + x);
  102.             indices.push_back((y + 1)*size.x + x+1);
  103.             indices.push_back(y*size.x + x + 1);
  104.         }
  105.     }
  106.     m_indicecount = indices.size();
  107.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
  108.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indicecount * sizeof(uint16_t), &indices[0], GL_STATIC_DRAW);
  109.     SAGEGLCheck();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement