Ejemplo n.º 1
0
Ray Renderer::RayThruPixel(int i, int j)
{
	Ray viewRay;

	Camera* camera = m_scene.GetCamera();

	viewRay.SetOrigin(camera->GetlookfromPoint());

	int d = camera->Getfov();
	int l, r, b, t;
	double u, v;

	l = -(m_scene.GetImageWidth() / 2);
	r = m_scene.GetImageWidth() / 2;
	b = -(m_scene.GetImageHeight() / 2);
	t = m_scene.GetImageHeight() / 2;

	u = l + (r - l) * (i + 0.5) / m_scene.GetImageWidth();
	v = b + (t - b) * (j + 0.5) / m_scene.GetImageHeight();

	Vector w(camera->GetlookatPoint().Inverse());
	Vector upVector(camera->GetupVector());
	Vector uVector = w * upVector;

	Vector directionVector;
	
	directionVector = (w * (-d)) + (uVector * u) + (upVector * v);

	viewRay.SetDirection(directionVector);

	return viewRay;
}
Ejemplo n.º 2
0
void
RayTracer::Draw(unsigned char *framebuf, int framewidth, int frameheight)
{
	float screen_x_step = (screen_max.vec[0] - screen_min.vec[0]) / (float)framewidth;
	float screen_y_step = (screen_max.vec[1] - screen_min.vec[1]) / (float)frameheight;

	float fx = screen_min.vec[0];
	float fy = screen_min.vec[1];

	for(int y = 0; y < frameheight; y++) {
		for(int x = 0; x < framewidth; x++) {
			Vector dir;
			dir.Clear();

			dir.vec[0] = fx;
			dir.vec[1] = fy;
			dir.vec[2] = 2.0f;

			Ray ray;
			ray.SetOrigin(Vector(0.0f, 0.0f, 0.0f));
			ray.SetDirection(dir);

			unsigned int p = TestPixelRay(x, y, ray);

			unsigned char r = p >> 24;
			unsigned char g = p >> 16;
			unsigned char b = p >> 8;

			framebuf[framewidth * y * 4 + x * 4 + 0] = r;
			framebuf[framewidth * y * 4 + x * 4 + 1] = g;
			framebuf[framewidth * y * 4 + x * 4 + 2] = b;
			framebuf[framewidth * y * 4 + x * 4 + 3] = 0xff;

			fx += screen_x_step;
		}

		fx = screen_min.vec[0];
		fy += screen_y_step;
	}
}
Ejemplo n.º 3
0
void SoftBodyNode::DivideTriangle(TDVertexContainer& Vertices, 
								TDConnectContainer& Connections, 
								TDConnectContainer::iterator itFace)
{
	TDConnect::iterator itIdx = itFace->begin();
	TDConnect::size_type nIdxA = *itIdx;
	TDConnect::size_type nIdxB = *(itIdx + 1);
	TDConnect::size_type nIdxC = *(itIdx + 2);
	TDConnect::size_type nIdxD = -1;
	TDConnect::size_type nIdxE = -1;
	TDConnect::size_type nIdxF = -1;
	TDVertex A(m_Vertices[nIdxA]);
	TDVertex B(m_Vertices[nIdxB]);
	TDVertex C(m_Vertices[nIdxC]);
	TDVertex AB(B-A);
	TDVertex AC(C-A);
	TDVertex BC(C-B);
	//TDVertex Normal(AB.Cross(AC));

	//normals of the edges - I don't know if this idea makes sense - I've never come
	//across an edge normal before in computer graphics literature. In this program I've defined the normal of
	//an edge to be the sum of the normals of the adjoining vertices. This is not true in general. 
	//It should be the sum of the normals of the adjoining faces, ie the faces that the edge border, 
	//but in this case because the kernel polyhedron is an octahedron, we may be able to get away with it.
	TDVertex NormalAB(*(m_Normals[nIdxA]) + *(m_Normals[nIdxB]));
	TDVertex NormalAC(*(m_Normals[nIdxA]) + *(m_Normals[nIdxC]));
	TDVertex NormalBC(*(m_Normals[nIdxB]) + *(m_Normals[nIdxC]));
		
	NormalAB = 1.0f/bnu::norm_2(NormalAB) * NormalAB;
	NormalAC = 1.0f/bnu::norm_2(NormalAC) * NormalAC;
	NormalBC = 1.0f/bnu::norm_2(NormalBC) * NormalBC;

	TDVertex MidAB(A + 0.5*AB);
	TDVertex MidAC(A + 0.5*AC);
	TDVertex MidBC(B + 0.5*BC);
	
	Sphere BoundSphere;
	bnu::vector<double> Center(3);
	Center[0] = 0.0; Center[1] = 1.0; Center[2] = 0.0;
	BoundSphere.SetCenter(Center);
	BoundSphere.SetRadius(0.5);
	
	Ray Ray;
	Ray.SetOrigin(MidAB);
	Ray.SetDirection(NormalAB);
	TDVertexContainer Points;	
	FindIntersection(Ray, BoundSphere, Points);
	assert(Points.size() == 1);
	TDVertex D(Points[0]);

	Ray.SetOrigin(MidBC);
	Ray.SetDirection(NormalBC);
	FindIntersection(Ray, BoundSphere, Points);
	assert(Points.size()== 1);
	TDVertex E(Points[0]);

	Ray.SetOrigin(MidAC);
	Ray.SetDirection(NormalAC);
	FindIntersection(Ray, BoundSphere, Points);
	assert(Points.size()== 1);
	TDVertex F(Points[0]);

	TDVertexContainer::iterator itVec;
	if ((itVec = find_if(Vertices.begin(), Vertices.end(), bind(Equal<TDVertex>(), _1, D))) == Vertices.end())
	{
		Vertices.push_back(D);
		nIdxD = Vertices.size() - 1;
	}
	else
	{
		nIdxD = Vertices.size() - (Vertices.end() - itVec);
	}
	if ((itVec = find_if(Vertices.begin(), Vertices.end(), bind(Equal<TDVertex>(), _1, E))) == Vertices.end())
	{
		Vertices.push_back(E);
		nIdxE = Vertices.size() - 1;
	}
	else
	{
		nIdxE = Vertices.size() - (Vertices.end() - itVec);
	}
	if ((itVec = find_if(Vertices.begin(), Vertices.end(), bind(Equal<TDVertex>(), _1, F))) == Vertices.end())
	{
		Vertices.push_back(F);
		nIdxF = Vertices.size() - 1;
	}
	else
	{
		nIdxF = Vertices.size() - (Vertices.end() - itVec);
	}
	
	vector<unsigned> vecFace;
	vecFace.push_back(nIdxA);
	vecFace.push_back(nIdxD);
	vecFace.push_back(nIdxF);
	Connections.push_back(vecFace);
	
	vecFace.clear();
	vecFace.push_back(nIdxD);
	vecFace.push_back(nIdxB);
	vecFace.push_back(nIdxE);
	Connections.push_back(vecFace);

	vecFace.clear();
	vecFace.push_back(nIdxF);
	vecFace.push_back(nIdxE);
	vecFace.push_back(nIdxC);
	Connections.push_back(vecFace);

	vecFace.clear();
	vecFace.push_back(nIdxD);
	vecFace.push_back(nIdxE);
	vecFace.push_back(nIdxF);
	Connections.push_back(vecFace);
}