2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** quad.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 QUAD_H
|
|
|
|
#define QUAD_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
|
|
|
|
|
|
|
struct Quad
|
|
|
|
{
|
|
|
|
Vertex vert[4];
|
|
|
|
VBO::ID vbo;
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::VAO vao;
|
2013-09-01 14:27:21 +00:00
|
|
|
bool vboDirty;
|
|
|
|
|
2014-08-12 17:44:57 +00:00
|
|
|
template<typename V>
|
|
|
|
static void setPosRect(V *vert, const FloatRect &r)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
vert[i++].pos = r.topLeft();
|
|
|
|
vert[i++].pos = r.topRight();
|
|
|
|
vert[i++].pos = r.bottomRight();
|
|
|
|
vert[i++].pos = r.bottomLeft();
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:44:57 +00:00
|
|
|
template<typename V>
|
|
|
|
static void setTexRect(V *vert, const FloatRect &r)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
vert[i++].texPos = r.topLeft();
|
|
|
|
vert[i++].texPos = r.topRight();
|
|
|
|
vert[i++].texPos = r.bottomRight();
|
|
|
|
vert[i++].texPos = r.bottomLeft();
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:44:57 +00:00
|
|
|
template<typename V>
|
|
|
|
static int setTexPosRect(V *vert, const FloatRect &tex, const FloatRect &pos)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
setPosRect(vert, pos);
|
|
|
|
setTexRect(vert, tex);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-12 17:44:57 +00:00
|
|
|
template<typename V>
|
|
|
|
static void setColor(V *vert, const Vec4 &c)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-12 17:44:57 +00:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
vert[i].color = c;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Quad()
|
|
|
|
: vbo(VBO::gen()),
|
|
|
|
vboDirty(true)
|
|
|
|
{
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoFillInVertexData<Vertex>(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, true);
|
2014-05-16 19:37:28 +00:00
|
|
|
VBO::allocEmpty(sizeof(Vertex[4]), GL_DYNAMIC_DRAW);
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoUnbind(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
setColor(Vec4(1, 1, 1, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
~Quad()
|
|
|
|
{
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoFini(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
VBO::del(vbo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateBuffer()
|
|
|
|
{
|
|
|
|
VBO::bind(vbo);
|
2014-05-16 19:37:28 +00:00
|
|
|
VBO::uploadSubData(0, sizeof(Vertex[4]), vert);
|
|
|
|
VBO::unbind();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setPosRect(const FloatRect &r)
|
|
|
|
{
|
|
|
|
setPosRect(vert, r);
|
|
|
|
vboDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTexRect(const FloatRect &r)
|
|
|
|
{
|
|
|
|
setTexRect(vert, r);
|
|
|
|
vboDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTexPosRect(const FloatRect &tex, const FloatRect &pos)
|
|
|
|
{
|
|
|
|
setTexPosRect(vert, tex, pos);
|
|
|
|
vboDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setColor(const Vec4 &c)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
vert[i].color = c;
|
|
|
|
|
|
|
|
vboDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw()
|
|
|
|
{
|
|
|
|
if (vboDirty)
|
|
|
|
{
|
|
|
|
updateBuffer();
|
|
|
|
vboDirty = false;
|
|
|
|
}
|
|
|
|
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoBind(vao);
|
2014-07-11 00:09:53 +00:00
|
|
|
gl.DrawElements(GL_TRIANGLES, 6, _GL_INDEX_TYPE, 0);
|
2014-07-13 12:03:18 +00:00
|
|
|
GLMeta::vaoUnbind(vao);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // QUAD_H
|