Hello Carsten,
>>> // if you want it to work when compiling with enable_osg2_prep
rAct->setWindow(get_pointer(pwin));
// otherwise
rAct->setWindow(pwin.getCPtr());
before the call to vpleft->render(rAct);
The program crashes if I call this. However, shouldn`t 29FBOViewport
produce the same warning? On my setup, it does not.
hm, I'm wondering if this is some kind of ref count problem. The thing
to watch out for in 1.x is that one often uses objects with a ref
count
of 0. This is fine most of the time unless they are used in some
operation that temporarily increases the ref count and then drops it
again (back to 0) and then the object is destroyed.
Can you add a call to addRefCP for the global objects you have ?
I added an addRefCP to all objects but it doen`s help to avoid the warnings.
When I turn on rAct->setWindow(get_pointer(pwin));, the program crashes at
#if !defined(OSG_DEBUG_NO_FCPTR_ARITHM)
return
*(reinterpret_cast<FieldContainerTypeT *>(
Self::getElemP(Thread::getAspect())));
in function
template <class BasePtrTypeT, class FieldContainerTypeT> inline
FieldContainerTypeT &FCPtr<BasePtrTypeT,
FieldContainerTypeT>::operator *(void) const
29FBOViewport works fine, but my derivative only produces black
images. I wrote the Texture to png right after the rendering in
order to avoid errors with the shaders or whatever.
Please note that for the FBO case it is possible that the textures
live *only* in GPU memory. You need to instruct the FBOViewport do
download
them to CPU memory explicitly (setReadBuffer(true)).
OK, I didn`t take this into account. Now the program renders the two
textures and writes them alternately to the screen. I added the slightly
modified version, maybe it can help someone else in the future.
However, there is still another open problem besides the warnings: Both
cameradecorators render the same image, so there is still no stereo effect
and I don`t know why. Maybe someone knows a solution?
Thanks for your help and 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>
#include <OpenSG/OSGGradientBackground.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;
NodePtr scene;
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 two = 2.0;\n\
int x = mod( gl_FragCoord[1], two );\n\
\n\
if ( x == 0 ) 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\
}";
int counter = 0;
// redraw the window
void display(void)
{
// get the RenderAction from SimpleSceneManager
RenderAction *rAct = static_cast<RenderAction*>(mgr->getAction());
rAct->setWindow(get_pointer(pwin));
// render scene from left eye
vpleft->render(rAct);
// render scene from right eye
vpright->render(rAct);
counter++;
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();
beginEditCP(pwin);
pwin->setId(winid);
pwin->init();
endEditCP(pwin);
// create the 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);
// set a nicer background
GradientBackgroundPtr bkg = GradientBackground::create();
beginEditCP(bkg);
bkg->addLine(Color3f(0.7, 0.7, 0.8), 0);
bkg->addLine(Color3f(0.0, 0.1, 0.3), 1);
endEditCP(bkg);
vp->setBackground(bkg);
PerspectiveCameraPtr cam = PerspectiveCameraPtr::dcast(vp->getCamera());
beginEditCP(cam);
cam->setFov(deg2rad(90));
cam->setAspect(1);
endEditCP (cam);
// 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->setEnvMode(GL_REPLACE);
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->setEnvMode(GL_REPLACE);
vpleftTexture->setTarget(GL_TEXTURE_2D);
vpleftTexture->setInternalFormat(GL_RGBA8);
vpleftTexture->setImage(rightimg);
endEditCP(vpleftTexture);
vpright = FBOViewport::create();
vpleft = FBOViewport::create();
beginEditCP(vpright);
vpright->setCamera(decoright);
vpright->setBackground(vp->getBackground());
vpright->setRoot(vp->getRoot());
vpright->setSize(0, 0, 1, 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->setCamera(decoleft);
vpleft->setBackground(vp->getBackground());
vpleft->setRoot(vp->getRoot());
vpleft->setSize(0, 0, 1, 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->setTile(false);
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));
polyFrgnd->setNormalizedX(false);
polyFrgnd->setNormalizedY(false);
endEditCP(polyFrgnd);
beginEditCP(vp);
MFForegroundPtr *foregrounds = vp->getMFForegrounds();
foregrounds->push_back(polyFrgnd);
endEditCP(vp);
mgr->showAll();
// 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