00001 #include <esg/OSGPolygon.h>
00002
00003 using namespace esg;
00004
00005 void OSGPolygon::_duplicate_attributes(const Geometry& src)
00006 {
00007
00008
00009
00010
00011 Geometry::_duplicate_attributes(src);
00012
00013
00014
00015 _nVert = ((OSGPolygon&)src)._nVert;
00016 _vertices = ((OSGPolygon&)src)._vertices;
00017 _normals = ((OSGPolygon&)src)._normals;
00018 _normal.set(((OSGPolygon&)src)._normal);
00019 _normalFixed = ((OSGPolygon&)src)._normalFixed;
00020 _fxyz = ((OSGPolygon&)src)._fxyz;
00021 _edgeProjection = ((OSGPolygon&)src)._edgeProjection;
00022 _vindices = NULL;
00023 _nindices = NULL;
00024
00025 _proj = NULL;
00026 _tangible = true;
00027
00028
00029 _firstVertexOfPrimitive = ((OSGPolygon&)src)._firstVertexOfPrimitive;
00030 _firstVertex = ((OSGPolygon&)src)._firstVertex;
00031 _vertexMode = ((OSGPolygon&)src)._vertexMode;
00032 _globalVertexArray = ((OSGPolygon&)src)._globalVertexArray;
00033 _pDrawArray = ((OSGPolygon&)src)._pDrawArray;
00034 }
00035
00036 Vector3 OSGPolygon::_get_vertex(unsigned index) const
00037 {
00038 unsigned int vindex = index;
00039
00040 #if 0
00041 if (_vertexMode == osg::PrimitiveSet::TRIANGLE_FAN && index == 2) {
00042 vindex = _firstVertexOfPrimitive;
00043 } else {
00044
00045 switch (_primitiveType) {
00046 case osg::PrimitiveSet::DrawElementsUBytePrimitiveType:
00047 vindex = (unsigned int) (*static_cast<osg::DrawElementsUByte*>(_pDrawArray))[index];
00048 break;
00049 case osg::PrimitiveSet::DrawElementsUShortPrimitiveType:
00050 vindex = (unsigned int) (*static_cast<osg::DrawElementsUShort*>(_pDrawArray))[index];
00051 break;
00052 case osg::PrimitiveSet::DrawElementsUIntPrimitiveType:
00053 vindex = (unsigned int) (*static_cast<osg::DrawElementsUInt*>(_pDrawArray))[index];
00054 break;
00055 case osg::PrimitiveSet::PrimitiveType:
00056 case osg::PrimitiveSet::DrawArraysPrimitiveType:
00057 case osg::PrimitiveSet::DrawArrayLengthsPrimitiveType:
00058 default:
00059 vindex = index;
00060 break;
00061 }
00062 vindex += _firstVertexOfPrimitive + _firstVertex;
00063 }
00064 #endif
00065
00066
00067 switch (_vertexMode) {
00068 case osg::PrimitiveSet::TRIANGLES:
00069 case osg::PrimitiveSet::QUADS:
00070 case osg::PrimitiveSet::TRIANGLE_STRIP:
00071 case osg::PrimitiveSet::QUAD_STRIP:
00072 case osg::PrimitiveSet::POLYGON:
00073 vindex += _firstVertexOfPrimitive + _firstVertex;
00074 break;
00075 case osg::PrimitiveSet::TRIANGLE_FAN:
00076 if (index < 2) vindex += _firstVertexOfPrimitive + _firstVertex;
00077 vindex = ((index == 2)
00078 ? _firstVertexOfPrimitive
00079 : _firstVertexOfPrimitive + _firstVertex + index);
00080 break;
00081 default:
00082 fprintf(stderr,"OSGPolygon::_get_vertex(): Unsupported vertex mode\n");
00083 return Vector3(0,0,0);
00084 break;
00085 }
00086
00087
00088 switch (_primitiveType) {
00089 case osg::PrimitiveSet::DrawElementsUBytePrimitiveType:
00090 vindex = (*static_cast<osg::DrawElementsUByte*>(_pDrawArray))[vindex];
00091 break;
00092 case osg::PrimitiveSet::DrawElementsUShortPrimitiveType:
00093 vindex = (*static_cast<osg::DrawElementsUShort*>(_pDrawArray))[vindex];
00094 break;
00095 case osg::PrimitiveSet::DrawElementsUIntPrimitiveType:
00096 vindex = (*static_cast<osg::DrawElementsUInt*>(_pDrawArray))[vindex];
00097 break;
00098 case osg::PrimitiveSet::PrimitiveType:
00099 case osg::PrimitiveSet::DrawArraysPrimitiveType:
00100 case osg::PrimitiveSet::DrawArrayLengthsPrimitiveType:
00101
00102 break;
00103 default:
00104 break;
00105 }
00106
00107 return Vector3((*_globalVertexArray)[vindex].x(),
00108 (*_globalVertexArray)[vindex].y(),
00109 (*_globalVertexArray)[vindex].z());
00110 }
00111
00112
00113 Vector3 OSGPolygon::_get_vert_normal(unsigned index) const
00114 {
00115 unsigned int vindex = index;
00116
00117 if (_normal_binding == osg::Geometry::BIND_PER_VERTEX) {
00118
00119 switch (_vertexMode) {
00120 case osg::PrimitiveSet::TRIANGLES:
00121 case osg::PrimitiveSet::QUADS:
00122 case osg::PrimitiveSet::TRIANGLE_STRIP:
00123 case osg::PrimitiveSet::QUAD_STRIP:
00124 case osg::PrimitiveSet::POLYGON:
00125 vindex += _firstVertexOfPrimitive + _firstVertex;
00126 break;
00127 case osg::PrimitiveSet::TRIANGLE_FAN:
00128 if (index < 2) vindex += _firstVertexOfPrimitive + _firstVertex;
00129 vindex = ((index == 2)
00130 ? _firstVertexOfPrimitive
00131 : _firstVertexOfPrimitive + _firstVertex + index);
00132 break;
00133 default:
00134 fprintf(stderr,"OSGPolygon::_get_vertex(): Unsupported vertex mode\n");
00135 return Vector3(0,0,0);
00136 break;
00137 }
00138
00139
00140 switch (_primitiveType) {
00141 case osg::PrimitiveSet::DrawElementsUBytePrimitiveType:
00142 vindex = (*static_cast<osg::DrawElementsUByte*>(_pDrawArray))[vindex];
00143 break;
00144 case osg::PrimitiveSet::DrawElementsUShortPrimitiveType:
00145 vindex = (*static_cast<osg::DrawElementsUShort*>(_pDrawArray))[vindex];
00146 break;
00147 case osg::PrimitiveSet::DrawElementsUIntPrimitiveType:
00148 vindex = (*static_cast<osg::DrawElementsUInt*>(_pDrawArray))[vindex];
00149 break;
00150 case osg::PrimitiveSet::PrimitiveType:
00151 case osg::PrimitiveSet::DrawArraysPrimitiveType:
00152 case osg::PrimitiveSet::DrawArrayLengthsPrimitiveType:
00153
00154 break;
00155 default:
00156 break;
00157 }
00158
00159 return Vector3((*_globalNormalArray)[vindex].x(),
00160 (*_globalNormalArray)[vindex].y(),
00161 (*_globalNormalArray)[vindex].z());
00162
00163 }
00164
00165 fprintf(stderr,"OSGPolygon::_get_vert_normal(): Non-existing vertex normal required.\n");
00166 return Vector3(0.0, 0.0, 0.0);
00167 }
00168
00169
00170
00171 OSGPolygon::OSGPolygon(GLint fgv,
00172 GLint fv,
00173 osg::PrimitiveSet::Type t,
00174 osg::PrimitiveSet::Mode m,
00175 osg::Vec3Array * a,
00176 osg::PrimitiveSet * d,
00177 osg::Vec3Array * n,
00178 osg::Geometry::AttributeBinding nb)
00179 : _firstVertexOfPrimitive(fgv),
00180 _firstVertex(fv),
00181 _primitiveType(t),
00182 _vertexMode(m),
00183 _globalVertexArray(a),
00184 _pDrawArray(d),
00185 _globalNormalArray(n),
00186 _normal_binding(nb)
00187 {
00188 _vertices = NULL;
00189 _normals = NULL;
00190 _vindices = NULL;
00191 _nindices = NULL;
00192
00193 if (_vertexMode == osg::PrimitiveSet::TRIANGLES ||
00194 _vertexMode == osg::PrimitiveSet::TRIANGLE_STRIP ||
00195 _vertexMode == osg::PrimitiveSet::TRIANGLE_FAN) _nVert = 3;
00196 else
00197 if (_vertexMode == osg::PrimitiveSet::QUADS ||
00198 _vertexMode == osg::PrimitiveSet::QUAD_STRIP) _nVert = 4;
00199 else _nVert = 0;
00200
00201 _normal.set(_get_facet_normal());
00202 _normalFixed = false;
00203 _fxyz = - _normal.dot(_get_vertex(0));
00204 _set_edge_projection();
00205
00206 _tangible = true;
00207 }
00208
00209 SceneGraphObject* OSGPolygon::clone() const
00210 {
00211 OSGPolygon* pRet = new OSGPolygon();
00212 pRet->_duplicate_attributes(*this);
00213 return (SceneGraphObject*) pRet;
00214 }
00215
00216 Geometry* OSGPolygon::clone(const Matrix4* pTrMat) const
00217 {
00218 OSGPolygon* pRet = new OSGPolygon();
00219 pRet->_duplicate_attributes(*this);
00220 if (pTrMat) pRet->_transform(*pTrMat);
00221 return pRet;
00222 }
00223
00224 Vertex3 OSGPolygon::getVertex(unsigned index) const
00225 {
00226 return ((_pDrawArray && _globalVertexArray)
00227 ? _get_vertex(index)
00228 :Vertex3(0,0,0));
00229 }
00230
00231 Vertex3 OSGPolygon::getVertNormal(unsigned index) const
00232 {
00233 return ((haveVertNormals()) ? _get_vert_normal(index) : Vertex3(0,0,0));
00234 }
00235
00236 bool OSGPolygon::haveVertNormals() const
00237 {
00238 return (_pDrawArray &&
00239 _globalNormalArray &&
00240 _normal_binding == osg::Geometry::BIND_PER_VERTEX);
00241 }
00242