Added new ARB vertex program state attribute and demo program,

sent in my Brede Johansen.
This commit is contained in:
Robert Osfield
2003-01-04 20:45:53 +00:00
parent 060975a05c
commit 3ef3666d0f
15 changed files with 661 additions and 46 deletions

View File

@@ -0,0 +1,15 @@
TOPDIR = ../../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgvertexprogram.cpp\
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
Makefile.inst=Makefile
EXEC = osgvertexprogram
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,11 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgvertexprogram.cpp\
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgvertexprogram
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,188 @@
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/Vec3>
#include <osg/VertexProgram>
#include <osgGA/TrackballManipulator>
#include <osgGLUT/Viewer>
#include <osgGLUT/glut>
// A simple ambient, specular, and diffuse infinite lighting computation
// with a single light and an eye-space normal
// This vertex program is a slightly modified version of an example in
// the ARB_vertex_program spec. It uses local parameter 0 for ambient.
// If your cube have a blue ambient component it shows the vertex-
// program is working.
const char vpstr[] =
"!!ARBvp1.0 \n"
"ATTRIB iPos = vertex.position; \n"
"ATTRIB iNormal = vertex.normal; \n"
"PARAM mvinv[4] = { state.matrix.modelview.invtrans }; \n"
"PARAM mvp[4] = { state.matrix.mvp }; \n"
"PARAM lightDir = state.light[0].position; \n"
"PARAM halfDir = state.light[0].half; \n"
"PARAM specExp = state.material.shininess; \n"
"PARAM ambientCol = program.local[0]; \n"
"PARAM diffuseCol = state.lightprod[0].diffuse; \n"
"PARAM specularCol = state.lightprod[0].specular; \n"
"TEMP xfNormal, temp, dots, newDiffuse; \n"
"OUTPUT oPos = result.position; \n"
"OUTPUT oColor = result.color; \n"
" \n"
"# Transform the vertex to clip coordinates. \n"
"DP4 oPos.x, mvp[0], iPos; \n"
"DP4 oPos.y, mvp[1], iPos; \n"
"DP4 oPos.z, mvp[2], iPos; \n"
"DP4 oPos.w, mvp[3], iPos; \n"
" \n"
"# Transform the normal to eye coordinates. \n"
"DP3 xfNormal.x, mvinv[0], iNormal; \n"
"DP3 xfNormal.y, mvinv[1], iNormal; \n"
"DP3 xfNormal.z, mvinv[2], iNormal; \n"
" \n"
"# Compute diffuse and specular dot products and use LIT to compute \n"
"# lighting coefficients. \n"
"DP3 dots.x, xfNormal, lightDir; \n"
"DP3 dots.y, xfNormal, halfDir; \n"
"MOV dots.w, specExp.x; \n"
"LIT dots, dots; \n"
" \n"
"# Accumulate color contributions. \n"
"MAD temp, dots.y, diffuseCol, ambientCol; \n"
"MAD oColor.xyz, dots.z, specularCol, temp; \n"
"MOV oColor.w, diffuseCol.w; \n"
"END \n";
osg::Geode* createGeometryCube()
{
osg::Geode* geode = new osg::Geode();
// -------------------------------------------
// Set up a new Geometry which will be our cube
// -------------------------------------------
osg::Geometry* cube = new osg::Geometry();
// set up the primitives
cube->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,4));
cube->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,4,4));
cube->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,8,4));
cube->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,12,4));
cube->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,16,4));
cube->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,20,4));
// set up coords.
osg::Vec3Array* coords = new osg::Vec3Array;
coords->resize(24);
(*coords)[0].set( -1.0000f, 1.0000f, -1.000f );
(*coords)[1].set( 1.0000f, 1.0000f, -1.0000f );
(*coords)[2].set( 1.0000f, -1.0000f, -1.0000f );
(*coords)[3].set( -1.0000f, -1.0000f, -1.000 );
(*coords)[4].set( 1.0000f, 1.0000f, -1.0000f );
(*coords)[5].set( 1.0000f, 1.0000f, 1.0000f );
(*coords)[6].set( 1.0000f, -1.0000f, 1.0000f );
(*coords)[7].set( 1.0000f, -1.0000f, -1.0000f );
(*coords)[8].set( 1.0000f, 1.0000f, 1.0000f );
(*coords)[9].set( -1.0000f, 1.0000f, 1.000f );
(*coords)[10].set( -1.0000f, -1.0000f, 1.000f );
(*coords)[11].set( 1.0000f, -1.0000f, 1.0000f );
(*coords)[12].set( -1.0000f, 1.0000f, 1.000 );
(*coords)[13].set( -1.0000f, 1.0000f, -1.000 );
(*coords)[14].set( -1.0000f, -1.0000f, -1.000 );
(*coords)[15].set( -1.0000f, -1.0000f, 1.000 );
(*coords)[16].set( -1.0000f, 1.0000f, 1.000 );
(*coords)[17].set( 1.0000f, 1.0000f, 1.0000f );
(*coords)[18].set( 1.0000f, 1.0000f, -1.0000f );
(*coords)[19].set( -1.0000f, 1.0000f, -1.000f );
(*coords)[20].set( -1.0000f, -1.0000f, 1.000f );
(*coords)[21].set( -1.0000f, -1.0000f, -1.000f );
(*coords)[22].set( 1.0000f, -1.0000f, -1.0000f );
(*coords)[23].set( 1.0000f, -1.0000f, 1.0000f );
cube->setVertexArray( coords );
// set up the normals.
osg::Vec3Array* cubeNormals = new osg::Vec3Array;
cubeNormals->resize(6);
(*cubeNormals)[0].set(0.0f,0.0f,-1.0f);
(*cubeNormals)[1].set(1.0f,0.0f,0.0f);
(*cubeNormals)[2].set(0.0f,0.0f,1.0f);
(*cubeNormals)[3].set(-1.0f,0.0f,0.0f);
(*cubeNormals)[4].set(0.0f,1.0f,0.0f);
(*cubeNormals)[5].set(0.0f,-1.0f,0.0f);
cube->setNormalArray( cubeNormals );
cube->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
// -------------------------
// make diffuse material red
// -------------------------
osg::StateSet* cubeState = new osg::StateSet();
osg::Material* redMaterial = new osg::Material();
const osg::Vec4 red( 1.0f, 0.0f, 0.0f, 1.0f );
redMaterial->setAmbient( osg::Material::FRONT_AND_BACK, red );
redMaterial->setDiffuse( osg::Material::FRONT_AND_BACK, red );
cubeState->setAttribute( redMaterial );
// ---------------------------------------------------
// Use vp local parameter 0 for ambient product (blue)
// ---------------------------------------------------
osg::VertexProgram* vp = new osg::VertexProgram();
vp->setVertexProgram( vpstr );
const osg::Vec4 blue( 0.0f, 0.0f, 1.0f, 1.0f );
vp->setProgramLocalParameter( 0, blue );
cubeState->setAttributeAndModes( vp, osg::StateAttribute::ON );
cube->setStateSet( cubeState );
geode->addDrawable( cube );
return geode;
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
// create the commandline args.
std::vector<std::string> commandLine;
for(int i=1;i<argc;++i) commandLine.push_back(argv[i]);
// create the viewer and the model to it.
osgGLUT::Viewer viewer;
viewer.setWindowTitle(argv[0]);
// configure the viewer from the commandline arguments, and eat any
// parameters that have been matched.
viewer.readCommandLine(commandLine);
// add model to viewer.
viewer.addViewport( createGeometryCube() );
// register trackball maniupulators.
viewer.registerCameraManipulator(new osgGA::TrackballManipulator);
viewer.open();
viewer.run();
return 0;
}

View File

@@ -160,22 +160,23 @@ class DrawFogCoord : public osg::ConstValueVisitor
{
public:
DrawFogCoord(const FloatArray* fogcoords,const IndexArray* indices,
FogCoordProc fogCoordProc):
DrawFogCoord(const Array* fogcoords,const IndexArray* indices,FogCoordProc fogCoordProc):
_fogcoords(fogcoords),
_indices(indices),
_glFogCoord1fv(fogCoordProc) {}
void operator () (unsigned int pos)
{
if (_indices) _glFogCoord1fv(&(*_fogcoords)[_indices->index(pos)]);
else _glFogCoord1fv(&(*_fogcoords)[pos]);
if (_indices) _fogcoords->accept(_indices->index(pos),*this);
else _fogcoords->accept(pos,*this);
}
const FloatArray* _fogcoords;
virtual void apply(const GLfloat& v) { _glFogCoord1fv(&v); }
const Array* _fogcoords;
const IndexArray* _indices;
FogCoordProc _glFogCoord1fv;
FogCoordProc _glFogCoord1fv;
};

View File

@@ -87,6 +87,7 @@ CXXFILES =\
UnitTestFramework.cpp\
Version.cpp\
Vec3.cpp\
VertexProgram.cpp\
Viewport.cpp\
DEF += -DSG_LIBRARY

100
src/osg/VertexProgram.cpp Normal file
View File

@@ -0,0 +1,100 @@
#include <osg/Notify>
#include <osg/GLExtensions>
#include <osg/VertexProgram>
using namespace osg;
VertexProgram::VertexProgram() :
_vertexProgramId(0)
{
}
VertexProgram::VertexProgram(const VertexProgram& vp,const CopyOp& copyop):
osg::StateAttribute(vp,copyop),
_vertexProgramId(vp._vertexProgramId)
{}
// virtual
VertexProgram::~VertexProgram()
{}
typedef void (APIENTRY * BindProgramProc) (GLenum target, GLuint id);
typedef void (APIENTRY * GenProgramsProc) (GLsizei n, GLuint *programs);
typedef void (APIENTRY * ProgramStringProc) (GLenum target, GLenum format, GLsizei len, const void *string);
typedef void (APIENTRY * ProgramLocalParameter4fvProc) (GLenum target, GLuint index, const GLfloat *params);
void VertexProgram::apply(State& state) const
{
static bool supported = osg::isGLExtensionSupported("GL_ARB_vertex_program");
if (!supported) return;
static BindProgramProc s_glBindProgram =
(BindProgramProc)osg::getGLExtensionFuncPtr("glBindProgramARB");
static GenProgramsProc s_glGenPrograms =
(GenProgramsProc)osg::getGLExtensionFuncPtr("glGenProgramsARB");
static ProgramStringProc s_glProgramString =
(ProgramStringProc)osg::getGLExtensionFuncPtr("glProgramStringARB");
static ProgramLocalParameter4fvProc s_glProgramLocalParameter4fv =
(ProgramLocalParameter4fvProc)osg::getGLExtensionFuncPtr("glProgramLocalParameter4fvARB");
// Vertex Program
if (_vertexProgramId != 0)
{
s_glBindProgram( GL_VERTEX_PROGRAM_ARB, _vertexProgramId );
}
else if (!_vertexProgram.empty())
{
::glGetError(); // Reset Error flags.
s_glGenPrograms( 1, &_vertexProgramId );
s_glBindProgram( GL_VERTEX_PROGRAM_ARB, _vertexProgramId );
s_glProgramString( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
_vertexProgram.length(), _vertexProgram.c_str());
// Check for errors
int errorposition;
::glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorposition);
if (errorposition != -1)
{
notify(osg::FATAL) << "VertexProgram: " << ::glGetString(GL_PROGRAM_ERROR_STRING_ARB) << std::endl;
std::string::size_type start = _vertexProgram.rfind('\n', errorposition);
std::string::size_type stop = _vertexProgram.find('\n', errorposition);
if (start!=std::string::npos && stop!=std::string::npos)
{
notify(osg::FATAL) << " : " << _vertexProgram.substr(start+1, stop-start-2) << std::endl;
std::string pointAtproblem(errorposition-(start+1), ' ');
notify(osg::FATAL) << " : " << pointAtproblem << '^' << std::endl;
}
exit(1);
}
}
// Update local program parameters
{
for(LocalParamList::const_iterator itr=_programLocalParameters.begin();
itr!=_programLocalParameters.end();
++itr)
{
s_glProgramLocalParameter4fv(GL_VERTEX_PROGRAM_ARB, (*itr).first, (*itr).second.ptr());
}
}
// Update matrix
if (!_matrixList.empty())
{
for(MatrixList::const_iterator itr = _matrixList.begin();
itr!=_matrixList.end();
++itr)
{
::glMatrixMode((*itr).first);
::glLoadMatrixf((*itr).second->ptr());
}
::glMatrixMode(GL_MODELVIEW); // restore matrix mode
}
}

View File

@@ -230,36 +230,15 @@ bool Geometry_readLocalData(Object& obj, Input& fr)
iteratorAdvanced = true;
}
if (fr.matchSequence("FogCoordArray %i {"))
if (fr.matchSequence("FogCoordArray %w %i {"))
{
int entry = fr[0].getNoNestedBrackets();
int capacity;
fr[1].getInt(capacity);
FloatArray* fogcoords = new FloatArray;
fogcoords->reserve(capacity);
fr += 3;
while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
{
float fc;
if (fr[0].getFloat(fc))
{
++fr;
fogcoords->push_back(fc);
}
else
{
++fr;
}
}
geom.setFogCoordArray(fogcoords);
iteratorAdvanced = true;
++fr;
Array* fogcoords = Array_readLocalData(fr);
if (fogcoords)
{
geom.setFogCoordArray(fogcoords);
iteratorAdvanced = true;
}
}
if (fr.matchSequence("FogCoordIndices %w %i {"))
@@ -963,12 +942,8 @@ bool Geometry_writeLocalData(const Object& obj, Output& fw)
if (geom.getFogCoordArray())
{
fw.indent()<<"FogCoordBinding "<<Geometry_getBindingTypeStr(geom.getFogCoordBinding())<<std::endl;
const FloatArray& fogcoords = *geom.getFogCoordArray();
fw.indent()<<"FogCoordArray "<<fogcoords.size()<<std::endl;
Array_writeLocalData(fw,fogcoords.begin(),fogcoords.end());
fw.indent()<<"FogCoordArray ";
Array_writeLocalData(*geom.getFogCoordArray(),fw);
}
if (geom.getFogCoordIndices())
{