2016-11-26 07:54:45 +00:00
|
|
|
#include "loaders/texture.h"
|
2017-01-22 01:29:20 +00:00
|
|
|
#include "vendor/stb/stb_image.h"
|
2016-11-26 07:54:45 +00:00
|
|
|
#include <stdlib.h>
|
2017-01-22 01:29:20 +00:00
|
|
|
#include <string.h>
|
2016-11-26 07:54:45 +00:00
|
|
|
|
2017-02-03 23:16:30 +00:00
|
|
|
TextureData* lovrTextureDataGetBlank(int width, int height, uint8_t value, TextureFormat format) {
|
2016-11-27 10:06:47 +00:00
|
|
|
TextureData* textureData = malloc(sizeof(TextureData));
|
|
|
|
if (!textureData) return NULL;
|
|
|
|
|
2017-02-03 23:16:30 +00:00
|
|
|
int channels = 0;
|
|
|
|
switch (format) {
|
|
|
|
case FORMAT_RED: channels = 1; break;
|
|
|
|
case FORMAT_RG: channels = 2; break;
|
|
|
|
case FORMAT_RGB: channels = 3; break;
|
|
|
|
case FORMAT_RGBA: channels = 4; break;
|
|
|
|
}
|
|
|
|
|
2017-01-09 05:29:16 +00:00
|
|
|
int size = sizeof(uint8_t) * width * height * channels;
|
|
|
|
uint8_t* data = malloc(size);
|
|
|
|
memset(data, value, size);
|
2016-11-27 10:06:47 +00:00
|
|
|
|
2017-01-09 05:29:16 +00:00
|
|
|
textureData->data = data;
|
|
|
|
textureData->width = width;
|
|
|
|
textureData->height = height;
|
|
|
|
textureData->channels = channels;
|
2017-02-03 23:16:30 +00:00
|
|
|
textureData->format = format;
|
2016-11-27 10:06:47 +00:00
|
|
|
|
|
|
|
return textureData;
|
|
|
|
}
|
|
|
|
|
2017-02-06 04:30:17 +00:00
|
|
|
TextureData* lovrTextureDataGetEmpty(int width, int height, TextureFormat format) {
|
2017-01-09 06:51:43 +00:00
|
|
|
TextureData* textureData = malloc(sizeof(TextureData));
|
|
|
|
if (!textureData) return NULL;
|
|
|
|
|
2017-02-06 04:30:17 +00:00
|
|
|
int channels = 0;
|
|
|
|
switch (format) {
|
|
|
|
case FORMAT_RED: channels = 1; break;
|
|
|
|
case FORMAT_RG: channels = 2; break;
|
|
|
|
case FORMAT_RGB: channels = 3; break;
|
|
|
|
case FORMAT_RGBA: channels = 4; break;
|
|
|
|
}
|
|
|
|
|
2017-01-09 06:51:43 +00:00
|
|
|
textureData->data = NULL;
|
|
|
|
textureData->width = width;
|
|
|
|
textureData->height = height;
|
2017-02-06 04:30:17 +00:00
|
|
|
textureData->channels = channels;
|
|
|
|
textureData->format = format;
|
2017-01-09 06:51:43 +00:00
|
|
|
|
|
|
|
return textureData;
|
|
|
|
}
|
|
|
|
|
2016-11-26 07:54:45 +00:00
|
|
|
TextureData* lovrTextureDataFromFile(void* data, int size) {
|
|
|
|
TextureData* textureData = malloc(sizeof(TextureData));
|
|
|
|
if (!textureData) return NULL;
|
|
|
|
|
2017-01-22 01:29:20 +00:00
|
|
|
int* w = &textureData->width;
|
|
|
|
int* h = &textureData->height;
|
|
|
|
int* c = &textureData->channels;
|
|
|
|
stbi_set_flip_vertically_on_load(1);
|
|
|
|
void* image = stbi_load_from_memory(data, size, w, h, c, 4);
|
2016-11-26 07:54:45 +00:00
|
|
|
|
|
|
|
if (image) {
|
|
|
|
textureData->data = image;
|
2017-02-03 23:16:30 +00:00
|
|
|
textureData->format = FORMAT_RGBA;
|
2016-11-26 07:54:45 +00:00
|
|
|
return textureData;
|
|
|
|
}
|
2016-12-01 04:32:14 +00:00
|
|
|
|
|
|
|
free(textureData);
|
|
|
|
return NULL;
|
2016-11-26 07:54:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-26 08:31:44 +00:00
|
|
|
TextureData* lovrTextureDataFromOpenVRModel(OpenVRModel* vrModel) {
|
2016-11-26 07:54:45 +00:00
|
|
|
TextureData* textureData = malloc(sizeof(TextureData));
|
|
|
|
if (!textureData) return NULL;
|
|
|
|
|
2016-11-26 08:31:44 +00:00
|
|
|
RenderModel_TextureMap_t* texture = vrModel->texture;
|
|
|
|
textureData->width = texture->unWidth;
|
|
|
|
textureData->height = texture->unHeight;
|
|
|
|
textureData->data = texture->rubTextureMapData;
|
2017-02-03 23:16:30 +00:00
|
|
|
textureData->format = FORMAT_RGBA;
|
2016-11-26 07:54:45 +00:00
|
|
|
return textureData;
|
|
|
|
}
|
2017-02-06 04:30:17 +00:00
|
|
|
|
|
|
|
void lovrTextureDataResize(TextureData* textureData, int width, int height, uint8_t value) {
|
|
|
|
int size = sizeof(uint8_t) * width * height * textureData->channels;
|
|
|
|
textureData->width = width;
|
|
|
|
textureData->height = height;
|
|
|
|
textureData->data = realloc(textureData->data, size);
|
|
|
|
memset(textureData->data, value, size);
|
|
|
|
}
|