From c26686476d0dca9e8951c4e8a3242d77e93af16e Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Wed, 28 May 2014 10:29:05 +0100 Subject: [PATCH] Use Qt's resource system to store the shader source The shader source is included inside each comipled binary file. The OpenGL example also provides its own shader files which overrides the default. Common shader code is in the 'common' directory and each example's custom shader is alongside the example's C++ code. --- examples/Basic/CMakeLists.txt | 8 ++-- examples/OpenGL/CMakeLists.txt | 9 ++-- examples/OpenGL/main.cpp | 77 +----------------------------- examples/OpenGL/openglexample.frag | 45 +++++++++++++++++ examples/OpenGL/openglexample.qrc | 6 +++ examples/OpenGL/openglexample.vert | 26 ++++++++++ examples/Paging/CMakeLists.txt | 5 +- examples/SmoothLOD/CMakeLists.txt | 5 +- examples/common/OpenGLWidget.cpp | 4 +- examples/common/example.qrc | 6 +++ 10 files changed, 104 insertions(+), 87 deletions(-) create mode 100644 examples/OpenGL/openglexample.frag create mode 100644 examples/OpenGL/openglexample.qrc create mode 100644 examples/OpenGL/openglexample.vert create mode 100644 examples/common/example.qrc diff --git a/examples/Basic/CMakeLists.txt b/examples/Basic/CMakeLists.txt index 4bf89fcb..2d89215b 100644 --- a/examples/Basic/CMakeLists.txt +++ b/examples/Basic/CMakeLists.txt @@ -47,17 +47,17 @@ FIND_PACKAGE(OpenGL REQUIRED) INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${PolyVoxCore_BINARY_DIR}/include ${PolyVoxCore_SOURCE_DIR}/include ${GLEW_SOURCE_DIR}) LINK_DIRECTORIES(${PolyVoxCore_BINARY_DIR}) +#This will include the shader files inside the compiled binary +QT4_ADD_RESOURCES(COMMON_RESOURCES_RCC ../common/example.qrc) + #Build -ADD_EXECUTABLE(BasicExample ${SRC_FILES}) +ADD_EXECUTABLE(BasicExample ${SRC_FILES} ${COMMON_RESOURCES_RCC}) IF(MSVC) SET_TARGET_PROPERTIES(BasicExample PROPERTIES COMPILE_FLAGS "/W4 /wd4127") ENDIF(MSVC) TARGET_LINK_LIBRARIES(BasicExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore) SET_PROPERTY(TARGET BasicExample PROPERTY FOLDER "Examples") -configure_file(../common/example.vert example.vert COPYONLY) -configure_file(../common/example.frag example.frag COPYONLY) - #Install - Only install the example in Windows IF(WIN32) INSTALL(TARGETS BasicExample diff --git a/examples/OpenGL/CMakeLists.txt b/examples/OpenGL/CMakeLists.txt index 90d91b99..d6db1712 100644 --- a/examples/OpenGL/CMakeLists.txt +++ b/examples/OpenGL/CMakeLists.txt @@ -49,17 +49,18 @@ FIND_PACKAGE(OpenGL REQUIRED) INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${PolyVoxCore_BINARY_DIR}/include ${PolyVoxCore_SOURCE_DIR}/include ${GLEW_SOURCE_DIR}) LINK_DIRECTORIES(${PolyVoxCore_BINARY_DIR}) +#This will include the shader files inside the compiled binary +QT4_ADD_RESOURCES(COMMON_RESOURCES_RCC ../common/example.qrc) +QT4_ADD_RESOURCES(OPENGLEXAMPLE_RESOURCES_RCC openglexample.qrc) + #Build -ADD_EXECUTABLE(OpenGLExample ${SRC_FILES}) +ADD_EXECUTABLE(OpenGLExample ${SRC_FILES} ${COMMON_RESOURCES_RCC} ${OPENGLEXAMPLE_RESOURCES_RCC}) IF(MSVC) SET_TARGET_PROPERTIES(OpenGLExample PROPERTIES COMPILE_FLAGS "/W4 /wd4127") ENDIF(MSVC) TARGET_LINK_LIBRARIES(OpenGLExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore) SET_PROPERTY(TARGET OpenGLExample PROPERTY FOLDER "Examples") -configure_file(../common/example.vert example.vert COPYONLY) -configure_file(../common/example.frag example.frag COPYONLY) - #Install - Only install the example in Windows IF(WIN32) INSTALL(TARGETS OpenGLExample diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index c339fa0b..a4dc8ed4 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -90,86 +90,13 @@ int main(int argc, char *argv[]) QSharedPointer shader(new QGLShaderProgram); - if (!shader->addShaderFromSourceCode(QGLShader::Vertex, - "#version 140\n" - - "in vec4 position; // This will be the position of the vertex in model-space\n" - "in vec4 normal; // The normal data may not have been set\n" - "in ivec2 material;\n" - - "uniform mat4 cameraToClipMatrix;\n" - "uniform mat4 worldToCameraMatrix;\n" - "uniform mat4 modelToWorldMatrix;\n" - - "out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals\n" - "out vec3 normalFromVS;\n" - "flat out ivec2 materialFromVS;\n" - - "void main()\n" - "{\n" - " // Compute the usual OpenGL transformation to clip space.\n" - " gl_Position = cameraToClipMatrix * worldToCameraMatrix * modelToWorldMatrix * position;\n" - - " // This example is demonstrating the marching cubes mesh, which does have per-vertex normals. We can \n" - " // just pass them through, though real code might want to deal with transforming normals appropriatly.\n" - " normalFromVS = normal.xyz;\n" - - " // Nothing special here, we just pass the material through to the fragment shader.\n" - " materialFromVS = material;\n" - "}\n" - )) + if (!shader->addShaderFromSourceFile(QGLShader::Vertex, ":/openglexample.vert")) { std::cerr << shader->log().toStdString() << std::endl; exit(EXIT_FAILURE); } - if (!shader->addShaderFromSourceCode(QGLShader::Fragment, - "#version 130\n" - - "in vec4 worldPosition; //Passed in from the vertex shader\n" - "in vec3 normalFromVS;\n" - "flat in ivec2 materialFromVS;\n" - - "out vec4 outputColor;\n" - - "void main()\n" - "{\n" - " // The first byte of our voxel data is the material.\n" - " // We use this to decide how to color the fragment.\n" - " vec4 surfaceColor;\n" - " switch(materialFromVS.x)\n" - " {\n" - " case 1:\n" - " surfaceColor = vec4(1.0, 0.0, 0.0, 1.0);\n" - " break;\n" - " case 2:\n" - " surfaceColor = vec4(0.0, 1.0, 0.0, 1.0);\n" - " break;\n" - " case 3:\n" - " surfaceColor = vec4(0.0, 0.0, 1.0, 1.0);\n" - " break;\n" - " case 4:\n" - " surfaceColor = vec4(1.0, 1.0, 0.0, 1.0);\n" - " break;\n" - " case 5:\n" - " surfaceColor = vec4(1.0, 0.0, 1.0, 1.0);\n" - " break;\n" - " default:\n" - " surfaceColor = vec4(1.0, 1.0, 1.0, 1.0);\n" - " break;\n" - " }\n" - - " // Quick and dirty lighting, obviously a real implementation\n" - " // should pass light properties as shader parameters, etc.\n" - " vec3 lightDir = vec3(0.0, 0.0, 1.0);\n" - " float diffuse = clamp(dot(lightDir, normalFromVS), 0.0, 1.0);\n" - " diffuse *= 0.7; // Dim the diffuse a bit\n" - " float ambient = 0.3; // Add some ambient\n" - " float lightIntensity = diffuse + ambient; // Compute the final light intensity\n" - - " outputColor = surfaceColor * lightIntensity; //Compute final rendered color\n" - "}\n" - )) + if (!shader->addShaderFromSourceFile(QGLShader::Fragment, ":/openglexample.frag")) { std::cerr << shader->log().toStdString() << std::endl; exit(EXIT_FAILURE); diff --git a/examples/OpenGL/openglexample.frag b/examples/OpenGL/openglexample.frag new file mode 100644 index 00000000..75aa0e74 --- /dev/null +++ b/examples/OpenGL/openglexample.frag @@ -0,0 +1,45 @@ +#version 130 + +in vec4 worldPosition; //Passed in from the vertex shader +in vec3 normalFromVS; +flat in ivec2 materialFromVS; + +out vec4 outputColor; + +void main() +{ + // The first byte of our voxel data is the material. + // We use this to decide how to color the fragment. + vec4 surfaceColor; + switch(materialFromVS.x) + { + case 1: + surfaceColor = vec4(1.0, 0.0, 0.0, 1.0); + break; + case 2: + surfaceColor = vec4(0.0, 1.0, 0.0, 1.0); + break; + case 3: + surfaceColor = vec4(0.0, 0.0, 1.0, 1.0); + break; + case 4: + surfaceColor = vec4(1.0, 1.0, 0.0, 1.0); + break; + case 5: + surfaceColor = vec4(1.0, 0.0, 1.0, 1.0); + break; + default: + surfaceColor = vec4(1.0, 1.0, 1.0, 1.0); + break; + } + + // Quick and dirty lighting, obviously a real implementation + // should pass light properties as shader parameters, etc. + vec3 lightDir = vec3(0.0, 0.0, 1.0); + float diffuse = clamp(dot(lightDir, normalFromVS), 0.0, 1.0); + diffuse *= 0.7; // Dim the diffuse a bit + float ambient = 0.3; // Add some ambient + float lightIntensity = diffuse + ambient; // Compute the final light intensity + + outputColor = surfaceColor * lightIntensity; //Compute final rendered color +} diff --git a/examples/OpenGL/openglexample.qrc b/examples/OpenGL/openglexample.qrc new file mode 100644 index 00000000..128acb5a --- /dev/null +++ b/examples/OpenGL/openglexample.qrc @@ -0,0 +1,6 @@ + + + openglexample.vert + openglexample.frag + + diff --git a/examples/OpenGL/openglexample.vert b/examples/OpenGL/openglexample.vert new file mode 100644 index 00000000..381fac12 --- /dev/null +++ b/examples/OpenGL/openglexample.vert @@ -0,0 +1,26 @@ +#version 140 + +in vec4 position; // This will be the position of the vertex in model-space +in vec4 normal; // The normal data may not have been set +in ivec2 material; + +uniform mat4 cameraToClipMatrix; +uniform mat4 worldToCameraMatrix; +uniform mat4 modelToWorldMatrix; + +out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals +out vec3 normalFromVS; +flat out ivec2 materialFromVS; + +void main() +{ + // Compute the usual OpenGL transformation to clip space. + gl_Position = cameraToClipMatrix * worldToCameraMatrix * modelToWorldMatrix * position; + + // This example is demonstrating the marching cubes mesh, which does have per-vertex normals. We can + // just pass them through, though real code might want to deal with transforming normals appropriatly. + normalFromVS = normal.xyz; + + // Nothing special here, we just pass the material through to the fragment shader. + materialFromVS = material; +} diff --git a/examples/Paging/CMakeLists.txt b/examples/Paging/CMakeLists.txt index 54db88bd..3566e76e 100644 --- a/examples/Paging/CMakeLists.txt +++ b/examples/Paging/CMakeLists.txt @@ -49,8 +49,11 @@ FIND_PACKAGE(OpenGL REQUIRED) INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${PolyVoxCore_BINARY_DIR}/include ${PolyVoxCore_SOURCE_DIR}/include ${GLEW_SOURCE_DIR}) LINK_DIRECTORIES(${PolyVoxCore_BINARY_DIR}) +#This will include the shader files inside the compiled binary +QT4_ADD_RESOURCES(COMMON_RESOURCES_RCC ../common/example.qrc) + #Build -ADD_EXECUTABLE(PagingExample ${SRC_FILES}) +ADD_EXECUTABLE(PagingExample ${SRC_FILES} ${COMMON_RESOURCES_RCC}) IF(MSVC) SET_TARGET_PROPERTIES(PagingExample PROPERTIES COMPILE_FLAGS "/W4 /wd4127") ENDIF(MSVC) diff --git a/examples/SmoothLOD/CMakeLists.txt b/examples/SmoothLOD/CMakeLists.txt index c2fa3170..4a024664 100644 --- a/examples/SmoothLOD/CMakeLists.txt +++ b/examples/SmoothLOD/CMakeLists.txt @@ -47,8 +47,11 @@ FIND_PACKAGE(OpenGL REQUIRED) INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${PolyVoxCore_BINARY_DIR}/include ${PolyVoxCore_SOURCE_DIR}/include ${GLEW_SOURCE_DIR}) LINK_DIRECTORIES(${PolyVoxCore_BINARY_DIR}) +#This will include the shader files inside the compiled binary +QT4_ADD_RESOURCES(COMMON_RESOURCES_RCC ../common/example.qrc) + #Build -ADD_EXECUTABLE(SmoothLODExample ${SRC_FILES}) +ADD_EXECUTABLE(SmoothLODExample ${SRC_FILES} ${COMMON_RESOURCES_RCC}) IF(MSVC) SET_TARGET_PROPERTIES(SmoothLODExample PROPERTIES COMPILE_FLAGS "/W4 /wd4127") #All warnings ENDIF(MSVC) diff --git a/examples/common/OpenGLWidget.cpp b/examples/common/OpenGLWidget.cpp index 788d934d..439264bf 100644 --- a/examples/common/OpenGLWidget.cpp +++ b/examples/common/OpenGLWidget.cpp @@ -92,7 +92,7 @@ void OpenGLWidget::initializeGL() // This is basically a simple fallback vertex shader which does the most basic rendering possible. // PolyVox examples are able to provide their own shaders to demonstrate certain effects if desired. - if (!mShader->addShaderFromSourceFile(QGLShader::Vertex, QCoreApplication::applicationDirPath()+"/"+"example.vert")) + if (!mShader->addShaderFromSourceFile(QGLShader::Vertex, ":/example.vert")) { std::cerr << mShader->log().toStdString() << std::endl; exit(EXIT_FAILURE); @@ -100,7 +100,7 @@ void OpenGLWidget::initializeGL() // This is basically a simple fallback fragment shader which does the most basic rendering possible. // PolyVox examples are able to provide their own shaders to demonstrate certain effects if desired. - if (!mShader->addShaderFromSourceFile(QGLShader::Fragment, QCoreApplication::applicationDirPath()+"/"+"example.frag")) + if (!mShader->addShaderFromSourceFile(QGLShader::Fragment, ":/example.frag")) { std::cerr << mShader->log().toStdString() << std::endl; exit(EXIT_FAILURE); diff --git a/examples/common/example.qrc b/examples/common/example.qrc new file mode 100644 index 00000000..8efd5747 --- /dev/null +++ b/examples/common/example.qrc @@ -0,0 +1,6 @@ + + + example.vert + example.frag + +