Clean up textures;

This commit is contained in:
bjorn 2016-11-27 02:06:47 -08:00
parent 9b0712bb66
commit d44cbfa5e7
5 changed files with 26 additions and 42 deletions

View File

@ -68,8 +68,8 @@ void lovrBufferDestroy(const Ref* ref) {
void lovrBufferDraw(Buffer* buffer) {
int usingIbo = buffer->map.length > 0;
lovrGraphicsSetTexture(buffer->texture);
lovrGraphicsPrepare();
lovrGraphicsBindTexture(buffer->texture);
glBindVertexArray(buffer->vao);
// Figure out how many vertex attributes there are

View File

@ -1,4 +1,5 @@
#include "graphics/graphics.h"
#include "loaders/texture.h"
#include "util.h"
#include "glfw.h"
#define _USE_MATH_DEFINES
@ -19,24 +20,13 @@ void lovrGraphicsInit() {
state.skyboxShader = lovrShaderCreate(lovrSkyboxVertexShader, lovrSkyboxFragmentShader);
int uniformId = lovrShaderGetUniformId(state.skyboxShader, "cube");
lovrShaderSendInt(state.skyboxShader, uniformId, 1);
state.defaultTexture = lovrTextureCreate(lovrTextureDataGetEmpty());
glGenBuffers(1, &state.shapeBuffer);
glGenBuffers(1, &state.shapeIndexBuffer);
glGenVertexArrays(1, &state.shapeArray);
vec_init(&state.shapeData);
vec_init(&state.shapeIndices);
state.depthTest = -1;
// Create default texture
TextureData* defaultTextureData = malloc(sizeof(TextureData));
defaultTextureData->width = 1;
defaultTextureData->height = 1;
uint8_t* data = malloc(4 * sizeof(uint8_t));
data[0] = data[1] = data[2] = data[3] = 0xff;
defaultTextureData->data = data;
state.defaultTexture = lovrTextureCreate(defaultTextureData);
state.lastTexture = NULL;
glActiveTexture(GL_TEXTURE0);
lovrGraphicsReset();
}
@ -67,8 +57,8 @@ void lovrGraphicsReset() {
vec_push(&state.transforms, mat4_init());
lovrGraphicsSetProjection(.1f, 100.f, 67 * M_PI / 180); // TODO customize via lovr.conf
lovrGraphicsSetShader(state.defaultShader);
lovrGraphicsSetTexture(state.defaultTexture);
lovrGraphicsSetShader(NULL);
lovrGraphicsBindTexture(NULL);
lovrGraphicsSetBackgroundColor(0, 0, 0, 0);
lovrGraphicsSetColor(255, 255, 255, 255);
lovrGraphicsSetColorMask(1, 1, 1, 1);
@ -102,16 +92,6 @@ void lovrGraphicsPresent() {
void lovrGraphicsPrepare() {
Shader* shader = lovrGraphicsGetShader();
lovrShaderBind(shader, vec_last(&state.transforms), state.projection, state.color, 0);
Texture* texture = lovrGraphicsGetTexture();
if (texture) {
if (texture != state.lastTexture) {
lovrTextureBind(texture);
state.lastTexture = texture;
}
} else {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
// State
@ -202,23 +182,12 @@ void lovrGraphicsSetShader(Shader* shader) {
}
}
Texture* lovrGraphicsGetTexture() {
return state.activeTexture;
}
void lovrGraphicsSetTexture(Texture* texture) {
void lovrGraphicsBindTexture(Texture* texture) {
if (!texture) {
texture = state.defaultTexture;
}
if (texture != state.activeTexture) {
if (state.activeTexture) {
lovrRelease(&state.activeTexture->ref);
}
state.activeTexture = texture;
lovrRetain(&state.activeTexture->ref);
}
lovrTextureBind(texture);
}
void lovrGraphicsSetProjection(float near, float far, float fov) {
@ -394,6 +363,7 @@ void lovrGraphicsDrawLinedShape(GLenum mode) {
vec_uint_t* indices = &state.shapeIndices;
lovrGraphicsPrepare();
lovrGraphicsBindTexture(NULL);
glBindVertexArray(state.shapeArray);
glBindBuffer(GL_ARRAY_BUFFER, state.shapeBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices->length * sizeof(float), vertices->data, GL_STREAM_DRAW);
@ -415,6 +385,7 @@ void lovrGraphicsDrawFilledShape() {
int strideBytes = stride * sizeof(float);
lovrGraphicsPrepare();
lovrGraphicsBindTexture(NULL);
glBindVertexArray(state.shapeArray);
glBindBuffer(GL_ARRAY_BUFFER, state.shapeBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices->length * sizeof(float), vertices->data, GL_STREAM_DRAW);

View File

@ -39,8 +39,6 @@ typedef struct {
Shader* activeShader;
Shader* defaultShader;
Shader* skyboxShader;
Texture* activeTexture;
Texture* lastTexture;
Texture* defaultTexture;
vec_mat4_t transforms;
mat4 projection;
@ -84,8 +82,7 @@ void lovrGraphicsGetScissor(int* x, int* y, int* width, int* height);
void lovrGraphicsSetScissor(int x, int y, int width, int height);
Shader* lovrGraphicsGetShader();
void lovrGraphicsSetShader(Shader* shader);
Texture* lovrGraphicsGetTexture();
void lovrGraphicsSetTexture(Texture* texture);
void lovrGraphicsBindTexture(Texture* texture);
void lovrGraphicsSetProjection(float near, float far, float fov);
void lovrGraphicsSetProjectionRaw(mat4 projection);
float lovrGraphicsGetLineWidth();

View File

@ -2,6 +2,21 @@
#include "util.h"
#include <stdlib.h>
TextureData* lovrTextureDataGetEmpty() {
TextureData* textureData = malloc(sizeof(TextureData));
if (!textureData) return NULL;
uint8_t* pixel = malloc(4 * sizeof(uint8_t));
pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0xff;
textureData->width = 1;
textureData->height = 1;
textureData->channels = 4;
textureData->data = pixel;
return textureData;
}
TextureData* lovrTextureDataFromFile(void* data, int size) {
TextureData* textureData = malloc(sizeof(TextureData));
if (!textureData) return NULL;

View File

@ -1,5 +1,6 @@
#include "graphics/texture.h"
#include "openvr.h"
TextureData* lovrTextureDataGetEmpty();
TextureData* lovrTextureDataFromFile(void* data, int size);
TextureData* lovrTextureDataFromOpenVRModel(OpenVRModel* vrModel);