|
| 1 | + |
| 2 | + |
| 3 | +// #include <UT/UT_DSOVersion.h> |
| 4 | +#include <GU/GU_Detail.h> |
| 5 | +#include "VRAY_nCache.h" |
| 6 | +#include <MCXMemoryReader.h> |
| 7 | +#include <MCMemoryReader.h> |
| 8 | + |
| 9 | +static VRAY_ProceduralArg theArgs[] = { |
| 10 | + VRAY_ProceduralArg("file", "string", ""), |
| 11 | + VRAY_ProceduralArg() |
| 12 | +}; |
| 13 | + |
| 14 | +VRAY_Procedural * |
| 15 | +allocProcedural(const char *) |
| 16 | +{ |
| 17 | + return new VRAY_nCache(); |
| 18 | +} |
| 19 | + |
| 20 | +const VRAY_ProceduralArg * |
| 21 | +getProceduralArgs(const char *) |
| 22 | +{ |
| 23 | + return theArgs; |
| 24 | +} |
| 25 | + |
| 26 | +VRAY_nCache::VRAY_nCache() |
| 27 | +{ |
| 28 | + myBox.initBounds(0, 0, 0); |
| 29 | +} |
| 30 | + |
| 31 | +VRAY_nCache::~VRAY_nCache() |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +const char * |
| 36 | +VRAY_nCache::className() const |
| 37 | +{ |
| 38 | + return "VRAY_nCache"; |
| 39 | +} |
| 40 | + |
| 41 | +int |
| 42 | +VRAY_nCache::initialize(const UT_BoundingBox *box) |
| 43 | +{ |
| 44 | + // Import the filenames |
| 45 | + import("file", myFile); |
| 46 | + fprintf(stderr,"VRAY_nCache file = %s\n",myFile.c_str()); |
| 47 | + |
| 48 | + if (myFile.endsWith(".mcx")) |
| 49 | + _memory_reader_ptr.reset(new nCache::MCXMemoryReader(myFile.c_str())); |
| 50 | + else if (myFile.endsWith(".mc")) |
| 51 | + _memory_reader_ptr.reset(new nCache::MCMemoryReader(myFile.c_str())); |
| 52 | + else |
| 53 | + return 0; |
| 54 | + |
| 55 | + // The user is required to specify the bounds of the geometry. |
| 56 | + // Alternatively, if the bounds aren't specified, we could forcibly load |
| 57 | + // the geometry at this point and compute the bounds ourselves. |
| 58 | + if (!box) |
| 59 | + { |
| 60 | + const nCache::ChannelDataContainer& channels_data = _memory_reader_ptr->get_channels_data(); |
| 61 | + if (!channels_data.empty()) |
| 62 | + { |
| 63 | + nCache::ChannelDataContainer::const_iterator iter; |
| 64 | + nCache::ChannelDataContainer::const_iterator eIter = channels_data.end(); |
| 65 | + for (iter = channels_data.begin();iter!=eIter;++iter) |
| 66 | + { |
| 67 | + UT_StringHolder channel_name(iter->first.c_str()); |
| 68 | + if (channel_name.endsWith("_position")) |
| 69 | + { |
| 70 | + size_t num_points = iter->second._fvca.size(); |
| 71 | + for (size_t ptIndex=0;ptIndex<num_points;ptIndex++) |
| 72 | + { |
| 73 | + UT_Vector3F P(iter->second._fvca[ptIndex].x,iter->second._fvca[ptIndex].y,iter->second._fvca[ptIndex].z); |
| 74 | + _current_frame_points.append(P); |
| 75 | + myBox.enlargeBounds(P); |
| 76 | + } |
| 77 | + break; // Found, no need to continue |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + else |
| 82 | + return 0; |
| 83 | + } |
| 84 | + else |
| 85 | + // Stash away the box the user specified |
| 86 | + myBox = *box; |
| 87 | + |
| 88 | + fpreal fps = 24.0, shutter[2] = {0}; |
| 89 | + import("global:fps", &fps, 1); |
| 90 | + import("camera:shutter", shutter, 2); |
| 91 | + |
| 92 | + return 1; |
| 93 | +} |
| 94 | + |
| 95 | +void |
| 96 | +VRAY_nCache::getBoundingBox(UT_BoundingBox &box) |
| 97 | +{ |
| 98 | + box = myBox; |
| 99 | +} |
| 100 | + |
| 101 | +void VRAY_nCache::addSamples(const Vector3Container& i_points, |
| 102 | + VRAY_ProceduralGeo& o_geo) |
| 103 | +{ |
| 104 | + GA_RWAttributeRef id_attrib = o_geo->addIntTuple(GA_ATTRIB_POINT, "id", 1); |
| 105 | + if (!id_attrib.isValid()) |
| 106 | + { |
| 107 | + std::cerr << "Failed to add point id attribute" << std::endl; |
| 108 | + return; |
| 109 | + } |
| 110 | + GA_Offset ptoff = o_geo->appendPointBlock(i_points.entries()); |
| 111 | + |
| 112 | + UT_ValArray<int32> point_id; |
| 113 | + for (GA_Size i=0;i<i_points.entries();i++) |
| 114 | + { |
| 115 | + o_geo->setPos3(ptoff, i_points.getRawArray()[i]); |
| 116 | + point_id.append(i); |
| 117 | + ptoff++; |
| 118 | + } |
| 119 | + |
| 120 | + o_geo->setAttributeFromArray(id_attrib,o_geo->getPointRange(), point_id); |
| 121 | +} |
| 122 | +void |
| 123 | +VRAY_nCache::render() |
| 124 | +{ |
| 125 | + // Allocate geometry. |
| 126 | + // Warning: When allocating geometry for a procedural, do not simply |
| 127 | + // construct a GU_Detail, but call VRAY_Procedural::createGeometry(). |
| 128 | + VRAY_ProceduralGeo g0 = createGeometry(); |
| 129 | + |
| 130 | + addSamples(this->_current_frame_points,g0); |
| 131 | + |
| 132 | + VRAY_ProceduralChildPtr obj = createChild(); |
| 133 | + obj->addGeometry(g0); |
| 134 | +} |
0 commit comments