implemented mesh class, and added gridmesh implementation

This commit is contained in:
unknown 2015-06-26 12:41:17 +02:00
parent 9e19c2211d
commit 6eb437fe23
5 changed files with 137 additions and 10 deletions

42
gridmesh.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "gridmesh.h"
GridMesh::GridMesh(int width, int height, bool alternate) : Mesh()
{
for(int i=0; i<=width; ++i)
{
for(int j=0; j<=height; ++j)
{
Vertex v;
v.position = glm::vec3((float)i/(float)width, (float)j/(float)height, 0);
v.normal = glm::vec3(0, 0, 1);
v.texCoord = glm::vec2(v.position.x, v.position.y);
addVertex(v);
if(i > 0 && j > 0)
{
if(alternate && (i+j)%2)
{
addFace(getVertexId(i, j),
getVertexId(i-1, j),
getVertexId(i, j-1));
addFace(getVertexId(i-1, j-1),
getVertexId(i, j-1),
getVertexId(i-1, j));
}
else
{
addFace(getVertexId(i, j),
getVertexId(i-1, j),
getVertexId(i-1, j-1));
addFace(getVertexId(i, j),
getVertexId(i-1, j-1),
getVertexId(i, j-1));
}
}
}
}
}
int GridMesh::getVertexId(int i, int j)
{
return i*(height+1) + j;
}

14
gridmesh.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef GRIDMESH_H
#define GRIDMESH_H
#include "mesh.h"
class GridMesh : public Mesh
{
public:
GridMesh(int width, int height, bool alternate);
private:
int getVertexId(int i, int j);
};
#endif // GRIDMESH_H

View File

@ -1,23 +1,83 @@
#include "mesh.h"
#include "glassert.h"
Mesh::Mesh()
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
Mesh::~Mesh()
{
destroyGL();
}
void Mesh::addVertex(Vertex& v)
{
vertices.push_back(v);
if(!locked)
vertices.push_back(v);
}
void Mesh::addFace(int i1, int i2, int i3)
{
indices.push_back(i1);
indices.push_back(i2);
indices.push_back(i3);
if(!locked)
{
indices.push_back(i1);
indices.push_back(i2);
indices.push_back(i3);
}
}
void Mesh::initGL()
{
if(locked)
destroyGL();
// create VAO
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
// create VBOs
glAssert(glGenBuffers(NB_BUFFERS, vbo));
// init vertex vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW));
// position attribute
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
// normal attribute
glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(1));
// texCoord attribute
glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(2*sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(2));
// init indices vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW));
// unbind vao
glAssert(glBindVertexArray(0));
locked = true;
}
void Mesh::destroyGL()
{
if(locked)
{
locked = false;
glAssert(glDeleteVertexArrays(1, &vao));
vao = 0;
glAssert(glDeleteBuffers(2, vbo));
vbo[0] = 0;
vbo[1] = 0;
}
}
void Mesh::draw()
{
if(locked)
{
glAssert(glBindVertexArray(vao));
glAssert(glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0));
}
}

11
mesh.h
View File

@ -3,6 +3,7 @@
#include <vector>
#include <glm/glm.hpp>
#include <glew/glew.h>
class Mesh
{
@ -14,14 +15,22 @@ public:
glm::vec2 texCoord;
} Vertex;
Mesh();
~Mesh();
void addVertex(Vertex& v);
void addFace(int i1, int i2, int i3);
void initGL();
void destroyGL();
void draw();
bool isLocked(){return locked;}
private:
enum {VERTEX_BUFFER, INDICES_BUFFER, NB_BUFFERS};
std::vector<Vertex> vertices;
std::vector<int> indices;
GLuint vao;
GLuint vbo[2];
bool locked;
};
#endif // MESH_H

View File

@ -27,7 +27,8 @@ SOURCES += main.cpp\
camera.cpp \
sparrowrenderer.cpp \
scene.cpp \
material.cpp
material.cpp \
gridmesh.cpp
HEADERS += mainwindow.h \
myglwidget.h \
@ -37,7 +38,8 @@ HEADERS += mainwindow.h \
sparrowrenderer.h \
glassert.h \
scene.h \
material.h
material.h \
gridmesh.h
FORMS += mainwindow.ui