void WorldLoader::readString(std::string s){
    jsonRoot = parseJSON(s);
    JSONValue& jo = *jsonRoot;
    
    if (!serverMode){
        //Read and load the textures
        std::cout<<"Reading textures\n";
        std::vector<std::string> textures = jo["textures"].keys();
        
        for (std::vector<std::string>::iterator it=textures.begin(); it!=textures.end(); it++){
            //std::cout<<*it<<std::endl;
            tm.addTexture(*jo["textures"][*it],*it);
        }
    }
    //Read Projection details..
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(parseDouble(*jo["projection"]["fov"]),
                    parseDouble(*jo["projection"]["aspectRatio"]),
                    parseDouble(*jo["projection"]["near"]),
                    parseDouble(*jo["projection"]["far"])
    );

    //std::cout<<jo.stringise()<<std::endl;

    //load objects..
    int objectsCount = jo["objects"].length();
    //std::cout<<"Loading "<<objectsCount<<" objects."<<std::endl;
    for (int i=0; i<objectsCount; i++){
        GLObject* glo;
        if (*jo["objects"][i]["type"] == "cube"){
            glo = new GLCube();
            glo->setTextureManager(&tm);
            glo->setTexturePrefix(*jo["objects"][i]["texturePrefix"]);
            if (serverMode){
                jo["objects"][i].setMapValue("oid",parseJSON("\"" + getNewRandomOID() + "\""));
            }
            glo->setObjectID(*jo["objects"][i]["oid"]);
            
            
            //set up transformations..
            double position[3], rotation[3], size[3];
            std::istringstream  issp(*jo["objects"][i]["position"]),
                                isso(*jo["objects"][i]["orientation"]),
                                isss(*jo["objects"][i]["dimensions"]);
            
            issp>>position[0]>>position[1]>>position[2];
            isso>>rotation[0]>>rotation[1]>>rotation[2];
            isss>>size[0]>>size[1]>>size[2];
            glo->setPosition(position[0], position[1], position[2]);
            glo->setRotation(rotation[0], rotation[1], rotation[2]);
            glo->setSize(size[0], size[1], size[2]);
        }else{
Exemplo n.º 2
0
txConstrcutCSVdata::txConstrcutCSVdata(char *f)
{
	// hard code the scale to 10
	m_Scale = 10.0;

	std::ifstream infile(f);
	double x, y, z;
	std::string line;
	txPoint3D fP;
	txPoint3D min;
	txPoint3D max;

	std::getline(infile,line);
	std::istringstream isso(line);
	isso>>x>>y>>z;
	min.x = max.x = x;
	min.y = max.y = y;
	min.z = max.z = z;

	fP.x = x; fP.y = y; fP.z = z;
	this->m_Points.push_back(fP);

	while(std::getline(infile,line)) {
		std::istringstream iss(line);
		if(!(iss>>x>>y>>z)) { break;}
		
		fP.x = x; fP.y = y; fP.z = z;
		this->m_Points.push_back(fP);

		min.x = x<min.x?x:min.x;
		min.y = y<min.y?y:min.y;
		min.z = z<min.z?z:min.z;

		max.x = x>max.x?x:max.x;
		max.y = y>max.y?y:max.y;
		max.z = z>max.z?z:max.z;
	}



	// change the double in to integar to make the algorithm more robust!
	m_PointsI.reserve(m_Points.size());
	txPoint3I iP;
	for (size_t i=0; i<m_Points.size(); i++) {
		iP.x = (long) (m_Points[i].x-min.x);
		iP.y = (long) (m_Points[i].y-min.y);
		iP.z = (long) (m_Points[i].z-min.z);
		m_PointsI.push_back(iP);
	}

}