void SGQuad::setup(float w, float h) { float hw = 0.5f * w; float hh = 0.5f * h; vec3f n = vec3f(0,0,1); Geometry g; g.init(); g.addVertex(vec3f(-hw, -hh, 0.0f)); g.addVertex(vec3f(hw, -hh, 0.0f)); g.addVertex(vec3f(hw, hh, 0.0f)); g.addVertex(vec3f(-hw, hh, 0.0f)); g.addNormal(n); g.addNormal(n); g.addNormal(n); g.addNormal(n); g.addTexCoord(vec2f(0, 0)); g.addTexCoord(vec2f(1, 0)); g.addTexCoord(vec2f(1, 1)); g.addTexCoord(vec2f(0, 1)); g.addTriangle(vec3u32(0, 1, 2)); g.addTriangle(vec3u32(2, 3, 0)); SGMesh::setup(g); if(TheShaderManager::Instance().has("textured")) { m_spEffect = SmartPtrSGEffect(new TexturedEffect(TheShaderManager::Instance().get("textured"))); } }
bool loadObj( std::vector<Geometry> &geomList, const std::string &filename, float scale, int flags) { std::ifstream file; file.open(filename.c_str(), std::ios::in); std::cout<<"loading "<<filename<<std::endl; if(file.fail()) { std::cout<<"loadObj failed, could not read "<<std::endl; return 1; } VertexBank vb; Geometry g; std::string line,param; std::vector<vec3> tempVertex; std::vector<vec3> tempNormal; std::vector<vec2> tempTexCoord; tempVertex.reserve(10000); tempNormal.reserve(10000); tempTexCoord.reserve(10000); std::vector<std::vector<int> > vertexUsed; std::vector<int> texCoordUsed; int tempSG = 0; std::vector<size_t> vertexRemap; std::vector<size_t> normalRemap; std::vector<size_t> texCoordRemap; std::vector<int> resetVector; resetVector.resize(1,-1); std::string tempName; while( !file.eof() && file.good() ) { std::getline(file,line); #ifdef DEBUG std::cout<<line<<"\n"; #endif Tokenizer token(line); param = token.getToken(); if(param == "v") { vec3 vertex; vertex.x = scale*toFloat(token.getToken()); vertex.y = scale*toFloat(token.getToken()); vertex.z = scale*toFloat(token.getToken()); //tempVertex.push_back(vertex); //vertexUsed.push_back(resetVector); vertexRemap.push_back( insertUnique(tempVertex, vertex) ); } else if(param == "f") { ivec4 vdata(-1), tdata(-1), ndata(-1), fdata(-1); for(int i=0; i<(int)token.size()-1; ++i) { param = token.getToken(); getIndices(param, vdata[i], tdata[i], ndata[i], hasVertex, hasTexCoord && !(flags & LOADOBJ_IGNORE_TEXCOORDS), hasNormal && !(flags & LOADOBJ_IGNORE_NORMALS) ); int remappedV = (vdata[i] > -1) ? vdata[i] : -1; int remappedN = (ndata[i] > -1) ? ndata[i] : -1; int remappedT = (tdata[i] > -1) ? tdata[i] : -1; int index; //printf("Checking vertex uniqueness \n"); if(vb.isUnique(remappedV, remappedN, remappedT, index)) { index = g.getVertexSize(); Geometry::sVertex tv; assert( remappedV < (int)tempVertex.size() ); tv.position = tempVertex[ remappedV ]; if(remappedT > -1) { assert( remappedT < (int)tempTexCoord.size() ); tv.texCoord = tempTexCoord[ remappedT ]; } if(remappedN > -1) { assert( remappedN < (int)tempNormal.size() ); tv.normal = tempNormal[ remappedN ]; } g.addVertex(tv); } assert(index < (int)g.getVertexSize()); fdata[i] = index; // if(tempSG > (int)vertexUsed[vdata[i]].size()-1) // vertexUsed[vdata[i]].resize(tempSG+1,-1); // if(vertexUsed[vdata[i]][tempSG] > -1) // fdata[i] = vertexUsed[vdata[i]][tempSG]; // else // { // vertexUsed[vdata[i]][tempSG] = (int)g.vertices.size(); // fdata[i] = g.getVertexSize(); // Geometry::sVertex tv; // tv.position = tempVertex[vdata[i]]; // //tv.nx = tv.ny = tv.nz = tv.s = tv.t = 0.0f; // if(vtdata[i]>-1 && !(flags & LOADOBJ_IGNORE_TEXCOORDS)) // { // assert( vtdata[i] < tempTexCoord.size() ); // tv.texCoord = tempTexCoord[vtdata[i]]; // } // if(ndata[i]>-1 && !(flags & LOADOBJ_IGNORE_NORMALS)) // { // assert( ndata[i] < tempNormal.size() ); // tv.normal = tempNormal[ndata[i]]; // } // g.addVertex(tv); // } } // if its a triangle, just insert. // However if its a quad, then insert the two triangles forming the quad. uvec3 t; t[0] = fdata[0]; t[1] = fdata[1]; t[2] = fdata[2]; g.addTriangle(t); if(fdata[3] != -1) { t[0] = fdata[3]; t[1] = fdata[0]; t[2] = fdata[2]; g.addTriangle(t); } } else if(param == "vt") { vec2 tc; tc.x = toFloat(token.getToken()); tc.y = toFloat(token.getToken()); //tempTexCoord.push_back(tc); texCoordRemap.push_back( insertUnique(tempTexCoord, tc) ); } else if(param == "vn") { vec3 normal; normal.x = toFloat(token.getToken()); normal.y = toFloat(token.getToken()); normal.z = toFloat(token.getToken()); //tempNormal.push_back(normal); normalRemap.push_back( insertUnique(tempNormal, normal) ); } else if(param == "s") tempSG = toInt(token.getToken()); else if(param == "g") { /*if(first) first=false; else { g.process(); geomList.push_back(g); } for(unsigned int i=0; i<vertexUsed.size(); ++i) vertexUsed[i].clear(); g.clear(); */ } if(file.eof()) break; } file.close(); printf("tempVertex.size() = %i \n", (int)tempVertex.size()); printf("tempNormal.size() = %i \n", (int)tempNormal.size()); printf("tempTexCoord.size() = %i \n", (int)tempTexCoord.size()); printf("Reading is done, gonna process \n"); g.process(); geomList.push_back(g); std::cout<<"done reading "<<filename<<std::endl; return 0; }