示例#1
0
文件: server.cpp 项目: pdh11/chorale
/** Construct the UUID for the UPnP Universal Device Name (UDN).
 *
 * In order to get the same UUID each time we run, we bake the hostname
 * and the path into the UUID using a simple hash.
 */
std::string Server::Impl::MakeUUID(const std::string& resource)
{
    union {
        unsigned char ch[16];
        uint32_t ui[4];
    } u;

    char hostname[256];
    hostname[0] = '\0';
    gethostname(hostname, sizeof(hostname));

    strcpy((char*)u.ch, "chorale ");
    u.ui[2] = SimpleHash(hostname);
    u.ui[3] = SimpleHash(resource.c_str());

    return util::SPrintf("uuid:%08x-%04x-%04x-%04x-%04x%08x",
                         u.ui[0], u.ui[1] >> 16, u.ui[1] & 0xFFFF, u.ui[2] >> 16,
                         u.ui[2] & 0xFFFF, u.ui[3]);
}
void GenerateModelThread::CompressAndGenerateJS()
{
	string utf8Name=assembledOBJFileName.toStdString();
	int pos=utf8Name.find_last_of('\\');
	utf8Name.erase(0,pos+1);
	pos=utf8Name.find('.');
	utf8Name=utf8Name.substr(0,pos);
	string jsName=utf8Name+".js";
	jsName="web\\models\\human\\"+jsName;
	FILE *jsfile=fopen(jsName.c_str(),"w");

	if(jsfile==NULL)
	{
		qDebug()<<"open failed";
	}
	FILE* fp = fopen(assembledOBJFileName.toStdString().c_str(), "r");
	WavefrontObjFile obj(fp);
	fclose(fp);
	qDebug()<<StripLeadingDir(assembledOBJFileName.toStdString().c_str());
	fprintf(jsfile,"MODELS[\'%s\'] = {\n",StripLeadingDir(assembledOBJFileName.toStdString().c_str()));
	//puts("  materials: {");
	fputs("  materials: {\n",jsfile);

	const MaterialList& materials = obj.materials();
	for (size_t i = 0; i < materials.size(); ++i) {

		materials[i].DumpJson(jsfile);
		if(i<materials.size()-1)
			fprintf(jsfile,",");
	}
	// puts("  },");
	fputs("  },\n",jsfile);
	const MaterialBatches& batches = obj.material_batches();

	// Pass 1: compute bounds.
	webgl_loader::Bounds bounds;
	bounds.Clear();
	for (MaterialBatches::const_iterator iter = batches.begin();
		iter != batches.end(); ++iter) {
			const DrawBatch& draw_batch = iter->second;
			bounds.Enclose(draw_batch.draw_mesh().attribs);
	}
	webgl_loader::BoundsParams bounds_params = 
		webgl_loader::BoundsParams::FromBounds(bounds);
	fprintf(jsfile,"  decodeParams: ");

	bounds_params.DumpJson(jsfile);

	fputs("  urls: {\n",jsfile);
	std::vector<char> utf8;
	webgl_loader::VectorSink sink(&utf8);
	// Pass 2: quantize, optimize, compress, report.
	for (MaterialBatches::const_iterator iter = batches.begin();
		iter != batches.end(); ++iter) {
			size_t offset = 0;
			utf8.clear();
			const DrawMesh& draw_mesh = iter->second.draw_mesh();
			if (draw_mesh.indices.empty()) continue;

			QuantizedAttribList quantized_attribs;
			webgl_loader::AttribsToQuantizedAttribs(draw_mesh.attribs, bounds_params,
				&quantized_attribs);
			VertexOptimizer vertex_optimizer(quantized_attribs);
			const std::vector<GroupStart>& group_starts = iter->second.group_starts();
			WebGLMeshList webgl_meshes;
			std::vector<size_t> group_lengths;
			for (size_t i = 1; i < group_starts.size(); ++i) {
				const size_t here = group_starts[i-1].offset;
				const size_t length = group_starts[i].offset - here;
				group_lengths.push_back(length);
				vertex_optimizer.AddTriangles(&draw_mesh.indices[here], length,
					&webgl_meshes);
			}
			const size_t here = group_starts.back().offset;
			const size_t length = draw_mesh.indices.size() - here;
			const bool divisible_by_3 = length % 3 == 0;
			CHECK(divisible_by_3);
			group_lengths.push_back(length);
			vertex_optimizer.AddTriangles(&draw_mesh.indices[here], length,
				&webgl_meshes);

			std::vector<std::string> material;
			std::vector<size_t> attrib_start, attrib_length, index_start, index_length;
			for (size_t i = 0; i < webgl_meshes.size(); ++i) {
				const size_t num_attribs = webgl_meshes[i].attribs.size();
				const size_t num_indices = webgl_meshes[i].indices.size();
				const bool kBadSizes = num_attribs % 8 || num_indices % 3;
				CHECK(!kBadSizes);
				webgl_loader::CompressQuantizedAttribsToUtf8(webgl_meshes[i].attribs, 
					&sink);
				webgl_loader::CompressIndicesToUtf8(webgl_meshes[i].indices, &sink);
				material.push_back(iter->first);
				attrib_start.push_back(offset);
				attrib_length.push_back(num_attribs / 8);
				index_start.push_back(offset + num_attribs);
				index_length.push_back(num_indices / 3);
				offset += num_attribs + num_indices;
			}
			const uint32 hash = SimpleHash(&utf8[0], utf8.size());
			char buf[9] = { '\0' };
			ToHex(hash, buf);
			// TODO: this needs to handle paths.
			string strUTF8Name=utf8Name+".utf8";
			std::string out_fn = std::string(buf) + "." + strUTF8Name;

			string out="web\\models\\human\\"+out_fn;
			FILE* out_fp = fopen(out.c_str(), "wb");
			fprintf(jsfile,"    \'%s\': [\n", out_fn.c_str());
			size_t group_index = 0;
			for (size_t i = 0; i < webgl_meshes.size(); ++i) {
				fprintf(jsfile,"      { material: \'%s\',\n"
					"        attribRange: [" PRIuS ", " PRIuS "],\n"
					"        indexRange: [" PRIuS ", " PRIuS "],\n"
					"        bboxes: " PRIuS ",\n"
					"        names: [",
					material[i].c_str(),
					attrib_start[i], attrib_length[i],
					index_start[i], index_length[i],
					offset);
				std::vector<size_t> buffered_lengths;
				size_t group_start = 0;
				while (group_index < group_lengths.size()) {
					fprintf(jsfile,"\'%s\', ",
						obj.LineToGroup(group_starts[group_index].group_line).c_str());
					const size_t group_length = group_lengths[group_index];
					const size_t next_start = group_start + group_length;
					const size_t webgl_index_length = webgl_meshes[i].indices.size();
					// TODO: bbox info is better placed at the head of the file,
					// perhaps transposed. Also, when a group gets split between
					// batches, the bbox gets stored twice.
					webgl_loader::CompressAABBToUtf8(group_starts[group_index].bounds,
						bounds_params, &sink);
					offset += 6;
					if (next_start < webgl_index_length) {
						buffered_lengths.push_back(group_length);
						group_start = next_start;
						++group_index;
					} else {
						const size_t fits = webgl_index_length - group_start;
						buffered_lengths.push_back(fits);
						group_start = 0;
						group_lengths[group_index] -= fits;
						break;
					}
				}
				fprintf(jsfile,"],\n        lengths: [");
				for (size_t k = 0; k < buffered_lengths.size(); ++k) {
					fprintf(jsfile,PRIuS ", ", buffered_lengths[k]);
				}
				fputs("],\n      },\n",jsfile);
			}
			fwrite(&utf8[0], 1, utf8.size(), out_fp);
			fclose(out_fp);
			fputs("    ],\n",jsfile);
	}
	fputs("  }\n};\n",jsfile);
	fclose(jsfile);

}