Hi Oliver,
On Wed, 2005-08-24 at 19:42 +0200, Oliver Kutter wrote:
>
> I've tried to set up some 16 bit float images and textures, but it is
> not as easy as it seems. I tried the following (just for the test a 1x1
> pixel sized image):
>
> float array[] = {0.1f, 0.2f, 0.3f};
>
> beginEditCP(myImage);
> myImage->set(Image::OSG_RGB_PF, 1, 1, 1, 1, 1, 0, (UInt8*)array,
> Image::OSG_FLOAT32_IMAGEDATA, true, 1);
> endEditCP(myImage);
OK. The true and 1 are not necessary, but don't hurt.
> But when I want to check the three values, like this,
>
> cout << (float)myImage->getPixel(0) << endl;
> cout << (float)myImage->getPixel(1) << endl;
> cout << (float)myImage->getPixel(2) << endl;
Yeah, that doesn't work. The getPixel() will give you the nth byte, as
that is what's stored, it is not specific to the type. You need to cast
the pointer to get that:
float *fdata = (float*)myImage->getData();
cout << fdata[0] << endl;
cout << fdata[1] << endl;
cout << fdata[2] << endl;
> I'm not getting the values, that I stored before. Why?
> What happend to my values, when I cast my array ((UInt8*)array) to UInt8?
Nothing, you're just accessing the byte data, not the floats you expect.
> Or is something wrong with my code? And how can I access it in a shader?
> Something like this?
>
> vec3 myVec = vec3 (texture2D (myTexture, vec2 (gl_TexCoord[0])));
Yup.
> What kind of float values are stored in the myVec Vektor? Values from 0
> to 1 or from 0 to 32bit?
Full floats.
> >and tell the TextureChunk which internal format
> >you want (probably GL_RGB32F_ARB).
> >
> >
> And I tried to set up the texture:
>
> beginEditCP (_tcInPart);
> _tcInPart->setImage(myImage);
> _tcInPart->setInternalFormat(GL_RGB32F_ARB);
> endEditCP (_tcInPart);
>
> But the compiler don't know the GL_RGB32F_ARB. What else do I have to
> include and where do I get it from? I'm using a GeForce 6.
That should come from OpenSG/OSGOGLExt.h, which is included
automatically. You just need a reasonably current version of OpenSG, 1.4
doesn't have that yet.
See attached example, which divides the texture data by a factor between
10 and 30 before using it as a color to prove it's really values > 1
that are stored in the texture.
Hope it helps
Dirk
// OpenSG Tutorial Example: Shader
//
// This example shows how to use GLSL shaders.
// It creates a bouncing ball animation (completely calculated on the GPU).
// You need a graphics card supporting the GL_ARB_shading_language_100 extension
// to run this tutorial.
// Headers
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGBaseFunctions.h>
#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGGroup.h>
#include <OpenSG/OSGPointLight.h>
#include <OpenSG/OSGRenderAction.h>
#include <OpenSG/OSGSceneFileHandler.h>
#include <OpenSG/OSGSimpleMaterial.h>
#include <OpenSG/OSGImage.h>
#include <OpenSG/OSGTextureChunk.h>
#include <OpenSG/OSGGradientBackground.h>
#include <OpenSG/OSGSHLChunk.h>
// Activate the OpenSG namespace
OSG_USING_NAMESPACE
// The SimpleSceneManager to manage simple applications
SimpleSceneManager *_mgr = NULL;
NodePtr _scene = NullFC;
SHLChunkPtr _shl = NullFC;
// vertex shader program.
static std::string _vp_program =
"varying vec2 tc;\n"
"void main(void)\n"
"{\n"
" tc = gl_MultiTexCoord0.st;\n"
" gl_Position = ftransform();\n"
"}\n";
// fragment shader program for bump mapping in surface local coordinates
static std::string _fp_program =
"uniform float scale;\n"
"uniform sampler2D texture;\n"
"varying vec2 tc;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = texture2D(texture, tc) / scale;\n"
"}\n";
// forward declaration so we can have the interesting stuff upfront
int setupGLUT(int *argc, char *argv[]);
// redraw the window
void display(void)
{
Real32 t = glutGet(GLUT_ELAPSED_TIME);
beginEditCP(_shl, SHLChunk::ParametersFieldMask);
_shl->setUniformParameter("scale", (osgsin(t/1000.f)+2)*10);
endEditCP(_shl, SHLChunk::ParametersFieldMask);
_mgr->redraw();
}
// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
// OSG init
osgInit(argc,argv);
// GLUT init
int winid = setupGLUT(&argc, argv);
// create the scene
_scene = makeCoredNode<Group>();
// create bottom
NodePtr bottom = makePlane(50.0, 50.0, 2, 2);
Real32 imgdata[] =
{ 1.f, 0.f, 0.f,
0.0f, 10.f, 0.f,
0.f, 0.f, 30.f,
5.f, 5.f, 5.f
};
ImagePtr bottom_img = Image::create();
bottom_img->set(Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0,
reinterpret_cast<UInt8*>(imgdata),
Image::OSG_FLOAT32_IMAGEDATA);
float *fdata = (float*)bottom_img->getData();
std::cout << fdata[0] << std::endl;
std::cout << fdata[1] << std::endl;
std::cout << fdata[2] << std::endl;
TextureChunkPtr bottom_tex = TextureChunk::create();
beginEditCP(bottom_tex);
bottom_tex->setImage(bottom_img);
bottom_tex->setMinFilter(GL_NEAREST);
bottom_tex->setMagFilter(GL_NEAREST);
bottom_tex->setWrapS(GL_REPEAT);
bottom_tex->setWrapT(GL_REPEAT);
bottom_tex->setEnvMode(GL_MODULATE);
bottom_tex->setInternalFormat(GL_RGB16F_ARB);
endEditCP(bottom_tex);
// create the shader material
ChunkMaterialPtr cmat = ChunkMaterial::create();
_shl = SHLChunk::create();
beginEditCP(_shl);
_shl->setVertexProgram(_vp_program);
_shl->setFragmentProgram(_fp_program);
_shl->setUniformParameter("scale", 0.0f);
_shl->setUniformParameter("texture", 0);
endEditCP(_shl);
beginEditCP(cmat);
cmat->addChunk(bottom_tex);
cmat->addChunk(_shl);
endEditCP(cmat);
GeometryPtr bottom_geo = GeometryPtr::dcast(bottom->getCore());
beginEditCP(bottom_geo);
bottom_geo->setMaterial(cmat);
beginEditCP(bottom_geo);
beginEditCP(_scene);
_scene->addChild(bottom);
endEditCP(_scene);
// the connection between GLUT and OpenSG
GLUTWindowPtr gwin= GLUTWindow::create();
beginEditCP(gwin);//Window
gwin->setId(winid);
gwin->init();
endEditCP(gwin);
// create the SimpleSceneManager helper
_mgr = new SimpleSceneManager;
// tell the manager what to manage
_mgr->setWindow(gwin );
_mgr->setRoot (_scene);
_mgr->turnHeadlightOff();
// show the whole scene
_mgr->showAll();
_mgr->getNavigator()->setFrom(Pnt3f(0.0, 31, 47));
// GLUT main loop
glutMainLoop();
return 0;
}
//
// GLUT callback functions
//
// 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 x, int y)
{
switch(k)
{
case 27:
OSG::osgExit();
exit(0);
break;
case 'w':
SceneFileHandler::the().write(_scene, "scene.osb.gz", true);
printf("wrote scene.\n");
break;
}
}
// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
int winid = glutCreateWindow("OpenSG");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
// call the redraw function whenever there's nothing else to do
glutIdleFunc(display);
return winid;
}