From Mike Weiblen, support GLSL uniform arrays. A couple of tweaks and fixes from Robert Osfield.

This commit is contained in:
Robert Osfield
2006-05-15 15:46:08 +00:00
parent d42a8fd269
commit bf065ed3a4
7 changed files with 618 additions and 544 deletions

View File

@@ -10,8 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/* file: examples/osgshaders/GL2Scene.cpp
* author: Mike Weiblen 2005-05-01
/* file: examples/osgshaders/GL2Scene.cpp
* author: Mike Weiblen 2006-05-14
*
* Compose a scene of several instances of a model, with a different
* OpenGL Shading Language shader applied to each.
@@ -166,7 +166,7 @@ static const char *microshaderFragSource = {
static osg::ref_ptr<osg::Group> rootNode;
// Create some geometry upon which to render GL2 shaders.
// Create some geometry upon which to render GLSL shaders.
static osg::Geode*
CreateModel()
{
@@ -267,7 +267,7 @@ class AnimateCallback: public osg::Uniform::Callback
};
///////////////////////////////////////////////////////////////////////////
// Compose a scenegraph with examples of GL2 shaders
// Compose a scenegraph with examples of GLSL shaders
#define TEXUNIT_SINE 1
#define TEXUNIT_NOISE 2
@@ -280,7 +280,6 @@ GL2Scene::buildScene()
// the root of our scenegraph.
rootNode = new osg::Group;
//rootNode->setUpdateCallback( new AnimateCallback );
// attach some Uniforms to the root, to be inherited by Programs.
{
@@ -299,9 +298,6 @@ GL2Scene::buildScene()
ss->addUniform( SineUniform );
ss->addUniform( Color1Uniform );
ss->addUniform( Color2Uniform );
//ss->setUpdateCallback(new AnimateCallback2);
}
// the simple Microshader (its source appears earlier in this file)
@@ -362,6 +358,16 @@ GL2Scene::buildScene()
ss->addUniform( new osg::Uniform("NoiseTex", TEXUNIT_NOISE) );
ss->addUniform( new osg::Uniform("SineTex", TEXUNIT_SINE) );
osg::Uniform* MarbleColor = new osg::Uniform( osg::Uniform::FLOAT_VEC3, "MarbleColor[0]", 2 );
MarbleColor->setElement( 0, osg::Vec3( 0.7, 0.7, 0.7 ) );
MarbleColor->setElement( 1, osg::Vec3( 0.2, 0.2, 0.2 ) );
ss->addUniform( MarbleColor );
osg::Uniform* VeinColor = new osg::Uniform( osg::Uniform::FLOAT_VEC3, "VeinColor[0]", 2 );
VeinColor->setElement( 0, osg::Vec3( 0.0, 0.15, 0.0 ) );
VeinColor->setElement( 1, osg::Vec3( 0.9, 0.95, 0.9 ) );
ss->addUniform( VeinColor );
}
#ifdef INTERNAL_3DLABS //[

View File

@@ -281,7 +281,7 @@ class OSG_EXPORT StateSet : public Object
Uniform* getUniform(const std::string& name);
/** Get Uniform for specified name, if one is not available create it, add it to this StateSet and return a pointer to it.*/
Uniform* getOrCreateUniform(const std::string& name, Uniform::Type type);
Uniform* getOrCreateUniform(const std::string& name, Uniform::Type type, int numElements=1);
/** Get const Uniform for specified name.
* Returns NULL if no matching Uniform is contained within StateSet.*/

View File

@@ -1,4 +1,4 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
*
* This application is open source and may be redistributed and/or modified
@@ -11,13 +11,14 @@
*/
/* file: include/osg/Uniform
* author: Mike Weiblen 2005-06-03
* author: Mike Weiblen 2006-05-14
*/
#ifndef OSG_UNIFORM
#define OSG_UNIFORM 1
#include <osg/ref_ptr>
#include <osg/Array>
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
@@ -169,7 +170,7 @@ class OSG_EXPORT Uniform : public Object
public:
Uniform();
Uniform( Type type, const std::string& name );
Uniform( Type type, const std::string& name, int numElements=1 );
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Uniform(const Uniform& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
@@ -177,27 +178,41 @@ class OSG_EXPORT Uniform : public Object
META_Object(osg, Uniform);
/** Set the type of glUniform. */
/** Set the type of glUniform, ensuring it is only set once.*/
bool setType( Type t );
/** Get the type of glUniform as enum. */
const Type getType() const { return _type; }
/** Set the name of the glUniform, checking to make sure its only set once.*/
/** Set the name of the glUniform, ensuring it is only set once.*/
void setName( const std::string& name );
/** Set the length of a uniform array, ensuring it is only set once.*/
void setNumElements( unsigned int numElements );
/** Get the number of GLSL elements of the osg::Uniform (>1 == array) */
unsigned int getNumElements() const { return _numElements; }
/** Get the number of elements required for the internal scalar data array.
* Returns 0 if the osg::Uniform is not properly configured. */
unsigned int getInternalArrayNumElements() const;
/** Return the name of a Type enum as string. */
static const char* getTypename( Type t );
/** Return the the number of scalar components for a GLSL type. */
static int getTypeNumComponents( Type t );
/** Return the Type enum of a Uniform typename string */
static Uniform::Type getTypeId( const std::string& tname );
/** Return the GL API type corresponding to a GLSL type */
static Type getGlApiType( Type t );
/** convenient construction w/ assignment */
/** Return the internal scalar data array type corresponding to a GLSL type */
static GLenum getInternalArrayType( Type t );
/** convenient single-element constructors w/ assignment */
explicit Uniform( const char* name, float f );
explicit Uniform( const char* name, int i );
explicit Uniform( const char* name, bool b );
@@ -251,7 +266,7 @@ class OSG_EXPORT Uniform : public Object
inline unsigned int getNumParents() const { return _parents.size(); }
/** value assignment */
/** convenient single-element value assignment; may only be used w/ Uniforms where isScalar()==TRUE */
bool set( float f );
bool set( int i );
bool set( bool b );
@@ -269,7 +284,7 @@ class OSG_EXPORT Uniform : public Object
bool set( bool b0, bool b1, bool b2 );
bool set( bool b0, bool b1, bool b2, bool b3 );
/** value query */
/** convenient single-element value query; may only be used w/ Uniforms where isScalar()==TRUE */
bool get( float& f ) const;
bool get( int& i ) const;
bool get( bool& b ) const;
@@ -286,7 +301,13 @@ class OSG_EXPORT Uniform : public Object
bool get( bool& b0, bool& b1 ) const;
bool get( bool& b0, bool& b1, bool& b2 ) const;
bool get( bool& b0, bool& b1, bool& b2, bool& b3 ) const;
/** value assignment for array uniforms */
bool setElement( unsigned int index, float f );
bool setElement( unsigned int index, const osg::Vec2& v2 );
bool setElement( unsigned int index, const osg::Vec3& v3 );
bool setElement( unsigned int index, const osg::Vec4& v4 );
struct Callback : public virtual osg::Object
{
@@ -319,10 +340,20 @@ class OSG_EXPORT Uniform : public Object
/** Get the const EventCallback.*/
const Callback* getEventCallback() const { return _eventCallback.get(); }
/** Increment the modified count on the Uniform so ProgramObjects watching it know it update themselves.
* note, autoatmatically call bet Uniform::set(*). */
/** Increment the modified count on the Uniform so Programs watching it know it update themselves.
* note: autotomatically called by Uniform::set(*);
* you must call if modify a data array directly. */
inline void dirty() { ++_modifiedCount; }
bool setArray( FloatArray* array );
bool setArray( IntArray* array );
FloatArray* getFloatArray() { return _floatArray.get(); }
const FloatArray* getFloatArray() const { return _floatArray.get(); }
IntArray* getIntArray() { return _intArray.get(); }
const IntArray* getIntArray() const { return _intArray.get(); }
inline void setModifiedCount(unsigned int mc) { _modifiedCount = mc; }
inline unsigned int getModifiedCount() const { return _modifiedCount; }
@@ -336,6 +367,8 @@ class OSG_EXPORT Uniform : public Object
Uniform& operator=(const Uniform&) { return *this; }
bool isCompatibleType( Type t ) const;
bool isScalar() const { return _numElements==1; }
void allocateDataArray();
void addParent(osg::StateSet* object);
void removeParent(osg::StateSet* object);
@@ -343,27 +376,18 @@ class OSG_EXPORT Uniform : public Object
ParentList _parents;
friend class osg::StateSet;
Type _type;
Type _type;
unsigned int _numElements;
union
{
GLfloat f1; // float
GLfloat f2[2]; // vec2
GLfloat f3[3]; // vec3
GLfloat f4[4]; // vec4, mat2
GLfloat f9[9]; // mat3
GLfloat f16[16]; // mat4
GLint i1; // int, bool, sampler*
GLint i2[2]; // ivec2, bvec2
GLint i3[3]; // ivec3, bvec3
GLint i4[4]; // ivec4, bvec4
} _data;
// The internal data for osg::Uniforms are stored as an array of
// getInternalArrayType() of length getInternalArrayNumElements().
ref_ptr<FloatArray> _floatArray;
ref_ptr<IntArray> _intArray;
ref_ptr<Callback> _updateCallback;
ref_ptr<Callback> _eventCallback;
unsigned int _modifiedCount;
};
}

View File

@@ -924,7 +924,7 @@ Uniform* StateSet::getUniform(const std::string& name)
else return 0;
}
Uniform* StateSet::getOrCreateUniform(const std::string& name, Uniform::Type type)
Uniform* StateSet::getOrCreateUniform(const std::string& name, Uniform::Type type, int numElements)
{
// for look for an appropriate uniform.
UniformList::iterator itr = _uniformList.find(name);
@@ -936,7 +936,7 @@ Uniform* StateSet::getOrCreateUniform(const std::string& name, Uniform::Type typ
// no uniform found matching name so create it..
Uniform* uniform = new Uniform(type,name);
Uniform* uniform = new Uniform(type,name,numElements);
addUniform(uniform);
return uniform;

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,6 @@
// Mike Weiblen 2006-05-14
#include "osg/Uniform"
#include "osg/io_utils"
#include "osg/Notify"
@@ -12,6 +15,10 @@ using namespace osg;
using namespace osgDB;
using namespace std;
// reuse from Geometry.cpp
bool Array_writeLocalData(const Array& array,Output& fw);
Array* Array_readLocalData(Input& fr);
// forward declare functions to use later.
bool Uniform_readLocalData(Object& obj, Input& fr);
bool Uniform_writeLocalData(const Object& obj, Output& fw);
@@ -30,247 +37,178 @@ RegisterDotOsgWrapperProxy g_UniformProxy
bool Uniform_readLocalData(Object& obj, Input& fr)
{
bool iteratorAdvanced = false;
Uniform& uniform = static_cast<Uniform&>(obj);
if (fr[0].isWord())
if (fr[0].matchWord("type"))
{
// post-May 2006 format (OSG versions > 1.0)
uniform.setType( Uniform::getTypeId( fr[1].getStr() ) );
unsigned int numElements;
fr[2].getUInt(numElements);
uniform.setNumElements( numElements );
fr+=3;
iteratorAdvanced = true;
Array* data = Array_readLocalData(fr);
uniform.setArray( dynamic_cast<FloatArray*>(data) );
uniform.setArray( dynamic_cast<IntArray*>(data) );
}
#if 1 //[
// Deprecated; for backwards compatibility only.
// when can we safely delete this code, I wonder...
else
{
// pre-May 2006 format (OSG versions <= 1.0)
uniform.setType( Uniform::getTypeId(fr[0].getStr()) );
fr+=1;
iteratorAdvanced = true;
}
switch( Uniform::getGlApiType(uniform.getType()) )
{
case(osg::Uniform::FLOAT):
switch( Uniform::getGlApiType(uniform.getType()) )
{
float value;
if (fr[0].getFloat(value))
case(osg::Uniform::FLOAT):
{
uniform.set(value);
fr+=1;
iteratorAdvanced = true;
float value;
if (fr[0].getFloat(value))
{
uniform.set(value);
fr+=1;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::FLOAT_VEC2):
{
osg::Vec2 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]))
case(osg::Uniform::FLOAT_VEC2):
{
uniform.set(value);
fr+=2;
iteratorAdvanced = true;
osg::Vec2 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]))
{
uniform.set(value);
fr+=2;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::FLOAT_VEC3):
{
osg::Vec3 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]))
case(osg::Uniform::FLOAT_VEC3):
{
uniform.set(value);
fr+=3;
iteratorAdvanced = true;
osg::Vec3 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]))
{
uniform.set(value);
fr+=3;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::FLOAT_VEC4):
{
osg::Vec4 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) && fr[3].getFloat(value[3]))
case(osg::Uniform::FLOAT_VEC4):
{
uniform.set(value);
fr+=4;
iteratorAdvanced = true;
osg::Vec4 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) && fr[3].getFloat(value[3]))
{
uniform.set(value);
fr+=4;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::INT):
{
int value;
if (fr[0].getInt(value))
case(osg::Uniform::INT):
{
uniform.set(value);
fr+=1;
iteratorAdvanced = true;
int value;
if (fr[0].getInt(value))
{
uniform.set(value);
fr+=1;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::INT_VEC2):
{
int value[2];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]))
case(osg::Uniform::INT_VEC2):
{
uniform.set(value[0],value[1]);
fr+=2;
iteratorAdvanced = true;
int value[2];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]))
{
uniform.set(value[0],value[1]);
fr+=2;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::INT_VEC3):
{
int value[3];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]))
case(osg::Uniform::INT_VEC3):
{
uniform.set(value[0],value[1],value[2]);
fr+=3;
iteratorAdvanced = true;
int value[3];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]))
{
uniform.set(value[0],value[1],value[2]);
fr+=3;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::INT_VEC4):
{
int value[4];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]) && fr[3].getInt(value[3]))
case(osg::Uniform::INT_VEC4):
{
uniform.set(value[0],value[1],value[2],value[3]);
fr+=4;
iteratorAdvanced = true;
int value[4];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]) && fr[3].getInt(value[3]))
{
uniform.set(value[0],value[1],value[2],value[3]);
fr+=4;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::FLOAT_MAT2):
{
osg::Matrix2 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) &&
fr[2].getFloat(value[2]) && fr[3].getFloat(value[3]))
case(osg::Uniform::FLOAT_MAT2):
{
uniform.set(value);
fr+=4;
iteratorAdvanced = true;
osg::Matrix2 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) &&
fr[2].getFloat(value[2]) && fr[3].getFloat(value[3]))
{
uniform.set(value);
fr+=4;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::FLOAT_MAT3):
{
osg::Matrix3 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) &&
fr[3].getFloat(value[3]) && fr[4].getFloat(value[4]) && fr[5].getFloat(value[5]) &&
fr[6].getFloat(value[6]) && fr[7].getFloat(value[7]) && fr[8].getFloat(value[8]))
case(osg::Uniform::FLOAT_MAT3):
{
uniform.set(value);
fr+=9;
iteratorAdvanced = true;
osg::Matrix3 value;
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) &&
fr[3].getFloat(value[3]) && fr[4].getFloat(value[4]) && fr[5].getFloat(value[5]) &&
fr[6].getFloat(value[6]) && fr[7].getFloat(value[7]) && fr[8].getFloat(value[8]))
{
uniform.set(value);
fr+=9;
iteratorAdvanced = true;
}
break;
}
break;
}
case(osg::Uniform::FLOAT_MAT4):
{
Matrix value;
if( readMatrix(value,fr) )
case(osg::Uniform::FLOAT_MAT4):
{
uniform.set(value);
iteratorAdvanced = true;
Matrix value;
if( readMatrix(value,fr) )
{
uniform.set(value);
iteratorAdvanced = true;
}
break;
}
break;
}
default:
{
break;
default:
break;
}
}
#endif //]
return iteratorAdvanced;
}
bool Uniform_writeLocalData(const Object& obj,Output& fw)
{
const Uniform& uniform = static_cast<const Uniform&>(obj);
fw.indent() << Uniform::getTypename( uniform.getType() ) << " ";
switch( Uniform::getGlApiType(uniform.getType()) )
{
case(osg::Uniform::FLOAT):
{
float value = 0.0f;
uniform.get(value);
fw << value;
break;
}
case(osg::Uniform::FLOAT_VEC2):
{
Vec2 value;
uniform.get(value);
fw << value;
break;
}
case(osg::Uniform::FLOAT_VEC3):
{
Vec3 value;
uniform.get(value);
fw << value;
break;
}
case(osg::Uniform::FLOAT_VEC4):
{
Vec4 value;
uniform.get(value);
fw << value;
break;
}
case(osg::Uniform::INT):
{
int value = 0;
uniform.get(value);
fw << value;
break;
}
case(osg::Uniform::INT_VEC2):
{
int value[2];
uniform.get(value[0],value[1]);
fw << value[0]<<" "<<value[1];
break;
}
case(osg::Uniform::INT_VEC3):
{
int value[3];
uniform.get(value[0],value[1],value[2]);
fw << value[0]<<" "<<value[1]<<" "<<value[2];
break;
}
case(osg::Uniform::INT_VEC4):
{
int value[4];
uniform.get(value[0],value[1],value[2],value[3]);
fw << value[0]<<" "<<value[1]<<" "<<value[2]<<" "<<value[3];
break;
}
case(osg::Uniform::FLOAT_MAT2):
{
osg::Matrix2 value;
uniform.get(value);
fw << value[0]<<" "<<value[1]<<" "
<<value[2]<<" "<<value[3];
break;
}
case(osg::Uniform::FLOAT_MAT3):
{
osg::Matrix3 value;
uniform.get(value);
fw << value[0]<<" "<<value[1]<<" "<<value[2]<<" "
<<value[3]<<" "<<value[4]<<" "<<value[5]<<" "
<<value[6]<<" "<<value[7]<<" "<<value[8];
break;
}
case(osg::Uniform::FLOAT_MAT4):
{
Matrix value;
uniform.get(value);
writeMatrix(value,fw);
break;
}
default:
{
break;
}
}
fw << std::endl;
// post-May 2006 format (OSG versions > 1.0)
fw.indent() << "type "
<< Uniform::getTypename( uniform.getType() ) << " "
<< uniform.getNumElements() << " ";
if( uniform.getFloatArray() ) Array_writeLocalData( *uniform.getFloatArray(), fw );
if( uniform.getIntArray() ) Array_writeLocalData( *uniform.getIntArray(), fw );
return true;
}

View File

@@ -923,8 +923,7 @@ ViewerEventHandler::ViewerEventHandler(OsgCameraGroup* cg):
Viewer* viewer = dynamic_cast<Viewer*>(cg);
if (viewer) setWriteImageFileName(viewer->getWriteImageFileName());
else setWriteImageFileName("saved_image.jpg");
else setWriteImageFileName( Viewer::getDefaultImageFileName() );
}
void ViewerEventHandler::setWriteImageOnNextFrame(bool writeImageOnNextFrame)