示例#1
0
// == Scene class =============================================================
//    -----------
Scene::Scene()
{
	// scene initialization
	// Material: Texture/Color, DIFF, REFL, SPEC, ABSORB
	_scene = this;

	lightList = new Light*[16];
	lightCount = 0;

	//lightList[lightCount++] = new Light( float3(    0,  -5,  -0   ), float3( 1.0f, 1.0f, 1.0f ), 10.0f, 0 );
	lightList[lightCount++] = new Light( float3(    -5,  -3,  0   ), float3( 1.0f, 1.0f, 1.0f ), 10.0f, 1.0f );
	//lightList[lightCount++] = new Light( float3(    5,  -3,  -10  ), float3( 1.0f, 0.0f, 0.0f ), 5.0f, 1.0f );
	lightList[lightCount++] = new Light( float3(    -5,  -3,  -30  ), float3( 1.0f, 1.0f, 1.0f ), 5.0f, 1.0f );
	//lightList[lightCount++] = new Light( float3(    -2,  -3,  -3   ), float3( 1.0f, 0.0f, 0.0f ) );

	Material* floormat = new Material(float3(1,1,1), 1.0f, 0.5f, .0f, 0);
	Material* green = new Material(float3(0.0f, 1.0f, 0.0f), 1.0f, 0.0f, .0f, 0);

	//m_PrimList.push_back(	new Triangle(float3(30,2,-30), float3(30,2,30), float3(-30,2,30),	floormat));
	//m_PrimList.push_back(	new Triangle(float3(-30,2,-30), float3(30,2,-30), float3(-30,2,30), floormat));

	ObjLoader loader;
	//loader.LoadObj("assets/frog.obj", float3(-3,-.7f,-10), new Material(float3(.5f, 1.0f, .5f), 1.0f, 0.0f, .4f,0.0f));
	
	//loader.LoadObj("assets/california.obj", float3(5,0.3f,0), new Material(float3(1.0f, .5f, 0), .1f, .5f, .2f, 1));
	//loader.LoadObj("assets/sphere.obj", float3(2,0,-30), new Material(float3(0.0f, 0, 0), 1.0f, .5f, 0, 1.00f));
	loader.LoadObj("assets/asdf.obj", float3(1,.5f,-0), new Material(float3(1.0f, 0.0f, 0.0f), .1f, 1.0f, 0, 1.00f));
	loader.LoadObj("assets/sphere_small.obj", float3(0,.5f,-0), new Material(float3(1.0f, .0f, .0f), 1.0f, .0f, 0, .50f));
	loader.LoadObj("assets/sphere_small.obj", float3(0,.5f,-1), new Material(float3(1.0f, 0.0f, 1.0f), 1.0f, .5f, 0.2f, .50f));
	loader.LoadObj("assets/sphere_small.obj", float3(-1,.5f,-2), new Material(float3(0.0f, .0f, .0f), 1.0f, .0f, 0, 0.10f));
	//
	//loader.LoadObj("assets/sphere.obj", float3(5,-.5f,3.0f), new Material(float3(1.0f, 1, 1), 1.0f, .50f, 0, 1.0f));
	////loader.LoadObj("assets/sphere_big.obj", float3(8,0,-30), new Material(float3(1.0f, 0, 0), .4f, 1.00f, 0.5f, 0.0f));
	//
	//loader.LoadObj("assets/plane.obj", float3(2,1,2), new Material(new Texture("assets/woodwall.jpg"), .5f, 0.0f, 0.0f, 1.0f));
	loader.LoadObj("assets/stone.obj", float3(7.5f,1.5f,-0), new Material(new Texture("assets/stone.jpg"), .5f, 0.0f, 0.0f, 1.0f));
	loader.LoadObj("assets/stone.obj", float3(0,1.5f,-0), new Material(new Texture("assets/stone.jpg"), .5f, 0.0f, 0.0f, 1.0f));
	loader.LoadObj("assets/wall1.obj", float3(0,0,-5), new Material(new Texture("assets/woodwall.jpg"), .5f, 0.0f, 0.0f, 1.0f));
	loader.LoadObj("assets/wall2.obj", float3(0,0,5), new Material(new Texture("assets/woodwall.jpg"), .5f, 0.0f, 0.0f, 1.0f));


	
	loader.LoadObj("assets/german_shepard/dog.obj", float3(-2,0.8f,0), new Material(new Texture("assets/german_shepard/body_d.tga"), .5f, 0.0f, .0f, 1.0f));

	rootNode = new BVHNode(0);
	rootNode->triList = m_PrimList;
	rootNode->GenerateBounds();
	if(rootNode->triList.size() > MAXTRIS) rootNode->Split();

}
示例#2
0
void doConvert(const TCHAR* objFileName, const TCHAR* pbrtFileName, bool mirror = false,
			   bool cut = false, float a = 0, float b = 0, float c = 0, float d = -1)
{
	ObjLoader loader;
	loader.LoadObj(ANSIStringFromTString(objFileName));

	ObjModel* pModel = loader.ReturnObj();
	// Cut the model first
	if (cut)
		removeModelPart(pModel, a, b, c, d);

	// Calculate smoothed normals
	computeNormals(pModel);
	computeTangentSpace(pModel);
	duplicateVerticesForDifferentTexCoords(pModel);

	// for each triangle iterator
	auto forEachTriangle = [&] (function<void (const ObjTriangle&)> callback) {
		for (const ObjPart& part : pModel->Parts) {
			for (int idxTri = part.TriIdxMin; idxTri < part.TriIdxMax; idxTri++) {
				const ObjTriangle& tri = pModel->Triangles[idxTri];
				callback(tri);
			}
		}
	};

	// Build vector -> { texCoord, normal, tangent } mapping
	unordered_map<int, MapEntry> mapVectorToEntry;
	forEachTriangle([&] (const ObjTriangle& tri) {
		for (int j = 0; j < 3; j++) {
			auto iter = mapVectorToEntry.find(tri.Vertex[j]);
			if (iter != mapVectorToEntry.end()) continue;

			MapEntry& entry = mapVectorToEntry[tri.Vertex[j]];
			entry.id = -1;
			entry.normal = tri.Normal[j];
			entry.tangent = tri.Tangent[j];
			entry.texCoord  = tri.TexCoord[j];
		}
	});

	ofstream pbrtOut;
	if (pbrtFileName)
	{
		pbrtOut.open(pbrtFileName, ios::trunc);
		if (!pbrtOut) {
			cout << "Failed to open output file!" << endl;
			return;
		}
	}
	ostream& out = pbrtFileName ? pbrtOut : cout;

	out << "AttributeBegin" << endl;
	out << "  Shape \"trianglemesh\"" << endl;
    out << "    \"point P\" [" << endl;
	// record order of vertices
	vector<int> vertexOrder;
	forEachTriangle([&] (const ObjTriangle& tri) {
		for (int j = 0; j < 3; j++) {
			int vertexId = tri.Vertex[j];
			auto iter = mapVectorToEntry.find(vertexId);
			if (iter != mapVectorToEntry.end()) {
				MapEntry& entry = iter->second;
				if (entry.id < 0) {
					entry.id = vertexOrder.size();
					vertexOrder.push_back(vertexId);
					const ObjVertex& v = pModel->Vertices[vertexId];
					out << "      " << v.x << " " << v.y << " " << v.z << endl;
				}
			}
		}
	});
	out << "    ]" << endl;
	if (pModel->Normals.size() > 1) {
		out << "    \"normal N\" [" << endl;
		for (int vertexId : vertexOrder) {
			const MapEntry& entry = mapVectorToEntry[vertexId];
			const ObjNormal& n = pModel->Normals[entry.normal];
			out << "      " << n.x << " " << n.y << " " << n.z << endl;
		}
		out << "    ]" << endl;
	}
	if (pModel->Tangents.size() > 1) {
		out << "    \"vector S\" [" << endl;
		for (int vertexId : vertexOrder) {
			const MapEntry& entry = mapVectorToEntry[vertexId];
			const ObjTangent& t = pModel->Tangents[entry.tangent];
			out << "      " << t.x << " " << t.y << " " << t.z << endl;
		}
		out << "    ]" << endl;
	}
	if (pModel->TexCoords.size() > 1) {
		out << "    \"float uv\" [" << endl;
		for (int vertexId : vertexOrder) {
			const MapEntry& entry = mapVectorToEntry[vertexId];
			const ObjTexCoord& tc = pModel->TexCoords[entry.texCoord];
			out << "      " << tc.U << " " << (mirror ? 1 - tc.V : tc.V) << endl;
		}
		out << "    ]" << endl;
	}
	out << "    \"integer indices\" [" << endl;
	forEachTriangle([&] (const ObjTriangle& tri) {
		out << "      " << mapVectorToEntry[tri.Vertex[0]].id
			     << " " << mapVectorToEntry[tri.Vertex[1]].id
				 << " " << mapVectorToEntry[tri.Vertex[2]].id << endl;
	});
	out << "    ]" << endl;
	out << "AttributeEnd" << endl;

	if (pbrtFileName)
		pbrtOut.close();
	delete pModel;
}