示例#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
MATRIX3 SPHERE::springJacobian(const VEC3F& collisionPoint)
{
  VEC3F direction = collisionPoint - _center;
  Real dot = direction.dot(direction);
  Real sqrtDot = sqrt(dot);
  //Real invSqrtDot = 1.0 / sqrtDot;
  MATRIX3 final;
  final.setZero();
示例#3
0
//////////////////////////////////////////////////////////////////////
// compute the tet volume
//////////////////////////////////////////////////////////////////////
Real TET::volume()
{
  // formula for a tet volume with vertices (a,b,c,d) is:
  // |(a - d) dot ((b - d) cross (c - d))| / 6
  VEC3F a = (*vertices[1]) - (*vertices[0]);
  VEC3F b = (*vertices[2]) - (*vertices[0]);
  VEC3F c = (*vertices[3]) - (*vertices[0]);

  return fabs(a.dot(b.cross(c)) / 6.0);
}
示例#4
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;
}
示例#5
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;
};
示例#6
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;
    }
}
示例#7
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;
    }
}
示例#8
0
VEC3F SPHERE::contactPoint(const VEC3F& point)
{
  VEC3F normal = point - _center;
  normal.normalize();
  return _center + (normal * _radius);
}
示例#9
0
bool vcylinder::define()
{
	glList = glGenLists(1);
	glNewList(glList, GL_COMPILE);
	glShadeModel(GL_FLAT);
	float angle = (float)(15 * (M_PI / 180));
	int iter = (int)(360 / 15);

	float h_len = length * 0.5f;
	VEC3F to = VEC3F(pb[0] - origin[0], pb[1] - origin[1], pb[2] - origin[2]);
	VEC3F u = to / to.length();
	double th = M_PI * 0.5;
	double ap = acos(u.z);
	double xi = asin(-u.y);

	if (ap > M_PI)
		ap = ap - M_PI;

	ang[0] = 180 * xi / M_PI;
	ang[1] = 180 * th / M_PI;
	ang[2] = 180 * ap / M_PI;

	EPD ep;
	ep.setFromEuler(xi, th, ap);

	glPushMatrix();
	glBegin(GL_TRIANGLE_FAN);
	{
		VEC3D p = VEC3D( 0.f, length * 0.5f, 0.f );
		glColor3f(0.0f, 0.f, 1.f);
	//	VEC3F p2_ = ep.A() * VEC3F(p2[0], p2[1], p2[2]);
		//glVertex3f(p2[0], p2[1], p2[2]);
		//p = ep.A() * p;
		glVertex3f(p.x, p.y, p.z);
		for (int i = 0; i < iter + 1; i++){
			float rad = angle * i;
			glColor3f(i % 2, 0.f, i % 2 + 1.f);
			VEC3D q(sin(rad)*topRadius, length * 0.5, cos(rad) * topRadius);
			//q = ep.A() * q;
			glVertex3f(/*origin[0] + */(float)q.x, /*origin[1] + */(float)q.y, /*origin[2] + */(float)q.z);	
		}
	}
	glEnd();
	glPopMatrix();
	glBegin(GL_TRIANGLE_FAN);
	{
		VEC3D p = VEC3D(0.f, -length * 0.5f, 0.f);
		//float p[3] = { 0.f, -length * 0.5f, 0.f };
		glColor3f(0.f, 0.f, 1.f);
		//glVertex3f(p1[0], p1[1], p1[2]);
		//p = ep.A() * p;
		glVertex3f(p.x, p.y, p.z);
		//glVertex3f(p[0], p[1], p[2]);
		for (int i = 0; i < iter + 1; i++){
			float rad = angle * i;
			glColor3f(i % 2, 0.0f, i % 2 + 1.0f);
			VEC3D q(sin(-rad)*baseRadius, -length * 0.5, cos(-rad) * baseRadius);
			//q = ep.A() * q;
			glVertex3f(/*origin[0] + */q.x, /*origin[1] + */q.y, /*origin[2] +*/ q.z);
		}
	}
	glEnd();
	glBegin(GL_QUAD_STRIP);
	{
		for (int i = 0; i < iter + 1; i++){
			float rad = angle * i;
			VEC3D q1(sin(rad) * topRadius, length * 0.5, cos(rad) * topRadius);
			VEC3D q2(sin(rad) * baseRadius, -length * 0.5, cos(rad) * baseRadius);
			//q1 = ep.A() * q1;
			//q2 = ep.A() * q2;
			glVertex3f(/*origin[0] + */q2.x, /*origin[1] + */q2.y, /*origin[2] + */q2.z);
			glVertex3f(/*origin[0] + */q1.x, /*origin[1] + */q1.y, /*origin[2] + */q1.z);
		}
	}
	glEnd();
	glEndList();


	return true;
}