Move shader code to external files rather than embedded
A bit of CMake code in each example copies the files to the correct location and Qt loads them from the application's binary directory.
This commit is contained in:
parent
1d8fc25cb7
commit
140cbc0fc7
@ -55,6 +55,9 @@ ENDIF(MSVC)
|
|||||||
TARGET_LINK_LIBRARIES(BasicExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
TARGET_LINK_LIBRARIES(BasicExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
||||||
SET_PROPERTY(TARGET BasicExample PROPERTY FOLDER "Examples")
|
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
|
#Install - Only install the example in Windows
|
||||||
IF(WIN32)
|
IF(WIN32)
|
||||||
INSTALL(TARGETS BasicExample
|
INSTALL(TARGETS BasicExample
|
||||||
|
@ -57,6 +57,9 @@ ENDIF(MSVC)
|
|||||||
TARGET_LINK_LIBRARIES(OpenGLExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
TARGET_LINK_LIBRARIES(OpenGLExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
||||||
SET_PROPERTY(TARGET OpenGLExample PROPERTY FOLDER "Examples")
|
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
|
#Install - Only install the example in Windows
|
||||||
IF(WIN32)
|
IF(WIN32)
|
||||||
INSTALL(TARGETS OpenGLExample
|
INSTALL(TARGETS OpenGLExample
|
||||||
|
@ -57,6 +57,9 @@ ENDIF(MSVC)
|
|||||||
TARGET_LINK_LIBRARIES(PagingExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
TARGET_LINK_LIBRARIES(PagingExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
||||||
SET_PROPERTY(TARGET PagingExample PROPERTY FOLDER "Examples")
|
SET_PROPERTY(TARGET PagingExample 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
|
#Install - Only install the example in Windows
|
||||||
IF(WIN32)
|
IF(WIN32)
|
||||||
INSTALL(TARGETS PagingExample
|
INSTALL(TARGETS PagingExample
|
||||||
|
@ -55,6 +55,9 @@ ENDIF(MSVC)
|
|||||||
TARGET_LINK_LIBRARIES(SmoothLODExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
TARGET_LINK_LIBRARIES(SmoothLODExample glew ${QT_LIBRARIES} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} PolyVoxCore)
|
||||||
SET_PROPERTY(TARGET SmoothLODExample PROPERTY FOLDER "Examples")
|
SET_PROPERTY(TARGET SmoothLODExample 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
|
#Install - Only install the example in Windows
|
||||||
IF(WIN32)
|
IF(WIN32)
|
||||||
INSTALL(TARGETS SmoothLODExample
|
INSTALL(TARGETS SmoothLODExample
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QMatrix4x4>
|
#include <QMatrix4x4>
|
||||||
|
#include <QCoreApplication>
|
||||||
//#include <QtMath>
|
//#include <QtMath>
|
||||||
|
|
||||||
using namespace PolyVox;
|
using namespace PolyVox;
|
||||||
@ -91,28 +92,7 @@ void OpenGLWidget::initializeGL()
|
|||||||
|
|
||||||
// This is basically a simple fallback vertex shader which does the most basic rendering possible.
|
// 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.
|
// PolyVox examples are able to provide their own shaders to demonstrate certain effects if desired.
|
||||||
if (!mShader->addShaderFromSourceCode(QGLShader::Vertex,
|
if (!mShader->addShaderFromSourceFile(QGLShader::Vertex, QCoreApplication::applicationDirPath()+"/"+"example.vert"))
|
||||||
"#version 140\n"
|
|
||||||
|
|
||||||
"in vec4 position; // This will be the position of the vertex in model-space\n"
|
|
||||||
|
|
||||||
"// The usual matrices are provided\n"
|
|
||||||
"uniform mat4 cameraToClipMatrix;\n"
|
|
||||||
"uniform mat4 worldToCameraMatrix;\n"
|
|
||||||
"uniform mat4 modelToWorldMatrix;\n"
|
|
||||||
|
|
||||||
"// This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach\n"
|
|
||||||
"// but we use it in this example framework because not all surface extractor generate surface normals.\n"
|
|
||||||
"out vec4 worldPosition;\n"
|
|
||||||
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" // Standard sequence of OpenGL transformations.\n"
|
|
||||||
" worldPosition = modelToWorldMatrix * position;\n"
|
|
||||||
" vec4 cameraPosition = worldToCameraMatrix * worldPosition;\n"
|
|
||||||
" gl_Position = cameraToClipMatrix * cameraPosition;\n"
|
|
||||||
"}\n"
|
|
||||||
))
|
|
||||||
{
|
{
|
||||||
std::cerr << mShader->log().toStdString() << std::endl;
|
std::cerr << mShader->log().toStdString() << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -120,27 +100,7 @@ void OpenGLWidget::initializeGL()
|
|||||||
|
|
||||||
// This is basically a simple fallback fragment shader which does the most basic rendering possible.
|
// 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.
|
// PolyVox examples are able to provide their own shaders to demonstrate certain effects if desired.
|
||||||
if (!mShader->addShaderFromSourceCode(QGLShader::Fragment,
|
if (!mShader->addShaderFromSourceFile(QGLShader::Fragment, QCoreApplication::applicationDirPath()+"/"+"example.frag"))
|
||||||
"#version 130\n"
|
|
||||||
|
|
||||||
"// Passed in from the vertex shader\n"
|
|
||||||
"in vec4 worldPosition;\n"
|
|
||||||
"in vec4 worldNormal;\n"
|
|
||||||
|
|
||||||
"// the color that gets written to the display\n"
|
|
||||||
"out vec4 outputColor;\n"
|
|
||||||
|
|
||||||
"void main()\n"
|
|
||||||
"{\n"
|
|
||||||
" // Again, for the purposes of these examples we cannot be sure that per-vertex normals are provided. A sensible fallback \n"
|
|
||||||
" // is to use this little trick to compute per-fragment flat-shaded normals from the world positions using derivative operations.\n"
|
|
||||||
" vec3 normal = normalize(cross(dFdy(worldPosition.xyz), dFdx(worldPosition.xyz)));\n"
|
|
||||||
|
|
||||||
" // We are just using the normal as the output color, and making it lighter so it looks a bit nicer. \n"
|
|
||||||
" // Obviously a real shader would also do texuring, lighting, or whatever is required for the application.\n"
|
|
||||||
" outputColor = vec4(abs(normal) * 0.5 + vec3(0.5, 0.5, 0.5), 1.0);\n"
|
|
||||||
"}\n"
|
|
||||||
))
|
|
||||||
{
|
{
|
||||||
std::cerr << mShader->log().toStdString() << std::endl;
|
std::cerr << mShader->log().toStdString() << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
19
examples/common/example.frag
Normal file
19
examples/common/example.frag
Normal 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);
|
||||||
|
}
|
20
examples/common/example.vert
Normal file
20
examples/common/example.vert
Normal 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user