Showing posts with label OSG. Show all posts
Showing posts with label OSG. Show all posts

Thursday, January 12, 2012

Render-to-texture in OpenSceneGraph

 // Create a render-to-texture camera with an absolute
 // reference frame and pre-render
 osg::ref_ptr<osg::Camera> camera = new osg::Camera();
 camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
 camera->setRenderOrder(osg::Camera::PRE_RENDER);

 // Set the camera's projection matrix to reflect the
 // world of the geometries that will appear in the texture
 camera->setProjectionMatrixAsOrtho2D(minx, maxx, miny, maxy);

 //Set the camera's viewport to the same size of the texture
 camera->setViewport(0, 0, textureWidth, textureHeight);

 // Set the camera's clear color and masks
 camera->setClearColor(clearColor);
 camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 //Create the texture image
 osg::ref_ptr<osg::Image> image = new osg::Image();
 image->allocateImage(textureWidth,
                      textureHeight,
                      1,   // 2D texture is 1 pixel deep
                      GL_RGBA,
                      GL_UNSIGNED_BYTE);
 image->setInternalTextureFormat(GL_RGBA8);

 // Create the texture object and set the image
 osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D();
 texture2D->setImage(image);

 // Attach the texture to the camera. You can also attach
 // the image to the camera instead. Attaching the image may
 // work better in the Windows Remote Desktop
 camera->attach(osg::Camera::COLOR_BUFFER, texture2D.get());

 // Add the geometries that will appear in the texture to the camera
 camera->addChild(geometriesInTexture);

 // Create a geode to display the texture
 osg::ref_ptr<osg::Geode> geode = new osg::Geode();

 // Create a geometry for the geode
 osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
 geode->addDrawable(geometry);

 // The coordinates can be different if we want to display the
 // texture at a location different from the coordinates of the
 // geometries inside the texture
 osg::ref_ptr<osg::Vec3Array> vertexArray = new osg::Vec3Array();
 vertexArray->push_back(osg::Vec3(minx, miny, minz));
 vertexArray->push_back(osg::Vec3(maxx, miny, minz));
 vertexArray->push_back(osg::Vec3(maxx, maxy, minz));
 vertexArray->push_back(osg::Vec3(minx, maxy, minz));
 geometry->setVertexArray(vertexArray);

 // The geometry color for the texture mapping should be white
 // (1.f, 1.f, 1.f, 1.f) so that color blending won't affect
 // the texture real color
 osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array();
 colorArray->push_back(osg::Vec4(1.f, 1.f, 1.f, 1.f));
 geometry->setColorArray(colorArray);
 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);

 // We are using the entire texture by using the four corners
 // of the texture coordinates
 osg::ref_ptr<osg::Vec2Array> texCoordArray = new osg::Vec2Array();
 texCoordArray->push_back(osg::Vec2(0.f, 0.f));
 texCoordArray->push_back(osg::Vec2(1.f, 0.f));
 texCoordArray->push_back(osg::Vec2(1.f, 1.f));
 texCoordArray->push_back(osg::Vec2(0.f, 1.f));
 geometry->setTexCoordArray(0, texCoordArray);

 // We always create a square texture.
 geometry->addPrimitiveSet(
    new osg::DrawArrays(GL_TRIANGLE_FAN, 0, 4));

 // Add the texture to the geode
 geode->getOrCreateStateSet()->setTextureAttributeAndModes(
    0, texture2D, osg::StateAttribute::ON);

 // Make sure that we are using color blending
 // and the transparent bin, since the features may not
 // cover the entire texture and the empty
 // space should be transparent.
 geode->getOrCreateStateSet()->setRenderingHint(
    osg::StateSet::TRANSPARENT_BIN);
 osg::ref_ptr<osg::BlendFunc> blendFunc = new osg::BlendFunc;
 blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 geode->getOrCreateStateSet()->setAttributeAndModes(blendFunc);

 // Add both the rtt camera and the geode to the scene graph
 sceneGraph->addChild(camera);
 sceneGraph->addChild(geode);

* NOTE: If we only want to render one frame on the texture, we can remove the camera from the scene graph after the frame is rendered. The geode will still display the static texture in the scene graph properly.

Saturday, August 20, 2011

Building OpenSceneGraph for iOS [In Progress]

After spending a few hours on trying to build OpenSceneGraph 3.0.1 on Xcode 4.2 for iOS 4.3/5.0, I finally gave up ...

The README.txt in the official OSG package describes how to generate an Xcode project using CMake. I tried to use CMake 2.8.5. Following the cmake command line described in README.txt, I got an error about not able to build a test project. Then I tried CMake GUI, but it still didn't work.

I searched the web and found a modified version at git://github.com/stmh/osg.git. This time, CMake GUI generated an Xcode project. However, when I tried to open and build this project in XCode 4.2, I got the following error:

OpenSceneGraph-2.9.10/src/OpenThreads/pthreads/PThread.c++:1: error: -mmacosx-version-min not allowed with -miphoneos-version-min

Reference: http://forum.openscenegraph.org/viewtopic.php?t=7433

It seems that CMake always made a mistake in setting the Architectures, Base SDK, Supported Platforms, and Valid Architectures consistently. Then I created a new OpenGL ES game project in XCode and followed the settings:

Architectures: Standard (armv7)
Base SDK: Latest iOS (iOS 5.0)
Supported Platforms: iphonesimulator iphoneos
Valid Architectures: armv6 armv7 armv7f armv7k

I also made sure that i386 did not appear in the CMAKE_CXX_FLAGS since I was building OSG for iOS Device.

The error disappeared, but a lot of new errors showed up:

GL

'*' was not declared in this scope
'glLoadMatrixf' was not declared in this scope
...

It seems that XCode was not able to find the OpenGL ES library. The XCode project does not contain the OpenGLES.framework. Therefore, I created a new OpenGL ES project and added the OSG project to it. I also set the Header Search Paths to point to the root of the OpenSceneGraph package.

However, I still got the following error at the end:

'vector' file not found in lwo2chunks.h

It seems XCode cannot locate the standard library. I have no clue how that can happen.

============

I just found a post about how to minimize the size of the osg library for iphone:
http://forum.openscenegraph.org/viewtopic.php?t=7224

Summary:
we should build the library in arm 7 only for the device, since arm 7 is for 3GS and above. I think there is no point to build the library in arm 6 now, since iPhone 3G or earlier are relatively old now.

We also only need OpenThreads, osg, osgDB, osgGA, osgUtil and osgViewer. We can probably throw away all of the plugins.

We should also build the osg libs with Optimization set to Fastest-Smallest.

BTW, the same post also mentions that display lists are not available in OpenGL ES. Therefore, we should turn off OSG_GL_DISPLAYLISTS_AVAILABLE.

==========

I found another post that mentions Apple loosened the terms on their license agreement to allow for dynamic linking and plug-ins now:

http://forum.openscenegraph.org/viewtopic.php?t=6582

BTW, Thomas Hogarth is a person who successfully built OSG for iOS and also published an iOS game called Alient Attack.


Monday, June 6, 2011

osg::ClearNode clears at the start of rendering

From what I read from the mail archive below and what I tried, osg::ClearNode actually clears at the start of rendering and not just when it's reached in the draw traversal.

Reference:
http://www.mail-archive.com/osg-users@lists.openscenegraph.org/msg27321.html

Friday, February 25, 2011

Using an osg::ref_ptr for 'this' in the constructor is a bad idea

Inside a constructor, the reference count of 'this' is still zero. If you use a local osg::ref_ptr to reference 'this', the reference count will increment to one. However, when the constructor ends, the lifetime of the osg::ref_ptr also ends, and the reference count will become zero again, and 'this' will be destroyed.