Rotate, translate and scale added to ogl2

This commit is contained in:
Henrique Santiago 2013-01-19 02:18:29 +00:00
parent 36e95b2f48
commit 8e9d137608
10 changed files with 109 additions and 3 deletions

View File

@ -20,4 +20,3 @@ Module
- client_terminal
- client_modulemanager
//- client_stats

View File

@ -25,6 +25,7 @@
#include "game.h"
#include "tile.h"
#include <framework/core/eventdispatcher.h>
#include <framework/graphics/graphics.h>
LocalPlayer::LocalPlayer()
{
@ -56,6 +57,41 @@ LocalPlayer::LocalPlayer()
m_totalCapacity = -1;
}
void LocalPlayer::draw(const Point& dest, float scaleFactor, bool animate, LightView *lightView)
{
Creature::draw(dest, scaleFactor, animate, lightView);
// This is a test to rotation, translate and scale transformations.
/*
g_painter->saveAndResetState();
g_painter->rotate(dest.x, dest.y, Fw::pi / 4.);
Creature::draw(dest, scaleFactor, animate, lightView);
g_painter->restoreSavedState();
*/
// 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));
*/
}
void LocalPlayer::lockWalk(int millis)
{
m_walkLockExpiration = std::max(m_walkLockExpiration, (ticks_t) g_clock.millis() + millis);

View File

@ -35,6 +35,8 @@ class LocalPlayer : public Player
public:
LocalPlayer();
virtual void draw(const Point& dest, float scaleFactor, bool animate, LightView *lightView = nullptr);
void unlockWalk() { m_walkLockExpiration = 0; }
void lockWalk(int millis = 250);
void stopAutoWalkUpdate();

View File

@ -108,6 +108,20 @@ public:
return pos;
}
double getAngleFromPosition(const Position& position) const {
// Returns angle in radians from 0 to 2Pi. -1 means positions are equal.
int dx = position.x - x;
int dy = position.y - y;
if(dx == 0 && dy == 0)
return -1;
float angle = std::atan2(dy * -1, dx);
if(angle < 0)
angle += 2 * Fw::pi;
return angle;
}
Otc::Direction getDirectionFromPosition(const Position& position) const {
int dx = position.x - x;
int dy = position.y - y;

View File

@ -64,6 +64,7 @@ void Painter::saveState()
{
assert(m_oldStateIndex<10);
m_olderStates[m_oldStateIndex].resolution = m_resolution;
m_olderStates[m_oldStateIndex].transformMatrix = m_transformMatrix;
m_olderStates[m_oldStateIndex].projectionMatrix = m_projectionMatrix;
m_olderStates[m_oldStateIndex].textureMatrix = m_textureMatrix;
m_olderStates[m_oldStateIndex].color = m_color;
@ -86,6 +87,7 @@ void Painter::restoreSavedState()
{
m_oldStateIndex--;
setResolution(m_olderStates[m_oldStateIndex].resolution);
setTransformMatrix(m_olderStates[m_oldStateIndex].transformMatrix);
setProjectionMatrix(m_olderStates[m_oldStateIndex].projectionMatrix);
setTextureMatrix(m_olderStates[m_oldStateIndex].textureMatrix);
setColor(m_olderStates[m_oldStateIndex].color);
@ -183,6 +185,34 @@ void Painter::setResolution(const Size& resolution)
updateGlViewport();
}
void Painter::scale(double x, double y)
{
m_transformMatrix.data()[0] *= x;
m_transformMatrix.data()[4] *= y;
}
void Painter::translate(double x, double y)
{
m_transformMatrix.data()[6] += x / m_resolution.width();
m_transformMatrix.data()[7] -= y / m_resolution.height();
}
void Painter::rotate(double x, double y, double angle)
{
// TODO: use x, y vectors to properly rotate.
if(m_transformMatrix.data()[1] == 0)
m_transformMatrix.data()[1] = 1;
if(m_transformMatrix.data()[3] == 0)
m_transformMatrix.data()[3] = 1;
double s = sin(angle);
double c = cos(angle);
m_transformMatrix.data()[0] *= c;
m_transformMatrix.data()[1] *= s;
m_transformMatrix.data()[3] *= -s;
m_transformMatrix.data()[4] *= c;
}
void Painter::updateGlTexture()
{
if(m_glTextureId != 0)

View File

@ -46,6 +46,7 @@ public:
struct PainterState {
Size resolution;
Matrix3 transformMatrix;
Matrix3 projectionMatrix;
Matrix3 textureMatrix;
Color color;
@ -82,6 +83,7 @@ public:
virtual void drawFilledTriangle(const Point& a, const Point& b, const Point& c) = 0;
virtual void drawBoundingRect(const Rect& dest, int innerLineWidth = 1) = 0;
virtual void setTransformMatrix(const Matrix3& transformMatrix) { m_transformMatrix = transformMatrix; }
virtual void setProjectionMatrix(const Matrix3& projectionMatrix) { m_projectionMatrix = projectionMatrix; }
virtual void setTextureMatrix(const Matrix3& textureMatrix) { m_textureMatrix = textureMatrix; }
virtual void setColor(const Color& color) { m_color = color; }
@ -96,6 +98,11 @@ public:
void setTexture(const TexturePtr& texture) { setTexture(texture.get()); }
void setResolution(const Size& resolution);
void scale(double x, double y);
void translate(double x, double y);
void rotate(double x, double y, double angle);
Matrix3 getTransformMatrix() { return m_transformMatrix; }
Matrix3 getProjectionMatrix() { return m_projectionMatrix; }
Matrix3 getTextureMatrix() { return m_textureMatrix; }
Color getColor() { return m_color; }
@ -104,7 +111,7 @@ public:
Rect getClipRect() { return m_clipRect; }
PainterShaderProgram *getShaderProgram() { return m_shaderProgram; }
bool getAlphaWriting() { return m_alphaWriting; }
Size getResolution() { return m_resolution; };
Size getResolution() { return m_resolution; }
void resetColor() { setColor(Color::white); }
void resetOpacity() { setOpacity(1.0f); }
@ -125,6 +132,7 @@ protected:
CoordsBuffer m_coordsBuffer;
Matrix3 m_transformMatrix;
Matrix3 m_projectionMatrix;
Matrix3 m_textureMatrix;
Color m_color;

View File

@ -79,6 +79,7 @@ void PainterOGL2::drawCoords(CoordsBuffer& coordsBuffer, DrawMode drawMode)
// update shader with the current painter state
m_drawProgram->bind();
m_drawProgram->setTransformMatrix(m_transformMatrix);
m_drawProgram->setProjectionMatrix(m_projectionMatrix);
if(textured) {
m_drawProgram->setTextureMatrix(m_textureMatrix);

View File

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

View File

@ -38,6 +38,7 @@ PainterShaderProgram::PainterShaderProgram()
void PainterShaderProgram::setupUniforms()
{
bindUniformLocation(TRANSFORM_MATRIX_UNIFORM, "u_TransformMatrix");
bindUniformLocation(PROJECTION_MATRIX_UNIFORM, "u_ProjectionMatrix");
bindUniformLocation(TEXTURE_MATRIX_UNIFORM, "u_TextureMatrix");
bindUniformLocation(COLOR_UNIFORM, "u_Color");
@ -49,6 +50,7 @@ void PainterShaderProgram::setupUniforms()
bindUniformLocation(TEX3_UNIFORM, "u_Tex3");
bindUniformLocation(RESOLUTION_UNIFORM, "u_Resolution");
setUniformValue(TRANSFORM_MATRIX_UNIFORM, m_transformMatrix);
setUniformValue(PROJECTION_MATRIX_UNIFORM, m_projectionMatrix);
setUniformValue(TEXTURE_MATRIX_UNIFORM, m_textureMatrix);
setUniformValue(COLOR_UNIFORM, m_color);
@ -75,6 +77,16 @@ bool PainterShaderProgram::link()
return false;
}
void PainterShaderProgram::setTransformMatrix(const Matrix3& transformMatrix)
{
if(transformMatrix == m_transformMatrix)
return;
bind();
setUniformValue(TRANSFORM_MATRIX_UNIFORM, transformMatrix);
m_transformMatrix = transformMatrix;
}
void PainterShaderProgram::setProjectionMatrix(const Matrix3& projectionMatrix)
{
if(projectionMatrix == m_projectionMatrix)

View File

@ -43,6 +43,7 @@ protected:
TEX2_UNIFORM = 7,
TEX3_UNIFORM = 8,
RESOLUTION_UNIFORM = 9,
TRANSFORM_MATRIX_UNIFORM = 10
};
friend class PainterOGL2;
@ -54,6 +55,7 @@ public:
bool link();
void setTransformMatrix(const Matrix3& transformMatrix);
void setProjectionMatrix(const Matrix3& projectionMatrix);
void setTextureMatrix(const Matrix3& textureMatrix);
void setColor(const Color& color);
@ -69,6 +71,7 @@ private:
Color m_color;
float m_opacity;
Matrix3 m_transformMatrix;
Matrix3 m_projectionMatrix;
Matrix3 m_textureMatrix;
Size m_resolution;