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.
This commit is contained in:
parent
35049b7a53
commit
c26686476d
@ -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
|
||||
|
@ -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
|
||||
|
@ -90,86 +90,13 @@ int main(int argc, char *argv[])
|
||||
|
||||
QSharedPointer<QGLShaderProgram> 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);
|
||||
|
45
examples/OpenGL/openglexample.frag
Normal file
45
examples/OpenGL/openglexample.frag
Normal file
@ -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
|
||||
}
|
6
examples/OpenGL/openglexample.qrc
Normal file
6
examples/OpenGL/openglexample.qrc
Normal file
@ -0,0 +1,6 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource>
|
||||
<file>openglexample.vert</file>
|
||||
<file>openglexample.frag</file>
|
||||
</qresource>
|
||||
</RCC>
|
26
examples/OpenGL/openglexample.vert
Normal file
26
examples/OpenGL/openglexample.vert
Normal file
@ -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;
|
||||
}
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
6
examples/common/example.qrc
Normal file
6
examples/common/example.qrc
Normal file
@ -0,0 +1,6 @@
|
||||
<!DOCTYPE RCC><RCC version="1.0">
|
||||
<qresource>
|
||||
<file>example.vert</file>
|
||||
<file>example.frag</file>
|
||||
</qresource>
|
||||
</RCC>
|
Loading…
x
Reference in New Issue
Block a user