diff --git a/CMakeLists.txt b/CMakeLists.txt index 7575d0b..f0640b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,6 @@ find_package(SWIG REQUIRED) find_package(Lua REQUIRED) file(GLOB_RECURSE sources core/*.cpp core/*.h) -file(GLOB shaders shaders/*) add_compile_definitions(LUA_SCRIPTING) add_executable(couch ${sources}) @@ -26,8 +25,6 @@ target_link_libraries(couch OpenGL::GL) target_link_libraries(couch GLEW::GLEW) target_link_libraries(couch ${LUA_LIBRARIES}) -file(COPY shaders DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - include(UseSWIG) set_property(SOURCE scripting/couch.i PROPERTY CPLUSPLUS ON) swig_add_library(couchlua @@ -39,6 +36,11 @@ target_link_libraries(couch couchlua) add_subdirectory(thirdparty) +add_subdirectory(shaders) +target_include_directories(couch + PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders") + + if(WIN32) target_link_libraries(couch glfw3dll) target_link_libraries(couch ssp) diff --git a/core/Shader.cpp b/core/Shader.cpp index f13f5a4..d6ef08c 100644 --- a/core/Shader.cpp +++ b/core/Shader.cpp @@ -1,42 +1,12 @@ #include "Shader.h" -Shader::Shader(const char* vertexPath, const char* fragmentPath) { - std::string vertexCode; - std::string fragmentCode; - - std::ifstream vShaderFile; - std::ifstream fShaderFile; - - vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); - fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); - - try { - vShaderFile.open(vertexPath); - fShaderFile.open(fragmentPath); - std::stringstream vShaderStream, fShaderStream; - - vShaderStream << vShaderFile.rdbuf(); - fShaderStream << fShaderFile.rdbuf(); - - vShaderFile.close(); - fShaderFile.close(); - - vertexCode = vShaderStream.str(); - fragmentCode = fShaderStream.str(); - } catch (std::ifstream::failure e) { - std::cerr << "Error reading shader file." << std::endl; - } - - const char * vShaderCode = vertexCode.c_str(); - const char * fShaderCode = fragmentCode.c_str(); - - +Shader::Shader(const char* vertexCode, const char* fragmentCode) { Id vertex, fragment; int success; char infoLog[512]; vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vShaderCode, NULL); + glShaderSource(vertex, 1, &vertexCode, NULL); glCompileShader(vertex); glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); if (!success) { @@ -45,7 +15,7 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) { } fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fShaderCode, NULL); + glShaderSource(fragment, 1, &fragmentCode, NULL); glCompileShader(fragment); glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); if (!success) { diff --git a/core/Shader.h b/core/Shader.h index bb5b30e..872cefe 100644 --- a/core/Shader.h +++ b/core/Shader.h @@ -1,11 +1,6 @@ #ifndef SHADER_H #define SHADER_H -#include -#include -#include -#include - #include #include "types.h" @@ -15,7 +10,7 @@ class Shader { public: Id id; - Shader(const char *vertexPath, const char *fragmentPath); + Shader(const char *vertexCode, const char *fragmentCode); void Use(); void UpdateView(Matrix view); void UpdateModel(Matrix model); diff --git a/core/Shaders/FlatShader.cpp b/core/Shaders/FlatShader.cpp index 1ab44d4..70e4f57 100644 --- a/core/Shaders/FlatShader.cpp +++ b/core/Shaders/FlatShader.cpp @@ -1,6 +1,8 @@ #include "FlatShader.h" +#include "flat.vert.h" +#include "flat.frag.h" -FlatShader::FlatShader() : Shader("shaders/flat.vert", "shaders/flat.frag") {} +FlatShader::FlatShader() : Shader(flat_vert, flat_frag) {} void FlatShader::UpdateColor(Vector3 color) { glUniform3f(glGetUniformLocation(id, "color"), color.r, color.g, color.b); diff --git a/container.png b/demo/container.png similarity index 100% rename from container.png rename to demo/container.png diff --git a/main.lua b/demo/main.lua similarity index 100% rename from main.lua rename to demo/main.lua diff --git a/shaders/CMakeLists.txt b/shaders/CMakeLists.txt new file mode 100644 index 0000000..fedf513 --- /dev/null +++ b/shaders/CMakeLists.txt @@ -0,0 +1,14 @@ +project(Couch) + +file(GLOB shaders *.vert *.frag) +list( + TRANSFORM shaders + APPEND .h + OUTPUT_VARIABLE shader_header_files) + +add_custom_target(shader_headers + BYPRODUCTS ${shader_header_files} + COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl" + DEPENDS ${shaders} ${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl) + +add_dependencies(couch shader_headers) diff --git a/shaders/makeheaders.pl b/shaders/makeheaders.pl new file mode 100644 index 0000000..c6d5a6b --- /dev/null +++ b/shaders/makeheaders.pl @@ -0,0 +1,33 @@ +use strict; +use warnings; + +sub convert { + my $filename = $1; + + my $headerguard = "$filename.h"; + $headerguard =~ tr/a-z./A-Z_/; + + my $constname = "$filename"; + $constname =~ tr/./_/; + + open my $fin, '<', $filename; + open my $fout, '>', "$filename.h"; + + print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n"; + + while(my $line = <$fin>) { + $line =~ s/\n/\\n/; + print $fout "\"$line\"\n"; + } + + print $fout ";\n#endif // $headerguard\n"; + close $fin; + close $fout; +} + +opendir DIR, "."; +while (readdir DIR) { + if (/(.*\.(frag|vert))/) { + convert; + } +}