Browse Source

camera stuff

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
0e2f1ecfa5

+ 10
- 6
include/swan-common/Matrix3.h View File

@@ -4,10 +4,14 @@
#include <cmath>
#include <array>

#include "Vector2.h"

namespace SwanCommon {

template<typename T>
struct Matrix3 {
using Vec = Vector2<T>;

static constexpr std::array<T, 9> identity = {1, 0, 0, 0, 1, 0, 0, 0, 1};
std::array<T, 9> vals;

@@ -39,15 +43,15 @@ struct Matrix3 {
return *this;
}

constexpr Matrix3<T> &translate(T x, T y) {
at(2, 0) += x;
at(2, 1) += y;
constexpr Matrix3<T> &translate(Vec vec) {
at(2, 0) += vec.x;
at(2, 1) += vec.y;
return *this;
}

constexpr Matrix3<T> &scale(T x, T y) {
at(0, 0) *= x;
at(1, 1) *= y;
constexpr Matrix3<T> &scale(Vec vec) {
at(0, 0) *= vec.x;
at(1, 1) *= vec.y;
return *this;
}


+ 9
- 0
include/swan-common/concurrency.h View File

@@ -0,0 +1,9 @@
#pragma once

namespace SwanCommon {

template<typename T>
class Lock {
};

}

+ 6
- 1
libcygnet/include/cygnet/Renderer.h View File

@@ -17,6 +17,11 @@ struct RenderChunk {
GLuint tex;
};

struct RenderCamera {
SwanCommon::Vec2 pos;
float zoom;
};

class Renderer {
public:
using TileID = uint16_t;
@@ -26,7 +31,7 @@ public:

void drawChunk(SwanCommon::Vec2 pos, RenderChunk chunk);

void draw();
void draw(const RenderCamera &cam);

void registerTileTexture(TileID tileId, const void *data, size_t len);
void uploadTileTexture();

+ 5
- 6
libcygnet/src/Renderer.cc View File

@@ -143,24 +143,24 @@ struct RendererState {

TileAtlas atlas;
GlTexture atlasTex;

Mat3gf camera;
};

Renderer::Renderer(): state_(std::make_unique<RendererState>()) {}

Renderer::~Renderer() = default;

void Renderer::draw() {
void Renderer::draw(const RenderCamera &cam) {
Mat3gf camMat;
camMat.translate({ -cam.pos.x, cam.pos.y }); // TODO: Change something to make this -cam.pos
camMat.scale({ cam.zoom, cam.zoom });
auto &chunkProg = state_->chunkProg;
state_->camera.reset().translate(-0.9, 0.9).scale(0.125, 0.125);

chunkProg.enable();

glUniform2f(chunkProg.tileAtlasSize,
(float)(int)(state_->atlasTex.width() / SwanCommon::TILE_SIZE),
(float)(int)(state_->atlasTex.height() / SwanCommon::TILE_SIZE));
glUniformMatrix3fv(chunkProg.camera, 1, GL_TRUE, state_->camera.data());
glUniformMatrix3fv(chunkProg.camera, 1, GL_TRUE, camMat.data());
glCheck();

glActiveTexture(GL_TEXTURE0);
@@ -168,7 +168,6 @@ void Renderer::draw() {
glCheck();

glActiveTexture(GL_TEXTURE1);

for (auto [pos, chunk]: draw_chunks_) {
glUniform2f(chunkProg.pos, pos.x, pos.y);
glBindTexture(GL_TEXTURE_2D, chunk.tex);

+ 6
- 1
src/cygnet-test.cc View File

@@ -41,6 +41,11 @@ int main() {
chunk = rnd.createChunk(tiles);
}

Cygnet::RenderCamera cam = {
.pos = { 0.9, 0.9 },
.zoom = 0.125,
};

while (true) {
SDL_Event evt;
while (SDL_PollEvent(&evt)) {
@@ -53,7 +58,7 @@ int main() {
rnd.drawChunk({0, 0}, chunk);

win.clear();
rnd.draw();
rnd.draw(cam);
win.flip();
}


Loading…
Cancel
Save