rework map rendering

This commit is contained in:
Eduardo Bart
2012-01-29 22:00:12 -02:00
parent 4276bd680d
commit 9db7bd2602
62 changed files with 1244 additions and 752 deletions

View File

@@ -26,6 +26,15 @@
uint FrameBuffer::boundFbo = 0;
FrameBuffer::FrameBuffer()
{
m_clearColor = Fw::alpha;
glGenFramebuffers(1, &m_fbo);
if(!m_fbo)
logFatal("Unable to create framebuffer object");
}
FrameBuffer::FrameBuffer(const Size& size)
{
m_clearColor = Fw::alpha;
@@ -44,6 +53,9 @@ FrameBuffer::~FrameBuffer()
void FrameBuffer::resize(const Size& size)
{
if(!size.isValid())
return;
if(m_texture && m_texture->getSize() == size)
return;
@@ -88,6 +100,11 @@ void FrameBuffer::generateMipmaps()
m_texture->generateMipmaps();
}
void FrameBuffer::draw(const Rect& dest, const Rect& src)
{
g_painter.drawTexturedRect(dest, m_texture, src);
}
void FrameBuffer::draw(const Rect& dest)
{
g_painter.drawTexturedRect(dest, m_texture);

View File

@@ -24,10 +24,12 @@
#define FRAMEBUFFER_H
#include "declarations.h"
#include "texture.h"
class FrameBuffer
{
public:
FrameBuffer();
FrameBuffer(const Size& size);
virtual ~FrameBuffer();
@@ -36,10 +38,12 @@ public:
void release();
void generateMipmaps();
void draw(const Rect& dest);
void draw(const Rect& dest, const Rect& src);
void setClearColor(const Color& color) { m_clearColor = color; }
TexturePtr getTexture() { return m_texture; }
const Size& getSize() { return m_texture->getSize(); }
private:
void internalBind();

View File

@@ -106,3 +106,10 @@ void Graphics::setViewportSize(const Size& size)
m_viewportSize = size;
}
int Graphics::getMaxTextureSize()
{
static GLint maxTexSize = -1;
if(maxTexSize == -1)
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
return maxTexSize;
}

View File

@@ -40,6 +40,7 @@ public:
void setViewportSize(const Size& size);
int getMaxTextureSize();
const Size& getViewportSize() { return m_viewportSize; }
TexturePtr getEmptyTexture() { return m_emptyTexture; }

View File

@@ -29,7 +29,7 @@ Particle::Particle(const Point& pos, const Size& startSize, const Size& finalSiz
m_colors = colors;
m_colorsStops = colorsStops;
m_pos = PointF(pos.x, pos.y);
m_position = PointF(pos.x, pos.y);
m_startSize = startSize;
m_finalSize = finalSize;
m_velocity = velocity;
@@ -80,18 +80,18 @@ void Particle::updatePosition(double elapsedTime)
PointF delta = m_velocity * elapsedTime;
delta.y *= -1; // painter orientate Y axis in the inverse direction
PointF position = m_pos + delta;
PointF position = m_position + delta;
if(m_pos != position) {
if(m_position != position) {
mustRedraw = true;
m_pos += delta;
m_position += delta;
}
// update acceleration
m_velocity += m_acceleration * elapsedTime;
}
m_rect.move((int)m_pos.x - m_size.width() / 2, (int)m_pos.y - m_size.height() / 2);
m_rect.move((int)m_position.x - m_size.width() / 2, (int)m_position.y - m_size.height() / 2);
}
void Particle::updateSize()

View File

@@ -36,10 +36,10 @@ public:
bool hasFinished() { return m_finished; }
PointF getPos() { return m_pos; }
PointF getPosition() { return m_position; }
PointF getVelocity() { return m_velocity; }
void setPos(const PointF& position) { m_pos = position; }
void setPosition(const PointF& position) { m_position = position; }
void setVelocity(const PointF& velocity) { m_velocity = velocity; }
private:
@@ -51,7 +51,7 @@ private:
std::vector<Color> m_colors;
std::vector<float> m_colorsStops;
TexturePtr m_texture;
PointF m_pos;
PointF m_position;
PointF m_velocity;
PointF m_acceleration;
Size m_size, m_startSize, m_finalSize;

View File

@@ -115,7 +115,7 @@ bool AttractionAffector::load(const OTMLNodePtr& node)
for(const OTMLNodePtr& childNode : node->children()) {
if(childNode->tag() == "position")
m_pos = childNode->value<Point>();
m_position = childNode->value<Point>();
else if(childNode->tag() == "acceleration")
m_acceleration = childNode->value<float>();
else if(childNode->tag() == "velocity-reduction-percent")
@@ -131,8 +131,8 @@ void AttractionAffector::updateParticle(const ParticlePtr& particle, double elap
if(!m_active)
return;
PointF pPosition = particle->getPos();
PointF d = PointF(m_pos.x - pPosition.x, pPosition.y - m_pos.y);
PointF pPosition = particle->getPosition();
PointF d = PointF(m_position.x - pPosition.x, pPosition.y - m_position.y);
if(d.length() == 0)
return;

View File

@@ -57,7 +57,7 @@ public:
void updateParticle(const ParticlePtr& particle, double elapsedTime);
private:
Point m_pos;
Point m_position;
float m_acceleration, m_reduction;
bool m_repelish;
};

View File

@@ -31,7 +31,7 @@ ParticleEmitter::ParticleEmitter(const ParticleSystemPtr& parent)
{
m_parent = parent;
m_pos = Point(0, 0);
m_position = Point(0, 0);
m_duration = -1;
m_delay = 0;
m_burstRate = 1; m_burstCount = 32;
@@ -65,7 +65,7 @@ bool ParticleEmitter::load(const OTMLNodePtr& node)
for(const OTMLNodePtr& childNode : node->children()) {
// self related
if(childNode->tag() == "position")
m_pos = childNode->value<Point>();
m_position = childNode->value<Point>();
else if(childNode->tag() == "duration")
m_duration = childNode->value<float>();
else if(childNode->tag() == "delay")
@@ -199,7 +199,7 @@ void ParticleEmitter::update(double elapsedTime)
float pRadius = Fw::randomRange(m_pMinPositionRadius, m_pMaxPositionRadius);
float pAngle = Fw::randomRange(m_pMinPositionAngle, m_pMaxPositionAngle);
Point pPosition = m_pos + Point(pRadius * cos(pAngle), pRadius * sin(pAngle));
Point pPosition = m_position + Point(pRadius * cos(pAngle), pRadius * sin(pAngle));
for(int p = 0; p < m_burstCount; ++p) {

View File

@@ -44,7 +44,7 @@ private:
ParticleSystemWeakPtr m_parent;
// self related
Point m_pos;
Point m_position;
float m_duration, m_delay;
double m_elapsedTime;
bool m_finished, m_active;

View File

@@ -128,6 +128,70 @@ std::vector<uint8> Texture::getPixels()
return pixels;
}
void Texture::generateBilinearMipmaps()
{
std::vector<uint8> inPixels = getPixels();
bind();
if(!m_useMipmaps) {
m_useMipmaps = true;
setupFilters();
}
Size inSize = getSize();
Size outSize = inSize / 2;
std::vector<uint8> outPixels(outSize.area()*4);
int mipmap = 1;
while(true) {
for(int x=0;x<outSize.width();++x) {
for(int y=0;y<outSize.height();++y) {
uint8 *inPixel[4];
inPixel[0] = &inPixels[((y*2)*inSize.width() + (x*2))*4];
inPixel[1] = &inPixels[((y*2)*inSize.width() + (x*2)+1)*4];
inPixel[2] = &inPixels[((y*2+1)*inSize.width() + (x*2))*4];
inPixel[3] = &inPixels[((y*2+1)*inSize.width() + (x*2)+1)*4];
uint8 *outPixel = &outPixels[(y*outSize.width() + x)*4];
int pixelsSum[4];
for(int i=0;i<4;++i)
pixelsSum[i] = 0;
int usedPixels = 0;
for(int j=0;j<4;++j) {
// ignore colors of complete alpha pixels
if(inPixel[j][3] < 16)
continue;
for(int i=0;i<4;++i)
pixelsSum[i] += inPixel[j][i];
usedPixels++;
}
for(int i=0;i<4;++i) {
if(usedPixels > 0)
outPixel[i] = pixelsSum[i] / usedPixels;
else
outPixel[i] = 0;
}
outPixel[3] = pixelsSum[3]/4;
}
}
glTexImage2D(GL_TEXTURE_2D, mipmap++, GL_RGBA, outSize.width(), outSize.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, &outPixels[0]);
if(inSize.width() == 1 || inSize.height() == 1)
break;
inPixels = outPixels;
inSize /= 2;
outSize /= 2;
}
}
void Texture::setupFilters()
{
GLint minFilter;

View File

@@ -35,6 +35,7 @@ public:
void bind() { glBindTexture(GL_TEXTURE_2D, m_textureId); }
void generateMipmaps();
void generateBilinearMipmaps();
void setSmooth(bool smooth);
GLuint getId() { return m_textureId; }