Initial version of decoding shaders.

This commit is contained in:
David Williams 2014-05-29 14:23:22 +02:00
parent f945fd4ce4
commit 8604d1209e
5 changed files with 64 additions and 2 deletions

View File

@ -44,12 +44,13 @@ LINK_DIRECTORIES(${PolyVoxCore_BINARY_DIR})
#This will include the shader files inside the compiled binary #This will include the shader files inside the compiled binary
QT4_ADD_RESOURCES(COMMON_RESOURCES_RCC ../common/example.qrc) QT4_ADD_RESOURCES(COMMON_RESOURCES_RCC ../common/example.qrc)
QT4_ADD_RESOURCES(DECODE_RESOURCES_RCC decode.qrc)
# Put the resources in a seperate folder in Visual Studio # Put the resources in a seperate folder in Visual Studio
SOURCE_GROUP("Resource Files" FILES ../common/example.qrc ${COMMON_RESOURCES_RCC}) SOURCE_GROUP("Resource Files" FILES ../common/example.qrc ${COMMON_RESOURCES_RCC} decode.qrc ${DECODE_RESOURCES_RCC})
#Build #Build
ADD_EXECUTABLE(DecodeOnGPUExample ${SRC_FILES} ${COMMON_RESOURCES_RCC}) ADD_EXECUTABLE(DecodeOnGPUExample ${SRC_FILES} ${COMMON_RESOURCES_RCC} ${DECODE_RESOURCES_RCC})
IF(MSVC) IF(MSVC)
SET_TARGET_PROPERTIES(DecodeOnGPUExample PROPERTIES COMPILE_FLAGS "/W4 /wd4127") SET_TARGET_PROPERTIES(DecodeOnGPUExample PROPERTIES COMPILE_FLAGS "/W4 /wd4127")
ENDIF(MSVC) ENDIF(MSVC)

View File

@ -0,0 +1,19 @@
#version 130
// Passed in from the vertex shader
in vec4 worldPosition;
in vec4 worldNormal;
// the color that gets written to the display
out vec4 outputColor;
void main()
{
// Again, for the purposes of these examples we cannot be sure that per-vertex normals are provided. A sensible fallback
// is to use this little trick to compute per-fragment flat-shaded normals from the world positions using derivative operations.
vec3 normal = normalize(cross(dFdy(worldPosition.xyz), dFdx(worldPosition.xyz)));
// We are just using the normal as the output color, and making it lighter so it looks a bit nicer.
// Obviously a real shader would also do texuring, lighting, or whatever is required for the application.
outputColor = vec4(abs(normal) * 0.5 + vec3(0.5, 0.5, 0.5), 1.0);
}

View File

@ -0,0 +1,6 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>decode.vert</file>
<file>decode.frag</file>
</qresource>
</RCC>

View File

@ -0,0 +1,20 @@
#version 140
in vec4 position; // This will be the position of the vertex in model-space
// The usual matrices are provided
uniform mat4 cameraToClipMatrix;
uniform mat4 worldToCameraMatrix;
uniform mat4 modelToWorldMatrix;
// This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach
// but we use it in this example framework because not all surface extractor generate surface normals.
out vec4 worldPosition;
void main()
{
// Standard sequence of OpenGL transformations.
worldPosition = modelToWorldMatrix * position;
vec4 cameraPosition = worldToCameraMatrix * worldPosition;
gl_Position = cameraToClipMatrix * cameraPosition;
}

View File

@ -125,6 +125,22 @@ int main(int argc, char *argv[])
OpenGLWidget openGLWidget(0); OpenGLWidget openGLWidget(0);
openGLWidget.show(); openGLWidget.show();
QSharedPointer<QGLShaderProgram> shader(new QGLShaderProgram);
if (!shader->addShaderFromSourceFile(QGLShader::Vertex, ":/decode.vert"))
{
std::cerr << shader->log().toStdString() << std::endl;
exit(EXIT_FAILURE);
}
if (!shader->addShaderFromSourceFile(QGLShader::Fragment, ":/decode.frag"))
{
std::cerr << shader->log().toStdString() << std::endl;
exit(EXIT_FAILURE);
}
openGLWidget.setShader(shader);
//Create an empty volume and then place a sphere in it //Create an empty volume and then place a sphere in it
SimpleVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63))); SimpleVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63)));
createSphereInVolume(volData, 30); createSphereInVolume(volData, 30);