2022-05-06 17:14:47 +00:00
|
|
|
#include "QuadGrid.h"
|
|
|
|
#include "Texture.h"
|
|
|
|
#include "RenderBase.h"
|
|
|
|
#include "Core.h"
|
|
|
|
|
|
|
|
QuadGrid::QuadGrid(size_t w, size_t h)
|
2022-05-14 03:14:08 +00:00
|
|
|
: pauseLevel(0), _w(w+1), _h(h+1)
|
2022-05-06 17:14:47 +00:00
|
|
|
{
|
|
|
|
addType(SCO_QUAD_GRID);
|
|
|
|
_points.resize((w+1) * (h+1));
|
2022-05-07 00:47:59 +00:00
|
|
|
resetUV();
|
2022-05-13 15:13:27 +00:00
|
|
|
resetPos(1, 1);
|
|
|
|
this->width = 2;
|
|
|
|
this->height = 2;
|
2022-05-14 03:14:08 +00:00
|
|
|
this->cull = false;
|
|
|
|
this->repeatTexture = true;
|
2022-05-06 17:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QuadGrid* QuadGrid::New(size_t w, size_t h)
|
|
|
|
{
|
|
|
|
return w && h ? new QuadGrid(w, h) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuadGrid::~QuadGrid()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuadGrid::resetUV(float xmul, float ymul)
|
|
|
|
{
|
2022-05-14 03:14:08 +00:00
|
|
|
const float incX = xmul / float(quadsX());
|
|
|
|
const float incY = ymul / float(quadsY());
|
|
|
|
const size_t NX = pointsX();
|
2022-05-06 17:14:47 +00:00
|
|
|
|
2022-05-14 03:14:08 +00:00
|
|
|
float v = 0;
|
2022-05-06 17:14:47 +00:00
|
|
|
|
2022-05-14 03:14:08 +00:00
|
|
|
// go over points
|
|
|
|
for(size_t y = 0; y < _h; ++y)
|
2022-05-06 17:14:47 +00:00
|
|
|
{
|
2022-05-14 03:14:08 +00:00
|
|
|
Point *row = &_points[y * NX];
|
|
|
|
float u = 0;
|
|
|
|
for(size_t x = 0; x < NX; ++x)
|
2022-05-06 17:14:47 +00:00
|
|
|
{
|
2022-05-14 03:14:08 +00:00
|
|
|
row[x].u = u;
|
|
|
|
row[x].v = v;
|
|
|
|
u += incX;
|
2022-05-06 17:14:47 +00:00
|
|
|
}
|
2022-05-14 03:14:08 +00:00
|
|
|
v += incY;
|
2022-05-06 17:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 15:13:27 +00:00
|
|
|
void QuadGrid::resetPos(float w, float h, float xoffs, float yoffs)
|
|
|
|
{
|
2022-05-14 03:14:08 +00:00
|
|
|
const float dx = w / float(quadsX());
|
|
|
|
const float dy = h / float(quadsY());
|
|
|
|
const size_t NX = pointsX();
|
2022-05-13 15:13:27 +00:00
|
|
|
|
|
|
|
float yy = yoffs;
|
2022-05-14 03:14:08 +00:00
|
|
|
// go over points
|
|
|
|
for(size_t y = 0; y < _h; ++y, yy += dy)
|
2022-05-13 15:13:27 +00:00
|
|
|
{
|
|
|
|
Point * const row = &_points[y * NX];
|
|
|
|
float xx = xoffs;
|
|
|
|
for(size_t x = 0; x < NX; ++x, xx += dx)
|
|
|
|
{
|
|
|
|
row[x].x = xx;
|
2022-05-14 03:14:08 +00:00
|
|
|
row[x].y = yy;
|
2022-05-13 15:13:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 17:14:47 +00:00
|
|
|
static inline void drawOnePoint(const QuadGrid::Point& p, float ox, float oy)
|
|
|
|
{
|
|
|
|
glTexCoord2f(p.u + ox, p.v + oy);
|
|
|
|
glVertex2f(p.x, p.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-19 23:04:19 +00:00
|
|
|
void QuadGrid::onRender(const RenderState& rs) const
|
2022-05-06 17:14:47 +00:00
|
|
|
{
|
|
|
|
glColor4f(color.x, color.y, color.z, alpha.x * alphaMod);
|
|
|
|
|
|
|
|
const float ox = texOffset.x;
|
|
|
|
const float oy = texOffset.y;
|
2022-05-14 03:14:08 +00:00
|
|
|
const size_t NX = pointsX();
|
2022-05-06 17:14:47 +00:00
|
|
|
|
2022-05-14 03:14:08 +00:00
|
|
|
// go over grids
|
|
|
|
const size_t W = quadsX();
|
|
|
|
const size_t H = quadsY();
|
|
|
|
for(size_t y = 0; y < H; ++y)
|
2022-05-06 17:14:47 +00:00
|
|
|
{
|
2022-05-19 03:17:00 +00:00
|
|
|
const Point * const row0 = &_points[y * NX];
|
|
|
|
const Point * const row1 = row0 + NX;
|
2022-05-14 03:14:08 +00:00
|
|
|
for(size_t x = 0; x < W; ++x)
|
2022-05-06 17:14:47 +00:00
|
|
|
{
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
drawOnePoint(row0[x], ox, oy);
|
|
|
|
drawOnePoint(row0[x+1], ox, oy);
|
|
|
|
drawOnePoint(row1[x+1], ox, oy);
|
|
|
|
drawOnePoint(row1[x], ox, oy);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuadGrid::onUpdate(float dt)
|
|
|
|
{
|
|
|
|
if(pauseLevel < core->particlesPaused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
texOffset.update(dt);
|
|
|
|
|
|
|
|
RenderObject::onUpdate(dt);
|
|
|
|
}
|
|
|
|
|
2022-05-13 15:13:27 +00:00
|
|
|
void QuadGrid::onSetTexture() // same as Quad::setTexture()
|
|
|
|
{
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
width = this->texture->width;
|
|
|
|
height = this->texture->height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = 64;
|
|
|
|
height = 64;
|
|
|
|
}
|
|
|
|
}
|