String VertexFormat::toString() const { const int bufSize = 512; char buf[bufSize+1] = "XYZ"; if ( hasRHW() ) append( buf, bufSize, "RHW" ); if ( hasNormal() ) append( buf, bufSize, "NORMAL" ); if ( hasDiffuse() ) append( buf, bufSize, "DIFFUSE" ); if ( hasSpecular() ) append( buf, bufSize, "SPECULAR" ); char msg[512]; for ( int i = 0 ; i < textureCoordinates() ; ++i ) { sprintf( msg, "TEXCOORDSIZE%i(%i)", getTextureCoordinateSize(i), i ); append( buf, bufSize, msg ); } if ( weights() > 0 ) { sprintf( msg, "WEIGHTS=%i", weights() ); append( buf, bufSize, msg ); } return buf; }
bool Badger::SpriteSheet::textureCoordinates(const char* name, double coords[4]) { for (unsigned int i = 0; i < _sprites.size(); i++) { if (strncmp(name, _sprites[i]->name, 64) == 0) { textureCoordinates(i, coords); return true; } } return false; }
void PHNormalImage::renderInFramePortionTint(PHGameManager * gm, const PHRect & frm,const PHRect & port,const PHColor & tint) { load(); gm->updateMatrixUniform(); const GLfloat squareVertices[] = { frm.x, frm.y, frm.x+frm.width,frm.y, frm.x, frm.y+frm.height, frm.x+frm.width,frm.y+frm.height, }; PHRect r = textureCoordinates(port); const GLfloat squareTexCoords[] = { r.x , r.y, r.x + r.width , r.y, r.x , r.y + r.height, r.x + r.width , r.y + r.height }; int states = PHGLBlending | PHGLVertexArray | PHGLTextureCoordArray | PHGLTexture0; gm->setGLStates(states); gm->setTextureUniform(texture()); gm->setColor(tint); gm->updateColorUniform(); gm->spriteUniformStates()->apply(gm->spriteShader()); if (gm->useShaders()) { PHGL::glVertexAttribPointer(PHIMAGEATTRIBUTE_POS, 2, GL_FLOAT, GL_FALSE, 0, squareVertices); PHGL::glVertexAttribPointer(PHIMAGEATTRIBUTE_TXC, 2, GL_FLOAT, GL_FALSE, 0, squareTexCoords); } else { PHGL::glVertexPointer(2, GL_FLOAT, 0, squareVertices); PHGL::glTexCoordPointer(2, GL_FLOAT, 0, squareTexCoords); } PHGL::glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); gm->setTextureUniform(NULL); }
MeshGeometry ObjLoader::LoadMesh(const std::string& path) const { // Try opening the file for starters. std::ifstream input(path.c_str()); std::string buffer; unsigned int numberOfTextureCoordinates = 0; unsigned int numberOfNormals = 0; unsigned int numberOfTriangles = 0; unsigned int numberOfVertices = 0; // Create the geometry cache object MeshGeometry output; output.vertices = NULL; output.indices = NULL; if(!input.is_open()) { // Load failed (file not found) std::cerr << "Could not open OBJ file \"" + path + "\"!" << std::endl; } else { // First we'll scan to figure out how many vertices, // texture coordinates, normals and triangles there are // in the file. while(!input.eof()) { // Read each line std::getline(input, buffer); if(buffer.substr(0, 2) == "vn") { // Vertex normal numberOfNormals++; } else if(buffer.substr(0, 2) == "vt") { // Vertex texture coordinate numberOfTextureCoordinates++; } else if(buffer.substr(0, 1) == "v") { // Vertex numberOfVertices++; } else if(buffer.substr(0, 1) == "f") { // Face // Not necessarily a triangle, so now we gotta count it. std::vector<std::string> faceParts = SplitString(buffer, ' '); numberOfTriangles++; if(faceParts.size() > 4) { // We have to make another triangle for this face. It's a quad. numberOfTriangles++; } } } // Instantiate the arrays std::vector<ObjVertex> vertices(numberOfVertices); std::vector<ObjNormal> normals(std::max(1u, numberOfNormals)); // if no normals, just predefine some std::vector<ObjTextureCoordinate> textureCoordinates(std::max(1u, numberOfTextureCoordinates)); std::vector<ObjTriangle> triangles(numberOfTriangles); // Reset the read pointer, otherwise it will just keep shouting eof. input.clear(); input.seekg(0, std::ios::beg); if(!input.is_open()) { std::cerr << "Could not reopen OBJ file for second stage load" << std::endl; } else { // Keep reading through line by line and break down the format now int normal = 0; int textureCoordinate = 0; int vertex = 0; int triangle = 0; while(!input.eof()) { // Fetch the line std::getline(input, buffer); // Create a stringstream for fast searching std::istringstream line(buffer); if(buffer.substr(0, 2) == "vn") { std::string temp, f1, f2, f3; // Parse out some floats and the parameters // Format vn nx ny nz line >> temp >> f1 >> f2 >> f3; float x = (float)atof(f1.c_str()); float y = (float)atof(f2.c_str()); float z = (float)atof(f3.c_str()); normals[normal].x = x; normals[normal].y = y; normals[normal].z = z; normal++; } else if(buffer.substr(0, 2) == "vt") { // format: vt u v std::string temp, f1, f2; line >> temp >> f1 >> f2; float u = (float)atof(f1.c_str()); float v = (float)atof(f2.c_str()); textureCoordinates[textureCoordinate].u = u; textureCoordinates[textureCoordinate].v = v; textureCoordinate++; } else if(buffer.substr(0, 1) == "v") {