Esempio n. 1
0
Material *
SceneParser::parseMaterial() 
{
    char token[MAX_PARSER_TOKEN_LENGTH];
    char filename[MAX_PARSER_TOKEN_LENGTH];
    filename[0] = 0;
    Vector3f diffuseColor(1,1,1), specularColor(0,0,0);
    float shininess=0;
    getToken(token); assert(!strcmp(token, "{"));
    while (true) {
        getToken(token); 
        if (strcmp(token, "diffuseColor")==0) {
            diffuseColor = readVector3f();
        }
        else if (strcmp(token, "specularColor")==0) {
            specularColor = readVector3f();
        }
        else if (strcmp(token, "shininess")==0) {
            shininess = readFloat();
        }
        else if (strcmp(token, "texture")==0) {
            getToken(filename);
        }
        else {
            assert(!strcmp(token, "}"));
            break;
        }
    }
    Material *answer = new Material(diffuseColor, specularColor, shininess);
    if(filename[0] !=0){
        answer->loadTexture(filename);
    }
    return answer;
}
Esempio n. 2
0
Transform *
SceneParser::parseTransform() 
{
    char token[MAX_PARSER_TOKEN_LENGTH];
    Matrix4f matrix = Matrix4f::identity();
    Object3D *object = NULL;
    getToken(token); assert(!strcmp(token, "{"));
    // read in transformations: 
    // apply to the LEFT side of the current matrix (so the first
    // transform in the list is the last applied to the object)
    getToken(token);

    while (true) {
        if (!strcmp(token,"Scale")) {
            Vector3f s = readVector3f();
            matrix = matrix * Matrix4f::scaling( s[0], s[1], s[2] );
        } else if (!strcmp(token,"UniformScale")) {
            float s = readFloat();
            matrix = matrix * Matrix4f::uniformScaling( s );
        } else if (!strcmp(token,"Translate")) {
            matrix = matrix * Matrix4f::translation( readVector3f() );
        } else if (!strcmp(token,"XRotate")) {
            matrix = matrix * Matrix4f::rotateX((float) DegreesToRadians(readFloat()));
        } else if (!strcmp(token,"YRotate")) {
            matrix = matrix * Matrix4f::rotateY((float) DegreesToRadians(readFloat()));
        } else if (!strcmp(token,"ZRotate")) {
            matrix = matrix * Matrix4f::rotateZ((float) DegreesToRadians(readFloat()));
        } else if (!strcmp(token,"Rotate")) {
            getToken(token); assert(!strcmp(token, "{"));
            Vector3f axis = readVector3f();
            float degrees = readFloat();
            float radians = (float) DegreesToRadians(degrees);
            matrix = matrix * Matrix4f::rotation(axis,radians);
            getToken(token); assert(!strcmp(token, "}"));
        } else if (!strcmp(token,"Matrix4f")) {
            Matrix4f matrix2 = Matrix4f::identity();
            getToken(token); assert(!strcmp(token, "{"));
            for (int j = 0; j < 4; j++) {
                for (int i = 0; i < 4; i++) {
                    float v = readFloat();
                    matrix2( i, j ) = v; 
                } 
            }
            getToken(token); assert(!strcmp(token, "}"));
            matrix = matrix2 * matrix;
        } else {
            // otherwise this must be an object,
            // and there are no more transformations
            object = parseObject(token);
            break;
        }
        getToken(token);
    }

    assert(object != NULL);
    getToken(token); assert(!strcmp(token, "}"));
    return new Transform(matrix, object);
}
Esempio n. 3
0
Light* SceneParser::parseDirectionalLight() {
    char token[MAX_PARSER_TOKEN_LENGTH];
    getToken(token); assert (!strcmp(token, "{"));
    getToken(token); assert (!strcmp(token, "direction"));
    Vector3f direction = readVector3f();
    getToken(token); assert (!strcmp(token, "color"));
    Vector3f color = readVector3f();
    getToken(token); assert (!strcmp(token, "}"));
    return new DirectionalLight(direction,color);
}
Esempio n. 4
0
Light *
SceneParser::parsePointLight() 
{
    char token[MAX_PARSER_TOKEN_LENGTH];
    getToken(token); assert(!strcmp(token, "{"));
    getToken(token); assert(!strcmp(token, "position"));
    Vector3f position = readVector3f();
    getToken(token); assert(!strcmp(token, "color"));
    Vector3f color = readVector3f();
    getToken(token); assert(!strcmp(token, "}"));
    return new PointLight(position,color);
}
Esempio n. 5
0
Triangle* SceneParser::parseTriangle() {
    char token[MAX_PARSER_TOKEN_LENGTH];
    getToken(token); assert (!strcmp(token, "{"));
    getToken(token); 
    assert (!strcmp(token, "vertex0"));
    Vector3f v0 = readVector3f();
    getToken(token); 
    assert (!strcmp(token, "vertex1"));
    Vector3f v1 = readVector3f();
    getToken(token); 
    assert (!strcmp(token, "vertex2"));
    Vector3f v2 = readVector3f();
    getToken(token); assert (!strcmp(token, "}"));
    assert (current_material != NULL);
    return new Triangle(v0,v1,v2,current_material);
}
Esempio n. 6
0
void SceneParser::parsePerspectiveCamera() {
    char token[MAX_PARSER_TOKEN_LENGTH];
    // read in the camera parameters
    getToken(token); assert (!strcmp(token, "{"));
    getToken(token); assert (!strcmp(token, "center"));
    Vector3f center = readVector3f();
    getToken(token); assert (!strcmp(token, "direction"));
    Vector3f direction = readVector3f();
    getToken(token); assert (!strcmp(token, "up"));
    Vector3f up = readVector3f();
    getToken(token); assert (!strcmp(token, "angle"));
    float angle_degrees = readFloat();
    float angle_radians = DegreesToRadians(angle_degrees);
    getToken(token); assert (!strcmp(token, "}"));
    camera = new PerspectiveCamera(center,direction,up,angle_radians);
}
Esempio n. 7
0
void SceneParser::parseOrthographicCamera() {
    cout << "Parsing ortho camera" << endl;
    char token[MAX_PARSER_TOKEN_LENGTH];
    // read in the camera parameters
    getToken(token); assert (!strcmp(token, "{"));
    getToken(token); assert (!strcmp(token, "center"));
    Vector3f center = readVector3f();
    getToken(token); assert (!strcmp(token, "direction"));
    Vector3f direction = readVector3f();
    getToken(token); assert (!strcmp(token, "up"));
    Vector3f up = readVector3f();
    getToken(token); assert (!strcmp(token, "size"));
    float size = readFloat();
    getToken(token); assert (!strcmp(token, "}"));
    camera = new OrthographicCamera(center,direction,up,size);
}
Esempio n. 8
0
Material* SceneParser::parseMaterial() {
    char token[MAX_PARSER_TOKEN_LENGTH];
	char filename[MAX_PARSER_TOKEN_LENGTH];
	filename[0] = 0;
    Vector3f diffuseColor(1,1,1), specularColor(0,0,0);
	float shininess=0;
	float refractionIndex =0;
    getToken(token); assert (!strcmp(token, "{"));
	Noise *noise =0;
    while (1) {
        getToken(token); 
        if (strcmp(token, "diffuseColor")==0) {
            diffuseColor = readVector3f();
        }
		else if (strcmp(token, "specularColor")==0) {
            specularColor = readVector3f();
        }
		else if (strcmp(token, "shininess")==0) {
            shininess = readFloat();
        }else if(strcmp(token, "refractionIndex")==0){
			refractionIndex = readFloat();
		}
		else if (strcmp(token, "texture")==0) {
            getToken(filename);
        }
		///unimplemented
		else if (strcmp(token, "bump")==0) {
            getToken(token);
        }
		else if(strcmp(token,"Noise")==0){
			noise = parseNoise();
		}
		else {
            assert (!strcmp(token, "}"));
            break;
        }
    }
    Material *answer = new Material(diffuseColor, specularColor, shininess,refractionIndex);
	if(filename[0] !=0){
		answer->loadTexture(filename);
	}
	if(noise != 0){
		answer->setNoise(*noise);
		delete noise;
	}
    return answer;
}
Esempio n. 9
0
void SceneParser::parseBackground() {
    cout << "parsing background" << endl;
    char token[MAX_PARSER_TOKEN_LENGTH];
    // read in the background color
    getToken(token); assert (!strcmp(token, "{"));    
    while (1) {
        getToken(token); 
        if (!strcmp(token, "}")) { 
            break;    
        } else if (!strcmp(token, "color")) {
            background_color = readVector3f();
        } else if (!strcmp(token, "ambientLight")) {
            ambient_light = readVector3f();
        } else {
            printf ("Unknown token in parseBackground: '%s'\n", token);
            assert(0);
        }
    }
}
Esempio n. 10
0
Plane* SceneParser::parsePlane() {
    char token[MAX_PARSER_TOKEN_LENGTH];
    getToken(token); assert (!strcmp(token, "{"));
    getToken(token); assert (!strcmp(token, "normal"));
    Vector3f normal = readVector3f();
    getToken(token); assert (!strcmp(token, "offset"));
    float offset = readFloat();
    getToken(token); assert (!strcmp(token, "}"));
    assert (current_material != NULL);
    return new Plane(normal,offset,current_material);
}
Esempio n. 11
0
Sphere* SceneParser::parseSphere() {
    char token[MAX_PARSER_TOKEN_LENGTH];
    getToken(token); assert (!strcmp(token, "{"));
    getToken(token); assert (!strcmp(token, "center"));
    Vector3f center = readVector3f();
    getToken(token); assert (!strcmp(token, "radius"));
    float radius = readFloat();
    getToken(token); assert (!strcmp(token, "}"));
    assert (current_material != NULL);
    return new Sphere(center,radius,current_material);
}
Esempio n. 12
0
Light* SceneParser::parsePointLight() {
    char token[MAX_PARSER_TOKEN_LENGTH];
    Vector3f position,color;
    float falloff =0;
    getToken(token); assert (!strcmp(token, "{"));
    while (1) {
        getToken(token); 
        if (strcmp(token, "position")==0) {
            position = readVector3f();
        }else if (strcmp(token, "color")==0) {
          color = readVector3f();
        }else if(strcmp(token,"falloff")==0){    
          falloff = readFloat();
        }else{
           assert (!strcmp(token, "}"));
          break;
        }
    }
    return new PointLight(position,color,falloff);
}
Esempio n. 13
0
Triangle *
SceneParser::parseTriangle() 
{
    char token[MAX_PARSER_TOKEN_LENGTH];
    getToken(token); assert(!strcmp(token, "{"));
    getToken(token); 
    assert(!strcmp(token, "vertex0"));
    Vector3f v0 = readVector3f();
    getToken(token); 
    assert(!strcmp(token, "vertex1"));
    Vector3f v1 = readVector3f();
    getToken(token); 
    assert(!strcmp(token, "vertex2"));
    Vector3f v2 = readVector3f();
    getToken(token); assert(!strcmp(token, "}"));
    assert(_current_material != NULL);
    Vector3f a = v1 - v0;
    Vector3f b = v2 - v0;
    Vector3f n = Vector3f::cross(a, b).normalized();
    return new Triangle(v0, v1, v2, n, n, n, _current_material);
}
Esempio n. 14
0
Material* SceneParser::parseMaterial() {
    cout << "PARSING INDIVIDUAL MATERIAL" << endl;
    char token[MAX_PARSER_TOKEN_LENGTH];
    Vector3f diffuseColor(1,1,1);
    float exponent = 1;
    getToken(token); assert (!strcmp(token, "{"));
    while (1) {
        getToken(token); 
        if (!strcmp(token, "diffuseColor")) {
            diffuseColor = readVector3f();
        } else {
            assert (!strcmp(token, "}"));
            break;
        }
    }
    Material *answer = new Material(diffuseColor);
    return answer;
}
Esempio n. 15
0
Noise * SceneParser::parseNoise()
{
    char token[MAX_PARSER_TOKEN_LENGTH];
	Vector3f color[2];
	int colorIdx = 0;
	int octaves=0;
	float frequency  = 1;
	float amplitude = 1;
	getToken(token); assert (!strcmp(token, "{"));
	Noise *noise =0;
    while (1) {
        getToken(token); 
        if (strcmp(token, "color")==0) {
			if(colorIdx > 1){
				printf("Error parsing noise\n");
			}else{
				color[colorIdx]= readVector3f();
				colorIdx++;
			}
        }
		else if (strcmp(token, "octaves")==0) {
            octaves= readInt();
        }
		else if (strcmp(token, "frequency")==0) {
            frequency= readFloat();
        }
		else if (strcmp(token, "amplitude")==0) {
            amplitude= readFloat();
        }
		else {
            assert (!strcmp(token, "}"));
            break;
        }
    }
	return new Noise(octaves, color[0],color[1],frequency,amplitude);
}
Esempio n. 16
0
void TextInputButton::focusClick(){


    std::stringstream ss;
    tooltip=(char*)input->inputText.c_str();

    //adding selectedActor stack to TextInputButton

    if (parent && buttonProperty!="NULL"){

        bool bParentSelected=false;
        for (int i=0;i<(int)input->selectedActors.size();i++ )
            if (input->selectedActors[i]==parent)
                bParentSelected=true;

       if (bParentSelected){

            for (int i=0;i<(int)input->selectedActors.size();i++){

                memberID * mID = &(input->selectedActors[i]->property[buttonProperty]);        //look for the property we should set
                cout << "we found the following memberID: " << mID->memberName << " with the following member type: " << mID->memberType->name() << endl;
                if (buttonProperty=="ROTATION"){
                    Vector3f rot = readVector3f((char*)input->inputText.c_str());
                    input->selectedActors[i]->setRotation(rot);
                }else if (buttonProperty=="LOCATION"){
                    Vector3f loc = readVector3f((char*)input->inputText.c_str());
                    input->selectedActors[i]->setLocation(loc);
                }else if (buttonProperty=="SCALE"){
                    Vector3f loc = readVector3f((char*)input->inputText.c_str());
                    input->selectedActors[i]->setScale(loc);
                }else if (mID){
                    memberFromString(mID,input->inputText);
                }else
                    cout << "no such property: " << buttonProperty;
            }
       }
       else{
           cout << "we are connected to " << parent->name <<endl;

           memberID * mID = &(parent->property[buttonProperty]);        //look for the property we should set
           cout << "we found the following memberID: " << mID->memberName << " with the following member type: " << mID->memberType->name() << endl;
           if (mID)
             {
             memberFromString(mID,input->inputText);
             }
           else
             cout << "no such property: " << buttonProperty;
        }

    }

        if (parent)
          parent->trigger(this);

        //clean up

        input=Input::getInstance();

        input->inputText="NULL";

        bEditing=false;

    BasicButton::focusClick();
}