aboutsummaryrefslogtreecommitdiff
path: root/BuddhaTest/src
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2018-03-10 20:34:35 +0100
committerAndreas Grois <andi@grois.info>2018-03-10 20:34:35 +0100
commit2759a3f45b5cd095bb79eff77b35978c8f8069c6 (patch)
tree4d0ded492e5de077fdad86f52729c1f9da556d86 /BuddhaTest/src
parent5127d05dfdc877b0fc06e6ec228cfef09b98539a (diff)
Prepare install script, and make shader loading work from PREFIX/share/...
Diffstat (limited to 'BuddhaTest/src')
-rw-r--r--BuddhaTest/src/BuddhaTest.cpp32
-rw-r--r--BuddhaTest/src/Helpers.cpp33
2 files changed, 51 insertions, 14 deletions
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();
+ }
+
}