@@ -61,10 +61,15 @@ struct Matrix3 { | |||
return *this; | |||
} | |||
static const Matrix3<T> IDENTITY; | |||
template<typename U> | |||
friend std::ostream &operator<<(std::ostream &os, const Matrix3<U> &mat); | |||
}; | |||
template<typename T> | |||
const Matrix3<T> Matrix3<T>::IDENTITY = Matrix3<T>(); | |||
template<typename T> | |||
std::ostream &operator<<(std::ostream &os, const Matrix3<T> &mat) { | |||
os << '(' |
@@ -15,8 +15,9 @@ namespace Cygnet { | |||
struct TexturedProg: public GlProgram { | |||
using GlProgram::GlProgram; | |||
GLint transform = uniformLoc("camera"); | |||
GLint position = attribLoc("position"); | |||
GLint camera = uniformLoc("camera"); | |||
GLint transform = uniformLoc("transform"); | |||
GLint vertex = attribLoc("vertex"); | |||
GLint texCoord = attribLoc("texCoord"); | |||
GLint tex = uniformLoc("tex"); | |||
}; | |||
@@ -58,14 +59,16 @@ void Renderer::draw() { | |||
}; | |||
state_->camera.translate(0.01, 0); | |||
glUniformMatrix3fv(state_->texturedProg.transform, 1, GL_TRUE, state_->camera.data()); | |||
glVertexAttribPointer(state_->texturedProg.position, 2, GL_FLOAT, GL_FALSE, | |||
glUniformMatrix3fv(state_->texturedProg.transform, 1, GL_TRUE, Mat3gf::IDENTITY.data()); | |||
glUniformMatrix3fv(state_->texturedProg.camera, 1, GL_TRUE, state_->camera.data()); | |||
glVertexAttribPointer(state_->texturedProg.vertex, 2, GL_FLOAT, GL_FALSE, | |||
4 * sizeof(GLfloat), vertexes); | |||
glVertexAttribPointer(state_->texturedProg.texCoord, 2, GL_FLOAT, GL_FALSE, | |||
4 * sizeof(GLfloat), &vertexes[2]); | |||
glCheck(); | |||
glEnableVertexAttribArray(state_->texturedProg.position); | |||
glEnableVertexAttribArray(state_->texturedProg.vertex); | |||
glEnableVertexAttribArray(state_->texturedProg.texCoord); | |||
glCheck(); | |||
@@ -28,6 +28,7 @@ TileAtlas::~TileAtlas() = default; | |||
void TileAtlas::addTile(size_t tileId, const void *data, size_t len) { | |||
size_t rows = len / (SwanCommon::TILE_SIZE * 4); | |||
std::cerr << "rows: " << rows << " (" << len << ")\n"; | |||
const unsigned char *bytes = (const unsigned char *)data; | |||
size_t x = tileId % state_->tilesPerLine; | |||
size_t y = tileId / state_->tilesPerLine; |
@@ -4,11 +4,12 @@ namespace Cygnet::Shaders { | |||
const char *texturedVx = R"glsl( | |||
uniform mat3 camera; | |||
attribute vec2 position; | |||
uniform mat3 transform; | |||
attribute vec2 vertex; | |||
attribute vec2 texCoord; | |||
varying vec2 v_texCoord; | |||
void main() { | |||
vec3 pos = camera * vec3(position, 1); | |||
vec3 pos = camera * transform * vec3(vertex, 1); | |||
gl_Position = vec4(pos.x, pos.y, 0, 1); | |||
v_texCoord = texCoord; | |||
} |
@@ -10,7 +10,7 @@ | |||
void addTile(Cygnet::Renderer &rnd, const char *path) { | |||
static size_t id = 0; | |||
SDL_Surface *surf = IMG_Load(path); | |||
rnd.registerTileTexture(id++, surf->pixels, surf->pitch * surf->h * 4); | |||
rnd.registerTileTexture(id++, surf->pixels, surf->pitch * surf->h); | |||
SDL_FreeSurface(surf); | |||
} | |||