2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** quadarray.h
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef QUADARRAY_H
|
|
|
|
#define QUADARRAY_H
|
|
|
|
|
2014-07-13 12:03:18 +00:00
|
|
|
#include "vertex.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "gl-util.h"
|
2014-07-13 12:03:18 +00:00
|
|
|
#include "gl-meta.h"
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "global-ibo.h"
|
2013-09-23 05:15:01 +00:00
|
|
|
#include "shader.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-26 19:18:33 +00:00
|
|
|
#include <vector>
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-02-03 14:32:50 +00:00
|
|
|
template<class VertexType>
|
|
|
|
struct QuadArray
|
|
|
|
{
|
|
|
|
std::vector<VertexType> vertices;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
VBO::ID vbo;
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::VAO vao;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-12 17:46:30 +00:00
|
|
|
size_t quadCount;
|
2014-02-03 14:32:50 +00:00
|
|
|
GLsizeiptr vboSize;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:32:50 +00:00
|
|
|
QuadArray()
|
|
|
|
: quadCount(0),
|
|
|
|
vboSize(-1)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
vbo = VBO::gen();
|
|
|
|
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoFillInVertexData<VertexType>(vao);
|
|
|
|
vao.vbo = vbo;
|
|
|
|
vao.ibo = shState->globalIBO().ibo;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoInit(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 14:32:50 +00:00
|
|
|
~QuadArray()
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoFini(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
VBO::del(vbo);
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:46:30 +00:00
|
|
|
void resize(size_t size)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
vertices.resize(size * 4);
|
|
|
|
quadCount = size;
|
|
|
|
}
|
|
|
|
|
2014-02-03 14:32:50 +00:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
vertices.clear();
|
|
|
|
quadCount = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
/* This needs to be called after the final 'append()' call
|
|
|
|
* and previous to the first 'draw()' call. */
|
|
|
|
void commit()
|
|
|
|
{
|
|
|
|
VBO::bind(vbo);
|
|
|
|
|
2014-02-03 14:32:50 +00:00
|
|
|
GLsizeiptr size = vertices.size() * sizeof(VertexType);
|
|
|
|
|
|
|
|
if (size > vboSize)
|
|
|
|
{
|
|
|
|
/* New data exceeds already allocated size.
|
|
|
|
* Reallocate VBO. */
|
2014-10-09 17:02:29 +00:00
|
|
|
VBO::uploadData(size, dataPtr(vertices), GL_DYNAMIC_DRAW);
|
2014-02-03 14:32:50 +00:00
|
|
|
vboSize = size;
|
|
|
|
|
|
|
|
shState->ensureQuadIBO(quadCount);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* New data fits in allocated size */
|
2014-10-09 17:02:29 +00:00
|
|
|
VBO::uploadSubData(0, size, dataPtr(vertices));
|
2014-02-03 14:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VBO::unbind();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 19:10:01 +00:00
|
|
|
void draw(size_t offset, size_t count)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoBind(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
const char *_offset = (const char*) 0 + offset * 6 * sizeof(index_t);
|
2014-05-30 21:01:35 +00:00
|
|
|
gl.DrawElements(GL_TRIANGLES, count * 6, _GL_INDEX_TYPE, _offset);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoUnbind(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw()
|
|
|
|
{
|
|
|
|
draw(0, quadCount);
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:46:30 +00:00
|
|
|
size_t count() const
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
return quadCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-03 14:32:50 +00:00
|
|
|
typedef QuadArray<Vertex> ColorQuadArray;
|
|
|
|
typedef QuadArray<SVertex> SimpleQuadArray;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
#endif // QUADARRAY_H
|