| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 virtual T& operator[](unsigned int translated_index) { | 175 virtual T& operator[](unsigned int translated_index) { |
| 176 if (translated_index >= translated_end_index_) { | 176 if (translated_index >= translated_end_index_) { |
| 177 O3D_ERROR(buffer_->service_locator()) | 177 O3D_ERROR(buffer_->service_locator()) |
| 178 << "Index " << real_start_index_ + translated_index | 178 << "Index " << real_start_index_ + translated_index |
| 179 << " into buffer '" << buffer_->name() << "' is out of range."; | 179 << " into buffer '" << buffer_->name() << "' is out of range."; |
| 180 translated_index = 0; | 180 translated_index = 0; |
| 181 } | 181 } |
| 182 return *reinterpret_cast<T*>(reinterpret_cast<char*>(data_) + offset_ + | 182 return *reinterpret_cast<T*>(reinterpret_cast<char*>(data_) + offset_ + |
| 183 stride_ * (translated_index + real_start_index_)); | 183 stride_ * (translated_index + real_start_index_)); |
| 184 } | 184 } |
| 185 void Initialize(const Field& field, unsigned int start_index, | 185 virtual void Initialize(const Field& field, unsigned int start_index, |
| 186 unsigned int length) { | 186 unsigned int length) { |
| 187 buffer_ = field.buffer(); | 187 buffer_ = field.buffer(); |
| 188 locked_ = false; | 188 locked_ = false; |
| 189 data_ = NULL; | 189 data_ = NULL; |
| 190 offset_ = field.offset(); | 190 offset_ = field.offset(); |
| 191 stride_ = buffer_->stride(); | 191 stride_ = buffer_->stride(); |
| 192 real_start_index_ = start_index; | 192 real_start_index_ = start_index; |
| 193 unsigned int max_index = std::min(length, buffer_->num_elements()); | 193 unsigned int max_index = std::min(length, buffer_->num_elements()); |
| 194 translated_end_index_ = max_index - start_index; | 194 translated_end_index_ = max_index - start_index; |
| 195 // Check for unsigned wrap. | 195 // Check for unsigned wrap. |
| 196 if (translated_end_index_ > max_index) { | 196 if (translated_end_index_ > max_index) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 218 bool locked_; | 218 bool locked_; |
| 219 void* data_; | 219 void* data_; |
| 220 unsigned int offset_; | 220 unsigned int offset_; |
| 221 unsigned int stride_; | 221 unsigned int stride_; |
| 222 unsigned int real_start_index_; | 222 unsigned int real_start_index_; |
| 223 unsigned int translated_end_index_; | 223 unsigned int translated_end_index_; |
| 224 | 224 |
| 225 DISALLOW_COPY_AND_ASSIGN(FieldReadAccessor); | 225 DISALLOW_COPY_AND_ASSIGN(FieldReadAccessor); |
| 226 }; | 226 }; |
| 227 | 227 |
| 228 // Specialization which pads out the fetched coordinates with zeros, |
| 229 // to be able to handle 2D Position streams. |
| 230 class FieldReadAccessorPoint3 : public FieldReadAccessor<Point3> { |
| 231 public: |
| 232 FieldReadAccessorPoint3() |
| 233 : FieldReadAccessor<Point3>(), |
| 234 num_components_(0), |
| 235 cache_index_(0) { } |
| 236 |
| 237 virtual void Initialize(const Field& field, unsigned int start_index, |
| 238 unsigned int length) { |
| 239 FieldReadAccessor<Point3>::Initialize(field, start_index, length); |
| 240 num_components_ = field.num_components(); |
| 241 } |
| 242 |
| 243 virtual Point3& operator[](unsigned int translated_index) { |
| 244 Point3& tmp = FieldReadAccessor<Point3>::operator[](translated_index); |
| 245 Point3& cur = cache_[cache_index_]; |
| 246 cache_index_ = (cache_index_ + 1) % 3; |
| 247 for (unsigned int i = 0; i < num_components_; i++) { |
| 248 cur[i] = tmp[i]; |
| 249 } |
| 250 for (unsigned int i = num_components_; i < 3; i++) { |
| 251 cur[i] = 0; |
| 252 } |
| 253 return cur; |
| 254 } |
| 255 |
| 256 protected: |
| 257 unsigned int num_components_; |
| 258 // We need a cache of the three most recently fetched vertices to |
| 259 // match the usage elsewhere in this file |
| 260 int cache_index_; |
| 261 Point3 cache_[3]; |
| 262 }; |
| 263 |
| 228 class FieldReadAccessorUnsignedInt : public FieldReadAccessor<unsigned int> { | 264 class FieldReadAccessorUnsignedInt : public FieldReadAccessor<unsigned int> { |
| 229 public: | 265 public: |
| 230 FieldReadAccessorUnsignedInt() | 266 FieldReadAccessorUnsignedInt() |
| 231 : FieldReadAccessor<unsigned int>(), | 267 : FieldReadAccessor<unsigned int>(), |
| 232 just_count_(false) { } | 268 just_count_(false) { } |
| 233 void InitializeJustCount(unsigned int start_index, unsigned int length) { | 269 void InitializeJustCount(unsigned int start_index, unsigned int length) { |
| 234 initialized_ = true; | 270 initialized_ = true; |
| 235 real_start_index_ = start_index; | 271 real_start_index_ = start_index; |
| 236 translated_end_index_ = length; | 272 translated_end_index_ = length; |
| 237 locked_ = false; | 273 locked_ = false; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 250 } else { | 286 } else { |
| 251 return FieldReadAccessor<unsigned int>::operator[](translated_index); | 287 return FieldReadAccessor<unsigned int>::operator[](translated_index); |
| 252 } | 288 } |
| 253 } | 289 } |
| 254 protected: | 290 protected: |
| 255 unsigned int counter_; | 291 unsigned int counter_; |
| 256 bool just_count_; | 292 bool just_count_; |
| 257 }; | 293 }; |
| 258 | 294 |
| 259 // Attempts to initialize a FieldReadAccessor for the stream of vertices of | 295 // Attempts to initialize a FieldReadAccessor for the stream of vertices of |
| 260 // the primitive. Returns success. | 296 // the primitive. Returns newly allocated accessor which must be deleted |
| 261 bool GetVerticesAccessor(const Primitive* primitive, | 297 // by caller, or NULL upon failure. |
| 262 int position_stream_index, | 298 FieldReadAccessor<Point3>* GetVerticesAccessor(const Primitive* primitive, |
| 263 FieldReadAccessor<Point3>* accessor) { | 299 int position_stream_index) { |
| 264 if (!(accessor && primitive)) | 300 if (!primitive) |
| 265 return false; | 301 return NULL; |
| 266 | 302 |
| 267 const StreamBank* stream_bank = primitive->stream_bank(); | 303 const StreamBank* stream_bank = primitive->stream_bank(); |
| 268 if (!stream_bank) { | 304 if (!stream_bank) { |
| 269 O3D_ERROR(primitive->service_locator()) | 305 O3D_ERROR(primitive->service_locator()) |
| 270 << "No stream bank on Primitive '" << primitive->name() << "'"; | 306 << "No stream bank on Primitive '" << primitive->name() << "'"; |
| 271 return false; | 307 return NULL; |
| 272 } | 308 } |
| 273 | 309 |
| 274 const Stream* vertex_stream = stream_bank->GetVertexStream( | 310 const Stream* vertex_stream = stream_bank->GetVertexStream( |
| 275 Stream::POSITION, | 311 Stream::POSITION, |
| 276 position_stream_index); | 312 position_stream_index); |
| 277 | 313 |
| 278 if (!vertex_stream) { | 314 if (!vertex_stream) { |
| 279 O3D_ERROR(primitive->service_locator()) | 315 O3D_ERROR(primitive->service_locator()) |
| 280 << "No POSITION stream index " | 316 << "No POSITION stream index " |
| 281 << position_stream_index; | 317 << position_stream_index; |
| 282 return false; | 318 return NULL; |
| 283 } | 319 } |
| 284 | 320 |
| 285 const Field& field = vertex_stream->field(); | 321 const Field& field = vertex_stream->field(); |
| 286 | 322 |
| 287 if (!field.buffer()) { | 323 if (!field.buffer()) { |
| 288 O3D_ERROR(primitive->service_locator()) << "Vertex Buffer not set"; | 324 O3D_ERROR(primitive->service_locator()) << "Vertex Buffer not set"; |
| 289 return false; | 325 return NULL; |
| 290 } | 326 } |
| 291 | 327 |
| 292 if (!field.IsA(FloatField::GetApparentClass())) { | 328 if (!field.IsA(FloatField::GetApparentClass())) { |
| 293 O3D_ERROR(primitive->service_locator()) << "POSITION stream index " | 329 O3D_ERROR(primitive->service_locator()) << "POSITION stream index " |
| 294 << position_stream_index | 330 << position_stream_index |
| 295 << " is not a FLOAT stream"; | 331 << " is not a FLOAT stream"; |
| 296 return false; | 332 return NULL; |
| 297 } | 333 } |
| 298 | 334 |
| 299 if (field.num_components() != 3) { | 335 // Don't even try to lock fields that don't have any data |
| 300 O3D_ERROR(primitive->service_locator()) | 336 if (vertex_stream->GetMaxVertices() == 0) { |
| 301 << "POSITION stream index " << position_stream_index | 337 return NULL; |
| 302 << " does not have 3 components"; | 338 } |
| 303 return false; | 339 |
| 340 FieldReadAccessor<Point3>* accessor; |
| 341 if (field.num_components() == 3) { |
| 342 accessor = new FieldReadAccessor<Point3>(); |
| 343 } else { |
| 344 accessor = new FieldReadAccessorPoint3(); |
| 304 } | 345 } |
| 305 | 346 |
| 306 accessor->Initialize(field, vertex_stream->start_index(), | 347 accessor->Initialize(field, vertex_stream->start_index(), |
| 307 vertex_stream->GetMaxVertices()); | 348 vertex_stream->GetMaxVertices()); |
| 308 | 349 |
| 309 if (!accessor->Valid()) { | 350 if (!accessor->Valid()) { |
| 310 O3D_ERROR(primitive->service_locator()) | 351 O3D_ERROR(primitive->service_locator()) |
| 311 << "Could not lock vertex buffer"; | 352 << "Could not lock vertex buffer"; |
| 312 return false; | 353 delete accessor; |
| 354 return NULL; |
| 313 } | 355 } |
| 314 | 356 |
| 315 return true; | 357 return accessor; |
| 316 } | 358 } |
| 317 | 359 |
| 318 // Attempts to initialize a FieldReadAccessor for the stream of indices of | 360 // Attempts to initialize a FieldReadAccessor for the stream of indices of |
| 319 // the primitive. Returns success. | 361 // the primitive. Returns success. |
| 320 bool GetIndicesAccessor(const Primitive* primitive, | 362 bool GetIndicesAccessor(const Primitive* primitive, |
| 321 FieldReadAccessor<unsigned int>* accessor, | 363 FieldReadAccessor<unsigned int>* accessor, |
| 322 unsigned start_index, | 364 unsigned start_index, |
| 323 unsigned index_count) { | 365 unsigned index_count) { |
| 324 if (!(accessor && primitive)) | 366 if (!(accessor && primitive)) |
| 325 return false; | 367 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 340 return true; | 382 return true; |
| 341 } | 383 } |
| 342 | 384 |
| 343 } // anonymous namespace | 385 } // anonymous namespace |
| 344 | 386 |
| 345 bool Primitive::WalkPolygons( | 387 bool Primitive::WalkPolygons( |
| 346 int position_stream_index, | 388 int position_stream_index, |
| 347 PolygonFunctor* polygon_functor) const { | 389 PolygonFunctor* polygon_functor) const { |
| 348 DLOG_ASSERT(polygon_functor); | 390 DLOG_ASSERT(polygon_functor); |
| 349 | 391 |
| 350 FieldReadAccessor<Point3> vertices; | |
| 351 FieldReadAccessorUnsignedInt indices; | 392 FieldReadAccessorUnsignedInt indices; |
| 352 | 393 scoped_ptr<FieldReadAccessor<Point3> > vertices_pointer( |
| 353 if (!GetVerticesAccessor(this, position_stream_index, &vertices)) | 394 GetVerticesAccessor(this, position_stream_index)); |
| 395 if (vertices_pointer.get() == NULL) |
| 354 return false; | 396 return false; |
| 397 FieldReadAccessor<Point3>& vertices = *vertices_pointer; |
| 355 | 398 |
| 356 unsigned int index_count; | 399 unsigned int index_count; |
| 357 if (indexed()) { | 400 if (indexed()) { |
| 358 if (!Primitive::GetIndexCount(primitive_type(), | 401 if (!Primitive::GetIndexCount(primitive_type(), |
| 359 number_primitives(), | 402 number_primitives(), |
| 360 &index_count)) { | 403 &index_count)) { |
| 361 O3D_ERROR(service_locator()) | 404 O3D_ERROR(service_locator()) |
| 362 << "Unknown Primitive Type in GetIndexCount: " << primitive_type_; | 405 << "Unknown Primitive Type in GetIndexCount: " << primitive_type_; |
| 363 return false; | 406 return false; |
| 364 } | 407 } |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 } | 698 } |
| 656 | 699 |
| 657 BoundingBoxHelper helper; | 700 BoundingBoxHelper helper; |
| 658 if (WalkPolygons(position_stream_index, &helper)) { | 701 if (WalkPolygons(position_stream_index, &helper)) { |
| 659 helper.GetBoundingBox(result); | 702 helper.GetBoundingBox(result); |
| 660 } else { | 703 } else { |
| 661 *result = BoundingBox(); | 704 *result = BoundingBox(); |
| 662 } | 705 } |
| 663 } | 706 } |
| 664 } // namespace o3d | 707 } // namespace o3d |
| OLD | NEW |