1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-02 20:43:35 +00:00

Load cubemap images better;

This commit is contained in:
bjorn 2018-09-03 20:59:12 -07:00
parent f17d57e987
commit 4e1757210e
6 changed files with 14 additions and 14 deletions

View file

@ -117,7 +117,7 @@ int l_lovrDataNewTextureData(lua_State* L) {
textureData = lovrTextureDataCreate(width, height, 0x0, format); textureData = lovrTextureDataCreate(width, height, 0x0, format);
} else { } else {
Blob* blob = luax_readblob(L, 1, "Texture"); Blob* blob = luax_readblob(L, 1, "Texture");
textureData = lovrTextureDataCreateFromBlob(blob); textureData = lovrTextureDataCreateFromBlob(blob, true);
lovrRelease(blob); lovrRelease(blob);
} }

View file

@ -238,12 +238,12 @@ static void stencilCallback(void* userdata) {
lua_call(L, 0, 0); lua_call(L, 0, 0);
} }
static TextureData* luax_checktexturedata(lua_State* L, int index) { static TextureData* luax_checktexturedata(lua_State* L, int index, bool flip) {
TextureData* textureData = luax_totype(L, index, TextureData); TextureData* textureData = luax_totype(L, index, TextureData);
if (!textureData) { if (!textureData) {
Blob* blob = luax_readblob(L, index, "Texture"); Blob* blob = luax_readblob(L, index, "Texture");
textureData = lovrTextureDataCreateFromBlob(blob); textureData = lovrTextureDataCreateFromBlob(blob, flip);
lovrRelease(blob); lovrRelease(blob);
} }
@ -1041,7 +1041,7 @@ int l_lovrGraphicsNewMaterial(lua_State* L) {
if (lua_type(L, index) == LUA_TSTRING) { if (lua_type(L, index) == LUA_TSTRING) {
Blob* blob = luax_readblob(L, index++, "Texture"); Blob* blob = luax_readblob(L, index++, "Texture");
TextureData* textureData = lovrTextureDataCreateFromBlob(blob); TextureData* textureData = lovrTextureDataCreateFromBlob(blob, true);
Texture* texture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, true, 0); Texture* texture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, true, 0);
lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, texture); lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, texture);
lovrRelease(blob); lovrRelease(blob);
@ -1136,7 +1136,7 @@ int l_lovrGraphicsNewModel(lua_State* L) {
if (lua_gettop(L) >= 2) { if (lua_gettop(L) >= 2) {
if (lua_type(L, 2) == LUA_TSTRING) { if (lua_type(L, 2) == LUA_TSTRING) {
Blob* blob = luax_readblob(L, 2, "Texture"); Blob* blob = luax_readblob(L, 2, "Texture");
TextureData* textureData = lovrTextureDataCreateFromBlob(blob); TextureData* textureData = lovrTextureDataCreateFromBlob(blob, true);
Texture* texture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, true, 0); Texture* texture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, true, 0);
Material* material = lovrMaterialCreate(); Material* material = lovrMaterialCreate();
lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, texture); lovrMaterialSetTexture(material, TEXTURE_DIFFUSE, texture);
@ -1260,7 +1260,7 @@ int l_lovrGraphicsNewTexture(lua_State* L) {
} else { } else {
if (type == TEXTURE_CUBE && depth == 0) { if (type == TEXTURE_CUBE && depth == 0) {
depth = 6; depth = 6;
const char* faces[6] = { "right", "left", "bottom", "top", "back", "front" }; const char* faces[6] = { "right", "left", "top", "bottom", "back", "front" };
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
lua_pushstring(L, faces[i]); lua_pushstring(L, faces[i]);
lua_rawget(L, 1); lua_rawget(L, 1);
@ -1270,7 +1270,7 @@ int l_lovrGraphicsNewTexture(lua_State* L) {
for (int i = 0; i < depth; i++) { for (int i = 0; i < depth; i++) {
lua_rawgeti(L, 1, i + 1); lua_rawgeti(L, 1, i + 1);
TextureData* textureData = luax_checktexturedata(L, -1); TextureData* textureData = luax_checktexturedata(L, -1, type != TEXTURE_CUBE);
if (i == 0) { if (i == 0) {
lovrTextureAllocate(texture, textureData->width, textureData->height, depth, textureData->format); lovrTextureAllocate(texture, textureData->width, textureData->height, depth, textureData->format);
} }

View file

@ -146,7 +146,7 @@ static int readMaterialTexture(struct aiMaterial* assimpMaterial, enum aiTexture
} }
Blob* blob = lovrBlobCreate(data, size, path); Blob* blob = lovrBlobCreate(data, size, path);
TextureData* textureData = lovrTextureDataCreateFromBlob(blob); TextureData* textureData = lovrTextureDataCreateFromBlob(blob, true);
lovrRelease(blob); lovrRelease(blob);
int textureIndex = modelData->textures.length; int textureIndex = modelData->textures.length;
vec_push(&modelData->textures, textureData); vec_push(&modelData->textures, textureData);

View file

@ -151,7 +151,7 @@ TextureData* lovrTextureDataCreate(int width, int height, uint8_t value, Texture
return textureData; return textureData;
} }
TextureData* lovrTextureDataCreateFromBlob(Blob* blob) { TextureData* lovrTextureDataCreateFromBlob(Blob* blob, bool flip) {
TextureData* textureData = lovrAlloc(TextureData, lovrTextureDataDestroy); TextureData* textureData = lovrAlloc(TextureData, lovrTextureDataDestroy);
if (!textureData) return NULL; if (!textureData) return NULL;
@ -163,7 +163,7 @@ TextureData* lovrTextureDataCreateFromBlob(Blob* blob) {
return textureData; return textureData;
} }
stbi_set_flip_vertically_on_load(1); stbi_set_flip_vertically_on_load(flip);
if (stbi_is_hdr_from_memory(blob->data, blob->size)) { if (stbi_is_hdr_from_memory(blob->data, blob->size)) {
textureData->format = FORMAT_RGBA32F; textureData->format = FORMAT_RGBA32F;
textureData->blob.data = stbi_loadf_from_memory(blob->data, blob->size, &textureData->width, &textureData->height, NULL, 4); textureData->blob.data = stbi_loadf_from_memory(blob->data, blob->size, &textureData->width, &textureData->height, NULL, 4);

View file

@ -42,7 +42,7 @@ typedef struct {
} TextureData; } TextureData;
TextureData* lovrTextureDataCreate(int width, int height, uint8_t value, TextureFormat format); TextureData* lovrTextureDataCreate(int width, int height, uint8_t value, TextureFormat format);
TextureData* lovrTextureDataCreateFromBlob(Blob* blob); TextureData* lovrTextureDataCreateFromBlob(Blob* blob, bool flip);
Color lovrTextureDataGetPixel(TextureData* textureData, int x, int y); Color lovrTextureDataGetPixel(TextureData* textureData, int x, int y);
void lovrTextureDataSetPixel(TextureData* textureData, int x, int y, Color color); void lovrTextureDataSetPixel(TextureData* textureData, int x, int y, Color color);
bool lovrTextureDataEncode(TextureData* textureData, const char* filename); bool lovrTextureDataEncode(TextureData* textureData, const char* filename);

View file

@ -107,7 +107,7 @@ const char* lovrDefaultFragmentShader = ""
const char* lovrCubeVertexShader = "" const char* lovrCubeVertexShader = ""
"out vec3 texturePosition[2]; \n" "out vec3 texturePosition[2]; \n"
"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" "vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n"
" texturePosition[lovrViewportIndex] = -inverse(mat3(transform)) * (inverse(projection) * vertex).xyz; \n" " texturePosition[lovrViewportIndex] = inverse(mat3(transform)) * (inverse(projection) * vertex).xyz; \n"
" return vertex; \n" " return vertex; \n"
"}"; "}";
@ -122,8 +122,8 @@ const char* lovrPanoFragmentShader = ""
"#define PI 3.141592653589 \n" "#define PI 3.141592653589 \n"
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" "vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
" vec3 direction = texturePosition[lovrViewportIndex]; \n" " vec3 direction = texturePosition[lovrViewportIndex]; \n"
" float theta = acos(direction.y / length(direction)); \n" " float theta = acos(-direction.y / length(direction)); \n"
" float phi = atan(-direction.x, direction.z); \n" " float phi = atan(direction.x, -direction.z); \n"
" uv = vec2(.5 + phi / (2. * PI), theta / PI); \n" " uv = vec2(.5 + phi / (2. * PI), theta / PI); \n"
" return graphicsColor * texture(lovrDiffuseTexture, uv); \n" " return graphicsColor * texture(lovrDiffuseTexture, uv); \n"
"}"; "}";