Allow loading shaders from files;

The first argument is always the vertex shader.
The second argument is always the fragment shader.
This commit is contained in:
bjorn 2016-11-14 14:42:40 -08:00
parent 5cb11f4b2c
commit 06c71ed07c
2 changed files with 18 additions and 2 deletions

View File

@ -99,10 +99,12 @@ GLuint linkShaders(GLuint vertexShader, GLuint fragmentShader) {
}
Shader* lovrShaderCreate(const char* vertexSource, const char* fragmentSource) {
vertexSource = vertexSource == NULL ? lovrDefaultVertexShader : vertexSource;
char fullVertexSource[1024];
snprintf(fullVertexSource, sizeof(fullVertexSource), "%s\n%s", lovrShaderVertexPrefix, vertexSource);
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, fullVertexSource);
fragmentSource = fragmentSource == NULL ? lovrDefaultFragmentShader : fragmentSource;
char fullFragmentSource[1024];
snprintf(fullFragmentSource, sizeof(fullFragmentSource), "%s\n%s", lovrShaderFragmentPrefix, fragmentSource);
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fullFragmentSource);

View File

@ -537,8 +537,22 @@ int l_lovrGraphicsNewModel(lua_State* L) {
}
int l_lovrGraphicsNewShader(lua_State* L) {
const char* vertexSource = luaL_checkstring(L, 1);
const char* fragmentSource = luaL_checkstring(L, 2);
for (int i = 0; i < 2; i++) {
if (lua_isnoneornil(L, i + 1)) continue;
const char* source = luaL_checkstring(L, i + 1);
if (!lovrFilesystemIsFile(source)) continue;
int bytesRead;
char* contents = lovrFilesystemRead(source, &bytesRead);
if (bytesRead <= 0) {
return luaL_error(L, "Could not read shader from file '%s'", source);
}
lua_pushlstring(L, contents, bytesRead);
lua_replace(L, i + 1);
free(contents);
}
const char* vertexSource = lua_tostring(L, 1);
const char* fragmentSource = lua_tostring(L, 2);
luax_pushshader(L, lovrShaderCreate(vertexSource, fragmentSource));
return 1;
}