Perspective projection textures;

This commit is contained in:
bjorn 2017-01-12 01:26:44 -08:00
parent 09fdb72d5c
commit af3957d3c3
3 changed files with 19 additions and 4 deletions

View File

@ -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);

View File

@ -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();

View File

@ -1,6 +1,7 @@
#include "graphics/texture.h"
#include "graphics/graphics.h"
#include "util.h"
#include <math.h>
#include <stdlib.h>
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);
}
}