From 2759a3f45b5cd095bb79eff77b35978c8f8069c6 Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Sat, 10 Mar 2018 20:34:35 +0100 Subject: Prepare install script, and make shader loading work from PREFIX/share/... --- BuddhaTest/CMakeLists.txt | 4 ++++ BuddhaTest/include/Helpers.h | 10 ++++++---- BuddhaTest/src/BuddhaTest.cpp | 32 +++++++++++++++++++++++++++----- BuddhaTest/src/Helpers.cpp | 33 ++++++++++++++++++++++++--------- 4 files changed, 61 insertions(+), 18 deletions(-) (limited to 'BuddhaTest') diff --git a/BuddhaTest/CMakeLists.txt b/BuddhaTest/CMakeLists.txt index be7d4d6..35c1fb4 100644 --- a/BuddhaTest/CMakeLists.txt +++ b/BuddhaTest/CMakeLists.txt @@ -14,4 +14,8 @@ find_package(PNG REQUIRED) target_include_directories(BuddhaTest PRIVATE "include" ${OPENGL_INCLUDE_DIR} ${PNG_INCLUDE_DIRS}) target_link_libraries(BuddhaTest glfw ${OPENGL_gl_LIBRARY} ${PNG_LIBRARIES}) add_definitions(${PNG_DEFINITIONS}) +add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}") # on Linux we need to link against libdl. Maybe add id here? + +install(DIRECTORY Shaders DESTINATION share/BuddhaShader FILES_MATCHING PATTERN *.glsl) +install(TARGETS BuddhaTest RUNTIME DESTINATION bin) diff --git a/BuddhaTest/include/Helpers.h b/BuddhaTest/include/Helpers.h index 61bd7a0..76a9a57 100644 --- a/BuddhaTest/include/Helpers.h +++ b/BuddhaTest/include/Helpers.h @@ -7,8 +7,10 @@ namespace Helpers { - GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path); - GLuint LoadComputeShader(const char * compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ); + GLuint LoadShaders(const std::string &vertex_file_path, const std::string &fragment_file_path); + GLuint LoadComputeShader(const std::string &compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ); + + bool DoesFileExist(const std::string& path); void WriteOutputPNG(const std::string& path, const std::vector& data, unsigned int width, unsigned int bufferHeight, double gamma, double colorScale); @@ -26,8 +28,8 @@ namespace Helpers struct RenderSettings { - unsigned int imageWidth = 1920; - unsigned int imageHeight = 1080; + unsigned int imageWidth = 1024; + unsigned int imageHeight = 576; unsigned int windowWidth = 1024; unsigned int windowHeight = 576; diff --git a/BuddhaTest/src/BuddhaTest.cpp b/BuddhaTest/src/BuddhaTest.cpp index 38bd0b5..c29a220 100644 --- a/BuddhaTest/src/BuddhaTest.cpp +++ b/BuddhaTest/src/BuddhaTest.cpp @@ -54,6 +54,33 @@ int main(int argc, char * argv[]) return 1; } + // Create and compile our GLSL program from the shaders + std::string vertexPath("Shaders/BuddhaVertex.glsl"); + std::string fragmentPath("Shaders/BuddhaFragment.glsl"); + std::string computePath("Shaders/BuddhaCompute.glsl"); + + if(!Helpers::DoesFileExist(vertexPath)) + { +#if defined _WIN32 || defined __CYGWIN__ + std::string separator("\\"); +#else + std::string separator("/"); +#endif + vertexPath = INSTALL_PREFIX + separator + "share" + separator + "BuddhaShader" + separator + vertexPath; + fragmentPath = INSTALL_PREFIX + separator + "share" + separator + "BuddhaShader" + separator + fragmentPath; + computePath = INSTALL_PREFIX + separator + "share" + separator + "BuddhaShader" + separator + computePath; + } + + GLuint VertexAndFragmentShaders = Helpers::LoadShaders(vertexPath, fragmentPath); + //Do the same for the compute shader: + GLuint ComputeShader = Helpers::LoadComputeShader(computePath, settings.localWorkgroupSizeX, settings.localWorkgroupSizeY, settings.localWorkgroupSizeZ); + if(VertexAndFragmentShaders == 0 || ComputeShader == 0) + { + std::cout << "Something went wrong with loading the shaders. Abort." << std::endl; + glfwTerminate(); + return 1; + } + GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); @@ -81,11 +108,6 @@ int main(int argc, char * argv[]) glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, drawBuffer); glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); - // Create and compile our GLSL program from the shaders - GLuint VertexAndFragmentShaders = Helpers::LoadShaders("Shaders/BuddhaVertex.glsl", "Shaders/BuddhaFragment.glsl"); - //Do the same for the compute shader: - GLuint ComputeShader = Helpers::LoadComputeShader("Shaders/BuddhaCompute.glsl", settings.localWorkgroupSizeX, settings.localWorkgroupSizeY, settings.localWorkgroupSizeZ); - uint32_t iterationCount{0}; glUseProgram(ComputeShader); GLint iterationCountUniformHandle = glGetUniformLocation(ComputeShader, "iterationCount"); diff --git a/BuddhaTest/src/Helpers.cpp b/BuddhaTest/src/Helpers.cpp index 889db7e..cb48d94 100644 --- a/BuddhaTest/src/Helpers.cpp +++ b/BuddhaTest/src/Helpers.cpp @@ -11,7 +11,7 @@ namespace Helpers { - GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path) { + GLuint LoadShaders(const std::string& vertex_file_path, const std::string& fragment_file_path) { // Create the shaders GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); @@ -27,8 +27,7 @@ namespace Helpers VertexShaderStream.close(); } else { - printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path); - getchar(); + std::cerr << "Failed to load vertex shader file from " << vertex_file_path << ". Is this installed correctly?" << std::endl; return 0; } @@ -41,12 +40,17 @@ namespace Helpers FragmentShaderCode = sstr.str(); FragmentShaderStream.close(); } + else + { + std::cerr << "Failed to load fragment shader file from " << fragment_file_path << ". Is this installed correctly?" << std::endl; + return 0; + } GLint Result = GL_FALSE; int InfoLogLength; // Compile Vertex Shader - printf("Compiling shader : %s\n", vertex_file_path); + std::cout << "Compiling shader : " << vertex_file_path << std::endl; char const * VertexSourcePointer = VertexShaderCode.c_str(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL); glCompileShader(VertexShaderID); @@ -62,7 +66,7 @@ namespace Helpers // Compile Fragment Shader - printf("Compiling shader : %s\n", fragment_file_path); + std::cout << "Compiling shader : " << fragment_file_path; char const * FragmentSourcePointer = FragmentShaderCode.c_str(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL); glCompileShader(FragmentShaderID); @@ -101,7 +105,7 @@ namespace Helpers return ProgramID; } - GLuint LoadComputeShader(const char* compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ) + GLuint LoadComputeShader(const std::string& compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ) { GLuint ComputeShaderID = glCreateShader(GL_COMPUTE_SHADER); // Read the compute shader @@ -119,6 +123,11 @@ namespace Helpers ComputeShaderCode = sstr.str(); ComputeShaderCodeStream.close(); } + else + { + std::cerr << "Failed to load compute shader file from " << compute_file_path << ". Is this installed correctly?" << std::endl; + return 0; + } } GLint Result = GL_FALSE; @@ -126,7 +135,7 @@ namespace Helpers { // Compile Compute Shader - printf("Compiling shader : %s\n", compute_file_path); + std::cout << "Compiling shader : " << compute_file_path << std::endl; char const * ComputeSourcePointer = ComputeShaderCode.c_str(); glShaderSource(ComputeShaderID, 1, &ComputeSourcePointer, NULL); glCompileShader(ComputeShaderID); @@ -335,8 +344,8 @@ namespace Helpers std::cout << "Draws a buddhabrot and iterates until the user closes the window. If a --output filename is given, a png file will afterwards be written there." <