Commit 3cb2cdcd authored by Robert Menzel's avatar Robert Menzel
Browse files

added default shaders

parent 444de9ee
...@@ -169,9 +169,21 @@ public: ...@@ -169,9 +169,21 @@ public:
private: private:
// for rendering:
ACGL::OpenGL::SharedShaderProgram mDistortShaderSideBySide; ACGL::OpenGL::SharedShaderProgram mDistortShaderSideBySide;
ACGL::OpenGL::SharedShaderProgram mDistortShaderTwoTextures; ACGL::OpenGL::SharedShaderProgram mDistortShaderTwoTextures;
ACGL::OpenGL::SharedVertexArrayObject mVAO;
GLint uLensCenter;
GLint uScreenCenter;
GLint uScale;
GLint uScaleIn;
GLint uHmdWarpParam;
GLint uChromAbParam;
GLint uDistort;
GLint uCorrectChromaticAberation;
bool mBuildInShader;
bool mUseDistortion; bool mUseDistortion;
bool mUseChromaticAberation; bool mUseChromaticAberation;
glm::uvec2 mOutputViewport; // if it's 0,0 -> use the Rifts screen dimensions! glm::uvec2 mOutputViewport; // if it's 0,0 -> use the Rifts screen dimensions!
......
...@@ -347,14 +347,214 @@ glm::vec4 SimpleRiftController::getChromAbParam() ...@@ -347,14 +347,214 @@ glm::vec4 SimpleRiftController::getChromAbParam()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// optional rendering: // optional rendering nuild-in shaders:
// //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const char *VS_SOURCE = "foo"; const char *VS_SOURCE = "#version 150 core \n\
const char *FS_SOURCE_SIDE_BY_SIDE = "bar"; \n\
const char *FS_SOURCE_TWO_TEXTURE = "TODO"; out vec2 vTexCoord;\n\
\n\
const vec2 quad[4] = vec2[] (\n\
vec2(-1.0, 1.0),\n\
vec2(-1.0,-1.0),\n\
vec2( 1.0, 1.0),\n\
vec2( 1.0,-1.0)\n\
);\n\
\n\
void main()\n\
{\n\
vec2 p = quad[ gl_VertexID ];\n\
\n\
vTexCoord = p * vec2(0.5, 0.5) + vec2(0.5, 0.5);\n\
gl_Position = vec4(p, 0.0, 1.0);\n\
}";
const char *FS_SOURCE_SIDE_BY_SIDE = "#version 150 core\n\
\n\
uniform sampler2D uSamplerColor;\n\
\n\
// defaults are the values of the first dev kit rendered at 1280 by 800:\n\
uniform vec4 uLensCenter = vec4(0.287994, 0.5, 0.712006, 0.5);\n\
uniform vec4 uScreenCenter = vec4(0.25, 0.5, 0.75, 0.5);\n\
uniform vec4 uScale = vec4(0.25, 0.4, 0.25, 0.4);\n\
uniform vec4 uScaleIn = vec4(4.0, 2.5, 4.0, 2.5);\n\
uniform vec4 uHmdWarpParam = vec4(1.0, 0.22, 0.24, 0.0);\n\
uniform vec4 uChromAbParam = vec4(0.996, -0.004, 1.014, 0.0);\n\
uniform bool uDistort = true;\n\
uniform bool uCorrectChromaticAberation = true;\n\
in vec2 vTexCoord;\n\
out vec4 oColor;\n\
\n\
//\n\
// taken and adjusted from the SDK sample:\n\
//\n\
vec4 getDistortedColorAt( in vec2 position, in bool leftEye )\n\
{\n\
vec2 LensCenter;\n\
vec2 ScreenCenter;\n\
vec2 Scale = uScale.xy;\n\
vec2 ScaleIn = uScaleIn.xy;\n\
if (leftEye) {\n\
// left half\n\
LensCenter = uLensCenter.xy;\n\
ScreenCenter = uScreenCenter.xy;\n\
} else {\n\
// right half\n\
LensCenter = uLensCenter.zw;\n\
ScreenCenter = uScreenCenter.zw;\n\
}\n\
// vector from the lens center to the current point:\n\
vec2 theta = (position - LensCenter) * ScaleIn; \n\
// scaled distance from the lens center:\n\
float rSq = theta.x * theta.x + theta.y * theta.y;\n\
vec2 theta1 = theta * (uHmdWarpParam.x + uHmdWarpParam.y * rSq + uHmdWarpParam.z * rSq * rSq + uHmdWarpParam.w * rSq * rSq * rSq);\n\
// Detect whether blue texture coordinates are out of range since these will scaled out the furthest.\n\
vec2 thetaBlue = theta1 * (uChromAbParam.z + uChromAbParam.w * rSq);\n\
vec2 tcBlue = Scale * thetaBlue + LensCenter;\n\
if (!all(equal(clamp(tcBlue, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tcBlue)))\n\
{\n\
return vec4(0.0);\n\
}\n\
// Do green lookup (no scaling).\n\
vec2 tcGreen = Scale * theta1 + LensCenter;\n\
vec4 center = texture(uSamplerColor, tcGreen);\n\
if (!uCorrectChromaticAberation) {\n\
return center;\n\
}\n\
// Now do blue texture lookup.\n\
float blue = texture(uSamplerColor, tcBlue).b;\n\
// Do red scale and lookup.\n\
vec2 thetaRed = theta1 * (uChromAbParam.x + uChromAbParam.y * rSq);\n\
vec2 tcRed = Scale * thetaRed + LensCenter;\n\
float red = texture(uSamplerColor, tcRed).r;\n\
return vec4(red, center.g, blue, center.a);\n\
}\n\
\n\
void main()\n\
{\n\
if (uDistort) {\n\
bool leftEye = (vTexCoord.x < 0.5);\n\
oColor = getDistortedColorAt( vTexCoord, leftEye );\n\
} else {\n\
oColor = texture(uSamplerColor, vTexCoord);\n\
}\n\
}\n\
";
const char *FS_SOURCE_TWO_TEXTURE = "#version 150 core\n\
\n\
uniform sampler2D uSamplerColorLeft;\n\
uniform sampler2D uSamplerColorRight;\n\
\n\
// defaults are the values of the first dev kit rendered at 1280 by 800:\n\
uniform vec4 uLensCenter = vec4(0.287994, 0.5, 0.712006, 0.5);\n\
uniform vec4 uScreenCenter = vec4(0.25, 0.5, 0.75, 0.5);\n\
uniform vec4 uScale = vec4(0.25, 0.4, 0.25, 0.4);\n\
uniform vec4 uScaleIn = vec4(4.0, 2.5, 4.0, 2.5);\n\
uniform vec4 uHmdWarpParam = vec4(1.0, 0.22, 0.24, 0.0);\n\
uniform vec4 uChromAbParam = vec4(0.996, -0.004, 1.014, 0.0);\n\
uniform bool uDistort = true;\n\
uniform bool uCorrectChromaticAberation = true;\n\
in vec2 vTexCoord;\n\
out vec4 oColor;\n\
\n\
//\n\
// taken and adjusted from the SDK sample:\n\
//\n\
vec4 getDistortedColorAt( in vec2 position, in bool leftEye )\n\
{\n\
vec2 LensCenter;\n\
vec2 ScreenCenter;\n\
vec2 Scale = uScale.xy;\n\
vec2 ScaleIn = uScaleIn.xy;\n\
if (leftEye) {\n\
// left half\n\
LensCenter = uLensCenter.xy;\n\
ScreenCenter = uScreenCenter.xy;\n\
} else {\n\
// right half\n\
LensCenter = uLensCenter.zw;\n\
ScreenCenter = uScreenCenter.zw;\n\
}\n\
\n\
// vector from the lens center to the current point:\n\
vec2 theta = (position - LensCenter) * ScaleIn;\n\ // Scales to [-1, 1]\n\
\n\
// scaled distance from the lens center:\n\
float rSq = theta.x * theta.x + theta.y * theta.y;\n\
\n\
vec2 theta1 = theta * (uHmdWarpParam.x + uHmdWarpParam.y * rSq + uHmdWarpParam.z * rSq * rSq + uHmdWarpParam.w * rSq * rSq * rSq);\n\
\n\
// Detect whether blue texture coordinates are out of range since these will scaled out the furthest.\n\
vec2 thetaBlue = theta1 * (uChromAbParam.z + uChromAbParam.w * rSq);\n\
vec2 tcBlue = Scale * thetaBlue + LensCenter;\n\
if (!all(equal(clamp(tcBlue, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tcBlue)))\n\
{\n\
return vec4(0.0);\n\
}\n\
// Do green lookup (no scaling).\n\
vec2 tcGreen = Scale * theta1 + LensCenter;\n\
vec4 center;\n\
if (leftEye) {\n\
tcGreen.x *= 2.0;\n\
center = texture(uSamplerColorLeft, tcGreen);\n\
} else {\n\
tcGreen.x -= 0.5;\n\
tcGreen.x *= 2.0;\n\
center = texture(uSamplerColorRight, tcGreen);\n\
}\n\
if (!uCorrectChromaticAberation) {\n\
return center;\n\
}\n\
// Now do blue texture lookup.\n\
float blue;\n\
if (leftEye) {\n\
tcBlue.x *= 2.0;\n\
blue = texture(uSamplerColorLeft, tcBlue).b;\n\
} else {\n\
tcBlue.x -= 0.5;\n\
tcBlue.x *= 2.0;\n\
blue = texture(uSamplerColorRight, tcBlue).b;\n\
}\n\
// Do red scale and lookup.\n\
vec2 thetaRed = theta1 * (uChromAbParam.x + uChromAbParam.y * rSq);\n\
vec2 tcRed = Scale * thetaRed + LensCenter;\n\
float red;\n\
if (leftEye) {\n\
tcRed.x *= 2.0;\n\
red = texture(uSamplerColorLeft, tcRed).r;\n\
} else {\n\
tcRed.x -= 0.5;\n\
tcRed.x *= 2.0;\n\
red = texture(uSamplerColorRight, tcRed).r;\n\
}\n\
return vec4(red, center.g, blue, center.a);\n\
}\n\
\n\
void main()\n\
{\n\
bool leftEye = (vTexCoord.x < 0.5);\n\
if (uDistort) {\n\
oColor = getDistortedColorAt( vTexCoord, leftEye );\n\
} else {\n\
vec2 inTextureCoordinate = vTexCoord;\n\
inTextureCoordinate.x *= 2.0;\n\
if (leftEye) {\n\
oColor = texture( uSamplerColorLeft, inTextureCoordinate );\n\
} else {\n\
oColor = texture( uSamplerColorRight, inTextureCoordinate );\n\
}\n\
}\n\
}\n\
\n\
";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// optional rendering:
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SimpleRiftController::renderDistorted( OpenGL::ConstSharedTexture2D _sideBySideTexture ) void SimpleRiftController::renderDistorted( OpenGL::ConstSharedTexture2D _sideBySideTexture )
{ {
...@@ -375,9 +575,27 @@ void SimpleRiftController::renderDistorted( OpenGL::ConstSharedTexture2D _sideBy ...@@ -375,9 +575,27 @@ void SimpleRiftController::renderDistorted( OpenGL::ConstSharedTexture2D _sideBy
mDistortShaderSideBySide->attachShader( vs ); mDistortShaderSideBySide->attachShader( vs );
mDistortShaderSideBySide->attachShader( fs ); mDistortShaderSideBySide->attachShader( fs );
mDistortShaderSideBySide->link(); mDistortShaderSideBySide->link();
mDistortShaderTwoTextures->use();
uLensCenter = mDistortShaderSideBySide->getUniformLocation("uLensCenter");
uScreenCenter = mDistortShaderSideBySide->getUniformLocation("uScreenCenter");
uScale = mDistortShaderSideBySide->getUniformLocation("uScale");
uScaleIn = mDistortShaderSideBySide->getUniformLocation("uScaleIn");
uHmdWarpParam = mDistortShaderSideBySide->getUniformLocation("uHmdWarpParam");
uChromAbParam = mDistortShaderSideBySide->getUniformLocation("uChromAbParam");
uDistort = mDistortShaderSideBySide->getUniformLocation("uDistort");
uCorrectChromaticAberation = mDistortShaderSideBySide->getUniformLocation("uCorrectChromaticAberation");
mBuildInShader = true;
mDistortShaderTwoTextures->setUniform(uLensCenter, getLensCenter() );
mDistortShaderTwoTextures->setUniform(uScreenCenter, getScreenCenter() );
mDistortShaderTwoTextures->setUniform(uScaleIn, getScaleIn() );
mDistortShaderTwoTextures->setUniform(uHmdWarpParam, getHmdWarpParam() );
mDistortShaderTwoTextures->setUniform(uChromAbParam, getChromAbParam() );
} else { } else {
ACGL::Utils::debug() << "SimpleRiftController: using application provided shaders" << std::endl; ACGL::Utils::debug() << "SimpleRiftController: using application provided shaders" << std::endl;
mBuildInShader = false;
} }
} }
...@@ -405,15 +623,34 @@ void SimpleRiftController::renderDistorted( OpenGL::ConstSharedTexture2D _leftTe ...@@ -405,15 +623,34 @@ void SimpleRiftController::renderDistorted( OpenGL::ConstSharedTexture2D _leftTe
mDistortShaderTwoTextures->attachShader( vs ); mDistortShaderTwoTextures->attachShader( vs );
mDistortShaderTwoTextures->attachShader( fs ); mDistortShaderTwoTextures->attachShader( fs );
mDistortShaderTwoTextures->link(); mDistortShaderTwoTextures->link();
mDistortShaderTwoTextures->use();
uLensCenter = mDistortShaderTwoTextures->getUniformLocation("uLensCenter");
uScreenCenter = mDistortShaderTwoTextures->getUniformLocation("uScreenCenter");
uScale = mDistortShaderTwoTextures->getUniformLocation("uScale");
uScaleIn = mDistortShaderTwoTextures->getUniformLocation("uScaleIn");
uHmdWarpParam = mDistortShaderTwoTextures->getUniformLocation("uHmdWarpParam");
uChromAbParam = mDistortShaderTwoTextures->getUniformLocation("uChromAbParam");
uDistort = mDistortShaderTwoTextures->getUniformLocation("uDistort");
uCorrectChromaticAberation = mDistortShaderTwoTextures->getUniformLocation("uCorrectChromaticAberation");
mBuildInShader = true;
mDistortShaderTwoTextures->setUniform(uLensCenter, getLensCenter() );
mDistortShaderTwoTextures->setUniform(uScreenCenter, getScreenCenter() );
mDistortShaderTwoTextures->setUniform(uScaleIn, getScaleIn() );
mDistortShaderTwoTextures->setUniform(uHmdWarpParam, getHmdWarpParam() );
mDistortShaderTwoTextures->setUniform(uChromAbParam, getChromAbParam() );
} else { } else {
ACGL::Utils::debug() << "SimpleRiftController: using application provided shaders" << std::endl; ACGL::Utils::debug() << "SimpleRiftController: using application provided shaders" << std::endl;
mBuildInShader = false;
} }
} }
mDistortShaderTwoTextures->use(); mDistortShaderTwoTextures->use();
mDistortShaderTwoTextures->setTexture("uSamplerColorLeft", _leftTexture, 0 ); mDistortShaderTwoTextures->setTexture("uSamplerColorLeft", _leftTexture, 0 );
mDistortShaderTwoTextures->setTexture("uSamplerColorRight", _rightTexture, 1 ); mDistortShaderTwoTextures->setTexture("uSamplerColorRight", _rightTexture, 1 );
renderDistorted( mDistortShaderTwoTextures ); renderDistorted( mDistortShaderTwoTextures );
} }
...@@ -430,19 +667,29 @@ void SimpleRiftController::renderDistorted( ACGL::OpenGL::ConstSharedShaderProgr ...@@ -430,19 +667,29 @@ void SimpleRiftController::renderDistorted( ACGL::OpenGL::ConstSharedShaderProgr
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// we assume that _program is in use as this should only get called from our own functions // we assume that _program is in use as this should only get called from our own functions
_program->setUniform("uLensCenter", getLensCenter() );
_program->setUniform("uScreenCenter", getScreenCenter() ); if (!mBuildInShader) {
_program->setUniform("uScale", getScale() ); // the shader might have been reloaded
_program->setUniform("uScaleIn", getScaleIn() ); _program->setUniform("uLensCenter", getLensCenter() );
_program->setUniform("uHmdWarpParam", getHmdWarpParam() ); _program->setUniform("uScreenCenter", getScreenCenter() );
_program->setUniform("uChromAbParam", getChromAbParam() ); _program->setUniform("uScale", getScale() );
_program->setUniform("uDistort", (int)mUseDistortion ); _program->setUniform("uScaleIn", getScaleIn() );
_program->setUniform("uCorrectChromaticAberation", (int)mUseChromaticAberation ); _program->setUniform("uHmdWarpParam", getHmdWarpParam() );
_program->setUniform("uChromAbParam", getChromAbParam() );
_program->setUniform("uDistort", (int)mUseDistortion );
_program->setUniform("uCorrectChromaticAberation", (int)mUseChromaticAberation );
} else {
_program->setUniform(uScaleIn, getScaleIn() );
_program->setUniform(uDistort, (int)mUseDistortion );
_program->setUniform(uCorrectChromaticAberation, (int)mUseChromaticAberation );
}
// attribute-less rendering: // attribute-less rendering:
// just rendering a fullscreen quad // just rendering a fullscreen quad
OpenGL::VertexArrayObject vao; if (!mVAO) {
vao.bind(); // 'empty' VAO -> no attributes are defined mVAO = SharedVertexArrayObject( new VertexArrayObject() );
}
mVAO->bind(); // 'empty' VAO -> no attributes are defined
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); // create 2 triangles with no attributes glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); // create 2 triangles with no attributes
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment