Commit 36d44291 authored by Jan Möbius's avatar Jan Möbius
Browse files

New shader pipeline


git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@17463 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 86d0a070
include (plugin)
openflipper_plugin ( DIRS widgets CRC )
openflipper_plugin ( INSTALLDATA Shaders DIRS widgets CRC )
......@@ -52,6 +52,7 @@
#include <iostream>
#include <ACG/GL/GLState.hh>
#include <ACG/GL/ScreenQuad.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>
......@@ -61,11 +62,6 @@
#include <QCursor>
PostProcessorPhilipsStereoPlugin::PostProcessorPhilipsStereoPlugin():
colorTextureBufferWidth_(0),
colorTextureBufferHeight_(0),
depthStencilTextureBufferWidth_(0),
depthStencilTextureBufferHeight_(0),
philipsStereoInitialized_(false),
pProgram_(0),
settingsWidget_(0)
{
......@@ -129,164 +125,23 @@ void PostProcessorPhilipsStereoPlugin::slotShowOptionsMenu() {
//-----------------------------------------------------------------------------
void PostProcessorPhilipsStereoPlugin::updateScenePhilipsStereo(ACG::GLState* _glstate)
{
int vp_l, vp_b, vp_w, vp_h;
_glstate->get_viewport (vp_l, vp_b, vp_w, vp_h);
// Does shader program exist?
if (!pProgram_) {
// ======================================================================================================
// Setup the shaders used to render color and depth info next to each other
// ======================================================================================================
GLSL::PtrVertexShader vertexShader;
GLSL::PtrFragmentShader fragmentShader;
QString vshaderFile = OpenFlipper::Options::shaderDirStr() + QDir::separator() + "Philips/Vertex.glsl";
QString fshaderFile = OpenFlipper::Options::shaderDirStr() + QDir::separator() + "Philips/Fragment42.glsl";
vertexShader = GLSL::loadVertexShader(vshaderFile.toUtf8());
fragmentShader = GLSL::loadFragmentShader(fshaderFile.toUtf8());
pProgram_ = GLSL::PtrProgram(new GLSL::Program());
if ((vertexShader == 0) || (fragmentShader == 0) || (pProgram_ == 0)) {
std::cerr << "Unable to load shaders for philips display rendering!";
philipsStereoInitialized_ = false;
return;
}
pProgram_->attach(vertexShader);
pProgram_->attach(fragmentShader);
pProgram_->link();
}
// Does color texture exist?
if (!pColorTexture_.is_valid()) {
// ======================================================================================================
// creating a color texture
// ======================================================================================================
pColorTexture_.enable();
pColorTexture_.bind();
GLenum texTarget = GL_TEXTURE_2D;
GLenum texInternalFormat = GL_RGBA;
GLenum texFormat = GL_RGBA;
GLenum texType = GL_UNSIGNED_BYTE;
GLenum texFilterMode = GL_NEAREST;
glTexImage2D(texTarget, 0, texInternalFormat, vp_w, vp_h, 0, texFormat, texType, NULL);
glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, texFilterMode);
glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, texFilterMode);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(texTarget, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
if(!pColorTexture_.is_valid()) {
philipsStereoInitialized_ = false;
return;
}
}
// Resize target textures
if (_glstate->viewport_width() != colorTextureBufferWidth_ || _glstate->viewport_height() != colorTextureBufferHeight_) {
// Color texture
pColorTexture_.bind();
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, _glstate->viewport_width(), _glstate->viewport_height(), 0,
GL_RGB, GL_UNSIGNED_BYTE, 0);
ACG::GLState::bindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
colorTextureBufferWidth_ = _glstate->viewport_width();
colorTextureBufferHeight_ = _glstate->viewport_height();
}
// Does depth stencil texture exist?
if (!pDepthStencilTexture_.is_valid()) {
// ======================================================================================================
// creating an 24-bit depth + 8-bit stencil texture
// ======================================================================================================
pDepthStencilTexture_.enable();
pDepthStencilTexture_.bind();
GLenum texTarget = GL_TEXTURE_2D;
GLenum texInternalFormat = GL_DEPTH24_STENCIL8_EXT;
GLenum texFormat = GL_DEPTH_STENCIL_EXT;
GLenum texType = GL_UNSIGNED_INT_24_8_EXT;
GLenum texFilterMode = GL_NEAREST;
glTexImage2D(texTarget, 0, texInternalFormat, vp_w, vp_h, 0, texFormat, texType, NULL);
glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, texFilterMode);
glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, texFilterMode);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(texTarget, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
if(!pDepthStencilTexture_.is_valid()) {
philipsStereoInitialized_ = false;
return;
}
}
// Resize target texture
if (_glstate->viewport_width() != depthStencilTextureBufferWidth_ || _glstate->viewport_height() != depthStencilTextureBufferHeight_) {
// Depth stencil texture
pDepthStencilTexture_.bind();
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, _glstate->viewport_width(), _glstate->viewport_height(), 0,
GL_RGB, GL_UNSIGNED_BYTE, 0);
ACG::GLState::bindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
depthStencilTextureBufferWidth_ = _glstate->viewport_width();
depthStencilTextureBufferHeight_ = _glstate->viewport_height();
}
philipsStereoInitialized_ = true;
}
void PostProcessorPhilipsStereoPlugin::postProcess(ACG::GLState* _glstate) {
void PostProcessorPhilipsStereoPlugin::postProcess(ACG::GLState* _glstate, const PostProcessorInput& _input, GLuint _targetFBO) {
if ( !ACG::checkExtensionSupported("GL_ARB_texture_rectangle") ) {
std::cerr << "GL_ARB_texture_rectangle not supported! " << std::endl;
return;
}
updateScenePhilipsStereo(_glstate);
// load shader if needed
if (!pProgram_)
pProgram_ = GLSL::loadProgram("Philips/screenquad.glsl", "Philips/Fragment42.glsl");
if (!philipsStereoInitialized_)
if (!pProgram_)
return;
int vp_l, vp_b, vp_w, vp_h;
_glstate->get_viewport(vp_l, vp_b, vp_w, vp_h);
// ======================================================================================================
// Disable textures
// ======================================================================================================
pDepthStencilTexture_.disable();
pColorTexture_.disable();
// ======================================================================================================
// Copy Scene to Textures
// ======================================================================================================
pColorTexture_.enable();
pColorTexture_.bind();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vp_l, vp_b, vp_w, vp_h);
pColorTexture_.disable();
pDepthStencilTexture_.enable();
pDepthStencilTexture_.bind();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, vp_l, vp_b, vp_w, vp_h);
pDepthStencilTexture_.disable();
// Turn on shader program
pProgram_->use();
......@@ -294,23 +149,26 @@ void PostProcessorPhilipsStereoPlugin::postProcess(ACG::GLState* _glstate) {
// ======================================================================================================
// Bind textures to different texture units and tell shader where to find them
// ======================================================================================================
ACG::GLState::activeTextureARB(GL_TEXTURE0_ARB);
pColorTexture_.enable();
pColorTexture_.bind();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _input.depthTex_);
ACG::GLState::activeTextureARB(GL_TEXTURE1_ARB);
pDepthStencilTexture_.enable();
pDepthStencilTexture_.bind();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _input.colorTex_);
pProgram_->setUniform("ColorTexture", 0);
pProgram_->setUniform("DepthStencil", 1);
// ======================================================================================================
// Render plain textured
// Setup render states
// ======================================================================================================
ACG::GLState::disable(GL_LIGHTING);
ACG::GLState::disable(GL_COLOR_MATERIAL);
ACG::GLState::disable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDepthMask(1);
glColorMask(1,1,1,1);
// ======================================================================================================
// Setup orthogonal projection
......@@ -328,6 +186,13 @@ void PostProcessorPhilipsStereoPlugin::postProcess(ACG::GLState* _glstate) {
// ======================================================================================================
glColor3f(1.0, 1.0, 1.0);
// ======================================================================================================
// Bind output FBO
// ======================================================================================================
glBindFramebuffer(GL_FRAMEBUFFER, _targetFBO);
// ======================================================================================================
// Clear buffers
// ======================================================================================================
......@@ -335,28 +200,14 @@ void PostProcessorPhilipsStereoPlugin::postProcess(ACG::GLState* _glstate) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// ======================================================================================================
// Render a simple quad (rest is done by shader)
// Execute
// ======================================================================================================
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2i(0, vp_h);
glTexCoord2f(1.0f, 1.0f);
glVertex2i(vp_w, vp_h);
glTexCoord2f(1.0f, 0.0f);
glVertex2i(vp_w, 0);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(0, 0);
glEnd();
ACG::ScreenQuad::draw(pProgram_);
pProgram_->disable();
ACG::GLState::bindTexture(GL_TEXTURE_2D, 0);
pProgram_->disable();
// ======================================================================================================
// Cleanup (color and depth textures)
// ======================================================================================================
pDepthStencilTexture_.del();
pColorTexture_.del();
glBindTexture(GL_TEXTURE_2D, 0);
// ======================================================================================================
// Compute the header required for the display
......
......@@ -81,37 +81,13 @@ class PostProcessorPhilipsStereoPlugin : public QObject, BaseInterface, PostProc
// PostProcessorInterface
QAction* optionsAction();
void postProcess(ACG::GLState* _glstate);
void postProcess(ACG::GLState* _glstate, const PostProcessorInput& _input, GLuint _targetFBO);
void slotShowOptionsMenu();
private:
void updateScenePhilipsStereo(ACG::GLState* _glstate);
private:
/// color texture buffer
ACG::Texture2D pColorTexture_;
/// Current width of the color texture buffer
int colorTextureBufferWidth_;
/// Current height of the color texture buffer
int colorTextureBufferHeight_;
/// depthStencil texture buffer
ACG::Texture2D pDepthStencilTexture_;
/// Current width of the depthStencil texture buffer
int depthStencilTextureBufferWidth_;
/// Current height of the depthStencil texture buffer
int depthStencilTextureBufferHeight_;
bool philipsStereoInitialized_;
GLSL::PtrProgram pProgram_;
PhilipsStereoSettingsWidget* settingsWidget_;
......
uniform sampler2D ColorTexture;
uniform sampler2D DepthStencil;
void main(void)
{
// Left side is the color image seen by the user
// Right side is the the depth map of the current view
// Texture coordinates have to be scaled
// and for the left image they also have to be shifted
if ( gl_TexCoord[0].s < 0.5 ) {
gl_TexCoord[0].s = gl_TexCoord[0].s * 2.0;
gl_FragColor = texture2D( ColorTexture, gl_TexCoord[0].st );
} else {
gl_TexCoord[0].s = (gl_TexCoord[0].s - 0.5) * 2.0;
vec4 convert = texture2D( DepthStencil, gl_TexCoord[0].st );
// This conversion is specified in the docs for a 20'' Display
convert.rgba = (-1586.34 *( 1.0 - (6.180772 / ( convert.r - 0.459813 + 6.180772)) ) + 127.5) / 255.0;
gl_FragColor = convert;
}
}
#version 150
uniform sampler2D ColorTexture;
uniform sampler2D DepthStencil;
in vec2 vTexCoord;
out vec4 oColor;
void main(void)
{
// Left side is the color image seen by the user
// Right side is the the depth map of the current view
// Texture coordinates have to be scaled
// and for the left image they also have to be shifted
if ( vTexCoord.x < 0.5 ) {
vTexCoord.x = vTexCoord.x * 2.0;
oColor = texture2D( ColorTexture, vTexCoord );
} else {
vTexCoord.x = (vTexCoord.x - 0.5) * 2.0;
vec4 convert = texture2D( DepthStencil, vTexCoord );
// This conversion is specified in the docs for a 42'' Display
convert.rgba = (-1960.37 *( 1.0 - (7.655192 / ( convert.r - 0.467481 + 7.655192)) ) + 127.5) / 255.0;
oColor = convert;
}
}
void main(void)
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
#version 150
in vec4 inPosition;
out vec2 vTexCoord;
void main()
{
gl_Position = inPosition;
vTexCoord = inPosition.xy * 0.5 + vec2(0.5, 0.5);
}
\ No newline at end of file
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