Geode* createTextureQuad(Texture2D *texture) { Vec3Array *vertices = new Vec3Array; vertices->push_back(Vec3(-1.0, -1.0, 0.0)); vertices->push_back(Vec3(1.0, -1.0, 0.0)); vertices->push_back(Vec3(1.0, 1.0, 0.0)); vertices->push_back(Vec3(-1.0, 1.0, 0.0)); Vec2Array *texcoord = new Vec2Array; texcoord->push_back(Vec2(0.0, 0.0)); texcoord->push_back(Vec2(1.0, 0.0)); texcoord->push_back(Vec2(1.0, 1.0)); texcoord->push_back(Vec2(0.0, 1.0)); Geometry *geom = new Geometry; geom->setVertexArray(vertices); geom->setTexCoordArray(0, texcoord); geom->addPrimitiveSet(new DrawArrays(GL_QUADS, 0, 4)); Geode *geode = new Geode; geode->addDrawable(geom); geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, StateAttribute::ON); return geode; }
Geode* ChessUtils::createRectangleWithTexture(Vec3 centerPosition, Image* image, int width, int height, Vec4 color) { int halfWidth = width / 2; int halfHeight = height / 2; Vec3Array* vertices = new Vec3Array(); vertices->push_back(Vec3(centerPosition.x() - halfWidth, centerPosition.y() - halfHeight, centerPosition.z())); vertices->push_back(Vec3(centerPosition.x() + halfWidth, centerPosition.y() - halfHeight, centerPosition.z())); vertices->push_back(Vec3(centerPosition.x() + halfWidth, centerPosition.y() + halfHeight, centerPosition.z())); vertices->push_back(Vec3(centerPosition.x() - halfWidth, centerPosition.y() + halfHeight, centerPosition.z())); Vec3Array* normals = new Vec3Array(); normals->push_back(Vec3(0.0f, 0.0f, 1.0f)); Vec2Array* texcoords = new Vec2Array(); texcoords->push_back(Vec2(0.0f, 0.0f)); texcoords->push_back(Vec2(1.0f, 0.0f)); texcoords->push_back(Vec2(1.0f, 1.0f)); texcoords->push_back(Vec2(0.0f, 1.0f)); Vec4Array* colors = new Vec4Array(); colors->push_back(color); Geometry* quad = new Geometry(); quad->setVertexArray(vertices); quad->setNormalArray(normals); quad->setNormalBinding(osg::Geometry::BIND_OVERALL); quad->setColorArray(colors); quad->setColorBinding(osg::Geometry::BIND_OVERALL); quad->setTexCoordArray(0, texcoords); quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4)); Texture2D* texture = new Texture2D(); if (image != NULL) { texture->setImage(image); } Geode* geode = new Geode(); geode->addDrawable(quad); osg::BlendFunc* blendFunc = new osg::BlendFunc(); blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); osg::TexEnv* blendTexEnv = new osg::TexEnv(); blendTexEnv->setMode(osg::TexEnv::BLEND); osg::StateSet* geodeStateset = geode->getOrCreateStateSet(); geodeStateset->setAttributeAndModes(blendFunc); geodeStateset->setTextureAttribute(0, blendTexEnv); geodeStateset->setTextureAttributeAndModes(0, texture); geodeStateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); return geode; }
Array* Array_readLocalData(Input& fr) { if (fr[0].matchWord("Use")) { if (fr[1].isString()) { Object* obj = fr.getObjectForUniqueID(fr[1].getStr()); if (obj) { fr+=2; return dynamic_cast<Array*>(obj); } } osg::notify(osg::WARN)<<"Warning: invalid uniqueID found in file."<<std::endl; return NULL; } std::string uniqueID; if (fr[0].matchWord("UniqueID") && fr[1].isString()) { uniqueID = fr[1].getStr(); fr += 2; } int entry = fr[0].getNoNestedBrackets(); const char* arrayName = fr[0].getStr(); unsigned int capacity = 0; fr[1].getUInt(capacity); ++fr; fr += 2; Array* return_array = 0; if (strcmp(arrayName,"ByteArray")==0) { ByteArray* array = new ByteArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { int int_value; if (fr[0].getInt(int_value)) { ++fr; array->push_back(int_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"ShortArray")==0) { ShortArray* array = new ShortArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { int int_value; if (fr[0].getInt(int_value)) { ++fr; array->push_back(int_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"IntArray")==0) { IntArray* array = new IntArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { int int_value; if (fr[0].getInt(int_value)) { ++fr; array->push_back(int_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UByteArray")==0) { UByteArray* array = new UByteArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int uint_value; if (fr[0].getUInt(uint_value)) { ++fr; array->push_back(uint_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UShortArray")==0) { UShortArray* array = new UShortArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int uint_value; if (fr[0].getUInt(uint_value)) { ++fr; array->push_back(uint_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UIntArray")==0) { UIntArray* array = new UIntArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int uint_value; if (fr[0].getUInt(uint_value)) { ++fr; array->push_back(uint_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"UVec4bArray")==0 || strcmp(arrayName,"Vec4ubArray")==0) { Vec4ubArray* array = new Vec4ubArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b,a; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b) && fr[3].getUInt(a)) { fr+=4; array->push_back(osg::Vec4ub(r,g,b,a)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"FloatArray")==0) { FloatArray* array = new FloatArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { float float_value; if (fr[0].getFloat(float_value)) { ++fr; array->push_back(float_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"DoubleArray")==0) { DoubleArray* array = new DoubleArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { double double_value; if (fr[0].getFloat(double_value)) { ++fr; array->push_back(double_value); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2Array")==0) { Vec2Array* array = new Vec2Array; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec2 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())) { fr += 2; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2dArray")==0) { Vec2dArray* array = new Vec2dArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec2d v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y())) { fr += 2; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3Array")==0) { Vec3Array* array = new Vec3Array; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec3 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z())) { fr += 3; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3dArray")==0) { Vec3dArray* array = new Vec3dArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec3d v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z())) { fr += 3; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4Array")==0) { Vec4Array* array = new Vec4Array; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec4 v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) { fr += 4; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4dArray")==0) { Vec4dArray* array = new Vec4dArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { Vec4d v; if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w())) { fr += 4; array->push_back(v); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2bArray")==0) { Vec2bArray* array = new Vec2bArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g; if (fr[0].getUInt(r) && fr[1].getUInt(g)) { fr+=2; array->push_back(osg::Vec2b(r,g)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3bArray")==0) { Vec3bArray* array = new Vec3bArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b)) { fr+=3; array->push_back(osg::Vec3b(r,g,b)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4bArray")==0) { Vec4bArray* array = new Vec4bArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b,a; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b) && fr[3].getUInt(a)) { fr+=4; array->push_back(osg::Vec4b(r,g,b,a)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec2sArray")==0) { Vec2sArray* array = new Vec2sArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g; if (fr[0].getUInt(r) && fr[1].getUInt(g)) { fr+=2; array->push_back(osg::Vec2s(r,g)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec3sArray")==0) { Vec3sArray* array = new Vec3sArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b)) { fr+=3; array->push_back(osg::Vec3s(r,g,b)); } else ++fr; } ++fr; return_array = array; } else if (strcmp(arrayName,"Vec4sArray")==0) { Vec4sArray* array = new Vec4sArray; array->reserve(capacity); while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) { unsigned int r,g,b,a; if (fr[0].getUInt(r) && fr[1].getUInt(g) && fr[2].getUInt(b) && fr[3].getUInt(a)) { fr+=4; array->push_back(osg::Vec4s(r,g,b,a)); } else ++fr; } ++fr; return_array = array; } if (return_array) { if (!uniqueID.empty()) fr.registerUniqueIDForObject(uniqueID.c_str(),return_array); } return return_array; }
PositionAttitudeTransform *VideoGeode::createVideoPlane(float sizeX, float sizeY, bool texRepeat) { // vertex array Vec3Array *vertexArray = new Vec3Array(); sizeX /= 2.0; sizeY /= 2.0; vertexArray->push_back(Vec3(-sizeX, 0, -sizeY)); vertexArray->push_back(Vec3(sizeX, 0, -sizeY)); vertexArray->push_back(Vec3(sizeX, 0, sizeY)); vertexArray->push_back(Vec3(-sizeX, 0, sizeY)); /*vertexArray->push_back(Vec3(-sizeX, -sizeY, 0)); vertexArray->push_back(Vec3(sizeX, -sizeY, 0)); vertexArray->push_back(Vec3(sizeX, sizeY, 0)); vertexArray->push_back(Vec3(-sizeX, sizeY, 0));*/ // face array DrawElementsUInt *faceArray = new DrawElementsUInt(PrimitiveSet::TRIANGLES, 0); faceArray->push_back(0); // face 1 faceArray->push_back(1); faceArray->push_back(2); faceArray->push_back(2); // face 2 faceArray->push_back(3); faceArray->push_back(0); // normal array Vec3Array *normalArray = new Vec3Array(); normalArray->push_back(Vec3(0, 0, 1)); // normal index TemplateIndexArray<unsigned int, Array::UIntArrayType, 24, 4> *normalIndexArray; normalIndexArray = new TemplateIndexArray<unsigned int, Array::UIntArrayType, 24, 4>(); normalIndexArray->push_back(0); normalIndexArray->push_back(0); normalIndexArray->push_back(0); normalIndexArray->push_back(0); // texture coordinates Vec2Array *texCoords = new Vec2Array(); texCoords->push_back(Vec2(0.0f, 0.0f)); texCoords->push_back(Vec2(1.0f, 0.0f)); texCoords->push_back(Vec2(1.0f, 1.0f)); texCoords->push_back(Vec2(0.0f, 1.0f)); Geometry *geometry = new Geometry(); geometry->setVertexArray(vertexArray); geometry->setNormalArray(normalArray); geometry->setNormalIndices(normalIndexArray); geometry->setNormalBinding(Geometry::BIND_PER_VERTEX); geometry->setTexCoordArray(0, texCoords); geometry->addPrimitiveSet(faceArray); Geode *plane = new Geode(); plane->addDrawable(geometry); // assign the material to the sphere StateSet *planeStateSet = plane->getOrCreateStateSet(); planeStateSet->ref(); planeStateSet->setAttribute(_material); try { planeStateSet->setTextureAttributeAndModes(0, createVideoTexture(texRepeat), StateAttribute::ON); } catch (char *e) { throw e; } PositionAttitudeTransform *planeTransform = new PositionAttitudeTransform(); planeTransform->addChild(plane); return planeTransform; }
osg::Node *JTOpenPlugin::createShape(JtkShape *partShape, const char *objName) { //cout << "JtkSHAPE\n"; Geode *geode = new Geode(); ref_ptr<Geometry> geom = new Geometry(); StateSet *geoState = geode->getOrCreateStateSet(); Vec3Array *vert = new Vec3Array; Vec3Array *normalArray = new Vec3Array(); Vec3Array *colorArray = new Vec3Array(); Vec2Array *tcArray = new Vec2Array(); DrawArrayLengths *primitives = NULL; if (partShape->typeID() == JtkEntity::JtkPOLYGONSET) { primitives = new DrawArrayLengths(PrimitiveSet::POLYGON); } else if (partShape->typeID() == JtkEntity::JtkLINESTRIPSET) { primitives = new DrawArrayLengths(PrimitiveSet::LINE_STRIP); } else if (partShape->typeID() == JtkEntity::JtkTRISTRIPSET) { primitives = new DrawArrayLengths(PrimitiveSet::TRIANGLE_STRIP); } else { cerr << "unknown partShape->typeID " << partShape->typeID() << endl; } geode->setName(objName); if (primitives) { for (int set = 0; set < partShape->numOfSets(); set++) { float *vertex = NULL, *normal = NULL, *color = NULL, *texture = NULL; int vertexCount = -1, normCount = -1, colorCount = -1, textCount = -1; partShape->getInternal(vertex, vertexCount, normal, normCount, color, colorCount, texture, textCount, set); primitives->push_back(vertexCount); // backFaceCulling nur dann, wenn es im CoviseConfig enabled ist /*if(backFaceCulling && (mask & Viewer::MASK_SOLID)) { CullFace *cullFace = new CullFace(); // da viele Modelle backface Culling nicht vertragen (nicht richtig modelliert sind) cullFace->setMode(CullFace::BACK); geoState->setAttributeAndModes(cullFace, StateAttribute::ON); } // already done in updateMaterial() #if 0 if(Blended) { BlendFunc *blendFunc = new BlendFunc(); blendFunc->setFunction(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA); geoState->setAttributeAndModes(blendFunc, StateAttribute::ON); #if 1 AlphaFunc *alphaFunc = new AlphaFunc(); alphaFunc->setFunction(AlphaFunc::ALWAYS,1.0); geoState->setAttributeAndModes(alphaFunc, StateAttribute::OFF); #endif } #endif #ifdef HAVE_OSGNV if((strncmp(d_currentObject->node->name(),"combineTextures",15)==0)||(strncmp(objName,"combineTextures",15)==0)) { geoState->setAttributeAndModes(combineTextures.get(), StateAttribute::ON); } if((strncmp(d_currentObject->node->name(),"combineEnvTextures",15)==0)||(strncmp(objName,"combineEnvTextures",15)==0)) { geoState->setAttributeAndModes(combineEnvTextures.get(), StateAttribute::ON); } #endif*/ if (vertex && (vertexCount > 0)) { for (int elems = 0; elems < vertexCount; elems++) { vert->push_back(Vec3(vertex[elems * 3 + 0], vertex[elems * 3 + 1], vertex[elems * 3 + 2])); } JtkEntityFactory::deleteMemory(vertex); } if (normal && (normCount > 0)) { for (int elems = 0; elems < normCount; elems++) { normalArray->push_back(Vec3(normal[elems * 3 + 0], normal[elems * 3 + 1], normal[elems * 3 + 2])); } if (normCount == vertexCount) { } else { //geom->setNormalBinding(Geometry::BIND_PER_PRIMITIVE); std::cerr << "JTOpen: normals per primitive not supported" << std::endl; } JtkEntityFactory::deleteMemory(normal); } else // generate normals { } if (color && (colorCount > 0)) { for (int elems = 0; elems < colorCount; elems++) { colorArray->push_back(Vec3(color[elems * 3 + 0], color[elems * 3 + 1], color[elems * 3 + 2])); } if (colorCount == vertexCount) { } else { //geom->setColorBinding(Geometry::BIND_PER_PRIMITIVE); std::cerr << "JTOpen: colors per primitive not supported" << std::endl; } JtkEntityFactory::deleteMemory(color); } if (texture && (textCount > 0)) { for (int elems = 0; elems < textCount; elems++) { tcArray->push_back(Vec2(texture[elems * 2 + 0], texture[elems * 2 + 1])); } JtkEntityFactory::deleteMemory(texture); } /* if(!(mask & Viewer::MASK_CONVEX)) { osgUtil::Tesselator *tess = new osgUtil::Tesselator; tess->retesselatePolygons(*geom); //delete[] tess; }*/ // if enabled, generate tri strips, but not for animated objects // if(genStrips && strncmp(objName, "Animated", 8)) { // d_stripper->stripify(*geom); } } geom->setVertexArray(vert); geom->addPrimitiveSet(primitives); if (normalArray->size() > 0) { geom->setNormalArray(normalArray); geom->setNormalBinding(Geometry::BIND_PER_VERTEX); } if (colorArray->size() > 0) { geom->setColorArray(colorArray); geom->setColorBinding(Geometry::BIND_PER_VERTEX); } if (tcArray->size() > 0) geom->setTexCoordArray(0, tcArray); if (normalArray->size() == 0) { osgUtil::SmoothingVisitor::smooth(*(geom.get()), 40.0 / 180.0 * M_PI); } geode->addDrawable(geom.get()); geode->setStateSet(geoState); return geode; } return NULL; }