Ported osg::Geometry across to supporting the aliasing of vertex, color and normal etc. calls to Vertex Attributes.

Added support for automatic aliasing of vertex, normal, color etc. arrays to Vertex Attribute equivelants.

Added new osg::GLBeginEndAdapter class for runtime conversion from glBegin/glEnd codes to vertex arrray equivelants.

Added automatic shader source conversion from gl_ to osg_ builtins.
This commit is contained in:
Robert Osfield
2009-10-16 16:26:27 +00:00
parent 9e2567cb88
commit aefd1513f4
14 changed files with 1479 additions and 537 deletions

View File

@@ -123,7 +123,7 @@ void GLBufferObject::compileBuffer()
entry.dataSource != bd ||
entry.dataSize != bd->getTotalDataSize())
{
GLsizeiptrARB previousEndOfBufferDataMarker = GLsizeiptrARB(entry.offset) + entry.dataSize;
unsigned int previousEndOfBufferDataMarker = entry.offset + entry.dataSize;
// osg::notify(osg::NOTICE)<<"GLBufferObject::compileBuffer(..) updating BufferEntry"<<std::endl;

View File

@@ -74,6 +74,7 @@ SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/GL
${HEADER_PATH}/GL2Extensions
${HEADER_PATH}/GLExtensions
${HEADER_PATH}/GLBeginEndAdapter
${HEADER_PATH}/GLObjects
${HEADER_PATH}/GLU
${HEADER_PATH}/GraphicsContext
@@ -236,6 +237,7 @@ ADD_LIBRARY(${LIB_NAME}
Geometry.cpp
GL2Extensions.cpp
GLExtensions.cpp
GLBeginEndAdapter.cpp
GLObjects.cpp
GraphicsContext.cpp
GraphicsThread.cpp

View File

@@ -0,0 +1,224 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/GLBeginEndAdapter>
#include <osg/State>
#include <osg/Notify>
#include <osg/io_utils>
using namespace osg;
GLBeginEndAdapter::GLBeginEndAdapter(State* state):
_state(state),
_mode(APPLY_LOCAL_MATRICES_TO_VERTICES),
_normalSet(false),
_normal(0.0f,0.0f,1.0f),
_colorSet(false),
_color(1.0f,1.0f,1.0f,1.0f),
_maxNumTexCoordComponents(0),
_texCoord(0.f,0.0f,0.0f,1.0f)
{
}
void GLBeginEndAdapter::PushMatrix()
{
if (_matrixStack.empty())
{
if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
else _matrixStack.push_back(_state->getModelViewMatrix());
}
else _matrixStack.push_back(_matrixStack.back());
}
void GLBeginEndAdapter::PopMatrix()
{
if (!_matrixStack.empty()) _matrixStack.pop_back();
}
void GLBeginEndAdapter::LoadIdentity()
{
if (_matrixStack.empty()) _matrixStack.push_back(Matrixd::identity());
else _matrixStack.back().makeIdentity();
}
void GLBeginEndAdapter::LoadMatrixd(const GLdouble* m)
{
if (_matrixStack.empty()) _matrixStack.push_back(Matrixd(m));
else _matrixStack.back().set(m);
}
void GLBeginEndAdapter::MultMatrixd(const GLdouble* m)
{
if (_matrixStack.empty())
{
if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
else _matrixStack.push_back(_state->getModelViewMatrix());
}
_matrixStack.back().preMult(Matrixd(m));
}
void GLBeginEndAdapter::Translated(GLdouble x, GLdouble y, GLdouble z)
{
if (_matrixStack.empty())
{
if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
else _matrixStack.push_back(_state->getModelViewMatrix());
}
_matrixStack.back().preMultTranslate(Vec3d(x,y,z));
}
void GLBeginEndAdapter::Scaled(GLdouble x, GLdouble y, GLdouble z)
{
if (_matrixStack.empty())
{
if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
else _matrixStack.push_back(_state->getModelViewMatrix());
}
_matrixStack.back().preMultScale(Vec3d(x,y,z));
}
void GLBeginEndAdapter::Rotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
{
if (_matrixStack.empty())
{
if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
else _matrixStack.push_back(_state->getModelViewMatrix());
}
_matrixStack.back().preMultRotate(Quat(DegreesToRadians(angle), Vec3d(x,y,z)));
}
void GLBeginEndAdapter::Color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
{
_normalSet = true;
_color.set(red,green,blue,alpha);
}
void GLBeginEndAdapter::Normal3f(GLfloat x, GLfloat y, GLfloat z)
{
_normalSet = true;
_normal.set(x,y,z);
}
void GLBeginEndAdapter::TexCoord1f(GLfloat x)
{
_maxNumTexCoordComponents = 1;
_texCoord.set(x,0.0f,0.0f,1.0f);
}
void GLBeginEndAdapter::TexCoord2f(GLfloat x, GLfloat y)
{
_maxNumTexCoordComponents = 2;
_texCoord.set(x,y,0.0f,1.0f);
}
void GLBeginEndAdapter::TexCoord3f(GLfloat x, GLfloat y, GLfloat z)
{
_maxNumTexCoordComponents = 3;
_texCoord.set(x,y,z,1.0);
}
void GLBeginEndAdapter::TexCoord4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
_maxNumTexCoordComponents = 4;
_texCoord.set(x,y,z,w);
}
void GLBeginEndAdapter::Vertex3f(GLfloat x, GLfloat y, GLfloat z)
{
osg::Vec3 vertex(x,y,z);
if (_vertices.valid()) _vertices->push_back(vertex);
if (_normal.valid()) _normals->push_back(_normal);
if (_colors.valid()) _colors->push_back(_color);
if (_texCoords.valid()) _texCoords->push_back(_texCoord);
}
void GLBeginEndAdapter::Begin(GLenum mode)
{
_overallNormal = _normal;
_overallColor = _color;
// reset geometry
_primitiveMode = mode;
if (!_vertices) _vertices = new osg::Vec3Array;
else _vertices->clear();
_normalSet = false;
if (!_normals) _normals = new osg::Vec3Array;
else _normals->clear();
_colorSet = false;
if (!_colors) _colors = new osg::Vec4Array;
else _colors->clear();
_maxNumTexCoordComponents = 0;
if (!_texCoords) _texCoords = new osg::Vec4Array;
else _texCoords->clear();
}
void GLBeginEndAdapter::End()
{
if (!_vertices || _vertices->empty()) return;
if (!_matrixStack.empty())
{
const osg::Matrixd& matrix = _matrixStack.back();
if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES)
{
for(Vec3Array::iterator itr = _vertices->begin();
itr != _vertices->end();
++itr)
{
*itr = *itr * matrix;
}
}
else
{
_state->applyModelViewMatrix(new RefMatrix(matrix));
}
}
_state->lazyDisablingOfVertexAttributes();
_state->setVertexPointer(_vertices.get());
if (_colorSet)
{
_state->setColorPointer(_colors.get());
}
else
{
glColor4fv(_overallColor.ptr());
}
if (_normalSet)
{
_state->setNormalPointer(_normals.get());
}
else
{
glNormal3fv(_overallNormal.ptr());
}
if (_maxNumTexCoordComponents!=0)
{
_state->setTexCoordPointer(0, _texCoords.get());
}
_state->applyDisablingOfVertexAttributes();
glDrawArrays(_primitiveMode, 0, _vertices->size());
}

View File

@@ -1275,6 +1275,7 @@ void Geometry::releaseGLObjects(State* state) const
void Geometry::drawImplementation(RenderInfo& renderInfo) const
{
State& state = *renderInfo.getState();
bool vertexAttribAlias = state.getUseVertexAttributeAliasing();
// unsigned int contextID = state.getContextID();
@@ -1304,11 +1305,13 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
return;
}
DrawNormal drawNormal(_normalData.array.get(),_normalData.indices.get());
DrawColor drawColor(_colorData.array.get(),_colorData.indices.get());
DrawSecondaryColor drawSecondaryColor(_secondaryColorData.array.get(),_secondaryColorData.indices.get(),extensions);
AttributeBinding normalBinding = _normalData.binding;
AttributeBinding colorBinding = _colorData.binding;
DrawFogCoord drawFogCoord(_fogCoordData.array.get(),_fogCoordData.indices.get(),extensions);
DrawNormal drawNormal(vertexAttribAlias?0:_normalData.array.get(),vertexAttribAlias?0:_normalData.indices.get());
DrawColor drawColor(vertexAttribAlias?0:_colorData.array.get(),vertexAttribAlias?0:_colorData.indices.get());
DrawSecondaryColor drawSecondaryColor(vertexAttribAlias?0:_secondaryColorData.array.get(),vertexAttribAlias?0:_secondaryColorData.indices.get(),extensions);
DrawFogCoord drawFogCoord(vertexAttribAlias?0:_fogCoordData.array.get(),vertexAttribAlias?0:_fogCoordData.indices.get(),extensions);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1334,6 +1337,7 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
fogCoordBinding = BIND_OFF;
}
unsigned int normalIndex = 0;
unsigned int colorIndex = 0;
unsigned int secondaryColorIndex = 0;
@@ -1353,13 +1357,40 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
DrawVertexAttribMap drawVertexAttribMap;
bool vertexVertexAttributesSupported = extensions->isVertexProgramSupported();
bool handleVertexAttributes = (!_vertexAttribList.empty() && vertexVertexAttributesSupported);
bool handleVertexAttributes = (vertexAttribAlias || !_vertexAttribList.empty()) && vertexVertexAttributesSupported;
bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
if (vertexAttribAlias)
{
if (normalBinding!=BIND_OFF && normalBinding!=BIND_PER_VERTEX)
{
osg::notify(osg::NOTICE)<<"normal assign"<<std::endl;
drawVertexAttribMap[normalBinding].push_back( new DrawVertexAttrib(extensions,2,false,_normalData.array.get(),_normalData.indices.get()) );
normalBinding = BIND_OFF;
}
if (colorBinding!=BIND_OFF && colorBinding!=BIND_PER_VERTEX)
{
osg::notify(osg::NOTICE)<<"color assign"<<std::endl;
drawVertexAttribMap[colorBinding].push_back( new DrawVertexAttrib(extensions,3,false,_colorData.array.get(),_colorData.indices.get()) );
colorBinding = BIND_OFF;
}
secondaryColorBinding = BIND_OFF;
fogCoordBinding = BIND_OFF;
}
if (areFastPathsUsed())
{
#define USE_LAZY_DISABLING
#ifdef USE_LAZY_DISABLING
state.lazyDisablingOfVertexAttributes();
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// fast path.
@@ -1368,99 +1399,55 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
{
// osg::notify(osg::NOTICE)<<"Geometry::drawImplementation() Using VertexBufferObjects"<<std::endl;
//
// Vertex Buffer Object path for defining vertex arrays.
//
state.setNormalPointer(_normalData.binding==BIND_PER_VERTEX ? _normalData.array.get() : 0);
state.setColorPointer(_colorData.binding==BIND_PER_VERTEX ? _colorData.array.get() : 0);
state.setSecondaryColorPointer(_secondaryColorData.binding==BIND_PER_VERTEX ? _secondaryColorData.array.get() : 0);
state.setFogCoordPointer(_fogCoordData.binding==BIND_PER_VERTEX ? _fogCoordData.array.get() : 0);
unsigned int unit;
for(unit=0;unit<_texCoordList.size();++unit)
{
state.setTexCoordPointer(unit, _texCoordList[unit].array.get());
}
state.disableTexCoordPointersAboveAndIncluding(unit);
if( handleVertexAttributes )
{
unsigned int index;
for( index = 0; index < _vertexAttribList.size(); ++index )
{
const Array* array = _vertexAttribList[index].array.get();
const AttributeBinding ab = _vertexAttribList[index].binding;
state.setVertexAttribPointer(index, (ab==BIND_PER_VERTEX ? array : 0), _vertexAttribList[index].normalize);
if(array && ab!=BIND_PER_VERTEX)
{
const IndexArray* indexArray = _vertexAttribList[index].indices.get();
if( indexArray && indexArray->getNumElements() > 0 )
{
drawVertexAttribMap[ab].push_back(
new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,indexArray) );
}
else
{
drawVertexAttribMap[ab].push_back(
new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,0) );
}
}
}
state.disableVertexAttribPointersAboveAndIncluding( index );
}
else if (vertexVertexAttributesSupported)
{
state.disableVertexAttribPointersAboveAndIncluding( 0 );
}
state.setVertexPointer(_vertexData.array.get());
}
else
{
// osg::notify(osg::NOTICE)<<"none VertexBuffer path"<<std::endl;
//
// Non Vertex Buffer Object path for defining vertex arrays.
//
if( _vertexData.array.valid() )
state.setVertexPointer(_vertexData.array->getDataSize(),_vertexData.array->getDataType(),0,_vertexData.array->getDataPointer());
state.setVertexPointer(_vertexData.array.get());
#ifndef USE_LAZY_DISABLING
else
state.disableVertexPointer();
#endif
if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid())
state.setNormalPointer(_normalData.array->getDataType(),0,_normalData.array->getDataPointer());
state.setNormalPointer(_normalData.array.get());
#ifndef USE_LAZY_DISABLING
else
state.disableNormalPointer();
#endif
if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid())
state.setColorPointer(_colorData.array->getDataSize(),_colorData.array->getDataType(),0,_colorData.array->getDataPointer());
state.setColorPointer(_colorData.array.get());
#ifndef USE_LAZY_DISABLING
else
state.disableColorPointer();
#endif
if (secondaryColorBinding==BIND_PER_VERTEX && _secondaryColorData.array.valid())
state.setSecondaryColorPointer(_secondaryColorData.array->getDataSize(),_secondaryColorData.array->getDataType(),0,_secondaryColorData.array->getDataPointer());
state.setSecondaryColorPointer(_secondaryColorData.array.get());
#ifndef USE_LAZY_DISABLING
else
state.disableSecondaryColorPointer();
#endif
if (fogCoordBinding==BIND_PER_VERTEX && _fogCoordData.array.valid())
state.setFogCoordPointer(GL_FLOAT,0,_fogCoordData.array->getDataPointer());
state.setFogCoordPointer(_fogCoordData.array.get());
#ifndef USE_LAZY_DISABLING
else
state.disableFogCoordPointer();
#endif
unsigned int unit;
for(unit=0;unit<_texCoordList.size();++unit)
{
const Array* array = _texCoordList[unit].array.get();
if (array)
state.setTexCoordPointer(unit,array->getDataSize(),array->getDataType(),0,array->getDataPointer());
state.setTexCoordPointer(unit,array);
#ifndef USE_LAZY_DISABLING
else
state.disableTexCoordPointer(unit);
#endif
}
#ifndef USE_LAZY_DISABLING
state.disableTexCoordPointersAboveAndIncluding(unit);
#endif
if( handleVertexAttributes )
{
@@ -1472,7 +1459,110 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
if( ab == BIND_PER_VERTEX && array )
{
state.setVertexAttribPointer( index, array->getDataSize(), array->getDataType(),
state.setVertexAttribPointer( index, array, _vertexAttribList[index].normalize );
}
else
{
if( array )
{
const IndexArray* indexArray = _vertexAttribList[index].indices.get();
if( indexArray && indexArray->getNumElements() > 0 )
{
drawVertexAttribMap[ab].push_back(
new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,indexArray) );
}
else
{
drawVertexAttribMap[ab].push_back(
new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,0) );
}
}
#ifndef USE_LAZY_DISABLING
state.disableVertexAttribPointer( index );
#endif
}
}
#ifndef USE_LAZY_DISABLING
state.disableVertexAttribPointersAboveAndIncluding( index );
#endif
}
#ifndef USE_LAZY_DISABLING
else if (vertexVertexAttributesSupported)
{
state.disableVertexAttribPointersAboveAndIncluding( 0 );
}
#endif
}
else
{
// osg::notify(osg::NOTICE)<<"none VertexBuffer path"<<std::endl;
//
// Non Vertex Buffer Object path for defining vertex arrays.
//
if( _vertexData.array.valid() )
state.setVertexPointer(_vertexData.array->getDataSize(),_vertexData.array->getDataType(),0,_vertexData.array->getDataPointer());
#ifndef USE_LAZY_DISABLING
else
state.disableVertexPointer();
#endif
if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid())
state.setNormalPointer(_normalData.array->getDataType(),0,_normalData.array->getDataPointer());
#ifndef USE_LAZY_DISABLING
else
state.disableNormalPointer();
#endif
if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid())
state.setColorPointer(_colorData.array->getDataSize(),_colorData.array->getDataType(),0,_colorData.array->getDataPointer());
#ifndef USE_LAZY_DISABLING
else
state.disableColorPointer();
#endif
if (secondaryColorBinding==BIND_PER_VERTEX && _secondaryColorData.array.valid())
state.setSecondaryColorPointer(_secondaryColorData.array->getDataSize(),_secondaryColorData.array->getDataType(),0,_secondaryColorData.array->getDataPointer());
#ifndef USE_LAZY_DISABLING
else
state.disableSecondaryColorPointer();
#endif
if (fogCoordBinding==BIND_PER_VERTEX && _fogCoordData.array.valid())
state.setFogCoordPointer(GL_FLOAT,0,_fogCoordData.array->getDataPointer());
#ifndef USE_LAZY_DISABLING
else
state.disableFogCoordPointer();
#endif
unsigned int unit;
for(unit=0;unit<_texCoordList.size();++unit)
{
const Array* array = _texCoordList[unit].array.get();
if (array)
state.setTexCoordPointer(unit,array->getDataSize(),array->getDataType(),0,array->getDataPointer());
#ifndef USE_LAZY_DISABLING
else
state.disableTexCoordPointer(unit);
#endif
}
#ifndef USE_LAZY_DISABLING
state.disableTexCoordPointersAboveAndIncluding(unit);
#endif
if( handleVertexAttributes )
{
unsigned int index;
for( index = 0; index < _vertexAttribList.size(); ++index )
{
const Array* array = _vertexAttribList[index].array.get();
const AttributeBinding ab = _vertexAttribList[index].binding;
if( ab == BIND_PER_VERTEX && array )
{
state.setVertexAttribPointer( index, array->getDataSize(), array->getDataType(),
_vertexAttribList[index].normalize, 0, array->getDataPointer() );
}
else
@@ -1483,34 +1573,44 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
if( indexArray && indexArray->getNumElements() > 0 )
{
drawVertexAttribMap[ab].push_back(
drawVertexAttribMap[ab].push_back(
new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,indexArray) );
}
else
{
drawVertexAttribMap[ab].push_back(
drawVertexAttribMap[ab].push_back(
new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,0) );
}
}
#ifndef USE_LAZY_DISABLING
state.disableVertexAttribPointer( index );
#endif
}
}
#ifndef USE_LAZY_DISABLING
state.disableVertexAttribPointersAboveAndIncluding( index );
#endif
}
#ifndef USE_LAZY_DISABLING
else if (vertexVertexAttributesSupported)
{
state.disableVertexAttribPointersAboveAndIncluding( 0 );
}
#endif
}
#ifdef USE_LAZY_DISABLING
state.applyDisablingOfVertexAttributes();
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// pass the overall binding values onto OpenGL.
//
if (_normalData.binding==BIND_OVERALL) drawNormal(normalIndex++);
if (_colorData.binding==BIND_OVERALL) drawColor(colorIndex++);
if (normalBinding==BIND_OVERALL) drawNormal(normalIndex++);
if (colorBinding==BIND_OVERALL) drawColor(colorIndex++);
if (secondaryColorBinding==BIND_OVERALL) drawSecondaryColor(secondaryColorIndex++);
if (fogCoordBinding==BIND_OVERALL) drawFogCoord(fogCoordIndex++);
if (handleVertexAttributes)
@@ -1532,8 +1632,8 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const
++itr)
{
if (_normalData.binding==BIND_PER_PRIMITIVE_SET) drawNormal(normalIndex++);
if (_colorData.binding==BIND_PER_PRIMITIVE_SET) drawColor(colorIndex++);
if (normalBinding==BIND_PER_PRIMITIVE_SET) drawNormal(normalIndex++);
if (colorBinding==BIND_PER_PRIMITIVE_SET) drawColor(colorIndex++);
if (secondaryColorBinding==BIND_PER_PRIMITIVE_SET) drawSecondaryColor(secondaryColorIndex++);
if (fogCoordBinding==BIND_PER_PRIMITIVE_SET) drawFogCoord(fogCoordIndex++);
if (handleVertexAttributes)

View File

@@ -181,10 +181,10 @@ void Program::compileGLObjects( osg::State& state ) const
for( unsigned int i=0; i < _shaderList.size(); ++i )
{
_shaderList[i]->compileShader( contextID );
_shaderList[i]->compileShader( state );
}
getPCP( contextID )->linkProgram();
getPCP( contextID )->linkProgram(state);
}
void Program::setThreadSafeRefUnref(bool threadSafe)
@@ -448,7 +448,7 @@ void Program::PerContextProgram::requestLink()
}
void Program::PerContextProgram::linkProgram()
void Program::PerContextProgram::linkProgram(osg::State& state)
{
if( ! _needsLink ) return;
_needsLink = false;
@@ -485,13 +485,27 @@ void Program::PerContextProgram::linkProgram()
_lastAppliedUniformList.clear();
// set any explicit vertex attribute bindings
const AttribBindingList& bindlist = _program->getAttribBindingList();
for( AttribBindingList::const_iterator itr = bindlist.begin();
itr != bindlist.end(); ++itr )
const AttribBindingList& programBindlist = _program->getAttribBindingList();
for( AttribBindingList::const_iterator itr = programBindlist.begin();
itr != programBindlist.end(); ++itr )
{
osg::notify(osg::NOTICE)<<"Program's vertex attrib binding "<<itr->second<<", "<<itr->first<<std::endl;
_extensions->glBindAttribLocation( _glProgramHandle, itr->second, itr->first.c_str() );
}
// set any explicit vertex attribute bindings that are set up via osg::State, such as the vertex arrays
// that have been aliase to vertex attrib arrays
if (state.getUseVertexAttributeAliasing())
{
const AttribBindingList& stateBindlist = state.getAttributeBindingList();
for( AttribBindingList::const_iterator itr = stateBindlist.begin();
itr != stateBindlist.end(); ++itr )
{
osg::notify(osg::NOTICE)<<"State's vertex attrib binding "<<itr->second<<", "<<itr->first<<std::endl;
_extensions->glBindAttribLocation( _glProgramHandle, itr->second, itr->first.c_str() );
}
}
// set any explicit frag data bindings
const FragDataBindingList& fdbindlist = _program->getFragDataBindingList();
for( FragDataBindingList::const_iterator itr = fdbindlist.begin();

View File

@@ -230,10 +230,10 @@ void Shader::releaseGLObjects(osg::State* state) const
}
}
void Shader::compileShader( unsigned int contextID ) const
void Shader::compileShader( osg::State& state ) const
{
PerContextShader* pcs = getPCS( contextID );
if( pcs ) pcs->compileShader();
PerContextShader* pcs = getPCS( state.getContextID() );
if( pcs ) pcs->compileShader( state );
}
@@ -374,12 +374,18 @@ namespace
}
}
void Shader::PerContextShader::compileShader()
void Shader::PerContextShader::compileShader(osg::State& state)
{
if( ! _needsCompile ) return;
_needsCompile = false;
std::string sourceWithLineNumbers = insertLineNumbers(_shader->getShaderSource());
std::string source = _shader->getShaderSource();
if (_shader->getType()==osg::Shader::VERTEX && (state.getUseVertexAttributeAliasing() || state.getUseModelViewAndProjectionUniforms()))
{
state.convertVertexShaderSourceToOsgBuiltIns(source);
}
std::string sourceWithLineNumbers = insertLineNumbers(source);
if (osg::getNotifyLevel()>=osg::INFO)
{
@@ -389,7 +395,7 @@ void Shader::PerContextShader::compileShader()
}
GLint compiled = GL_FALSE;
const char* sourceText = _shader->getShaderSource().c_str();
const char* sourceText = source.c_str();
_extensions->glShaderSource( _glShaderHandle, 1, &sourceText, NULL );
_extensions->glCompileShader( _glShaderHandle );
_extensions->glGetShaderiv( _glShaderHandle, GL_COMPILE_STATUS, &compiled );

View File

@@ -92,7 +92,9 @@ void DrawShapeVisitor::drawCylinderBody(unsigned int numSegments, float radius,
// The code is mostly duplicated in order to hoist the back/front face
// test out of the loop for efficiency
glBegin(GL_QUAD_STRIP);
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
gl.Begin(GL_QUAD_STRIP);
if (drawFrontFace) {
@@ -103,23 +105,23 @@ void DrawShapeVisitor::drawCylinderBody(unsigned int numSegments, float radius,
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(c,s,0.0f);
gl.Normal3f(c,s,0.0f);
glTexCoord2f(texCoord,1.0f);
glVertex3f(c*r,s*r,topz);
gl.TexCoord2f(texCoord,1.0f);
gl.Vertex3f(c*r,s*r,topz);
glTexCoord2f(texCoord,0.0f);
glVertex3f(c*r,s*r,basez);
gl.TexCoord2f(texCoord,0.0f);
gl.Vertex3f(c*r,s*r,basez);
}
// do last point by hand to ensure no round off errors.
glNormal3f(1.0f,0.0f,0.0f);
gl.Normal3f(1.0f,0.0f,0.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(r,0.0f,topz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(r,0.0f,topz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(r,0.0f,basez);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(r,0.0f,basez);
}
if (drawBackFace) {
@@ -130,26 +132,26 @@ void DrawShapeVisitor::drawCylinderBody(unsigned int numSegments, float radius,
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(-c,-s,0.0f);
gl.Normal3f(-c,-s,0.0f);
glTexCoord2f(texCoord,0.0f);
glVertex3f(c*r,s*r,basez);
gl.TexCoord2f(texCoord,0.0f);
gl.Vertex3f(c*r,s*r,basez);
glTexCoord2f(texCoord,1.0f);
glVertex3f(c*r,s*r,topz);
gl.TexCoord2f(texCoord,1.0f);
gl.Vertex3f(c*r,s*r,topz);
}
// do last point by hand to ensure no round off errors.
glNormal3f(-1.0f,0.0f,0.0f);
gl.Normal3f(-1.0f,0.0f,0.0f);
glTexCoord2f(1.0f,0.0f);
glVertex3f(r,0.0f,basez);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(r,0.0f,basez);
glTexCoord2f(1.0f,1.0f);
glVertex3f(r,0.0f,topz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(r,0.0f,topz);
}
glEnd();
gl.End();
}
@@ -176,6 +178,8 @@ void DrawShapeVisitor::drawHalfSphere(unsigned int numSegments, unsigned int num
unsigned int rowbegin = top?numRows/2:0;
unsigned int rowend = top?numRows:numRows/2;
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
for(unsigned int rowi=rowbegin; rowi<rowend; ++rowi)
{
@@ -186,7 +190,7 @@ void DrawShapeVisitor::drawHalfSphere(unsigned int numSegments, unsigned int num
float nzTop= sinf(lTop);
float nRatioTop= cosf(lTop);
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
float angle = 0.0f;
float texCoord = 0.0f;
@@ -204,28 +208,28 @@ void DrawShapeVisitor::drawHalfSphere(unsigned int numSegments, unsigned int num
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(c*nRatioTop,s*nRatioTop,nzTop);
gl.Normal3f(c*nRatioTop,s*nRatioTop,nzTop);
glTexCoord2f(texCoord,vTop);
glVertex3f(c*rTop,s*rTop,zTop+zOffset);
gl.TexCoord2f(texCoord,vTop);
gl.Vertex3f(c*rTop,s*rTop,zTop+zOffset);
glNormal3f(c*nRatioBase,s*nRatioBase,nzBase);
gl.Normal3f(c*nRatioBase,s*nRatioBase,nzBase);
glTexCoord2f(texCoord,vBase);
glVertex3f(c*rBase,s*rBase,zBase+zOffset);
gl.TexCoord2f(texCoord,vBase);
gl.Vertex3f(c*rBase,s*rBase,zBase+zOffset);
}
// do last point by hand to ensure no round off errors.
glNormal3f(nRatioTop,0.0f,nzTop);
gl.Normal3f(nRatioTop,0.0f,nzTop);
glTexCoord2f(1.0f,vTop);
glVertex3f(rTop,0.0f,zTop+zOffset);
gl.TexCoord2f(1.0f,vTop);
gl.Vertex3f(rTop,0.0f,zTop+zOffset);
glNormal3f(nRatioBase,0.0f,nzBase);
gl.Normal3f(nRatioBase,0.0f,nzBase);
glTexCoord2f(1.0f,vBase);
glVertex3f(rBase,0.0f,zBase+zOffset);
gl.TexCoord2f(1.0f,vBase);
gl.Vertex3f(rBase,0.0f,zBase+zOffset);
}
if (drawBackFace) {
@@ -236,33 +240,32 @@ void DrawShapeVisitor::drawHalfSphere(unsigned int numSegments, unsigned int num
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(-c*nRatioBase,-s*nRatioBase,-nzBase);
gl.Normal3f(-c*nRatioBase,-s*nRatioBase,-nzBase);
glTexCoord2f(texCoord,vBase);
glVertex3f(c*rBase,s*rBase,zBase+zOffset);
gl.TexCoord2f(texCoord,vBase);
gl.Vertex3f(c*rBase,s*rBase,zBase+zOffset);
glNormal3f(-c*nRatioTop,-s*nRatioTop,-nzTop);
gl.Normal3f(-c*nRatioTop,-s*nRatioTop,-nzTop);
glTexCoord2f(texCoord,vTop);
glVertex3f(c*rTop,s*rTop,zTop+zOffset);
gl.TexCoord2f(texCoord,vTop);
gl.Vertex3f(c*rTop,s*rTop,zTop+zOffset);
}
// do last point by hand to ensure no round off errors.
glNormal3f(-nRatioBase,0.0f,-nzBase);
gl.Normal3f(-nRatioBase,0.0f,-nzBase);
glTexCoord2f(1.0f,vBase);
glVertex3f(rBase,0.0f,zBase+zOffset);
gl.TexCoord2f(1.0f,vBase);
gl.Vertex3f(rBase,0.0f,zBase+zOffset);
glNormal3f(-nRatioTop,0.0f,-nzTop);
gl.Normal3f(-nRatioTop,0.0f,-nzTop);
glTexCoord2f(1.0f,vTop);
glVertex3f(rTop,0.0f,zTop+zOffset);
gl.TexCoord2f(1.0f,vTop);
gl.Vertex3f(rTop,0.0f,zTop+zOffset);
}
glEnd();
gl.End();
lBase=lTop;
rBase=rTop;
zBase=zTop;
@@ -271,15 +274,16 @@ void DrawShapeVisitor::drawHalfSphere(unsigned int numSegments, unsigned int num
nRatioBase=nRatioTop;
}
}
void DrawShapeVisitor::apply(const Sphere& sphere)
{
glPushMatrix();
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
glTranslatef(sphere.getCenter().x(),sphere.getCenter().y(),sphere.getCenter().z());
gl.PushMatrix();
gl.Translated(sphere.getCenter().x(),sphere.getCenter().y(),sphere.getCenter().z());
bool drawFrontFace = _hints ? _hints->getCreateFrontFace() : true;
bool drawBackFace = _hints ? _hints->getCreateBackFace() : false;
@@ -302,6 +306,8 @@ void DrawShapeVisitor::apply(const Sphere& sphere)
float angleDelta = osg::PI*2.0f/(float)numSegments;
float texCoordHorzDelta = 1.0f/(float)numSegments;
gl.Begin(GL_QUAD_STRIP);
if (drawBackFace)
{
float lBase=-osg::PI*0.5f;
@@ -321,7 +327,7 @@ void DrawShapeVisitor::apply(const Sphere& sphere)
float nzTop= sinf(lTop);
float nRatioTop= cosf(lTop);
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
float angle = 0.0f;
float texCoord = 0.0f;
@@ -333,32 +339,32 @@ void DrawShapeVisitor::apply(const Sphere& sphere)
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(-c*nRatioBase,-s*nRatioBase,-nzBase);
gl.Normal3f(-c*nRatioBase,-s*nRatioBase,-nzBase);
glTexCoord2f(texCoord,vBase);
glVertex3f(c*rBase,s*rBase,zBase);
gl.TexCoord2f(texCoord,vBase);
gl.Vertex3f(c*rBase,s*rBase,zBase);
glNormal3f(-c*nRatioTop,-s*nRatioTop,-nzTop);
gl.Normal3f(-c*nRatioTop,-s*nRatioTop,-nzTop);
glTexCoord2f(texCoord,vTop);
glVertex3f(c*rTop,s*rTop,zTop);
gl.TexCoord2f(texCoord,vTop);
gl.Vertex3f(c*rTop,s*rTop,zTop);
}
// do last point by hand to ensure no round off errors.
glNormal3f(-nRatioBase,0.0f,-nzBase);
gl.Normal3f(-nRatioBase,0.0f,-nzBase);
glTexCoord2f(1.0f,vBase);
glVertex3f(rBase,0.0f,zBase);
gl.TexCoord2f(1.0f,vBase);
gl.Vertex3f(rBase,0.0f,zBase);
glNormal3f(-nRatioTop,0.0f,-nzTop);
gl.Normal3f(-nRatioTop,0.0f,-nzTop);
glTexCoord2f(1.0f,vTop);
glVertex3f(rTop,0.0f,zTop);
gl.TexCoord2f(1.0f,vTop);
gl.Vertex3f(rTop,0.0f,zTop);
glEnd();
gl.End();
lBase=lTop;
@@ -391,7 +397,7 @@ void DrawShapeVisitor::apply(const Sphere& sphere)
float nzTop= sinf(lTop);
float nRatioTop= cosf(lTop);
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
float angle = 0.0f;
float texCoord = 0.0f;
@@ -403,30 +409,30 @@ void DrawShapeVisitor::apply(const Sphere& sphere)
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(c*nRatioTop,s*nRatioTop,nzTop);
gl.Normal3f(c*nRatioTop,s*nRatioTop,nzTop);
glTexCoord2f(texCoord,vTop);
glVertex3f(c*rTop,s*rTop,zTop);
gl.TexCoord2f(texCoord,vTop);
gl.Vertex3f(c*rTop,s*rTop,zTop);
glNormal3f(c*nRatioBase,s*nRatioBase,nzBase);
gl.Normal3f(c*nRatioBase,s*nRatioBase,nzBase);
glTexCoord2f(texCoord,vBase);
glVertex3f(c*rBase,s*rBase,zBase);
gl.TexCoord2f(texCoord,vBase);
gl.Vertex3f(c*rBase,s*rBase,zBase);
}
// do last point by hand to ensure no round off errors.
glNormal3f(nRatioTop,0.0f,nzTop);
gl.Normal3f(nRatioTop,0.0f,nzTop);
glTexCoord2f(1.0f,vTop);
glVertex3f(rTop,0.0f,zTop);
gl.TexCoord2f(1.0f,vTop);
gl.Vertex3f(rTop,0.0f,zTop);
glNormal3f(nRatioBase,0.0f,nzBase);
gl.Normal3f(nRatioBase,0.0f,nzBase);
glTexCoord2f(1.0f,vBase);
glVertex3f(rBase,0.0f,zBase);
gl.TexCoord2f(1.0f,vBase);
gl.Vertex3f(rBase,0.0f,zBase);
glEnd();
gl.End();
lBase=lTop;
@@ -439,12 +445,13 @@ void DrawShapeVisitor::apply(const Sphere& sphere)
}
}
glPopMatrix();
gl.PopMatrix();
}
void DrawShapeVisitor::apply(const Box& box)
{
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
// evaluate hints
bool createBody = (_hints ? _hints->getCreateBody() : true);
bool createTop = (_hints ? _hints->getCreateTop() : true);
@@ -454,130 +461,132 @@ void DrawShapeVisitor::apply(const Box& box)
float dy = box.getHalfLengths().y();
float dz = box.getHalfLengths().z();
glPushMatrix();
gl.PushMatrix();
glTranslatef(box.getCenter().x(),box.getCenter().y(),box.getCenter().z());
gl.Translated(box.getCenter().x(),box.getCenter().y(),box.getCenter().z());
if (!box.zeroRotation())
{
Matrix rotation(box.computeRotationMatrix());
glMultMatrix(rotation.ptr());
Matrixd rotation(box.computeRotationMatrix());
gl.MultMatrixd(rotation.ptr());
}
glBegin(GL_QUADS);
gl.Begin(GL_QUADS);
if (createBody) {
// -ve y plane
glNormal3f(0.0f,-1.0f,0.0f);
gl.Normal3f(0.0f,-1.0f,0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-dx,-dy,dz);
gl.TexCoord2f(0.0f,1.0f);
gl.Vertex3f(-dx,-dy,dz);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-dx,-dy,-dz);
gl.TexCoord2f(0.0f,0.0f);
gl.Vertex3f(-dx,-dy,-dz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(dx,-dy,-dz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(dx,-dy,-dz);
glTexCoord2f(1.0f,1.0f);
glVertex3f(dx,-dy,dz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(dx,-dy,dz);
// +ve y plane
glNormal3f(0.0f,1.0f,0.0f);
gl.Normal3f(0.0f,1.0f,0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(dx,dy,dz);
gl.TexCoord2f(0.0f,1.0f);
gl.Vertex3f(dx,dy,dz);
glTexCoord2f(0.0f,0.0f);
glVertex3f(dx,dy,-dz);
gl.TexCoord2f(0.0f,0.0f);
gl.Vertex3f(dx,dy,-dz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-dx,dy,-dz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(-dx,dy,-dz);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-dx,dy,dz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(-dx,dy,dz);
// +ve x plane
glNormal3f(1.0f,0.0f,0.0f);
gl.Normal3f(1.0f,0.0f,0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(dx,-dy,dz);
gl.TexCoord2f(0.0f,1.0f);
gl.Vertex3f(dx,-dy,dz);
glTexCoord2f(0.0f,0.0f);
glVertex3f(dx,-dy,-dz);
gl.TexCoord2f(0.0f,0.0f);
gl.Vertex3f(dx,-dy,-dz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(dx,dy,-dz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(dx,dy,-dz);
glTexCoord2f(1.0f,1.0f);
glVertex3f(dx,dy,dz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(dx,dy,dz);
// -ve x plane
glNormal3f(-1.0f,0.0f,0.0f);
gl.Normal3f(-1.0f,0.0f,0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-dx,dy,dz);
gl.TexCoord2f(0.0f,1.0f);
gl.Vertex3f(-dx,dy,dz);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-dx,dy,-dz);
gl.TexCoord2f(0.0f,0.0f);
gl.Vertex3f(-dx,dy,-dz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-dx,-dy,-dz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(-dx,-dy,-dz);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-dx,-dy,dz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(-dx,-dy,dz);
}
if (createTop) {
// +ve z plane
glNormal3f(0.0f,0.0f,1.0f);
gl.Normal3f(0.0f,0.0f,1.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-dx,dy,dz);
gl.TexCoord2f(0.0f,1.0f);
gl.Vertex3f(-dx,dy,dz);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-dx,-dy,dz);
gl.TexCoord2f(0.0f,0.0f);
gl.Vertex3f(-dx,-dy,dz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(dx,-dy,dz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(dx,-dy,dz);
glTexCoord2f(1.0f,1.0f);
glVertex3f(dx,dy,dz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(dx,dy,dz);
}
if (createBottom) {
// -ve z plane
glNormal3f(0.0f,0.0f,-1.0f);
gl.Normal3f(0.0f,0.0f,-1.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(dx,dy,-dz);
gl.TexCoord2f(0.0f,1.0f);
gl.Vertex3f(dx,dy,-dz);
glTexCoord2f(0.0f,0.0f);
glVertex3f(dx,-dy,-dz);
gl.TexCoord2f(0.0f,0.0f);
gl.Vertex3f(dx,-dy,-dz);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-dx,-dy,-dz);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(-dx,-dy,-dz);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-dx,dy,-dz);
gl.TexCoord2f(1.0f,1.0f);
gl.Vertex3f(-dx,dy,-dz);
}
glEnd();
gl.End();
glPopMatrix();
gl.PopMatrix();
}
void DrawShapeVisitor::apply(const Cone& cone)
{
glPushMatrix();
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
glTranslatef(cone.getCenter().x(),cone.getCenter().y(),cone.getCenter().z());
gl.PushMatrix();
gl.Translated(cone.getCenter().x(),cone.getCenter().y(),cone.getCenter().z());
if (!cone.zeroRotation())
{
Matrix rotation(cone.computeRotationMatrix());
glMultMatrix(rotation.ptr());
Matrixd rotation(cone.computeRotationMatrix());
gl.MultMatrixd(rotation.ptr());
}
// evaluate hints
@@ -624,7 +633,7 @@ void DrawShapeVisitor::apply(const Cone& cone)
// we can't use a fan for the cone top
// since we need different normals at the top
// for each face..
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
angle = 0.0f;
texCoord = 0.0f;
@@ -634,38 +643,38 @@ void DrawShapeVisitor::apply(const Cone& cone)
float c = cosf(angle);
float s = sinf(angle);
glNormal3f(c*normalRatio,s*normalRatio,normalz);
gl.Normal3f(c*normalRatio,s*normalRatio,normalz);
glTexCoord2f(texCoord,topv);
glVertex3f(c*topr,s*topr,topz);
gl.TexCoord2f(texCoord,topv);
gl.Vertex3f(c*topr,s*topr,topz);
glTexCoord2f(texCoord,basev);
glVertex3f(c*baser,s*baser,basez);
gl.TexCoord2f(texCoord,basev);
gl.Vertex3f(c*baser,s*baser,basez);
}
// do last point by hand to ensure no round off errors.
glNormal3f(normalRatio,0.0f,normalz);
gl.Normal3f(normalRatio,0.0f,normalz);
glTexCoord2f(1.0f,topv);
glVertex3f(topr,0.0f,topz);
gl.TexCoord2f(1.0f,topv);
gl.Vertex3f(topr,0.0f,topz);
glTexCoord2f(1.0f,basev);
glVertex3f(baser,0.0f,basez);
gl.TexCoord2f(1.0f,basev);
gl.Vertex3f(baser,0.0f,basez);
glEnd();
gl.End();
}
}
if (createBottom) {
glBegin(GL_TRIANGLE_FAN);
gl.Begin(GL_TRIANGLE_FAN);
angle = osg::PI*2.0f;
texCoord = 1.0f;
basez = cone.getBaseOffset();
glNormal3f(0.0f,0.0f,-1.0f);
glTexCoord2f(0.5f,0.5f);
glVertex3f(0.0f,0.0f,basez);
gl.Normal3f(0.0f,0.0f,-1.0f);
gl.TexCoord2f(0.5f,0.5f);
gl.Vertex3f(0.0f,0.0f,basez);
for(unsigned int bottomi=0;bottomi<numSegments;
++bottomi,angle-=angleDelta,texCoord-=texCoordHorzDelta) {
@@ -673,29 +682,31 @@ void DrawShapeVisitor::apply(const Cone& cone)
float c = cosf(angle);
float s = sinf(angle);
glTexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
glVertex3f(c*r,s*r,basez);
gl.TexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
gl.Vertex3f(c*r,s*r,basez);
}
glTexCoord2f(1.0f,0.0f);
glVertex3f(r,0.0f,basez);
gl.TexCoord2f(1.0f,0.0f);
gl.Vertex3f(r,0.0f,basez);
glEnd();
gl.End();
}
glPopMatrix();
gl.PopMatrix();
}
void DrawShapeVisitor::apply(const Cylinder& cylinder)
{
glPushMatrix();
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
glTranslatef(cylinder.getCenter().x(),cylinder.getCenter().y(),cylinder.getCenter().z());
gl.PushMatrix();
gl.Translated(cylinder.getCenter().x(),cylinder.getCenter().y(),cylinder.getCenter().z());
if (!cylinder.zeroRotation())
{
Matrix rotation(cylinder.computeRotationMatrix());
glMultMatrix(rotation.ptr());
Matrixd rotation(cylinder.computeRotationMatrix());
gl.MultMatrixd(rotation.ptr());
}
// evaluate hints
@@ -730,11 +741,11 @@ void DrawShapeVisitor::apply(const Cylinder& cylinder)
// cylinder top
if (createTop) {
glBegin(GL_TRIANGLE_FAN);
gl.Begin(GL_TRIANGLE_FAN);
glNormal3f(0.0f,0.0f,1.0f);
glTexCoord2f(0.5f,0.5f);
glVertex3f(0.0f,0.0f,topz);
gl.Normal3f(0.0f,0.0f,1.0f);
gl.TexCoord2f(0.5f,0.5f);
gl.Vertex3f(0.0f,0.0f,topz);
angle = 0.0f;
texCoord = 0.0f;
@@ -745,24 +756,24 @@ void DrawShapeVisitor::apply(const Cylinder& cylinder)
float c = cosf(angle);
float s = sinf(angle);
glTexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
glVertex3f(c*r,s*r,topz);
gl.TexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
gl.Vertex3f(c*r,s*r,topz);
}
glTexCoord2f(1.0f,0.5f);
glVertex3f(r,0.0f,topz);
gl.TexCoord2f(1.0f,0.5f);
gl.Vertex3f(r,0.0f,topz);
glEnd();
gl.End();
}
// cylinder bottom
if (createBottom)
{
glBegin(GL_TRIANGLE_FAN);
gl.Begin(GL_TRIANGLE_FAN);
glNormal3f(0.0f,0.0f,-1.0f);
glTexCoord2f(0.5f,0.5f);
glVertex3f(0.0f,0.0f,basez);
gl.Normal3f(0.0f,0.0f,-1.0f);
gl.TexCoord2f(0.5f,0.5f);
gl.Vertex3f(0.0f,0.0f,basez);
angle = osg::PI*2.0f;
texCoord = 1.0f;
@@ -773,29 +784,31 @@ void DrawShapeVisitor::apply(const Cylinder& cylinder)
float c = cosf(angle);
float s = sinf(angle);
glTexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
glVertex3f(c*r,s*r,basez);
gl.TexCoord2f(c*0.5f+0.5f,s*0.5f+0.5f);
gl.Vertex3f(c*r,s*r,basez);
}
glTexCoord2f(1.0f,0.5f);
glVertex3f(r,0.0f,basez);
gl.TexCoord2f(1.0f,0.5f);
gl.Vertex3f(r,0.0f,basez);
glEnd();
gl.End();
}
glPopMatrix();
gl.PopMatrix();
}
void DrawShapeVisitor::apply(const Capsule& capsule)
{
glPushMatrix();
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
glTranslatef(capsule.getCenter().x(),capsule.getCenter().y(),capsule.getCenter().z());
gl.PushMatrix();
gl.Translated(capsule.getCenter().x(),capsule.getCenter().y(),capsule.getCenter().z());
if (!capsule.zeroRotation())
{
Matrix rotation(capsule.computeRotationMatrix());
glMultMatrix(rotation.ptr());
Matrixd rotation(capsule.computeRotationMatrix());
gl.MultMatrixd(rotation.ptr());
}
// evaluate hints
@@ -828,7 +841,7 @@ void DrawShapeVisitor::apply(const Capsule& capsule)
if (createBottom)
drawHalfSphere(numSegments, numRows, capsule.getRadius(), SphereBottomHalf, -capsule.getHeight()/2.0f);
glPopMatrix();
gl.PopMatrix();
}
void DrawShapeVisitor::apply(const InfinitePlane&)
@@ -838,12 +851,14 @@ void DrawShapeVisitor::apply(const InfinitePlane&)
void DrawShapeVisitor::apply(const TriangleMesh& mesh)
{
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
const Vec3Array* vertices = mesh.getVertices();
const IndexArray* indices = mesh.getIndices();
if (vertices && indices)
{
glBegin(GL_TRIANGLES);
gl.Begin(GL_TRIANGLES);
for(unsigned int i=0;i+2<indices->getNumElements();i+=3)
{
@@ -853,15 +868,15 @@ void DrawShapeVisitor::apply(const TriangleMesh& mesh)
Vec3 normal = (v2-v1)^(v3-v2);
normal.normalize();
glNormal3fv(normal.ptr());
glVertex3fv(v1.ptr());
glVertex3fv(v2.ptr());
glVertex3fv(v3.ptr());
gl.Normal3fv(normal.ptr());
gl.Vertex3fv(v1.ptr());
gl.Vertex3fv(v2.ptr());
gl.Vertex3fv(v3.ptr());
}
glEnd();
}
gl.End();
}
}
void DrawShapeVisitor::apply(const ConvexHull& hull)
@@ -872,16 +887,18 @@ void DrawShapeVisitor::apply(const ConvexHull& hull)
void DrawShapeVisitor::apply(const HeightField& field)
{
if (field.getNumColumns()==0 || field.getNumRows()==0) return;
glPushMatrix();
glTranslatef(field.getOrigin().x(),field.getOrigin().y(),field.getOrigin().z());
GLBeginEndAdapter& gl = _state.getGLBeginEndAdapter();
gl.PushMatrix();
gl.Translated(field.getOrigin().x(),field.getOrigin().y(),field.getOrigin().z());
if (!field.zeroRotation())
{
Matrix rotation(field.computeRotationMatrix());
glMultMatrix(rotation.ptr());
Matrixd rotation(field.computeRotationMatrix());
gl.MultMatrixd(rotation.ptr());
}
float dx = field.getXInterval();
@@ -900,7 +917,7 @@ void DrawShapeVisitor::apply(const HeightField& field)
if (field.getSkirtHeight()!=0.0f)
{
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
float u = 0.0f;
@@ -913,20 +930,20 @@ void DrawShapeVisitor::apply(const HeightField& field)
vertTop.z() = field.getHeight(col,0);
normTop.set(field.getNormal(col,0));
glTexCoord2f(u,0.0f);
glNormal3fv(normTop.ptr());
gl.TexCoord2f(u,0.0f);
gl.Normal3fv(normTop.ptr());
glVertex3fv(vertTop.ptr());
gl.Vertex3fv(vertTop.ptr());
vertTop.z()-=field.getSkirtHeight();
glVertex3fv(vertTop.ptr());
gl.Vertex3fv(vertTop.ptr());
}
glEnd();
gl.End();
// draw top skirt
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
unsigned int row = field.getNumRows()-1;
@@ -938,17 +955,17 @@ void DrawShapeVisitor::apply(const HeightField& field)
vertTop.z() = field.getHeight(col,row);
normTop.set(field.getNormal(col,row));
glTexCoord2f(u,1.0f);
glNormal3fv(normTop.ptr());
gl.TexCoord2f(u,1.0f);
gl.Normal3fv(normTop.ptr());
glVertex3f(vertTop.x(),vertTop.y(),vertTop.z()-field.getSkirtHeight());
gl.Vertex3f(vertTop.x(),vertTop.y(),vertTop.z()-field.getSkirtHeight());
//vertTop.z()-=field.getSkirtHeight();
glVertex3fv(vertTop.ptr());
gl.Vertex3fv(vertTop.ptr());
}
glEnd();
gl.End();
}
@@ -961,7 +978,7 @@ void DrawShapeVisitor::apply(const HeightField& field)
float u = 0.0f;
glBegin(GL_QUAD_STRIP);
gl.Begin(GL_QUAD_STRIP);
// draw skirt at beginning of this row if required.
if (field.getSkirtHeight()!=0.0f)
@@ -972,13 +989,13 @@ void DrawShapeVisitor::apply(const HeightField& field)
vertBase.set(0.0f,dy*(float)row,field.getHeight(0,row)-field.getSkirtHeight());
normBase.set(field.getNormal(0,row));
glTexCoord2f(u,vTop);
glNormal3fv(normTop.ptr());
glVertex3fv(vertTop.ptr());
gl.TexCoord2f(u,vTop);
gl.Normal3fv(normTop.ptr());
gl.Vertex3fv(vertTop.ptr());
glTexCoord2f(u,vBase);
glNormal3fv(normBase.ptr());
glVertex3fv(vertBase.ptr());
gl.TexCoord2f(u,vBase);
gl.Normal3fv(normBase.ptr());
gl.Vertex3fv(vertBase.ptr());
}
// draw the actual row
@@ -990,13 +1007,13 @@ void DrawShapeVisitor::apply(const HeightField& field)
vertBase.set(dx*(float)col,dy*(float)row,field.getHeight(col,row));
normBase.set(field.getNormal(col,row));
glTexCoord2f(u,vTop);
glNormal3fv(normTop.ptr());
glVertex3fv(vertTop.ptr());
gl.TexCoord2f(u,vTop);
gl.Normal3fv(normTop.ptr());
gl.Vertex3fv(vertTop.ptr());
glTexCoord2f(u,vBase);
glNormal3fv(normBase.ptr());
glVertex3fv(vertBase.ptr());
gl.TexCoord2f(u,vBase);
gl.Normal3fv(normBase.ptr());
gl.Vertex3fv(vertBase.ptr());
}
@@ -1007,20 +1024,20 @@ void DrawShapeVisitor::apply(const HeightField& field)
vertBase.z()-=field.getSkirtHeight();
vertTop.z()-=field.getSkirtHeight();
glTexCoord2f(u,vTop);
glNormal3fv(normTop.ptr());
glVertex3fv(vertTop.ptr());
gl.TexCoord2f(u,vTop);
gl.Normal3fv(normTop.ptr());
gl.Vertex3fv(vertTop.ptr());
glTexCoord2f(u,vBase);
glNormal3fv(normBase.ptr());
glVertex3fv(vertBase.ptr());
gl.TexCoord2f(u,vBase);
gl.Normal3fv(normBase.ptr());
gl.Vertex3fv(vertBase.ptr());
}
glEnd();
gl.End();
}
glPopMatrix();
gl.PopMatrix();
}
@@ -1904,6 +1921,7 @@ void PrimitiveShapeVisitor::apply(const CompositeShape& group)
ShapeDrawable::ShapeDrawable():
_color(1.0f,1.0f,1.0f,1.0f)
{
//setUseDisplayList(false);
}
ShapeDrawable::ShapeDrawable(Shape* shape,TessellationHints* hints):
@@ -1911,6 +1929,7 @@ ShapeDrawable::ShapeDrawable(Shape* shape,TessellationHints* hints):
_tessellationHints(hints)
{
setShape(shape);
//setUseDisplayList(false);
}
ShapeDrawable::ShapeDrawable(const ShapeDrawable& pg,const CopyOp& copyop):
@@ -1918,6 +1937,7 @@ ShapeDrawable::ShapeDrawable(const ShapeDrawable& pg,const CopyOp& copyop):
_color(pg._color),
_tessellationHints(pg._tessellationHints)
{
//setUseDisplayList(false);
}
ShapeDrawable::~ShapeDrawable()
@@ -1944,10 +1964,11 @@ void ShapeDrawable::setTessellationHints(TessellationHints* hints)
void ShapeDrawable::drawImplementation(RenderInfo& renderInfo) const
{
osg::State& state = *renderInfo.getState();
GLBeginEndAdapter& gl = state.getGLBeginEndAdapter();
if (_shape.valid())
{
glColor4fv(_color.ptr());
gl.Color4fv(_color.ptr());
DrawShapeVisitor dsv(state,_tessellationHints.get());

View File

@@ -17,6 +17,8 @@
#include <osg/GLExtensions>
#include <osg/ApplicationUsage>
#include <sstream>
#ifndef GL_MAX_TEXTURE_COORDS
#define GL_MAX_TEXTURE_COORDS 0x8871
#endif
@@ -35,7 +37,8 @@ using namespace osg;
static ApplicationUsageProxy State_e0(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_GL_ERROR_CHECKING <type>","ONCE_PER_ATTRIBUTE | ON | on enables fine grained checking, ONCE_PER_FRAME enables coarse grained checking");
State::State():
Referenced(true)
Referenced(true),
_glBeginEndAdapter(this)
{
_graphicsContext = 0;
_contextID = 0;
@@ -48,6 +51,27 @@ State::State():
_modelViewMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewMatrix");
_projectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ProjectionMatrix");
_modelViewProjectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewProjectionMatrix");
_normalMatrixUniform = new Uniform(Uniform::FLOAT_MAT3,"osg_NormalMatrix");
{
_useVertexAttributeAliasing = false;
setUpVertexAttribAlias(_vertexAlias,0, "gl_Vertex","osg_Vertex","attribute vec4 ");
setUpVertexAttribAlias(_normalAlias, 2, "gl_Normal","osg_Normal","attribute vec3 ");
setUpVertexAttribAlias(_colorAlias, 3, "gl_Color","osg_Color","attribute vec4 ");
setUpVertexAttribAlias(_secondaryColorAlias, 4, "gl_SecondaryColor","osg_SecondaryColor","attribute vec4 ");
setUpVertexAttribAlias(_fogCoordAlias, 5, "gl_FogCoord","osg_FogCoord","attribute float ");
_texCoordAliasList.resize(8);
for(unsigned int i=0; i<_texCoordAliasList.size(); i++)
{
std::stringstream gl_MultiTexCoord;
std::stringstream osg_MultiTexCoord;
gl_MultiTexCoord<<"gl_MultiTexCoord"<<i;
osg_MultiTexCoord<<"osg_MultiTexCoord"<<i;
setUpVertexAttribAlias(_texCoordAliasList[i], 8+i, gl_MultiTexCoord.str(), osg_MultiTexCoord.str(), "attribute vec4 ");
}
}
_abortRenderingPtr = false;
@@ -95,12 +119,15 @@ State::State():
_maxTexturePoolSize = 0;
_maxBufferObjectPoolSize = 0;
}
State::~State()
{
//_texCoordArrayList.clear();
//_vertexAttribArrayList.clear();
// osg::notify(osg::NOTICE)<<"State::~State()"<<this<<std::endl;
for(AppliedProgramObjectSet::iterator itr = _appliedProgramObjectSet.begin();
itr != _appliedProgramObjectSet.end();
++itr)
@@ -748,6 +775,9 @@ void State::initializeExtensionProcs()
setGLExtensionFuncPtr(_glSecondaryColorPointer, "glSecondaryColorPointer","glSecondaryColorPointerEXT");
setGLExtensionFuncPtr(_glVertexAttribPointer, "glVertexAttribPointer","glVertexAttribPointerARB");
setGLExtensionFuncPtr(_glEnableVertexAttribArray, "glEnableVertexAttribArray","glEnableVertexAttribArrayARB");
setGLExtensionFuncPtr(_glMultiTexCoord4f, "glMultiTexCoord4f","glMultiTexCoord4fARB");
setGLExtensionFuncPtr(_glVertexAttrib4f, "glVertexAttrib4f");
setGLExtensionFuncPtr(_glVertexAttrib4fv, "glVertexAttrib4fv");
setGLExtensionFuncPtr(_glDisableVertexAttribArray, "glDisableVertexAttribArray","glDisableVertexAttribArrayARB");
setGLExtensionFuncPtr(_glBindBuffer, "glBindBuffer","glBindBufferARB");
@@ -816,40 +846,55 @@ bool State::setActiveTextureUnit( unsigned int unit )
void State::setFogCoordPointer(GLenum type, GLsizei stride, const GLvoid *ptr)
{
if (_glFogCoordPointer)
if (_useVertexAttributeAliasing)
{
if (!_fogArray._enabled || _fogArray._dirty)
{
_fogArray._enabled = true;
glEnableClientState(GL_FOG_COORDINATE_ARRAY);
}
//if (_fogArray._pointer!=ptr || _fogArray._dirty)
{
_fogArray._pointer=ptr;
_glFogCoordPointer( type, stride, ptr );
}
_fogArray._dirty = false;
setVertexAttribPointer(_fogCoordAlias._location, 1, type, GL_FALSE, stride, ptr);
}
else
{
if (_glFogCoordPointer)
{
if (!_fogArray._enabled || _fogArray._dirty)
{
_fogArray._enabled = true;
glEnableClientState(GL_FOG_COORDINATE_ARRAY);
}
//if (_fogArray._pointer!=ptr || _fogArray._dirty)
{
_fogArray._pointer=ptr;
_glFogCoordPointer( type, stride, ptr );
}
_fogArray._lazy_disable = false;
_fogArray._dirty = false;
}
}
}
void State::setSecondaryColorPointer( GLint size, GLenum type,
GLsizei stride, const GLvoid *ptr )
{
if (_glSecondaryColorPointer)
if (_useVertexAttributeAliasing)
{
if (!_secondaryColorArray._enabled || _secondaryColorArray._dirty)
setVertexAttribPointer(_secondaryColorAlias._location, size, type, GL_FALSE, stride, ptr);
}
else
{
if (_glSecondaryColorPointer)
{
_secondaryColorArray._enabled = true;
glEnableClientState(GL_SECONDARY_COLOR_ARRAY);
if (!_secondaryColorArray._enabled || _secondaryColorArray._dirty)
{
_secondaryColorArray._enabled = true;
glEnableClientState(GL_SECONDARY_COLOR_ARRAY);
}
//if (_secondaryColorArray._pointer!=ptr || _secondaryColorArray._dirty)
{
_secondaryColorArray._pointer=ptr;
_glSecondaryColorPointer( size, type, stride, ptr );
}
_secondaryColorArray._lazy_disable = false;
_secondaryColorArray._dirty = false;
}
//if (_secondaryColorArray._pointer!=ptr || _secondaryColorArray._dirty)
{
_secondaryColorArray._pointer=ptr;
_glSecondaryColorPointer( size, type, stride, ptr );
}
_secondaryColorArray._dirty = false;
}
}
@@ -861,20 +906,25 @@ void State::setVertexAttribPointer( unsigned int index,
{
if (_glVertexAttribPointer)
{
// osg::notify(osg::NOTICE)<<"State::setVertexAttribPointer("<<index<<",...)"<<std::endl;
if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1);
EnabledArrayPair& eap = _vertexAttribArrayList[index];
if (!eap._enabled || eap._dirty)
{
eap._enabled = true;
// osg::notify(osg::NOTICE)<<" _glEnableVertexAttribArray( "<<index<<" )"<<std::endl;
_glEnableVertexAttribArray( index );
}
//if (eap._pointer != ptr || eap._normalized!=normalized || eap._dirty)
{
// osg::notify(osg::NOTICE)<<" _glVertexAttribPointer( "<<index<<" )"<<std::endl;
_glVertexAttribPointer( index, size, type, normalized, stride, ptr );
eap._pointer = ptr;
eap._normalized = normalized;
}
eap._lazy_disable = false;
eap._dirty = false;
}
}
@@ -892,6 +942,7 @@ void State::disableVertexAttribPointer( unsigned int index )
{
eap._enabled = false;
eap._dirty = false;
// osg::notify(osg::NOTICE)<<" _glDisableVertexAttribArray( "<<index<<" )"<<std::endl;
_glDisableVertexAttribArray( index );
}
}
@@ -908,6 +959,7 @@ void State::disableVertexAttribPointersAboveAndIncluding( unsigned int index )
{
eap._enabled = false;
eap._dirty = false;
// osg::notify(osg::NOTICE)<<" State::disableVertexAttribPointersAboveAndIncluding(): _glDisableVertexAttribArray( "<<index<<" )"<<std::endl;
_glDisableVertexAttribArray( index );
}
++index;
@@ -915,6 +967,57 @@ void State::disableVertexAttribPointersAboveAndIncluding( unsigned int index )
}
}
void State::lazyDisablingOfVertexAttributes()
{
// osg::notify(osg::NOTICE)<<"lazyDisablingOfVertexAttributes()"<<std::endl;
if (!_useVertexAttributeAliasing)
{
_vertexArray._lazy_disable = true;
_normalArray._lazy_disable = true;
_colorArray._lazy_disable = true;
_secondaryColorArray._lazy_disable = true;
_indexArray._lazy_disable = true;
_fogArray._lazy_disable = true;
for(EnabledTexCoordArrayList::iterator itr = _texCoordArrayList.begin();
itr != _texCoordArrayList.end();
++itr)
{
itr->_lazy_disable = true;
}
}
for(EnabledVertexAttribArrayList::iterator itr = _vertexAttribArrayList.begin();
itr != _vertexAttribArrayList.end();
++itr)
{
itr->_lazy_disable = true;
}
}
void State::applyDisablingOfVertexAttributes()
{
//osg::notify(osg::NOTICE)<<"start of applyDisablingOfVertexAttributes()"<<std::endl;
if (!_useVertexAttributeAliasing)
{
if (_vertexArray._lazy_disable) disableVertexPointer();
if (_normalArray._lazy_disable) disableNormalPointer();
if (_colorArray._lazy_disable) disableColorPointer();
if (_secondaryColorArray._lazy_disable) disableSecondaryColorPointer();
if (_indexArray._lazy_disable) disableIndexPointer();
if (_fogArray._lazy_disable) disableFogCoordPointer();
for(unsigned int i=0; i<_texCoordArrayList.size(); ++i)
{
if (_texCoordArrayList[i]._lazy_disable) disableTexCoordPointer(i);
}
}
for(unsigned int i=0; i<_vertexAttribArrayList.size(); ++i)
{
if (_vertexAttribArrayList[i]._lazy_disable) disableVertexAttribPointer(i);
}
// osg::notify(osg::NOTICE)<<"end of applyDisablingOfVertexAttributes()"<<std::endl;
}
bool State::computeSecondaryColorSupported() const
{
_isSecondaryColorSupportResolved = true;
@@ -999,4 +1102,163 @@ void State::applyModelViewAndProjectionUniformsIfRequired()
if (_modelViewMatrixUniform.valid()) _lastAppliedProgramObject->apply(*_modelViewMatrixUniform);
if (_projectionMatrixUniform) _lastAppliedProgramObject->apply(*_projectionMatrixUniform);
if (_modelViewProjectionMatrixUniform) _lastAppliedProgramObject->apply(*_modelViewProjectionMatrixUniform);
if (_normalMatrixUniform) _lastAppliedProgramObject->apply(*_normalMatrixUniform);
}
namespace State_Utils
{
bool replace(std::string& str, const std::string& original_phrase, const std::string& new_phrase)
{
bool replacedStr = false;
std::string::size_type pos = 0;
while((pos=str.find(original_phrase, pos))!=std::string::npos)
{
std::string::size_type endOfPhrasePos = pos+original_phrase.size();
if (endOfPhrasePos<str.size())
{
char c = str[endOfPhrasePos];
if ((c>='0' && c<='9') ||
(c>='a' && c<='z') ||
(c>='A' && c<='Z'))
{
pos = endOfPhrasePos;
continue;
}
}
replacedStr = true;
str.replace(pos, original_phrase.size(), new_phrase);
}
return replacedStr;
}
void replaceAndInsertDeclaration(std::string& source, const std::string& originalStr, const std::string& newStr, const std::string& declarationPrefix)
{
if (replace(source, originalStr, newStr))
{
source.insert(0, declarationPrefix + newStr + std::string(";\n"));
}
}
}
bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
{
osg::notify(osg::NOTICE)<<"State::convertShaderSourceToOsgBuiltIns()"<<std::endl;
osg::notify(osg::NOTICE)<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl;
// replace ftransform as it only works with built-ins
State_Utils::replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex");
State_Utils::replaceAndInsertDeclaration(source, "gl_Normal", "osg_Normal", "attribute vec3 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_Vertex", "osg_Vertex", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_Color", "osg_Color", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_SecondaryColor", "osg_SecondaryColor", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_FogCoord", "osg_FogCoord", "attribute float ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord0", "osg_MultiTexCoord0", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord1", "osg_MultiTexCoord1", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord2", "osg_MultiTexCoord2", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord3", "osg_MultiTexCoord3", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord4", "osg_MultiTexCoord4", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord5", "osg_MultiTexCoord5", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord6", "osg_MultiTexCoord6", "attribute vec4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_MultiTexCoord7", "osg_MultiTexCoord7", "attribute vec4 ");
// replace built in uniform
State_Utils::replaceAndInsertDeclaration(source, "gl_ModelViewMatrix", "osg_ModeViewMatrix", "uniform mat4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix", "uniform mat4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix", "uniform mat4 ");
State_Utils::replaceAndInsertDeclaration(source, "gl_NormalMatrix", "osg_NormalMatrix", "uniform mat3 ");
osg::notify(osg::NOTICE)<<"-------- Converted source "<<std::endl<<source<<std::endl<<"----------------"<<std::endl;
return true;
}
void State::setUpVertexAttribAlias(VertexAttribAlias& alias, GLuint location, const std::string glName, const std::string osgName, const std::string& declaration)
{
alias = VertexAttribAlias(location, glName, osgName, declaration);
_attributeBindingList[osgName] = location;
}
void State::applyProjectionMatrix(const osg::RefMatrix* matrix)
{
if (_projection!=matrix)
{
if (matrix)
{
_projection=matrix;
}
else
{
_projection=_identity;
}
if (_useModelViewAndProjectionUniforms)
{
if (_projectionMatrixUniform.valid()) _projectionMatrixUniform->set(*_projection);
updateModelViewAndProjectionMatrixUniforms();
}
glMatrixMode( GL_PROJECTION );
glLoadMatrix(_projection->ptr());
glMatrixMode( GL_MODELVIEW );
}
}
void State::applyModelViewMatrix(const osg::RefMatrix* matrix)
{
if (_modelView!=matrix)
{
if (matrix)
{
_modelView=matrix;
}
else
{
_modelView=_identity;
}
if (_useModelViewAndProjectionUniforms)
{
if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView);
updateModelViewAndProjectionMatrixUniforms();
}
glLoadMatrix(_modelView->ptr());
}
}
#include <osg/io_utils>
void State::updateModelViewAndProjectionMatrixUniforms()
{
if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection));
if (_normalMatrixUniform.valid())
{
#if 0
Matrix mv(*_modelView);
mv.setTrans(0.0, 0.0, 0.0);
Matrix matrix;
matrix.invert(mv);
#else
Matrix matrix = *_modelView;
#endif
#if 0
Matrix3 normalMatrix(matrix(0,0), matrix(1,0), matrix(2,0),
matrix(0,1), matrix(1,1), matrix(2,1),
matrix(0,2), matrix(1,2), matrix(2,2));
#else
Matrix3 normalMatrix(matrix(0,0), matrix(0,0), matrix(0,0),
matrix(1,0), matrix(1,1), matrix(1,0),
matrix(2,0), matrix(2,0), matrix(2,2));
#endif
osg::notify(osg::NOTICE)<<"modelview "<<*_modelView<<std::endl;
_normalMatrixUniform->set(normalMatrix);
}
}

View File

@@ -35,8 +35,11 @@ using namespace osgUtil;
SceneGraphBuilder::SceneGraphBuilder():
_statesetAssigned(false),
_normalSet(false),
_normal(0.0f,0.0f,1.0f),
_colorSet(false),
_color(1.0f,1.0f,1.0f,1.0f),
_maxNumTexCoordComponents(0),
_texCoord(0.f,0.0f,0.0f,1.0f)
{
}