diff options
author | Andreas Grois <andi@grois.info> | 2018-03-10 20:34:35 +0100 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2018-03-10 20:34:35 +0100 |
commit | 2759a3f45b5cd095bb79eff77b35978c8f8069c6 (patch) | |
tree | 4d0ded492e5de077fdad86f52729c1f9da556d86 | |
parent | 5127d05dfdc877b0fc06e6ec228cfef09b98539a (diff) |
Prepare install script, and make shader loading work from PREFIX/share/...
-rw-r--r-- | BuddhaTest/CMakeLists.txt | 4 | ||||
-rw-r--r-- | BuddhaTest/include/Helpers.h | 10 | ||||
-rw-r--r-- | BuddhaTest/src/BuddhaTest.cpp | 32 | ||||
-rw-r--r-- | BuddhaTest/src/Helpers.cpp | 33 | ||||
-rw-r--r-- | CMakeLists.txt | 3 |
5 files changed, 63 insertions, 19 deletions
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<uint32_t>& 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." <<std::endl <<
"Supported options are:" << std::endl << std::endl <<
"--output [path] : File to write output to. Empty by default, meaning no output is written." << std::endl <<
- "--imageWidth [integer] : Width of the to be written image. 1920 by default. Ignored if no --output is given." << std::endl <<
- "--imageHeight [integer] : Height of the to be written image. 1080 by default. Ignored if no --output is given." << std::endl <<
+ "--imageWidth [integer] : Width of the to be written image. 1024 by default. Ignored if no --output is given." << std::endl <<
+ "--imageHeight [integer] : Height of the to be written image. 576 by default. Ignored if no --output is given." << std::endl <<
"--imageGamma [float] : Gamma to use when writing the image. 1.0 by default. Ignored if no --output is given." << std::endl <<
"--imageColorScale [float] : Image brightness is scaled by the brightest pixel. The result is multiplied by this value. 2.0 by default, as 1.0 leaves very little dynamic range." << std::endl <<
"--windowWidth [integer] : Width of the preview window. 1024 by default." << std::endl <<
@@ -393,4 +402,10 @@ namespace Helpers return true;
}
+ bool DoesFileExist(const std::string &path)
+ {
+ std::ifstream f(path);
+ return f.good();
+ }
+
}
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c233c2..31afe9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,5 +5,6 @@ project(BuddhaTest) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
+set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory("glfw-3.2.1")
-add_subdirectory("BuddhaTest")
\ No newline at end of file +add_subdirectory("BuddhaTest")
|