source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/utils/converters/utf8/src/obj2utf8.cc@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 5.0 KB
Line 
1#if 0 // A cute trick to making this .cc self-building from shell.
2g++ $0 -O2 -Wall -Werror -o `basename $0 .cc`;
3exit;
4#endif
5// Copyright 2011 Google Inc. All Rights Reserved.
6//
7// Licensed under the Apache License, Version 2.0 (the "License"); you
8// may not use this file except in compliance with the License. You
9// may obtain a copy of the License at
10//
11// http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16// implied. See the License for the specific language governing
17// permissions and limitations under the License.
18
19#include "bounds.h"
20#include "compress.h"
21#include "mesh.h"
22#include "optimize.h"
23#include "stream.h"
24
25int main(int argc, const char* argv[]) {
26 FILE* json_out = stdout;
27 if (argc != 3 && argc != 4) {
28 fprintf(stderr, "Usage: %s in.obj out.utf8\n\n"
29 "\tCompress in.obj to out.utf8 and writes JS to STDOUT.\n\n",
30 argv[0]);
31 return -1;
32 } else if (argc == 4) {
33 json_out = fopen(argv[3], "w");
34 CHECK(json_out != NULL);
35 }
36
37 FILE* fp = fopen(argv[1], "r");
38 WavefrontObjFile obj(fp);
39 fclose(fp);
40
41 fputs("{\n \"materials\": {\n", json_out);
42 const MaterialList& materials = obj.materials();
43 for (size_t i = 0; i < materials.size(); ++i) {
44 materials[i].DumpJson(json_out);
45 const bool last = i == materials.size() - 1;
46 fputs(",\n" + last, json_out);
47 }
48 fputs(" },\n", json_out);
49
50 const MaterialBatches& batches = obj.material_batches();
51
52 // Pass 1: compute bounds.
53 webgl_loader::Bounds bounds;
54 bounds.Clear();
55 for (MaterialBatches::const_iterator iter = batches.begin();
56 iter != batches.end(); ++iter) {
57 const DrawBatch& draw_batch = iter->second;
58 bounds.Enclose(draw_batch.draw_mesh().attribs);
59 }
60 webgl_loader::BoundsParams bounds_params =
61 webgl_loader::BoundsParams::FromBounds(bounds);
62 fputs(" \"decodeParams\": ", json_out);
63 bounds_params.DumpJson(json_out);
64 fputs(", \"urls\": {\n", json_out);
65 // Pass 2: quantize, optimize, compress, report.
66 FILE* utf8_out_fp = fopen(argv[2], "wb");
67 CHECK(utf8_out_fp != NULL);
68 fprintf(json_out, " \"%s\": [\n", argv[2]);
69 webgl_loader::FileSink utf8_sink(utf8_out_fp);
70 size_t offset = 0;
71 MaterialBatches::const_iterator iter = batches.begin();
72 while (iter != batches.end()) {
73 const DrawMesh& draw_mesh = iter->second.draw_mesh();
74 if (draw_mesh.indices.empty()) {
75 ++iter;
76 continue;
77 }
78 QuantizedAttribList quantized_attribs;
79 webgl_loader::AttribsToQuantizedAttribs(draw_mesh.attribs, bounds_params,
80 &quantized_attribs);
81 VertexOptimizer vertex_optimizer(quantized_attribs);
82 const std::vector<GroupStart>& group_starts = iter->second.group_starts();
83 WebGLMeshList webgl_meshes;
84 std::vector<size_t> group_lengths;
85 for (size_t i = 1; i < group_starts.size(); ++i) {
86 const size_t here = group_starts[i-1].offset;
87 const size_t length = group_starts[i].offset - here;
88 group_lengths.push_back(length);
89 vertex_optimizer.AddTriangles(&draw_mesh.indices[here], length,
90 &webgl_meshes);
91 }
92 const size_t here = group_starts.back().offset;
93 const size_t length = draw_mesh.indices.size() - here;
94 CHECK(length % 3 == 0);
95 group_lengths.push_back(length);
96 vertex_optimizer.AddTriangles(&draw_mesh.indices[here], length,
97 &webgl_meshes);
98
99 std::vector<std::string> material;
100 std::vector<size_t> attrib_start, attrib_length, index_start, index_length;
101 for (size_t i = 0; i < webgl_meshes.size(); ++i) {
102 const size_t num_attribs = webgl_meshes[i].attribs.size();
103 const size_t num_indices = webgl_meshes[i].indices.size();
104 CHECK(num_attribs % 8 == 0);
105 CHECK(num_indices % 3 == 0);
106 webgl_loader::CompressQuantizedAttribsToUtf8(webgl_meshes[i].attribs,
107 &utf8_sink);
108 webgl_loader::CompressIndicesToUtf8(webgl_meshes[i].indices, &utf8_sink);
109 material.push_back(iter->first);
110 attrib_start.push_back(offset);
111 attrib_length.push_back(num_attribs / 8);
112 index_start.push_back(offset + num_attribs);
113 index_length.push_back(num_indices / 3);
114 offset += num_attribs + num_indices;
115 }
116 for (size_t i = 0; i < webgl_meshes.size(); ++i) {
117 fprintf(json_out,
118 " { \"material\": \"%s\",\n"
119 " \"attribRange\": [" PRIuS ", " PRIuS "],\n"
120 " \"indexRange\": [" PRIuS ", " PRIuS "]\n"
121 " }",
122 material[i].c_str(),
123 attrib_start[i], attrib_length[i],
124 index_start[i], index_length[i]);
125 if (i != webgl_meshes.size() - 1) {
126 fputs(",\n", json_out);
127 }
128 }
129 const bool last = (++iter == batches.end());
130 fputs(",\n" + last, json_out);
131 }
132 fputs(" ]\n", json_out);
133 fputs(" }\n}", json_out);
134 return 0;
135}
Note: See TracBrowser for help on using the repository browser.