Fix rotate,translate,rotate in ogl painters

This commit is contained in:
Eduardo Bart 2013-01-19 13:44:27 -02:00
parent 0f9cacdde5
commit 2fcaf2cc40
6 changed files with 69 additions and 48 deletions

View File

@ -63,32 +63,11 @@ void LocalPlayer::draw(const Point& dest, float scaleFactor, bool animate, Light
// This is a test to rotation, translate and scale transformations. // This is a test to rotation, translate and scale transformations.
/* /*
g_painter->saveAndResetState(); Point rotateOffset = dest;
g_painter->rotate(dest.x, dest.y, Fw::pi / 4.); rotateOffset += ((animate ? m_walkOffset : Point(0,0)) + Point(16,16)) * scaleFactor;
g_painter->rotate(rotateOffset, Fw::pi * std::sin(g_clock.millis()/1000.0f));
Creature::draw(dest, scaleFactor, animate, lightView); Creature::draw(dest, scaleFactor, animate, lightView);
g_painter->restoreSavedState(); g_painter->resetTransformMatrix();
*/
// This depends on rotation to get done.
// Textured arrow pointing to desired position.
/*
Position pos = Position(1029, 997, 7);
int radius = 8;
double angle = m_position.getAngleFromPosition(pos);
if(angle < 0) {
radius = 0;
angle = 0;
}
Point animationOffset = animate ? m_walkOffset : Point(0,0);
Point center = Point(dest + (animationOffset - getDisplacement() -8 + Otc::TILE_PIXELS)*scaleFactor);
g_painter->setColor(Color::red);
g_painter->drawFilledRect(Rect(center, Size(3, 3)*scaleFactor));
center.x += radius * cos(angle);
center.y -= radius * sin(angle);
g_painter->setColor(Color::white);
g_painter->drawFilledRect(Rect(center, Size(3, 3)*scaleFactor));
*/ */
} }

View File

@ -49,6 +49,7 @@ void Painter::resetState()
resetShaderProgram(); resetShaderProgram();
resetTexture(); resetTexture();
resetAlphaWriting(); resetAlphaWriting();
resetTransformMatrix();
} }
void Painter::refreshState() void Painter::refreshState()
@ -185,32 +186,44 @@ void Painter::setResolution(const Size& resolution)
updateGlViewport(); updateGlViewport();
} }
void Painter::scale(double x, double y) void Painter::scale(float x, float y)
{ {
m_transformMatrix.data()[0] *= x; Matrix3 scaleMatrix = {
m_transformMatrix.data()[4] *= y; x, 0.0f, 0.0f,
0.0f, y, 0.0f,
0.0f, 0.0f, 1.0f
};
setTransformMatrix(m_transformMatrix * scaleMatrix.transposed());
} }
void Painter::translate(double x, double y) void Painter::translate(float x, float y)
{ {
m_transformMatrix.data()[6] += x / m_resolution.width(); Matrix3 translateMatrix = {
m_transformMatrix.data()[7] -= y / m_resolution.height(); 1.0f, 0.0f, x,
0.0f, 1.0f, y,
0.0f, 0.0f, 1.0f
};
setTransformMatrix(m_transformMatrix * translateMatrix.transposed());
} }
void Painter::rotate(double x, double y, double angle) void Painter::rotate(float angle)
{ {
// TODO: use x, y vectors to properly rotate. Matrix3 rotationMatrix = {
if(m_transformMatrix.data()[1] == 0) std::cos(angle), -std::sin(angle), 0.0f,
m_transformMatrix.data()[1] = 1; std::sin(angle), std::cos(angle), 0.0f,
if(m_transformMatrix.data()[3] == 0) 0.0f, 0.0f, 1.0f
m_transformMatrix.data()[3] = 1; };
double s = sin(angle); setTransformMatrix(m_transformMatrix * rotationMatrix.transposed());
double c = cos(angle); }
m_transformMatrix.data()[0] *= c;
m_transformMatrix.data()[1] *= s; void Painter::rotate(float x, float y, float angle)
m_transformMatrix.data()[3] *= -s; {
m_transformMatrix.data()[4] *= c; translate(-x, -y);
rotate(angle);
translate(x, y);
} }
void Painter::updateGlTexture() void Painter::updateGlTexture()

View File

@ -98,9 +98,13 @@ public:
void setTexture(const TexturePtr& texture) { setTexture(texture.get()); } void setTexture(const TexturePtr& texture) { setTexture(texture.get()); }
void setResolution(const Size& resolution); void setResolution(const Size& resolution);
void scale(double x, double y); void scale(float x, float y);
void translate(double x, double y); void scale(float factor) { scale(factor, factor); }
void rotate(double x, double y, double angle); void translate(float x, float y);
void translate(const Point& p) { translate(p.x, p.y); }
void rotate(float angle);
void rotate(float x, float y, float angle);
void rotate(const Point& p, float angle) { rotate(p.x, p.y, angle); }
Matrix3 getTransformMatrix() { return m_transformMatrix; } Matrix3 getTransformMatrix() { return m_transformMatrix; }
Matrix3 getProjectionMatrix() { return m_projectionMatrix; } Matrix3 getProjectionMatrix() { return m_projectionMatrix; }
@ -120,6 +124,7 @@ public:
void resetShaderProgram() { setShaderProgram(nullptr); } void resetShaderProgram() { setShaderProgram(nullptr); }
void resetTexture() { setTexture(nullptr); } void resetTexture() { setTexture(nullptr); }
void resetAlphaWriting() { setAlphaWriting(false); } void resetAlphaWriting() { setAlphaWriting(false); }
void resetTransformMatrix() { setTransformMatrix(Matrix3()); }
virtual bool hasShaders() = 0; virtual bool hasShaders() = 0;

View File

@ -38,6 +38,7 @@ void PainterOGL1::refreshState()
Painter::refreshState(); Painter::refreshState();
updateGlColor(); updateGlColor();
updateGlMatrixMode(); updateGlMatrixMode();
updateGlTransformMatrix();
updateGlProjectionMatrix(); updateGlProjectionMatrix();
updateGlTextureMatrix(); updateGlTextureMatrix();
updateGlTextureState(); updateGlTextureState();
@ -217,6 +218,13 @@ void PainterOGL1::setMatrixMode(PainterOGL1::MatrixMode matrixMode)
updateGlMatrixMode(); updateGlMatrixMode();
} }
void PainterOGL1::setTransformMatrix(const Matrix3& transformMatrix)
{
m_transformMatrix = transformMatrix;
if(g_painter == this)
updateGlTransformMatrix();
}
void PainterOGL1::setProjectionMatrix(const Matrix3& projectionMatrix) void PainterOGL1::setProjectionMatrix(const Matrix3& projectionMatrix)
{ {
m_projectionMatrix = projectionMatrix; m_projectionMatrix = projectionMatrix;
@ -259,12 +267,25 @@ void PainterOGL1::updateGlMatrixMode()
glMatrixMode(m_matrixMode); glMatrixMode(m_matrixMode);
} }
void PainterOGL1::updateGlTransformMatrix()
{
float glTransformMatrix[] = {
m_transformMatrix(1,1), m_transformMatrix(1,2), 0.0f, m_transformMatrix(1,3),
m_transformMatrix(2,1), m_transformMatrix(2,2), 0.0f, m_transformMatrix(2,3),
0.0f, 0.0f, 1.0f, 0.0f,
m_transformMatrix(3,1), m_transformMatrix(3,2), 0.0f, m_transformMatrix(3,3),
};
setMatrixMode(MatrixTransform);
glLoadMatrixf(glTransformMatrix);
}
void PainterOGL1::updateGlProjectionMatrix() void PainterOGL1::updateGlProjectionMatrix()
{ {
float glProjectionMatrix[] = { float glProjectionMatrix[] = {
m_projectionMatrix(1,1), m_projectionMatrix(1,2), 0.0f, m_projectionMatrix(1,3), m_projectionMatrix(1,1), m_projectionMatrix(1,2), 0.0f, m_projectionMatrix(1,3),
m_projectionMatrix(2,1), m_projectionMatrix(2,2), 0.0f, m_projectionMatrix(2,3), m_projectionMatrix(2,1), m_projectionMatrix(2,2), 0.0f, m_projectionMatrix(2,3),
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
m_projectionMatrix(3,1), m_projectionMatrix(3,2), 0.0f, m_projectionMatrix(3,3), m_projectionMatrix(3,1), m_projectionMatrix(3,2), 0.0f, m_projectionMatrix(3,3),
}; };

View File

@ -39,6 +39,7 @@ public:
enum MatrixMode { enum MatrixMode {
MatrixProjection = 0x1701, //GL_PROJECTION MatrixProjection = 0x1701, //GL_PROJECTION
MatrixTexture = 0x1702, //GL_TEXTURE MatrixTexture = 0x1702, //GL_TEXTURE
MatrixTransform = 0x1700 // GL_MODELVIEW
}; };
PainterOGL1(); PainterOGL1();
@ -58,6 +59,7 @@ public:
void drawBoundingRect(const Rect& dest, int innerLineWidth); void drawBoundingRect(const Rect& dest, int innerLineWidth);
void setMatrixMode(MatrixMode matrixMode); void setMatrixMode(MatrixMode matrixMode);
void setTransformMatrix(const Matrix3& projectionMatrix);
void setProjectionMatrix(const Matrix3& projectionMatrix); void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix3& textureMatrix); void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color); void setColor(const Color& color);
@ -69,6 +71,7 @@ private:
void updateGlColor(); void updateGlColor();
void updateGlMatrixMode(); void updateGlMatrixMode();
void updateGlProjectionMatrix(); void updateGlProjectionMatrix();
void updateGlTransformMatrix();
void updateGlTextureMatrix(); void updateGlTextureMatrix();
void updateGlTextureState(); void updateGlTextureState();

View File

@ -45,7 +45,7 @@ static std::string glslPositionOnlyVertexShader = "\n\
uniform highp mat3 u_TransformMatrix;\n\ uniform highp mat3 u_TransformMatrix;\n\
uniform highp mat3 u_ProjectionMatrix;\n\ uniform highp mat3 u_ProjectionMatrix;\n\
highp vec4 calculatePosition() {\n\ highp vec4 calculatePosition() {\n\
return vec4(u_TransformMatrix * u_ProjectionMatrix * vec3(a_Vertex.xy, 1.0), 1.0);\n\ return vec4(u_ProjectionMatrix * u_TransformMatrix * vec3(a_Vertex.xy, 1.0), 1.0);\n\
}\n"; }\n";
static const std::string glslMainFragmentShader = "\n\ static const std::string glslMainFragmentShader = "\n\