int main(int argc, char *argv[]) { // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate support for ARB_vertex_program."); arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); // construct the viewer. osgViewer::Viewer viewer; // add the stats handler viewer.addEventHandler(new osgViewer::StatsHandler); std::string shader("simple"); while(arguments.read("-s",shader)) {} std::string textureFileName("Images/lz.rgb"); while(arguments.read("-t",textureFileName)) {} std::string terrainFileName(""); while(arguments.read("-d",terrainFileName)) {} bool dynamic = true; while(arguments.read("--static")) { dynamic = false; } bool vbo = false; while(arguments.read("--vbo")) { vbo = true; } // if user request help write it out to cout. if (arguments.read("-h") || arguments.read("--help")) { arguments.getApplicationUsage()->write(std::cout); return 1; } // load the nodes from the commandline arguments. osg::Node* model = createModel(shader,textureFileName,terrainFileName, dynamic, vbo); if (!model) { return 1; } viewer.setSceneData(model); return viewer.run(); }
void ResourceModel::LoadMaterialLibrary(const std::string& filename) { std::string materialFilename = filename; std::string path = filename.substr(0, filename.find_last_of("\\/") + 1); // We keep the final dash // Replace the last character of the extension - tmf becomes tml materialFilename[filename.size()-1] = 'l'; std::ifstream fp; fp.open(materialFilename.c_str(), std::ios::in); if (!fp.is_open()) { FrameWork::GetLogger()->LogError("Couldn't load material library for %s", filename); return; } Material* currentMaterial = 0; char buffer[256]; char shaderName[64]; char parameterType[16]; char parameterName[64]; char parameterValue[64]; memset(shaderName, 0, sizeof(shaderName)); memset(parameterType, 0, sizeof(parameterType)); memset(parameterName, 0, sizeof(parameterName)); memset(parameterValue, 0, sizeof(parameterValue)); int numTextureMap = 0; while (fp.getline(buffer, sizeof(buffer))) { if (buffer[0] != ' ' && buffer[0] != '\n') { if (currentMaterial) { numTextureMap = 0; currentMaterial->shader = new Shader(shaderName, currentMaterial->shaderParameters); mMaterialList.push_back(currentMaterial); } currentMaterial = new Material(); currentMaterial->name = std::string(buffer); currentMaterial->shaderParameters = new ShaderParameters(); currentMaterial->shader = 0; continue; } sscanf_s(buffer, " %s", parameterType, sizeof(parameterType)); std::string sparameterType(parameterType); if (sparameterType == "INT") { int v = 0; sscanf_s(buffer, "%*s %s %d", parameterName, sizeof(parameterName), &v); currentMaterial->shaderParameters->SetParameter(parameterName, PARAMETER_TYPE_INT, v); } else if (sparameterType == "FLOAT") { float v = 0.0f; sscanf_s(buffer, "%*s %s %f", parameterName, sizeof(parameterName), &v); currentMaterial->shaderParameters->SetParameter(parameterName, PARAMETER_TYPE_FLOAT, v); } else if (sparameterType == "FLOAT2") { float v1, v2; sscanf_s(buffer, "%*s %s %f %f", parameterName, sizeof(parameterName), &v1, &v2); currentMaterial->shaderParameters->SetParameter(parameterName, PARAMETER_TYPE_FLOAT2, float2(v1, v2)); } else if (sparameterType == "FLOAT3") { float v1, v2, v3; sscanf_s(buffer, "%*s %s %f %f %f", parameterName, sizeof(parameterName), &v1, &v2, &v3); currentMaterial->shaderParameters->SetParameter(parameterName, PARAMETER_TYPE_FLOAT3, float3(v1, v2, v3)); } else if (sparameterType == "FLOAT4") { float v1, v2, v3, v4; sscanf_s(buffer, "%*s %s %f %f %f %f", parameterName, sizeof(parameterName), &v1, &v2, &v3, &v4); currentMaterial->shaderParameters->SetParameter(parameterName, PARAMETER_TYPE_FLOAT4, float4(v1, v2, v3, v4)); } else if (sparameterType == "TEXTUREMAP") { sscanf_s(buffer, "%*s %s", parameterName, sizeof(parameterName)); std::string textureFileName(path); textureFileName += parameterName; ResourceImage* image = (ResourceImage*)FrameWork::GetResourceManager()->GetResource(textureFileName); currentMaterial->shaderParameters->SetTextureMap((TextureMap)numTextureMap, image->GetTexture()); numTextureMap++; } else if (sparameterType == "SHADER") { sscanf_s(buffer, "%*s %s", shaderName, sizeof(parameterName)); } } if (currentMaterial) { currentMaterial->shader = new Shader(shaderName, currentMaterial->shaderParameters); mMaterialList.push_back(currentMaterial); } fp.close(); }
void writeFrames(const Kinect::FrameSource::IntrinsicParameters& ip,const Kinect::FrameBuffer& color,const Kinect::MeshBuffer& mesh,const char* lwoFileName) { /* Create the texture file name: */ std::string textureFileName(lwoFileName,Misc::getExtension(lwoFileName)); textureFileName.append("-color.png"); /* Write the color frame as a texture image: */ { Images::RGBImage texImage(color.getSize(0),color.getSize(1)); Images::RGBImage::Color* tiPtr=texImage.modifyPixels(); const unsigned char* cfPtr=reinterpret_cast<const unsigned char*>(color.getBuffer()); for(int y=0;y<color.getSize(1);++y) for(int x=0;x<color.getSize(0);++x,++tiPtr,cfPtr+=3) *tiPtr=Images::RGBImage::Color(cfPtr); Images::writeImageFile(texImage,textureFileName.c_str()); } /* Open the LWO file: */ IO::FilePtr lwoFile=IO::openFile(lwoFileName,IO::File::WriteOnly); lwoFile->setEndianness(Misc::BigEndian); /* Create the LWO file structure via the FORM chunk: */ { IFFChunkWriter form(lwoFile,"FORM"); form.write<char>("LWO2",4); /* Create the TAGS chunk: */ { IFFChunkWriter tags(&form,"TAGS"); tags.writeString("ColorImage"); tags.writeChunk(); } /* Create the LAYR chunk: */ { IFFChunkWriter layr(&form,"LAYR"); layr.write<Misc::UInt16>(0U); layr.write<Misc::UInt16>(0x0U); for(int i=0;i<3;++i) layr.write<Misc::Float32>(0.0f); layr.writeString("DepthImage"); layr.writeChunk(); } /* Create an index map for all vertices to omit unused vertices: */ unsigned int* indices=new unsigned int[mesh.numVertices]; for(unsigned int i=0;i<mesh.numVertices;++i) indices[i]=~0x0U; unsigned int numUsedVertices=0; /* Create the PNTS, BBOX and VMAP chunks in one go: */ { typedef Kinect::FrameSource::IntrinsicParameters::PTransform PTransform; typedef PTransform::Point Point; typedef Geometry::Box<Point::Scalar,3> Box; IFFChunkWriter bbox(&form,"BBOX"); IFFChunkWriter pnts(&form,"PNTS"); IFFChunkWriter vmap(&form,"VMAP"); /* Write the VMAP header: */ vmap.write<char>("TXUV",4); vmap.write<Misc::UInt16>(2U); vmap.writeString("ColorImageUV"); /* Process all triangle vertices: */ Box pBox=Box::empty; const Kinect::MeshBuffer::Vertex* vertices=mesh.getVertices(); const Kinect::MeshBuffer::Index* tiPtr=mesh.getTriangleIndices(); for(unsigned int i=0;i<mesh.numTriangles*3;++i,++tiPtr) { /* Check if the triangle vertex doesn't already have an index: */ if(indices[*tiPtr]==~0x0U) { /* Assign an index to the triangle vertex: */ indices[*tiPtr]=numUsedVertices; /* Transform the mesh vertex to camera space using the depth projection matrix: */ Point dp(vertices[*tiPtr].position.getXyzw()); Point cp=ip.depthProjection.transform(dp); /* Transform the depth-space point to texture space using the color projection matrix: */ Point tp=ip.colorProjection.transform(dp); /* Add the point to the bounding box: */ pBox.addPoint(cp); /* Store the point and its texture coordinates: */ pnts.writePoint(cp); vmap.writeVarIndex(numUsedVertices); for(int i=0;i<2;++i) vmap.write<Misc::Float32>(tp[i]); ++numUsedVertices; } } /* Write the bounding box: */ bbox.writeBox(pBox); /* Write the BBOX, PNTS, and VMAP chunks: */ bbox.writeChunk(); pnts.writeChunk(); vmap.writeChunk(); } /* Create the POLS chunk: */ { IFFChunkWriter pols(&form,"POLS"); pols.write<char>("FACE",4); const Kinect::MeshBuffer::Index* tiPtr=mesh.getTriangleIndices(); for(unsigned int triangleIndex=0;triangleIndex<mesh.numTriangles;++triangleIndex,tiPtr+=3) { pols.write<Misc::UInt16>(3U); for(int i=0;i<3;++i) pols.writeVarIndex(indices[tiPtr[2-i]]); } pols.writeChunk(); } /* Delete the vertex index map: */ delete[] indices; /* Create the PTAG chunk: */ { IFFChunkWriter ptag(&form,"PTAG"); ptag.write<char>("SURF",4); for(unsigned int triangleIndex=0;triangleIndex<mesh.numTriangles;++triangleIndex) { ptag.writeVarIndex(triangleIndex); ptag.write<Misc::UInt16>(0U); } ptag.writeChunk(); } /* Create the CLIP chunk: */ { IFFChunkWriter clip(&form,"CLIP"); clip.write<Misc::UInt32>(1U); /* Create the STIL chunk: */ { IFFChunkWriter stil(&clip,"STIL",true); stil.writeString(textureFileName.c_str()); stil.writeChunk(); } clip.writeChunk(); } /* Create the SURF chunk: */ { IFFChunkWriter surf(&form,"SURF"); surf.writeString("ColorImage"); surf.writeString(""); /* Create the SIDE subchunk: */ { IFFChunkWriter side(&surf,"SIDE",true); side.write<Misc::UInt16>(3U); side.writeChunk(); } /* Create the SMAN subchunk: */ { IFFChunkWriter sman(&surf,"SMAN",true); sman.write<Misc::Float32>(Math::rad(90.0f)); sman.writeChunk(); } /* Create the COLR subchunk: */ { IFFChunkWriter colr(&surf,"COLR",true); colr.writeColor(1.0f,1.0f,1.0f); colr.writeVarIndex(0U); colr.writeChunk(); } /* Create the DIFF subchunk: */ { IFFChunkWriter diff(&surf,"DIFF",true); diff.write<Misc::Float32>(1.0f); diff.writeVarIndex(0U); diff.writeChunk(); } /* Create the LUMI subchunk: */ { IFFChunkWriter lumi(&surf,"LUMI",true); lumi.write<Misc::Float32>(0.0f); lumi.writeVarIndex(0U); lumi.writeChunk(); } /* Create the BLOK subchunk: */ { IFFChunkWriter blok(&surf,"BLOK",true); /* Create the IMAP subchunk: */ { IFFChunkWriter imap(&blok,"IMAP",true); imap.writeString("1"); /* Create the CHAN subchunk: */ { IFFChunkWriter chan(&imap,"CHAN",true); chan.write<char>("COLR",4); chan.writeChunk(); } imap.writeChunk(); } /* Create the PROJ subchunk: */ { IFFChunkWriter proj(&blok,"PROJ",true); proj.write<Misc::UInt16>(5U); proj.writeChunk(); } /* Create the IMAG subchunk: */ { IFFChunkWriter imag(&blok,"IMAG",true); imag.writeVarIndex(1U); imag.writeChunk(); } /* Create the VMAP subchunk: */ { IFFChunkWriter vmap(&blok,"VMAP",true); vmap.writeString("ColorImageUV"); vmap.writeChunk(); } blok.writeChunk(); } /* Write the SURF chunk: */ surf.writeChunk(); } /* Write the FORM chunk: */ form.writeChunk(); } }
// Load Material void GLC_3dsToWorld::loadMaterial(Lib3dsMaterial* p3dsMaterial) { GLC_Material* pMaterial= new GLC_Material; // Set the material name const QString materialName(p3dsMaterial->name); pMaterial->setName(materialName); // Check if there is a texture if (p3dsMaterial->texture1_map.name[0]) { const QString textureName(p3dsMaterial->texture1_map.name); // Retrieve the .3ds file path QFileInfo fileInfo(m_FileName); QString textureFileName(fileInfo.absolutePath() + QDir::separator()); textureFileName.append(textureName); // TGA file type are not supported if (!textureName.right(3).contains("TGA", Qt::CaseInsensitive)) { QFile textureFile(textureFileName); if (textureFile.open(QIODevice::ReadOnly)) { // Create the texture and assign it to the material GLC_Texture *pTexture = new GLC_Texture(textureFile); pMaterial->setTexture(pTexture); m_ListOfAttachedFileName << textureFileName; textureFile.close(); } else { QStringList stringList(m_FileName); stringList.append("Open File : " + textureFileName + " failed"); GLC_ErrorLog::addError(stringList); } } else { QStringList stringList(m_FileName); stringList.append("Image : " + textureFileName + " not suported"); GLC_ErrorLog::addError(stringList); } } // Ambient Color QColor ambient; ambient.setRgbF(p3dsMaterial->ambient[0], p3dsMaterial->ambient[1], p3dsMaterial->ambient[2]); ambient.setAlphaF(p3dsMaterial->ambient[3]); pMaterial->setAmbientColor(ambient); // Diffuse Color QColor diffuse; diffuse.setRgbF(p3dsMaterial->diffuse[0], p3dsMaterial->diffuse[1], p3dsMaterial->diffuse[2]); diffuse.setAlphaF(p3dsMaterial->diffuse[3]); pMaterial->setDiffuseColor(diffuse); // Specular Color QColor specular; specular.setRgbF(p3dsMaterial->specular[0], p3dsMaterial->specular[1], p3dsMaterial->specular[2]); specular.setAlphaF(p3dsMaterial->specular[3]); pMaterial->setSpecularColor(specular); // Shininess if (0 != p3dsMaterial->shininess) { float matShininess= p3dsMaterial->shininess * 128.0f; if (matShininess > 128.0f) matShininess= 128.0f; if (matShininess < 5.0f) matShininess= 20.0f; pMaterial->setShininess(matShininess); } // Transparency pMaterial->setOpacity(1.0 - p3dsMaterial->transparency); // Add the material to the hash table m_Materials.insert(materialName, pMaterial); }