Browse Source

make everything pretend positive y is down, then flip with the camera matrix

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
eabd356117
2 changed files with 14 additions and 10 deletions
  1. 12
    8
      libcygnet/src/Renderer.cc
  2. 2
    2
      libcygnet/src/shaders.cc

+ 12
- 8
libcygnet/src/Renderer.cc View File

@@ -35,9 +35,9 @@ struct ChunkProg: public GlProgram {
static constexpr float cw = (float)SwanCommon::CHUNK_WIDTH;
static constexpr GLfloat vertexes[] = {
0.0f, 0.0f, // pos 0: top left
0.0f, -ch , // pos 1: bottom left
cw, -ch, // pos 2: bottom right
cw, -ch, // pos 2: bottom right
0.0f, ch , // pos 1: bottom left
cw, ch, // pos 2: bottom right
cw, ch, // pos 2: bottom right
cw, 0.0f, // pos 3: top right
0.0f, 0.0f, // pos 0: top left
};
@@ -87,9 +87,9 @@ struct SpriteProg: public GlProgram {

static constexpr GLfloat vertexes[] = {
0.0f, 0.0f, // pos 0: top left
0.0f, -1.0f, // pos 1: bottom left
1.0f, -1.0f, // pos 2: bottom right
1.0f, -1.0f, // pos 2: bottom right
0.0f, 1.0f, // pos 1: bottom left
1.0f, 1.0f, // pos 2: bottom right
1.0f, 1.0f, // pos 2: bottom right
1.0f, 0.0f, // pos 3: top right
0.0f, 0.0f, // pos 0: top left
};
@@ -143,9 +143,13 @@ Renderer::~Renderer() = default;

void Renderer::draw(const RenderCamera &cam) {
Mat3gf camMat;

// Make the matrix translate to { -camX, -camY }, fix up the aspect ratio,
// flip the Y axis so that positive Y direction is down, and scale according to zoom.
float ratio = (float)cam.size.y / (float)cam.size.x;
camMat.translate(cam.pos.scale(-ratio, 1) * cam.zoom); // TODO: Change something to make this -cam.pos
camMat.scale({ cam.zoom * ratio, cam.zoom });
camMat.translate(cam.pos.scale(-ratio, 1) * cam.zoom);
camMat.scale({ cam.zoom * ratio, -cam.zoom });

auto &chunkProg = state_->chunkProg;
auto &spriteProg = state_->spriteProg;


+ 2
- 2
libcygnet/src/shaders.cc View File

@@ -11,7 +11,7 @@ const char *spriteVx = R"glsl(
void main() {
vec3 pos = camera * transform * vec3(vertex, 1);
gl_Position = vec4(pos.xy, 0, 1);
v_texCoord = vec2(vertex.x, -vertex.y);
v_texCoord = vec2(vertex.x, vertex.y);
}
)glsl";

@@ -34,7 +34,7 @@ const char *chunkVx = R"glsl(
void main() {
vec3 pos = camera * vec3(pos + vertex, 1);
gl_Position = vec4(pos.xy, 0, 1);
v_tileCoord = vec2(vertex.x, -vertex.y);
v_tileCoord = vec2(vertex.x, vertex.y);
}
)glsl";


Loading…
Cancel
Save