mirror of https://github.com/bjornbytes/lovr.git
Simplify Skybox stuff;
This commit is contained in:
parent
45618f819e
commit
88a32fd951
|
@ -528,7 +528,7 @@ int l_lovrGraphicsSphere(lua_State* L) {
|
||||||
}
|
}
|
||||||
index = luax_readtransform(L, index, transform, 1);
|
index = luax_readtransform(L, index, transform, 1);
|
||||||
int segments = luaL_optnumber(L, index, 30);
|
int segments = luaL_optnumber(L, index, 30);
|
||||||
lovrGraphicsSphere(texture, transform, segments);
|
lovrGraphicsSphere(texture, transform, segments, NULL);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -715,7 +715,7 @@ void lovrGraphicsCylinder(float x1, float y1, float z1, float x2, float y2, floa
|
||||||
#undef PUSH_CYLINDER_TRIANGLE
|
#undef PUSH_CYLINDER_TRIANGLE
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments) {
|
void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments, Skybox* skybox) {
|
||||||
vec_clear(&state.buffer.data);
|
vec_clear(&state.buffer.data);
|
||||||
vec_clear(&state.buffer.indices);
|
vec_clear(&state.buffer.indices);
|
||||||
|
|
||||||
|
@ -732,9 +732,11 @@ void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments) {
|
||||||
vec_push(&state.buffer.data, y);
|
vec_push(&state.buffer.data, y);
|
||||||
vec_push(&state.buffer.data, z);
|
vec_push(&state.buffer.data, z);
|
||||||
|
|
||||||
vec_push(&state.buffer.data, x);
|
if (!skybox) {
|
||||||
vec_push(&state.buffer.data, y);
|
vec_push(&state.buffer.data, x);
|
||||||
vec_push(&state.buffer.data, z);
|
vec_push(&state.buffer.data, y);
|
||||||
|
vec_push(&state.buffer.data, z);
|
||||||
|
}
|
||||||
|
|
||||||
vec_push(&state.buffer.data, u);
|
vec_push(&state.buffer.data, u);
|
||||||
vec_push(&state.buffer.data, v);
|
vec_push(&state.buffer.data, v);
|
||||||
|
@ -756,21 +758,30 @@ void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lovrGraphicsBindTexture(texture);
|
if (skybox) {
|
||||||
lovrGraphicsPush();
|
Texture* oldTexture = lovrGraphicsGetTexture();
|
||||||
lovrGraphicsMatrixTransform(transform);
|
glBindTexture(GL_TEXTURE_2D, skybox->texture);
|
||||||
lovrGraphicsDrawPrimitive(GL_TRIANGLES, 1, 1, 1);
|
lovrGraphicsDrawPrimitive(GL_TRIANGLES, 0, 1, 1);
|
||||||
lovrGraphicsPop();
|
glBindTexture(GL_TEXTURE_2D, oldTexture->id);
|
||||||
|
} else {
|
||||||
|
lovrGraphicsBindTexture(texture);
|
||||||
|
lovrGraphicsPush();
|
||||||
|
lovrGraphicsMatrixTransform(transform);
|
||||||
|
lovrGraphicsDrawPrimitive(GL_TRIANGLES, 1, 1, 1);
|
||||||
|
lovrGraphicsPop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float az) {
|
void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float az) {
|
||||||
lovrGraphicsPush();
|
lovrGraphicsPush();
|
||||||
lovrGraphicsOrigin();
|
lovrGraphicsOrigin();
|
||||||
lovrGraphicsRotate(angle, ax, ay, az);
|
lovrGraphicsRotate(angle, ax, ay, az);
|
||||||
|
glDepthMask(GL_FALSE);
|
||||||
|
int wasCulling = lovrGraphicsIsCullingEnabled();
|
||||||
|
lovrGraphicsSetCullingEnabled(0);
|
||||||
|
|
||||||
if (skybox->type == SKYBOX_CUBE) {
|
if (skybox->type == SKYBOX_CUBE) {
|
||||||
Shader* lastShader = lovrGraphicsGetShader();
|
Shader* lastShader = lovrGraphicsGetShader();
|
||||||
|
|
||||||
if (lastShader == state.defaultShader) {
|
if (lastShader == state.defaultShader) {
|
||||||
lovrRetain(&lastShader->ref);
|
lovrRetain(&lastShader->ref);
|
||||||
lovrGraphicsSetShader(state.skyboxShader);
|
lovrGraphicsSetShader(state.skyboxShader);
|
||||||
|
@ -819,82 +830,22 @@ void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float a
|
||||||
};
|
};
|
||||||
|
|
||||||
lovrGraphicsSetShapeData(cube, 78);
|
lovrGraphicsSetShapeData(cube, 78);
|
||||||
|
|
||||||
glDepthMask(GL_FALSE);
|
|
||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->texture);
|
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox->texture);
|
||||||
|
|
||||||
int wasCulling = lovrGraphicsIsCullingEnabled();
|
|
||||||
lovrGraphicsSetCullingEnabled(0);
|
|
||||||
lovrGraphicsDrawPrimitive(GL_TRIANGLE_STRIP, 0, 0, 0);
|
lovrGraphicsDrawPrimitive(GL_TRIANGLE_STRIP, 0, 0, 0);
|
||||||
lovrGraphicsSetCullingEnabled(wasCulling);
|
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glDepthMask(GL_TRUE);
|
|
||||||
|
|
||||||
if (lastShader == state.defaultShader) {
|
if (lastShader == state.defaultShader) {
|
||||||
lovrGraphicsSetShader(lastShader);
|
lovrGraphicsSetShader(lastShader);
|
||||||
lovrRelease(&lastShader->ref);
|
lovrRelease(&lastShader->ref);
|
||||||
}
|
}
|
||||||
} else if (skybox->type == SKYBOX_PANORAMA) {
|
} else if (skybox->type == SKYBOX_PANORAMA) {
|
||||||
int resolution = 40;
|
lovrGraphicsSphere(NULL, NULL, 30, skybox);
|
||||||
int idx = 0;
|
|
||||||
float sphere[8610]; // (resolution + 1) * (resolution + 1) * 5
|
|
||||||
|
|
||||||
for (int i = 0; i <= resolution; i++) {
|
|
||||||
float theta = i * M_PI / (float) resolution;
|
|
||||||
float sinTheta = sin(theta);
|
|
||||||
float cosTheta = cos(theta);
|
|
||||||
|
|
||||||
for (int j = 0; j <= resolution; j++) {
|
|
||||||
float phi = j * 2 * M_PI / (float) resolution;
|
|
||||||
float sinPhi = sin(phi);
|
|
||||||
float cosPhi = cos(phi);
|
|
||||||
|
|
||||||
float x = sinPhi * sinTheta;
|
|
||||||
float y = cosTheta;
|
|
||||||
float z = -cosPhi * sinTheta;
|
|
||||||
float u = j / (float) resolution;
|
|
||||||
float v = i / (float) resolution;
|
|
||||||
|
|
||||||
sphere[idx++] = x;
|
|
||||||
sphere[idx++] = y;
|
|
||||||
sphere[idx++] = z;
|
|
||||||
sphere[idx++] = u;
|
|
||||||
sphere[idx++] = v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
idx = 0;
|
|
||||||
unsigned int indices[9600]; // resolution * resolution * 6
|
|
||||||
for (int i = 0; i < resolution; i++) {
|
|
||||||
unsigned int offset0 = i * (resolution + 1);
|
|
||||||
unsigned int offset1 = (i + 1) * (resolution + 1);
|
|
||||||
for (int j = 0; j < resolution; j++) {
|
|
||||||
unsigned int index0 = offset0 + j;
|
|
||||||
unsigned int index1 = offset1 + j;
|
|
||||||
indices[idx++] = index0;
|
|
||||||
indices[idx++] = index1;
|
|
||||||
indices[idx++] = index0 + 1;
|
|
||||||
indices[idx++] = index1;
|
|
||||||
indices[idx++] = index1 + 1;
|
|
||||||
indices[idx++] = index0 + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Texture* oldTexture = lovrGraphicsGetTexture();
|
|
||||||
glBindTexture(GL_TEXTURE_2D, skybox->texture);
|
|
||||||
|
|
||||||
lovrGraphicsSetShapeData(sphere, 8610);
|
|
||||||
lovrGraphicsSetIndexData(indices, 9600);
|
|
||||||
glDepthMask(GL_FALSE);
|
|
||||||
lovrGraphicsDrawPrimitive(GL_TRIANGLES, 0, 1, 1);
|
|
||||||
glDepthMask(GL_TRUE);
|
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, oldTexture->id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lovrGraphicsSetCullingEnabled(wasCulling);
|
||||||
|
glDepthMask(GL_TRUE);
|
||||||
lovrGraphicsPop();
|
lovrGraphicsPop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -159,7 +159,7 @@ void lovrGraphicsPlane(DrawMode mode, Texture* texture, mat4 transform);
|
||||||
void lovrGraphicsPlaneFullscreen(Texture* texture);
|
void lovrGraphicsPlaneFullscreen(Texture* texture);
|
||||||
void lovrGraphicsBox(DrawMode mode, Texture* texture, mat4 transform);
|
void lovrGraphicsBox(DrawMode mode, Texture* texture, mat4 transform);
|
||||||
void lovrGraphicsCylinder(float x1, float y1, float z1, float x2, float y2, float z2, float r1, float r2, int capped, int segments);
|
void lovrGraphicsCylinder(float x1, float y1, float z1, float x2, float y2, float z2, float r1, float r2, int capped, int segments);
|
||||||
void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments);
|
void lovrGraphicsSphere(Texture* texture, mat4 transform, int segments, Skybox* skybox);
|
||||||
void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float az);
|
void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float az);
|
||||||
void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign);
|
void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAlign halign, VerticalAlign valign);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue