示例#1
0
文件: HW1.cpp 项目: rjduran/MAT594G
VEC3F rayColor(VEC3F* ray) {	
	VEC3F colorRGB;
	VEC3F c(0, 0, 1); // sphere center 
	VEC3F n; 		  // normal vector
	
	VEC3F cr(1.0f, 1.0f, 1.0f); // surface color 
	VEC3F cl(1.0f, 1.0f, 1.0f); // light color
	VEC3F light(1, 1, -1); 		// light location
	VEC3F l;					// light direction
	
	if(!intersectScene(ray[0])) {
		//return black
		colorRGB = VEC3F(0.f, 0.f, 0.f);
	} else {		
		// return color								
		n = ray[0]-c;	   // calculate normal vector, p - c
		l = light-ray[0];  // calculate l vector, light - p
		
		// normalize n and l
		n.normalize();	
		l.normalize();				
		
		VEC3F crcl(cr[0]*cl[0], cr[1]*cl[1], cr[2]*cl[2]);		
		float nlDotProd = n*l; 		
		colorRGB = crcl*(nlDotProd);					
	}
		
	return colorRGB; // return vector with rgb values
}
示例#2
0
int main() {
	float f(0.5);
	VEC4F v(3.0);
	VEC4F t(VEC3F(4.0,.8,1.6), 3.2);
	VEC4F y(t);
	VEC4F c(1.0,0.5,0.25,0.65);
	VEC3F a = v.getVEC3F();
	VEC3F b = y.getVEC3F();

	cout<<"first vector v: "<<v<<endl;
	cout<<"second vector t: "<<t<<endl;
	cout<<"third vector y: "<<y<<endl;
	cout<<"fourth vector c "<<c<<endl;
	cout<<"fifth vector a: "<<a<<endl;
	cout<<"sixth vector b: "<<b<<endl<<endl;
	
	cout<<"item 3 of vector "<<y<<" is "<<y[3]<<endl;
	cout<<"change this item to "<<y[0]<<": ";
	y[3]=y[0];
	cout<<y<<endl<<endl;
	
	cout<<v<<" + "<<c<<" = "<<v+c<<endl;
	cout<<v<<" - "<<c<<" = "<<v-c<<endl;
	cout<<"-"<<v<<" = "<<-v<<endl;
	cout<<v<<" * "<<c<<" = "<<v*c<<endl;
	cout<<f<<" * "<<c<<" = "<<f*c<<endl;
	cout<<v<<" / "<<c<<" = "<<v/c<<endl;
	cout<<c<<" / "<<f<<" = "<<c/f<<endl<<endl;

	cout<<v<<" += "<<c<<" : ";
	v+=c;
	cout<<v<<endl;
	cout<<v<<" -= "<<c<<" : ";
	v-=c;
	cout<<v<<endl;
	cout<<v<<" *= "<<c<<" : ";
	v*=c;
	cout<<v<<endl;
	cout<<c<<" *= "<<f<<" : ";
	c*=f;
	cout<<c<<endl;
	cout<<v<<" /= "<<c<<" : ";
	v/=c;
	cout<<v<<endl;
	cout<<c<<" /= "<<f<<" : ";
	c/=f;
	cout<<c<<endl<<endl;
	
	cout<<a<<" cross "<<b<<" = "<<cross(a,b)<<endl;
	cout<<a<<" dot "<<b<<" = "<<dot(a,b)<<endl;
	cout<<a<<" normalized = "<<a.getNormalized()<<endl;
	cout<<"normalized "<<a<<" = ";
	a.normalize();
	cout<<a<<endl;
	cout<<a<<" magnitude = "<<a.getMagnitude()<<endl;
	cout<<b<<" magnitude = "<<b.getMagnitude()<<endl;
};
示例#3
0
VEC3F SPHERE::force(const VEC3F& collisionPoint, const VEC3F& collisionVelocity)
{
  VEC3F direction = collisionPoint - _center;
  VEC3F normal = direction;
  normal.normalize();

  Real velocityDot = normal.dot(collisionVelocity);

  VEC3F springForce = _collisionStiffness * (direction - (normal * _radius));
  VEC3F dampingForce = _collisionDamping * normal * velocityDot;

  return springForce + dampingForce;
}
示例#4
0
int SceneLoader::load(string scene_file, Scene** scene, int xRes, int yRes) {
    TiXmlDocument doc(scene_file);
    if ( !doc.LoadFile() ) {
        Logger::log(LOG_ERROR) << "Scene loading failed : " << scene_file << endl;
        Logger::log(LOG_ERROR) << "Error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
        return -1;
    } else {
        Logger::log(LOG_INFO) << "Start loading scene : " << scene_file << endl;
        Uint32 initial_tick = SDL_GetTicks();

        list<Object*> objects;
        list<Light*> lights;
        set<Material*> materials;
        AmbientLight ambientLight;
        Camera* camera=0;
        TiXmlElement* tmp_node = 0;

        TiXmlHandle hdl(&doc);
        TiXmlElement* node = hdl.FirstChildElement().FirstChildElement().Element();
        if (node == NULL) {
            Logger::log(LOG_ERROR)<<"Error on top of the file"<<endl;
            return -1;
        }

        while ( node ) {
            string nodeName(node->Value());
            if ( nodeName.compare("camera")==0 ) {
                VEC3F position, target, normal;
                float w = 8, d = 35;

                tmp_node = node->FirstChildElement("position");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <position> near line "<<node->Row()<<endl;
                } else {
                    position = readVec3Float(tmp_node);
                }

                tmp_node = node->FirstChildElement("target");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <target> near line "<<node->Row()<<endl;
                } else {
                    target = readVec3Float(tmp_node);
                }

                tmp_node = node->FirstChildElement("normal");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <normal> near line "<<node->Row()<<endl;
                } else {
                    normal = readVec3Float(tmp_node);
                }

                tmp_node = node->FirstChildElement("viewplane");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <viewplane> near line "<<node->Row()<<endl;
                } else {
                    tmp_node->QueryFloatAttribute("w", &w);
                    tmp_node->QueryFloatAttribute("d", &d);
                }

                camera = new Camera(position,
                                    target-position,
                                    normal,
                                    w, d,
                                    xRes, yRes,
                                    Settings::getAsFloat("camera_translation_factor"),
                                    Settings::getAsFloat("camera_rotation_angle"));
            } else if ( nodeName.compare("directionalLight")==0 ) {
                Color color;
                VEC3F direction;

                tmp_node = node->FirstChildElement("color");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <color> near line "<<node->Row()<<endl;
                } else {
                    color = readColor(tmp_node);
                }

                tmp_node = node->FirstChildElement("direction");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <direction> near line "<<node->Row()<<endl;
                } else {
                    direction = readVec3Float(tmp_node);
                }
                DirectionalLight *dirLight = new DirectionalLight(color, direction.normalize()); // !!!!!!!!!! NEEDS TO BE DESTROYED
                lights.push_back(dirLight);
            } else if ( nodeName.compare("pointLight")==0 ) {
                Color color;
                VEC3F point;

                tmp_node = node->FirstChildElement("color");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <color> near line "<<node->Row()<<endl;
                } else {
                    color = readColor(tmp_node);
                }

                tmp_node = node->FirstChildElement("point");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <point> near line "<<node->Row()<<endl;
                } else {
                    point = readVec3Float(tmp_node);
                }
                PointLight *pointLight = new PointLight(color, point); // !!!!!!!!!! NEEDS TO BE DESTROYED
                lights.push_back(pointLight);
            } else if ( nodeName.compare("ambientLight")==0 ) {
                Color color;

                tmp_node = node->FirstChildElement("color");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <color> near line "<<node->Row()<<endl;
                } else {
                    color = readColor(tmp_node);
                }

                ambientLight = AmbientLight(color);
            } else if ( nodeName.compare("object")==0 ) {
                Material* material = 0;

                tmp_node = node->FirstChildElement("material");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <material> near line "<<node->Row()<<endl;
                } else {
                    material = readMaterial(tmp_node);
                    if (material != NULL) {
                        if (materials.count(material) == 0) {
                            materials.insert(material);
                        }
                    }
                }

                tmp_node = node->FirstChildElement("shape");
                if (tmp_node == NULL) {
                    Logger::log(LOG_ERROR)<<"Missing <shape> near line "<<node->Row()<<endl;
                } else {
                    readShape(tmp_node->FirstChildElement(), &objects, material);
                }
            } else {
                Logger::log(LOG_ERROR)<<"Unknown primary node line "<<node->Row()<<endl;
            }

            node = node->NextSiblingElement();
        }

        float loading_time=(SDL_GetTicks()-initial_tick)/(float)1000;
        Logger::log(LOG_INFO) << "Scene loaded ("<<(int) objects.size()<<" objects) ("
                              << loading_time << " s)" << endl;

        *scene = new Scene(objects,lights,materials,ambientLight,camera);

        return 0;
    }
}
示例#5
0
void SceneLoader::readShape(TiXmlElement* node, list<Object*>* objects, Material* material) {
    string nodeName(node->Value());

    if ( nodeName.compare("sphere")==0 ) {
        VEC3F center = readVec3Float(node->FirstChildElement("center"));
        float radius = 0;
        node->FirstChildElement("radius")->QueryFloatAttribute("v", &radius);

#ifdef SCENELOADER_DEBUG
        Logger::log(LOG_DEBUG)<<"Sphere : ("<<center.x<<","<<center.y<<","<<center.z<<") "
                              <<radius<<endl;
#endif
        objects->push_back(new Sphere(center, radius, material));
    } else if ( nodeName.compare("triangle")==0 ) {
        VEC3F a = readVec3Float(node->FirstChildElement("a"));
        VEC3F b = readVec3Float(node->FirstChildElement("b"));
        VEC3F c = readVec3Float(node->FirstChildElement("c"));
        TiXmlElement* child_normal_a = node->FirstChildElement("normal_a");
        TiXmlElement* child_normal_b = node->FirstChildElement("normal_b");
        TiXmlElement* child_normal_c = node->FirstChildElement("normal_c");

        VEC3F na;
        VEC3F nb;
        VEC3F nc;

        VEC3F normal = ((b - a) * (b - c)).normalize();

        if(child_normal_a != NULL) {
            na = readVec3Float(child_normal_a);
        } else {
            na = normal;
        }

        if(child_normal_b != NULL) {
            nb = readVec3Float(child_normal_b);
        } else {
            nb = normal;
        }

        if(child_normal_c != NULL) {
            nc = readVec3Float(child_normal_c);
        } else {
            nc = normal;
        }

#ifdef SCENELOADER_DEBUG
        Logger::log(LOG_DEBUG)<<"Triangle : ("<<a.x<<","<<a.y<<","<<a.z<<") ("
                              <<b.x<<","<<b.y<<","<<b.z<<") ("
                              <<c.x<<","<<c.y<<","<<c.z<<")"<<endl;
#endif
        Triangle* tr = new Triangle(a, b, c, na, nb, nc, normal, material);
        objects->push_back(tr);
    } else if ( nodeName.compare("list")==0 ) {
        TiXmlElement* child = node->FirstChildElement();
        while ( child ) {
            readShape(child,  objects, material);
            child = child->NextSiblingElement();
        }
    } else if(nodeName.compare("plane")==0) {
        VEC3F normal = readVec3Float(node->FirstChildElement("normal"));
        VEC3F point = readVec3Float(node->FirstChildElement("point"));

        objects->push_back(new Plane(normal.normalize(), point, material));

    } else {
        Logger::log(LOG_ERROR)<<"Unknown shape : "<<nodeName<<" (line "<<node->Row()<<")"<<endl;
    }
}
示例#6
0
VEC3F SPHERE::contactPoint(const VEC3F& point)
{
  VEC3F normal = point - _center;
  normal.normalize();
  return _center + (normal * _radius);
}