src/gldata.cpp

changeset 443
a70dd25dd4bb
parent 442
4852e815df29
child 487
a350c4b25133
equal deleted inserted replaced
442:4852e815df29 443:a70dd25dd4bb
24 // ============================================================================= 24 // =============================================================================
25 // ----------------------------------------------------------------------------- 25 // -----------------------------------------------------------------------------
26 void VertexCompiler::Array::clear() { 26 void VertexCompiler::Array::clear() {
27 delete[] m_data; 27 delete[] m_data;
28 28
29 m_data = new DataType[64]; 29 m_data = new Vertex[64];
30 m_size = 64; 30 m_size = 64;
31 m_ptr = &m_data[0]; 31 m_ptr = &m_data[0];
32 } 32 }
33 33
34 // ============================================================================= 34 // =============================================================================
35 // ----------------------------------------------------------------------------- 35 // -----------------------------------------------------------------------------
36 void VertexCompiler::Array::resizeToFit ( 36 void VertexCompiler::Array::resizeToFit (Size newSize) {
37 VertexCompiler::Array::SizeType newSize
38 ) {
39 if (allocatedSize() >= newSize) 37 if (allocatedSize() >= newSize)
40 return; 38 return;
41 39
42 int32 cachedWriteSize = writtenSize(); 40 int32 cachedWriteSize = writtenSize();
43 41
44 // Add some lee-way space to reduce the amount of resizing. 42 // Add some lee-way space to reduce the amount of resizing.
45 newSize += 256; 43 newSize += 256;
46 44
47 const SizeType oldSize = allocatedSize(); 45 const Size oldSize = allocatedSize();
48 46
49 // We need to back up the data first 47 // We need to back up the data first
50 DataType* copy = new DataType[oldSize]; 48 Vertex* copy = new Vertex[oldSize];
51 memcpy (copy, m_data, oldSize); 49 memcpy (copy, m_data, oldSize);
52 50
53 // Re-create the buffer 51 // Re-create the buffer
54 delete[] m_data; 52 delete[] m_data;
55 m_data = new DataType[newSize]; 53 m_data = new Vertex[newSize];
56 m_size = newSize; 54 m_size = newSize;
57 m_ptr = &m_data[cachedWriteSize / sizeof (DataType)]; 55 m_ptr = &m_data[cachedWriteSize / sizeof (Vertex)];
58 56
59 // Copy the data back 57 // Copy the data back
60 memcpy (m_data, copy, oldSize); 58 memcpy (m_data, copy, oldSize);
61 delete[] copy; 59 delete[] copy;
62 } 60 }
63 61
64 // ============================================================================= 62 // =============================================================================
65 // ----------------------------------------------------------------------------- 63 // -----------------------------------------------------------------------------
66 const VertexCompiler::Array::DataType* VertexCompiler::Array::data() const { 64 const VertexCompiler::Vertex* VertexCompiler::Array::data() const {
67 return m_data; 65 return m_data;
68 } 66 }
69 67
70 // ============================================================================= 68 // =============================================================================
71 // ----------------------------------------------------------------------------- 69 // -----------------------------------------------------------------------------
72 const VertexCompiler::Array::SizeType& VertexCompiler::Array::allocatedSize() const { 70 const VertexCompiler::Array::Size& VertexCompiler::Array::allocatedSize() const {
73 return m_size; 71 return m_size;
74 } 72 }
75 73
76 // ============================================================================= 74 // =============================================================================
77 // ----------------------------------------------------------------------------- 75 // -----------------------------------------------------------------------------
78 VertexCompiler::Array::SizeType VertexCompiler::Array::writtenSize() const { 76 VertexCompiler::Array::Size VertexCompiler::Array::writtenSize() const {
79 return (m_ptr - m_data) * sizeof (DataType); 77 return (m_ptr - m_data) * sizeof (Vertex);
80 } 78 }
81 79
82 // ============================================================================= 80 // =============================================================================
83 // ----------------------------------------------------------------------------- 81 // -----------------------------------------------------------------------------
84 void VertexCompiler::Array::write ( 82 void VertexCompiler::Array::write (const Vertex& f) {
85 VertexCompiler::Array::DataType f 83 // Ensure there's enoughspace for the new vertex
86 ) {
87 // Ensure there's enoughspace for the new float
88 resizeToFit (writtenSize() + sizeof f); 84 resizeToFit (writtenSize() + sizeof f);
89 85
90 // Write the float in 86 // Write the float in
91 *m_ptr++ = f; 87 *m_ptr++ = f;
92 } 88 }
98 ) { 94 ) {
99 // Ensure there's room for both buffers 95 // Ensure there's room for both buffers
100 resizeToFit (writtenSize() + other->writtenSize()); 96 resizeToFit (writtenSize() + other->writtenSize());
101 97
102 memcpy (m_ptr, other->data(), other->writtenSize()); 98 memcpy (m_ptr, other->data(), other->writtenSize());
103 m_ptr += other->writtenSize() / sizeof (DataType); 99 m_ptr += other->writtenSize() / sizeof (Vertex);
104 } 100 }
105 101
106 // ============================================================================= 102 // =============================================================================
107 // ----------------------------------------------------------------------------- 103 // -----------------------------------------------------------------------------
108 VertexCompiler::VertexCompiler() : 104 VertexCompiler::VertexCompiler() : m_file (null) {
109 m_file (null)
110 {
111 for (int i = 0; i < NumArrays; ++i) { 105 for (int i = 0; i < NumArrays; ++i) {
112 m_mainArrays[i] = new Array; 106 m_mainArrays[i] = new Array;
113 m_changed[i] = false; 107 m_changed[i] = false;
114 } 108 }
115 } 109 }
121 delete array; 115 delete array;
122 } 116 }
123 117
124 // ============================================================================= 118 // =============================================================================
125 // ----------------------------------------------------------------------------- 119 // -----------------------------------------------------------------------------
126 void VertexCompiler::compileVertex ( 120 void VertexCompiler::compileVertex (::vertex v, QColor col, Array* array) {
127 vertex v,
128 QColor col,
129 Array* array
130 ) {
131 VertexCompiler::Vertex glvertex; 121 VertexCompiler::Vertex glvertex;
132 glvertex.x = v.x(); 122 glvertex.x = v.x();
133 glvertex.y = v.y(); 123 glvertex.y = -v.y();
134 glvertex.z = v.z(); 124 glvertex.z = v.z();
135 glvertex.color = 125 glvertex.color =
136 (col.red() & 0xFF) << 0x00 | 126 (col.red() & 0xFF) << 0x00 |
137 (col.green() & 0xFF) << 0x08 | 127 (col.green() & 0xFF) << 0x08 |
138 (col.blue() & 0xFF) << 0x10 | 128 (col.blue() & 0xFF) << 0x10 |
141 array->write (glvertex); 131 array->write (glvertex);
142 } 132 }
143 133
144 // ============================================================================= 134 // =============================================================================
145 // ----------------------------------------------------------------------------- 135 // -----------------------------------------------------------------------------
146 void VertexCompiler::compilePolygon ( 136 // Note: we use the true object's color but the draw object's vertices. This is
147 LDObject* drawobj, 137 // so that the index color is generated correctly - it has to reference the true
148 LDObject* trueobj 138 // object's ID. This is crucial for picking to work.
149 ) { 139 void VertexCompiler::compilePolygon (LDObject* drawobj, LDObject* trueobj) {
150 // Note: we use the true object's color but the draw object's vertices. This
151 // is so that the index color is generated correctly - it has to reference
152 // the true object's ID, this is crucial for picking to work.
153 Array** arrays = m_objArrays[trueobj]; 140 Array** arrays = m_objArrays[trueobj];
154 LDObject::Type objtype = drawobj->getType(); 141 LDObject::Type objtype = drawobj->getType();
155 bool isline = (objtype == LDObject::Line || objtype == LDObject::CondLine); 142 const bool isline = (objtype == LDObject::Line || objtype == LDObject::CondLine);
156 int verts = isline ? 2 : drawobj->vertices(); 143 const int verts = isline ? 2 : drawobj->vertices();
157 144
158 QColor normalColor = getObjectColor (trueobj, Normal), 145 QColor normalColor = getObjectColor (trueobj, Normal),
159 pickColor = getObjectColor (trueobj, PickColor); 146 pickColor = getObjectColor (trueobj, PickColor);
160 147
161 for (int i = 0; i < verts; ++i) { 148 for (int i = 0; i < verts; ++i) {
162 compileVertex (drawobj->getVertex (i), normalColor, arrays[isline ? EdgeArray : MainArray]); 149 compileVertex (drawobj->getVertex (i), normalColor, arrays[isline ? EdgeArray : MainArray]);
163 compileVertex (drawobj->getVertex (i), pickColor, arrays[isline ? EdgePickArray : PickArray]); 150 compileVertex (drawobj->getVertex (i), pickColor, arrays[isline ? EdgePickArray : PickArray]);
164 } 151 }
165 152
166 // For non-lines, compile BFC data 153 // For non-lines, compile BFC data. Note that the front objects are what get
154 // reversed here! This is because we invert the Y axis, which inverts the
155 // entire part model, so we remedy that here.
167 if (!isline) { 156 if (!isline) {
168 QColor col = getObjectColor (trueobj, BFCFront); 157 QColor col = getObjectColor (trueobj, BFCBack);
169 for (int i = 0; i < verts; ++i) 158 for (int i = 0; i < verts; ++i)
170 compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]); 159 compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]);
171 160
172 col = getObjectColor (trueobj, BFCBack); 161 col = getObjectColor (trueobj, BFCFront);
173 for (int i = verts - 1; i >= 0; --i) 162 for (int i = verts - 1; i >= 0; --i)
174 compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]); 163 compileVertex (drawobj->getVertex(i), col, arrays[BFCArray]);
175 } 164 }
176 } 165 }
177 166
178 // ============================================================================= 167 // =============================================================================
179 // ----------------------------------------------------------------------------- 168 // -----------------------------------------------------------------------------
180 void VertexCompiler::compileObject ( 169 void VertexCompiler::compileObject (LDObject* obj, LDObject* topobj) {
181 LDObject* obj
182 ) {
183 if (m_objArrays.find (obj) == m_objArrays.end()) { 170 if (m_objArrays.find (obj) == m_objArrays.end()) {
184 m_objArrays[obj] = new Array*[NumArrays]; 171 m_objArrays[obj] = new Array*[NumArrays];
185 172
186 for (int i = 0; i < NumArrays; ++i) 173 for (int i = 0; i < NumArrays; ++i)
187 m_objArrays[obj][i] = new Array; 174 m_objArrays[obj][i] = new Array;
188 } else { 175 } else {
189 for (int i = 0; i < NumArrays; ++i) 176 for (int i = 0; i < NumArrays; ++i)
190 m_objArrays[obj][i]->clear(); 177 m_objArrays[obj][i]->clear();
191 } 178 }
192 179
180 List<LDObject*> objs;
181
193 switch (obj->getType()) { 182 switch (obj->getType()) {
194 case LDObject::Triangle: 183 case LDObject::Triangle:
195 compilePolygon (obj, obj); 184 compilePolygon (obj, topobj);
196 m_changed[MainArray] = true; 185 m_changed[MainArray] = true;
197 break; 186 break;
198 187
199 case LDObject::Quad: 188 case LDObject::Quad:
200 for (LDTriangleObject* triangle : static_cast<LDQuadObject*> (obj)->splitToTriangles()) 189 for (LDTriangleObject* triangle : static_cast<LDQuadObject*> (obj)->splitToTriangles())
201 compilePolygon (triangle, obj); 190 compilePolygon (triangle, topobj);
202 m_changed[MainArray] = true; 191 m_changed[MainArray] = true;
203 break; 192 break;
204 193
205 case LDObject::Line: 194 case LDObject::Line:
206 compilePolygon (obj, obj); 195 compilePolygon (obj, topobj);
196 break;
197
198 case LDObject::Subfile:
199 objs = static_cast<LDSubfileObject*> (obj)->inlineContents (true, true);
200
201 for (LDObject* obj : objs) {
202 compileObject (obj, topobj);
203 delete obj;
204 }
207 break; 205 break;
208 206
209 default: 207 default:
210 break; 208 break;
211 } 209 }
213 211
214 // ============================================================================= 212 // =============================================================================
215 // ----------------------------------------------------------------------------- 213 // -----------------------------------------------------------------------------
216 void VertexCompiler::compileFile() { 214 void VertexCompiler::compileFile() {
217 for (LDObject* obj : *m_file) 215 for (LDObject* obj : *m_file)
218 compileObject (obj); 216 compileObject (obj, obj);
219 } 217 }
220 218
221 // ============================================================================= 219 // =============================================================================
222 // ----------------------------------------------------------------------------- 220 // -----------------------------------------------------------------------------
223 void VertexCompiler::forgetObject ( 221 void VertexCompiler::forgetObject (LDObject* obj) {
224 LDObject* obj
225 ) {
226 for (int i = 0; i < NumArrays; ++i) 222 for (int i = 0; i < NumArrays; ++i)
227 delete m_objArrays[obj][i]; 223 delete m_objArrays[obj][i];
228 224
229 delete m_objArrays[obj]; 225 delete m_objArrays[obj];
230 m_objArrays.remove (obj); 226 m_objArrays.remove (obj);
231 } 227 }
232 228
233 // ============================================================================= 229 // =============================================================================
234 // ----------------------------------------------------------------------------- 230 // -----------------------------------------------------------------------------
235 void VertexCompiler::setFile ( 231 void VertexCompiler::setFile (LDFile* file) {
236 LDFile* file
237 ) {
238 m_file = file; 232 m_file = file;
239 } 233 }
240 234
241 // ============================================================================= 235 // =============================================================================
242 // ----------------------------------------------------------------------------- 236 // -----------------------------------------------------------------------------
243 VertexCompiler::Array* VertexCompiler::getMergedBuffer ( 237 VertexCompiler::Array* VertexCompiler::getMergedBuffer (ArrayType type) {
244 VertexCompiler::MergedArrayType type
245 ) {
246 assert (type < NumArrays); 238 assert (type < NumArrays);
247 239
248 if (m_changed) { 240 if (m_changed) {
249 m_changed[type] = false; 241 m_changed[type] = false;
250 m_mainArrays[type]->clear(); 242 m_mainArrays[type]->clear();
261 } 253 }
262 254
263 // ============================================================================= 255 // =============================================================================
264 // ----------------------------------------------------------------------------- 256 // -----------------------------------------------------------------------------
265 static List<short> g_warnedColors; 257 static List<short> g_warnedColors;
266 QColor VertexCompiler::getObjectColor ( 258 QColor VertexCompiler::getObjectColor (LDObject* obj, ColorType colotype) const {
267 LDObject* obj,
268 ColorType colotype
269 ) const {
270 QColor qcol; 259 QColor qcol;
271 260
272 if (!obj->isColored()) 261 if (!obj->isColored())
273 return QColor(); 262 return QColor();
274 263

mercurial