Created
March 2, 2015 20:29
-
-
Save feliwir/9ad979abb01c9933c3ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Terrain.hpp" | |
#include "../Util/Logger.hpp" | |
#include <vector> | |
using namespace Graphics; | |
Terrain::Terrain() : m_vbo(0),m_ibo(0) | |
{ | |
glGenBuffers(1, &m_vbo); | |
glGenBuffers(1, &m_ibo); | |
m_shader.LoadFromFile(Shader::VERTEX_SHADER, "./shader/terrain.vert"); | |
m_shader.LoadFromFile(Shader::FRAGMENT_SHADER, "./shader/terrain.frag"); | |
m_shader.Link(); | |
m_shader.AddUniform("MVP"); | |
m_shader.AddAttribute("pos"); | |
m_shader.AddAttribute("color"); | |
} | |
Terrain::~Terrain() | |
{ | |
glDeleteBuffers(1, &m_vbo); | |
glDeleteBuffers(1, &m_ibo); | |
} | |
void Terrain::Render(const glm::mat4& mvp) | |
{ | |
m_shader.Use(); | |
glBindBuffer(GL_ARRAY_BUFFER, m_vbo); | |
glEnableVertexAttribArray(m_shader.Attribute("pos")); | |
glEnableVertexAttribArray(m_shader.Attribute("color")); | |
glVertexAttribPointer(m_shader.Attribute("pos"),3,GL_FLOAT, | |
GL_FALSE,sizeof(Vertex), (void*)offsetof(Vertex, pos)); | |
glVertexAttribPointer(m_shader.Attribute("color"),3,GL_FLOAT, | |
GL_FALSE,sizeof(Vertex),(void*)offsetof(Vertex,color)); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo); | |
glUniformMatrix4fv(m_shader.Uniform("MVP"), 1, false, &mvp[0][0]); | |
glDrawElements(GL_TRIANGLES, m_indicecount, | |
GL_UNSIGNED_SHORT,nullptr); | |
glDisableVertexAttribArray(m_shader.Attribute("pos")); | |
glDisableVertexAttribArray(m_shader.Attribute("color")); | |
m_shader.UnUse(); | |
} | |
void Terrain::Create(const glm::ivec2& size) | |
{ | |
//Create vertices first | |
std::vector<Vertex> vertices; | |
for (auto y = 0; y < size.y; ++y) | |
{ | |
for (auto x = 0; x < size.x; ++x) | |
{ | |
auto offset = y*size.x + x; | |
Vertex vert; | |
vert.pos = glm::vec3(x, y, 0.0f); | |
vert.color = glm::vec3(1.0f, 1.0f, 1.0f); | |
vertices.push_back(vert); | |
} | |
} | |
m_vertexcount = vertices.size(); | |
glBindBuffer(GL_ARRAY_BUFFER, m_vbo); | |
glBufferData(GL_ARRAY_BUFFER, m_vertexcount * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW); | |
std::vector<uint16_t> indices; | |
for (auto y = 0; y < size.y-1; ++y) | |
{ | |
for (auto x = 0; x < size.x-1; ++x) | |
{ | |
//lower left triangle | |
indices.push_back((y + 1)*size.x + x); | |
indices.push_back(y*size.x + x); | |
indices.push_back(y*size.x + x+1); | |
//upper right triangle | |
indices.push_back((y + 1)*size.x + x); | |
indices.push_back((y + 1)*size.x + x+1); | |
indices.push_back(y*size.x + x + 1); | |
} | |
} | |
m_indicecount = indices.size(); | |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo); | |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indicecount * sizeof(uint16_t), &indices[0], GL_STATIC_DRAW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment