Haaaa ça ye est ça fonctionne enfin!!!
Les shaders, c'est tout simplement magiiik!
uniform vec4 origin;
uniform mat4 modelViewMatrix;
void main () {
// transforme la position du vertex
gl_Position = gl_ProjectionMatrix * (modelViewMatrix * (gl_Vertex - origin) + origin);
// transforme les coordonnées de texture
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
// transmet la couleur
gl_FrontColor = gl_Color;
}
#include "tGround.h"
#include "ihm.h"
TileGround::TileGround (Tile *tile) : TexturedDynamicDrawableEntity (tile->getPosition(), tile->getSize(),tile->getSize() * 0.5f, Entity::E_GROUND) {
this->tile = tile;
}
const sf::Texture& TileGround::getTexture () {
return tile->getTexture();
}
IntRect TileGround::getTextRect() {
return tile->getTextRect();
}
void TileGround::setSelected(bool b) {
tile->setSelected(b);
}
bool TileGround::isSelected () {
return tile->isSelected();
}
void TileGround::setZOrder (int zOrder) {
tile->setZOrder(zOrder);
}
int TileGround::getZOrder () {
return tile->getZOrder();
}
void TileGround::draw (RenderTarget &target, RenderStates states) const {
states.transform = states.transform * getTransform();
Shader &shader = IHM::getShader();
shader.setParameter("modelViewMatrix", states.transform);
Vec2f center = const_cast<TileGround*> (this)->getCenter();
shader.setParameter("origin", center.x, center.y, 0, 0);
target.draw(*tile, &shader);
}
Tile* TileGround::getTile () {
return tile;
}
#include "tile.h"
#include "../world/gridMap.h"
#include "../Lighting/shadow.h"
#include "../Lighting/wall.h"
#include "ihm.h"
using namespace sf;
using namespace std;
/*Gère les différents tiles, cette classe hérite de Sprite.
*/
//Crée un tile, avec une image passée en paramètre.
Tile::Tile (sf::Texture &image, Vec2f position, Vec2f size, IntRect subRect, bool bToAnim) : TexturedDynamicDrawableEntity (position, Vec2f (subRect.width, subRect.height), size * 0.5f, E_TILE) {
sprite.setTexture(image);
sprite.setTextureRect(subRect);
sprite.setPosition(position.x, position.y);
float factorX = size.x / (float) subRect.width;
float factorY = size.y / (float) subRect.height;
setScale(Vec2f(factorX, factorY));
this->bToAnim = bToAnim;
selected = false;
zOrder = 0;
}
void Tile::setZOrder(int zOrder) {
this->zOrder = zOrder;
}
int Tile::getZOrder() {
return zOrder;
}
void Tile::setSelected (bool b) {
cout<<"Selected : "<<b<<endl;
if (b) {
int x = getPosition().x;
int y = getPosition().y;
int w = getSize().x;
int h = getSize().y;
border = RectangleShape (Vector2f(w, h));
border.setPosition(Vector2f(x, y));
border.setOutlineColor(Color(255, 0, 0));
border.setFillColor(Color(0, 0, 0, 0));
border.setOutlineThickness(1.f);
border.setZOrder(NB_LAYERS - 1);
} else {
border.setOutlineThickness(0.f);
}
selected = b;
}
bool Tile::isSelected () {
return selected;
}
const Texture& Tile::getTexture () {
return *sprite.getTexture();
}
sf::IntRect Tile::getTextRect() {
return sprite.getTextureRect();
}
bool Tile::belongToAnim () {
return bToAnim;
}
bool Tile::operator== (Tile &tile) {
return sprite.getTexture() == tile.sprite.getTexture() &&
getPosition().x == tile.getPosition().x &&
getPosition().y == tile.getPosition().y &&
getSize().x == tile.getSize().x &&
getSize().y == tile.getSize().y &&
getZOrder() == tile.getZOrder() &&
getType() == tile.getType () &&
sprite.getTextureRect().left == tile.sprite.getTextureRect().left &&
sprite.getTextureRect().top == tile.sprite.getTextureRect().top &&
sprite.getTextureRect().height == tile.sprite.getTextureRect().height &&
sprite.getTextureRect().width == tile.sprite.getTextureRect().width;
}
bool Tile::operator!= (Tile &tile) {
return !(*this == tile);
}
void Tile::draw(RenderTarget &target, RenderStates states) const {
states.transform = states.transform * getTransform();
Shader &shader = IHM::getShader();
shader.setParameter("modelViewMatrix", states.transform);
Vec2f center = const_cast<Tile*> (this)->getCenter();
shader.setParameter("origin", center.x, center.y, 0, 0);
target.draw(sprite, &shader);
target.draw (border);
}
//Getter.
Sprite& Tile::getSprite () {
return sprite;
}
Tile::~Tile () {
}
#ifndef DYNAMIC_DRAWABLE_ENTITY
#define DYNAMIC_DRAWABLE_ENTITY
#include "drawableEntity.h"
#include "../Math/transformMatrix.h"
class DynamicDrawableEntity : public DrawableEntity {
public :
BoundingRectangle getLocalBounds() {
return localBounds;
}
void setCenter(Vec2f center) {
Vec2f t = center - m_center ;
move(t);
}
Vec2f getCenter() {
return m_center;
}
void setPosition(Vec2f position) {
Vec2f t = position - m_position;
move(t);
}
Vec2f getSize() {
return m_size;
}
Vec2f getPosition() {
return m_position;
}
void setRotation (float angle) {
m_rotation = angle;
tm.setRotation(angle);
BoundingRectangle bounds = getGlobalBounds();
m_size.x = bounds.getWidth();
m_size.y = bounds.getHeight();
Vec2f delta (m_size.x / localBounds.getWidth() * m_origin.x, m_size.y / localBounds.getHeight() * m_origin.y);
m_position = m_center - delta;
onRotate(angle);
}
void setScale(Vec2f scale) {
m_scale = scale;
tm.setScale(scale.x, scale.y);
BoundingRectangle bounds = getGlobalBounds();
m_size.x = bounds.getWidth();
m_size.y = bounds.getHeight();
Vec2f delta (m_size.x / localBounds.getWidth() * m_origin.x, m_size.y / localBounds.getHeight() * m_origin.y);
m_position = m_center - delta;
onScale(scale);
}
void scale(Vec2f s) {
scale(m_scale * s);
}
void rotate (float angle) {
setRotation(m_rotation + angle);
}
void move (Vec2f t) {
m_position += t;
m_center += t;
onMove(t);
}
void setOrigin(Vec2f origin) {
tm.setOrigin(origin);
}
Vec2f getScale() {
return m_scale;
}
float getRotation() {
return m_rotation;
}
Vec2f getOrigin() {
return m_origin;
}
void setSize (Vec2f size) {
setScale(size / m_size);
}
BoundingRectangle getGlobalBounds() {
BoundingRectangle globalBounds = tm.transformRect(localBounds);
return globalBounds;
}
const sf::Transform getTransform() const {
return const_cast<DynamicDrawableEntity*> (this)->tm.convertToSFMLTransformMatrix();
}
const sf::Transform getInverseTransform()
{
return tm.convertToSFMLTransformMatrix().getInverse();
}
protected :
DynamicDrawableEntity(Vec2f position, Vec2f size, Vec2f origin, TYPE type) : DrawableEntity(type) {
localBounds = BoundingRectangle(position.x, position.y, size.x, size.y);
m_position = position;
m_size = size;
m_center = Vec2f (m_position.x + origin.x, m_position.y + origin.y);
m_origin = origin;
m_scale = Vec2f (1.f, 1.f);
m_rotation = 0;
}
virtual void onRotate(float angle) {}
virtual void onScale(Vec2f s) {}
virtual void onMove(Vec2f t) {}
private :
Vec2f m_position, m_center, m_size, m_origin, m_scale;
float m_rotation;
BoundingRectangle localBounds;
TransformMatrix tm;
};
#endif // TRANSF_ENTITY
Maintenant j'ai une position, un centre, une origine et je peux faire des transformation autour de plusieurs origines.
Faire ça sans shader ça aurait été, chaud.