From af3957d3c300b31a0adc57b6896e66798bdfd0ef Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 12 Jan 2017 01:26:44 -0800 Subject: [PATCH] Perspective projection textures; --- src/graphics/graphics.c | 6 ++++-- src/graphics/graphics.h | 3 ++- src/graphics/texture.c | 14 +++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 78da9892..c03b151f 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -19,7 +19,6 @@ void lovrGraphicsInit() { } for (int i = 0; i < MAX_CANVASES; i++) { state.canvases[i] = malloc(sizeof(CanvasState)); - state.canvases[i]->projection = mat4_init(); } state.defaultShader = lovrShaderCreate(lovrDefaultVertexShader, lovrDefaultFragmentShader); state.skyboxShader = lovrShaderCreate(lovrSkyboxVertexShader, lovrSkyboxFragmentShader); @@ -42,7 +41,6 @@ void lovrGraphicsDestroy() { mat4_deinit(state.transforms[i]); } for (int i = 0; i < MAX_CANVASES; i++) { - mat4_deinit(state.canvases[i]->projection); free(state.canvases[i]); } lovrRelease(&state.defaultShader->ref); @@ -196,6 +194,10 @@ void lovrGraphicsBindTexture(Texture* texture) { lovrTextureBind(texture); } +mat4 lovrGraphicsGetProjection() { + return state.canvases[state.canvas]->projection; +} + void lovrGraphicsSetProjection(float near, float far, float fov) { int width, height; glfwGetWindowSize(window, &width, &height); diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index b5d59f8b..fe3404a4 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -40,7 +40,7 @@ typedef struct { typedef struct CanvasState { int framebuffer; - mat4 projection; + float projection[16]; int viewport[4]; } CanvasState; @@ -94,6 +94,7 @@ void lovrGraphicsSetScissor(int x, int y, int width, int height); Shader* lovrGraphicsGetShader(); void lovrGraphicsSetShader(Shader* shader); void lovrGraphicsBindTexture(Texture* texture); +mat4 lovrGraphicsGetProjection(); void lovrGraphicsSetProjection(float near, float far, float fov); void lovrGraphicsSetProjectionRaw(mat4 projection); float lovrGraphicsGetLineWidth(); diff --git a/src/graphics/texture.c b/src/graphics/texture.c index 61dd4061..1edb8f25 100644 --- a/src/graphics/texture.c +++ b/src/graphics/texture.c @@ -1,6 +1,7 @@ #include "graphics/texture.h" #include "graphics/graphics.h" #include "util.h" +#include #include Texture* lovrTextureCreate(TextureData* textureData) { @@ -79,7 +80,18 @@ void lovrTextureBindFramebuffer(Texture* texture) { mat4_setOrthographic(projection, 0, w, 0, h, -1, 1); lovrGraphicsSetProjectionRaw(projection); } else if (texture->projection == PROJECTION_PERSPECTIVE) { - // lovrGraphicsSetProjection(); // TODO perspective + mat4 projection = lovrGraphicsGetProjection(); + float b = projection[5]; + float c = projection[10]; + float d = projection[14]; + float aspect = (float) w / h; + float k = (c - 1.f) / (c + 1.f); + float near = (d * (1.f - k)) / (2.f * k); + float far = k * near; + float fov = 2.f * atan(1.f / b); + float newProjection[16]; + mat4_setPerspective(newProjection, near, far, fov, aspect); + lovrGraphicsSetProjectionRaw(newProjection); } }