Thanks to all.
I tried to implement Dirk`s solution. Unfortunately, I`m not very familiar
with all that texture and shader stuff and the program crashes in the
display-method at the first call of vpleft->render( renderAction ). Maybe
someone can have a quick look at the attached code and tell me what I`m
doing wrong.
Best regards,
Rene
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGPassiveWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGPerspectiveCamera.h>
#include <OpenSG/OSGShearedStereoCameraDecorator.h>
#include <OpenSG/OSGSceneFileHandler.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSolidBackground.h>
#include <OpenSG/OSGDirectionalLight.h>
#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGViewport.h>
#include <OpenSG/OSGImage.h>
#include <OpenSG/OSGPolygonForeground.h>
#include <OpenSG/OSGFBOViewport.h>
#include <OpenSG/OSGSimpleMaterial.h>
#include <OpenSG/OSGTextureChunk.h>
#include <OpenSG/OSGSHLChunk.h>
OSG_USING_NAMESPACE
SimpleSceneManager *mgr;
SHLChunkPtr _shl = NullFC;
ShearedStereoCameraDecoratorPtr decoleft, decoright;
Real32 zpp = 2, ed = 1;
ViewportPtr vp;
GLUTWindowPtr pwin;
FBOViewportPtr vpright;
FBOViewportPtr vpleft;
unsigned int screenwidth = 1280;
unsigned int screenheight = 1024;
static std::string vshader_source="\n\
void main()\n\
{\n\
gl_TexCoord[0]= gl_MultiTexCoord0;\n\
gl_TexCoord[1]= gl_MultiTexCoord1;\n\
gl_Position=ftransform();\n\
}";
static std::string fshader_source="\n\
uniform sampler2D left_eye,right_eye;\n\
\n\
void main()\n\
{\n\
vec4 color;\n\
\n\
float x = gl_TexCoord[1].t - floor(gl_TexCoord[1].t);\n\
\n\
if ( x < 0.5) color = texture2D(right_eye,gl_TexCoord[0].st).rgba;\n\
else color = texture2D(left_eye ,gl_TexCoord[0].st).rgba;\n\
\n\
\n\
gl_FragColor = color;\n\
}";
// redraw the window
void display(void)
{
// get the RenderAction from SimpleSceneManager
RenderAction *rAct = static_cast<RenderAction*>(mgr->getAction());
// render scene from left eye
vpleft->render(rAct);
// render scene from right eye
vpright->render(rAct);
mgr->redraw();
}
// react to size changes
void reshape(int w, int h)
{
mgr->resize(w,h);
glutPostRedisplay();
}
// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
if (state)
mgr->mouseButtonRelease(button, x, y);
else
mgr->mouseButtonPress(button, x, y);
glutPostRedisplay();
}
// react to mouse motions with pressed buttons
void motion(int x, int y)
{
mgr->mouseMove(x, y);
glutPostRedisplay();
}
// react to keys
void keyboard(unsigned char k, int, int)
{
switch(k)
{
case 27: exit(1);
case 'a': zpp /= 1.1;
break;
case 's': zpp *= 1.1;
break;
case 'z': ed /= 1.1;
break;
case 'x': ed *= 1.1;
break;
}
std::cout << "ZPP:" << zpp << " ED:" << ed << std::endl;
beginEditCP(decoleft);
decoleft->setEyeSeparation(ed);
decoleft->setZeroParallaxDistance(zpp);
endEditCP (decoleft);
beginEditCP(decoright);
decoright->setEyeSeparation(ed);
decoright->setZeroParallaxDistance(zpp);
endEditCP (decoright);
}
int main(int argc, char **argv)
{
osgInit(argc,argv);
// GLUT init
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
glutGameModeString( "1280x1024:[EMAIL PROTECTED]" );
int winid = glutEnterGameMode();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
pwin = GLUTWindow::create();
pwin->setId(winid);
// create the scene
NodePtr scene;
if(argc > 1)
{
scene = SceneFileHandler::the().read(argv[1]);
}
else
{
scene = makeTorus(.5, 2, 40, 40);
}
// create the SimpleSceneManager helper
mgr = new SimpleSceneManager;
// create the window and initial camera/viewport
mgr->setWindow( pwin );
// tell the manager what to manage
mgr->setRoot (scene);
// now create vp
vp = pwin->getPort(0);
PerspectiveCameraPtr cam = PerspectiveCameraPtr::dcast(vp->getCamera());
beginEditCP(cam);
cam->setFov(deg2rad(90));
cam->setAspect(1);
endEditCP (cam);
Navigator *nav = mgr->getNavigator();
nav->setAt(Pnt3f(0,0,0));
nav->setDistance(1.5);
mgr->showAll();
// create the decorators and the second viewport
decoleft = ShearedStereoCameraDecorator::create();
decoright = ShearedStereoCameraDecorator::create();
beginEditCP(decoleft);
decoleft->setEyeSeparation(ed);
decoleft->setZeroParallaxDistance(zpp);
decoleft->setLeftEye(true);
decoleft->setDecoratee(cam);
endEditCP (decoleft);
beginEditCP(decoright);
decoright->setEyeSeparation(ed);
decoright->setZeroParallaxDistance(zpp);
decoright->setLeftEye(false);
decoright->setDecoratee(cam);
endEditCP (decoright);
TextureChunkPtr vprightTexture = TextureChunk::create();
TextureChunkPtr vpleftTexture = TextureChunk::create();
ImagePtr leftimg = Image::create();
beginEditCP(leftimg);
leftimg->set(Image::OSG_RGBA_PF, screenwidth, screenheight);
endEditCP(leftimg);
beginEditCP(vprightTexture);
vprightTexture->setMinFilter(GL_LINEAR);
vprightTexture->setMagFilter(GL_LINEAR);
vprightTexture->setTarget(GL_TEXTURE_2D);
vprightTexture->setInternalFormat(GL_RGBA8);
vprightTexture->setImage(leftimg);
endEditCP(vprightTexture);
ImagePtr rightimg = Image::create();
beginEditCP(rightimg);
rightimg->set(Image::OSG_RGBA_PF, screenwidth, screenheight);
endEditCP(rightimg);
beginEditCP(vpleftTexture);
vpleftTexture->setMinFilter(GL_LINEAR);
vpleftTexture->setMagFilter(GL_LINEAR);
vpleftTexture->setTarget(GL_TEXTURE_2D);
vpleftTexture->setInternalFormat(GL_RGBA8);
vpleftTexture->setImage(rightimg);
endEditCP(vpleftTexture);
vpright = FBOViewport::create();
vpleft = FBOViewport::create();
beginEditCP(vpright);
vpright->setLeft(0);
vpright->setRight(1);
vpright->setBottom(0);
vpright->setTop(1);
vpright->setCamera(decoleft);
vpright->setBackground(vp->getBackground());
vpright->setRoot(vp->getRoot());
vpright->setSize(0, 0, screenwidth - 1, screenheight - 1);
vpright->setStorageWidth(screenwidth);
vpright->setStorageHeight(screenheight);
vpright->setParent(pwin);
//attach texture as render target
vpright->getTextures().push_back(vprightTexture);
endEditCP(vpright);
beginEditCP(vpleft);
vpleft->setLeft(0);
vpleft->setRight(1);
vpleft->setBottom(0);
vpleft->setTop(1);
vpleft->setCamera(decoright);
vpleft->setBackground(vp->getBackground());
vpleft->setRoot(vp->getRoot());
vpleft->setSize(0, 0, screenwidth - 1, screenheight - 1);
vpleft->setStorageWidth(screenwidth);
vpleft->setStorageHeight(screenheight);
vpleft->setParent(pwin);
// attach texture as render target
vpleft->getTextures().push_back(vpleftTexture);
endEditCP (vpleft);
// create the shader material
ChunkMaterialPtr cmat = ChunkMaterial::create();
_shl = SHLChunk::create();
beginEditCP(_shl);
_shl->setVertexProgram(vshader_source);
_shl->setFragmentProgram(fshader_source);
_shl->setUniformParameter("left_eye", 0);
_shl->setUniformParameter("right_eye", 1);
endEditCP(_shl);
beginEditCP(cmat);
cmat->addChunk( vprightTexture );
cmat->addChunk( vpleftTexture );
cmat->addChunk(_shl);
endEditCP(cmat);
//The Polygon foreground
PolygonForegroundPtr polyFrgnd = PolygonForeground::create();
beginEditCP(polyFrgnd);
polyFrgnd->setMaterial(cmat);
polyFrgnd->getMFPositions()->push_back(Pnt2f(0.0, 0.0));
polyFrgnd->getMFPositions()->push_back(Pnt2f(0.0+screenwidth, 0.0));
polyFrgnd->getMFPositions()->push_back(Pnt2f(0.0+screenwidth, 0.0+screenheight));
polyFrgnd->getMFPositions()->push_back(Pnt2f(0.0, 0.0+screenheight));
polyFrgnd->getMFTexCoords()->push_back(Vec3f(0.0, 0.0, 0.0));
polyFrgnd->getMFTexCoords()->push_back(Vec3f(1.0, 0.0, 0.0));
polyFrgnd->getMFTexCoords()->push_back(Vec3f(1.0, 1.0, 0.0));
polyFrgnd->getMFTexCoords()->push_back(Vec3f(0.0, 1.0, 0.0));
endEditCP(polyFrgnd);
beginEditCP(vp);
MFForegroundPtr *foregrounds = vp->getMFForegrounds();
foregrounds->push_back(polyFrgnd);
endEditCP(vp);
// GLUT main loop
glutMainLoop();
return 0;
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users