Example #1
0
// データから読み込み
Figure* WFObjLoader::loadData(std::vector<char>* data, bool rightHanded) {
	
	std::vector<float> vertices;		//!< 頂点
	std::vector<float> normals;			//!< 法線
	std::vector<float> textureCoords;	//!< テクスチャ座標
	
	Figure *fig = new Figure();
	std::istringstream stream(std::string(data->begin(), data->end()));

	// 1行毎に処理
	unsigned short faceCount = 0;
	std::string line;
	while(std::getline(stream, line)) {
		//LOGD("line:%s", line.c_str());
		// 改行コードを削除
		if (line[line.size()-1] == '\n') line.erase(line.size()-1);
		if (line[line.size()-1] == '\r') line.erase(line.size()-1);
		
		// ポリゴン
		if (strncmp(line.c_str(), "f ", 2) == 0) {
			// ' 'で分解
			std::vector<std::string> value;
			WFObjLoader::split(line, ' ', value);
			if (value.size() > 3) {
				for (int i=0; i < 3; i++) {
					// '/'で分解
					std::vector<std::string> face;
					WFObjLoader::split(value[i+1], '/', face);
					if (value.size() > 3) {
						for (int j=0; j < 3; j++) {
							//LOGD("%s", face[i].c_str());
							// インデックスは1スタートなので、1引く
							int idx = atoi(face[j].c_str()) -1;
							switch (j) {
								case 0:
									// 頂点座標
									//LOGD("ff:%d,%f,%f,%f", idx, vertices[idx*3], vertices.at(idx*3+1), vertices.at(idx*3+2));
									fig->addVertices(&vertices[idx*3], 3);
									break;
								case 1:
									// テクスチャ座標
									if(idx<0) continue;
									fig->addTextureCoords(&textureCoords[idx*2], 2);
									break;
								case 2:
									// 法線ベクトル
									if(idx<0) continue;
									fig->addNormal(&normals[idx*3], 3);
									break;
							}
						}
						// インデックス作成
						fig->addVertexIndexes(&faceCount, 1);
						faceCount++;
					}
				}
			}
		} else
		// 頂点
		if (strncmp(line.c_str(), "v ", 2) == 0) {
			WFObjLoader::scanLine(line, vertices, 3, rightHanded);
		} else
		// 法線
		if (strncmp(line.c_str(), "vn ", 3) == 0) {
			WFObjLoader::scanLine(line, normals, 3, rightHanded);
		} else
		// テクスチャ
		if (strncmp(line.c_str(), "vt ", 3) == 0) {
			WFObjLoader::scanLine(line, textureCoords, 2, rightHanded);
		}
		
	}
	
	return fig;
}
Example #2
0
Figure* WFObjLoader::loadGCBData(std::vector<char>* data)
{
	BinaryStream stream(data);
	
	// マジックナンバーチェック
	if (stream.readByte() != 'g' ||
		stream.readByte() != 'c' ||
		stream.readByte() != 'u' ||
		stream.readByte() != 'b') {
		return NULL;
	}
	
	Figure *fig = new Figure();
	
	while (!stream.isEnd()) {
		short type = stream.readShort();
		switch (type) {
			case TYPE_VERTEX:
			{
				int size = stream.readInt();
				for (int i = 0; i < size; i++) {
					float v = stream.readFloat();
					fig->addVertices(&v, 1);
				}
			}	break;
			case TYPE_NORMAL:
			{
				int size = stream.readInt();
				for (int i = 0; i < size; i++) {
					float v = stream.readFloat();
					fig->addNormal(&v, 1);
				}
			}	break;
			case TYPE_TEXCOOD:
			{
				int size = stream.readInt();
				for (int i = 0; i < size; i++) {
					float v = stream.readFloat();
					fig->addTextureCoords(&v, 1);
				}
			}	break;
			case TYPE_INDEX:
			{
				int size = stream.readInt();
				for (int i = 0; i < size; i++) {
					unsigned short v = stream.readInt();
					fig->addVertexIndexes(&v, 1);
				}
			}	break;
			case TYPE_COLOR:
			{
				int size = stream.readInt();
				for (int i = 0; i < size; i++) {
					float v = stream.readFloat();
					fig->addColors(&v, 1);
				}
			}	break;
			case TYPE_WEIGHT:
			{
				loadWeight(stream, fig);
			}	break;
			case TYPE_NODE:
			{
				fig->joint = loadJoint(stream);
			}	break;
			case TYPE_TEXCOOD_MLT:
			{
				int size = stream.readInt();
				for (int i = 0; i < size; i++) {
					float v = stream.readFloat();
					fig->addTextureCoordsMlt(&v, 1);
				}
			}	break;
			default:
			{
				LOGE("Unknown Type. [type=%d]", type);
				return NULL;
			}	break;
		}
	}
	return fig;
}