obj.cpp
来自「最新osg包」· C++ 代码 · 共 736 行 · 第 1/2 页
CPP
736 行
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 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 <iostream>#include <sstream>#include <string>#include <stdio.h>#include "obj.h"#include <osg/Notify>#include <osgDB/FileUtils>#include <osgDB/FileNameUtils>#include <osgDB/fstream>using namespace obj;static std::string strip( const std::string& ss ){ std::string result; result.assign( ss.begin() + ss.find_first_not_of( ' ' ), ss.begin() + 1 + ss.find_last_not_of( ' ' ) ); return( result );}/* * parse a subset of texture options, following * http://local.wasp.uwa.edu.au/~pbourke/dataformats/mtl/ */static std::string parseTexture( const std::string& ss, Material& mat){ std::string s(ss); for (;;) { if (s[0] != '-') break; int n; if (s[1] == 's' || s[1] == 'o') { float x, y, z; if (sscanf(s.c_str(), "%*s %f %f %f%n", &x, &y, &z, &n) != 3) { break; } if (s[1] == 's') { // texture scale mat.uScale = x; mat.vScale = y; } else if (s[1] == 'o') { // texture offset mat.uOffset = x; mat.vOffset = y; } } else if (s[1] == 'm' && s[2] == 'm') { // texture color offset and gain float base, gain; if (sscanf(s.c_str(), "%*s %f %f%n", &base, &gain, &n) != 2) { break; } // UNUSED } else if (s[1] == 'b' && s[2] == 'm') { // blend multiplier float mult; if (sscanf(s.c_str(), "%*s %f%n", &mult, &n) != 2) { break; } // UNUSED } else break; s = strip(s.substr(n)); } return s;}bool Model::readline(std::istream& fin, char* line, const int LINE_SIZE){ if (LINE_SIZE<1) return false; bool eatWhiteSpaceAtStart = true; bool changeTabsToSpaces = true; char* ptr = line; char* end = line+LINE_SIZE-1; bool skipNewline = false; while (fin && ptr<end) { int c=fin.get(); int p=fin.peek(); if (c=='\r') { if (p=='\n') { // we have a windows line endings. fin.get(); // osg::notify(osg::NOTICE)<<"We have dos line ending"<<std::endl; if (skipNewline) { skipNewline = false; continue; } else break; } // we have Mac line ending // osg::notify(osg::NOTICE)<<"We have mac line ending"<<std::endl; if (skipNewline) { skipNewline = false; continue; } else break; } else if (c=='\n') { // we have unix line ending. // osg::notify(osg::NOTICE)<<"We have unix line ending"<<std::endl; if (skipNewline) { skipNewline = false; continue; } else break; } else if (c=='\\' && (p=='\r' || p=='\n')) { // need to keep return; skipNewline = true; } else if (c!=std::ifstream::traits_type::eof()) // don't copy eof. { skipNewline = false; if (!eatWhiteSpaceAtStart || (c!=' ' && c!='\t')) { eatWhiteSpaceAtStart = false; *ptr++ = c; } } } // strip trailing spaces while (ptr>line && *(ptr-1)==' ') { --ptr; } *ptr = 0; if (changeTabsToSpaces) { for(ptr = line; *ptr != 0; ++ptr) { if (*ptr == '\t') *ptr=' '; } } return true;}std::string Model::lastComponent(const char* linep){ std::string line = std::string(linep); int space = line.find_last_of(" "); if (space >= 0) { line = line.substr(space+1); } return line;}bool Model::readMTL(std::istream& fin){ osg::notify(osg::INFO)<<"Reading MTL file"<<std::endl; const int LINE_SIZE = 4096; char line[LINE_SIZE]; float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f; Material* material = 0;// &(materialMap[""]); std::string filename; while (fin) { readline(fin,line,LINE_SIZE); if (line[0]=='#' || line[0]=='$') { // comment line // osg::notify(osg::NOTICE) <<"Comment: "<<line<<std::endl; } else if (strlen(line)>0) { if (strncmp(line,"newmtl ",7)==0) { std::string materialName(line+7); material = & materialMap[materialName]; material->name = materialName; } else if (material) { if (strncmp(line,"Ka ",3)==0) { unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); if (fieldsRead==1) { material->ambient[ 0 ] = r; } else if (fieldsRead==2) { material->ambient[ 0 ] = r; material->ambient[ 1 ] = g; } else if (fieldsRead==3) { material->ambient[ 0 ] = r; material->ambient[ 1 ] = g; material->ambient[ 2 ] = b; } else if (fieldsRead==4) { material->ambient[ 0 ] = r; material->ambient[ 1 ] = g; material->ambient[ 2 ] = b; material->ambient[ 3 ] = a; } } else if (strncmp(line,"Kd ",3)==0) { unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); if (fieldsRead==1) { material->diffuse[ 0 ] = r; } else if (fieldsRead==2) { material->diffuse[ 0 ] = r; material->diffuse[ 1 ] = g; } else if (fieldsRead==3) { material->diffuse[ 0 ] = r; material->diffuse[ 1 ] = g; material->diffuse[ 2 ] = b; } else if (fieldsRead==4) { material->diffuse[ 0 ] = r; material->diffuse[ 1 ] = g; material->diffuse[ 2 ] = b; material->diffuse[ 3 ] = a; } } else if (strncmp(line,"Ks ",3)==0) { unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); if (fieldsRead==1) { material->specular[ 0 ] = r; } else if (fieldsRead==2) { material->specular[ 0 ] = r; material->specular[ 1 ] = g; } else if (fieldsRead==3) { material->specular[ 0 ] = r; material->specular[ 1 ] = g; material->specular[ 2 ] = b; } else if (fieldsRead==4) { material->specular[ 0 ] = r; material->specular[ 1 ] = g; material->specular[ 2 ] = b; material->specular[ 3 ] = a; } } else if (strncmp(line,"Ke ",3)==0) { unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); if (fieldsRead==1) { material->emissive[ 0 ] = r; } else if (fieldsRead==2) { material->emissive[ 0 ] = r; material->emissive[ 1 ] = g; } else if (fieldsRead==3) { material->emissive[ 0 ] = r; material->emissive[ 1 ] = g; material->emissive[ 2 ] = b; } else if (fieldsRead==4) { material->emissive[ 0 ] = r; material->emissive[ 1 ] = g; material->emissive[ 2 ] = b; material->emissive[ 3 ] = a; } } else if (strncmp(line,"Tf ",3)==0) { unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); if (fieldsRead==1) { material->Tf[ 0 ] = r; } else if (fieldsRead==2) { material->Tf[ 0 ] = r; material->Tf[ 1 ] = g; } else if (fieldsRead==3) { material->Tf[ 0 ] = r; material->Tf[ 1 ] = g; material->Tf[ 2 ] = b; } else if (fieldsRead==4) { material->Tf[ 0 ] = r; material->Tf[ 1 ] = g; material->Tf[ 2 ] = b; material->Tf[ 3 ] = a; } } else if (strncmp(line,"sharpness ",10)==0) { float sharpness = 0.0f; unsigned int fieldsRead = sscanf(line+10,"%f", &sharpness); if (fieldsRead==1) material->sharpness = sharpness; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?