History

Ray query fix.

Andrzej Kapolka committed 12 years ago

Commit 8c42a3a1 parent 56c6c3c3 files · +26 −31Browse files

Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 3 files). Show all

Unified · Split

interface/src/MetavoxelSystem.cpp+22−18
@@ -136,7 +136,6 @@ RayHeightfieldIntersectionVisitor::RayHeightfieldIntersectionVisitor(const glm:: · 135 unmodified lines
136}136}
137 137
138static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / 255.0f;138static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / 255.0f;
139static const int HEIGHT_BORDER = 1;
140 139
141int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) {140int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) {
142 if (!info.isLeaf) {141 if (!info.isLeaf) {
@@ -151,12 +150,13 @@ int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance · 8 unmodified lines
151 const uchar* src = (const uchar*)contents.constData();150 const uchar* src = (const uchar*)contents.constData();
152 int size = glm::sqrt((float)contents.size());151 int size = glm::sqrt((float)contents.size());
153 int unextendedSize = size - HeightfieldBuffer::HEIGHT_EXTENSION;152 int unextendedSize = size - HeightfieldBuffer::HEIGHT_EXTENSION;
154 int highest = HEIGHT_BORDER + unextendedSize;153 int highest = HeightfieldBuffer::HEIGHT_BORDER + unextendedSize;
155 float heightScale = unextendedSize * EIGHT_BIT_MAXIMUM_RECIPROCAL;154 float heightScale = unextendedSize * EIGHT_BIT_MAXIMUM_RECIPROCAL;
156 155
157 // find the initial location in heightfield coordinates156 // find the initial location in heightfield coordinates
158 glm::vec3 entry = (_origin + distance * _direction - info.minimum + glm::vec3(HEIGHT_BORDER, 0.0f, HEIGHT_BORDER)) *157 glm::vec3 entry = (_origin + distance * _direction - info.minimum) * (float)unextendedSize / info.size;
159 (float)unextendedSize / info.size;158 entry.x += HeightfieldBuffer::HEIGHT_BORDER;
159 entry.z += HeightfieldBuffer::HEIGHT_BORDER;
160 glm::vec3 floors = glm::floor(entry);160 glm::vec3 floors = glm::floor(entry);
161 glm::vec3 ceils = glm::ceil(entry);161 glm::vec3 ceils = glm::ceil(entry);
162 if (floors.x == ceils.x) {162 if (floors.x == ceils.x) {
@@ -178,10 +178,10 @@ int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance · 15 unmodified lines
178 float accumulatedDistance = 0.0f;178 float accumulatedDistance = 0.0f;
179 while (withinBounds) {179 while (withinBounds) {
180 // find the heights at the corners of the current cell180 // find the heights at the corners of the current cell
181 int floorX = qMin(qMax((int)floors.x, HEIGHT_BORDER), highest);181 int floorX = qMin(qMax((int)floors.x, HeightfieldBuffer::HEIGHT_BORDER), highest);
182 int floorZ = qMin(qMax((int)floors.z, HEIGHT_BORDER), highest);182 int floorZ = qMin(qMax((int)floors.z, HeightfieldBuffer::HEIGHT_BORDER), highest);
183 int ceilX = qMin(qMax((int)ceils.x, HEIGHT_BORDER), highest);183 int ceilX = qMin(qMax((int)ceils.x, HeightfieldBuffer::HEIGHT_BORDER), highest);
184 int ceilZ = qMin(qMax((int)ceils.z, HEIGHT_BORDER), highest);184 int ceilZ = qMin(qMax((int)ceils.z, HeightfieldBuffer::HEIGHT_BORDER), highest);
185 float upperLeft = src[floorZ * size + floorX] * heightScale;185 float upperLeft = src[floorZ * size + floorX] * heightScale;
186 float upperRight = src[floorZ * size + ceilX] * heightScale;186 float upperRight = src[floorZ * size + ceilX] * heightScale;
187 float lowerLeft = src[ceilZ * size + floorX] * heightScale;187 float lowerLeft = src[ceilZ * size + floorX] * heightScale;
@@ -215,13 +215,13 @@ int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance · 27 unmodified lines
215 } else {215 } else {
216 // find the exit point and the next cell, and determine whether it's still within the bounds216 // find the exit point and the next cell, and determine whether it's still within the bounds
217 exit = entry + exitDistance * _direction;217 exit = entry + exitDistance * _direction;
218 withinBounds = (exit.y >= HEIGHT_BORDER && exit.y <= highest);218 withinBounds = (exit.y >= HeightfieldBuffer::HEIGHT_BORDER && exit.y <= highest);
219 if (exitDistance == xDistance) {219 if (exitDistance == xDistance) {
220 if (_direction.x > 0.0f) {220 if (_direction.x > 0.0f) {
221 nextFloors.x += 1.0f;221 nextFloors.x += 1.0f;
222 withinBounds &= (nextCeils.x += 1.0f) <= highest;222 withinBounds &= (nextCeils.x += 1.0f) <= highest;
223 } else {223 } else {
224 withinBounds &= (nextFloors.x -= 1.0f) >= HEIGHT_BORDER;224 withinBounds &= (nextFloors.x -= 1.0f) >= HeightfieldBuffer::HEIGHT_BORDER;
225 nextCeils.x -= 1.0f;225 nextCeils.x -= 1.0f;
226 }226 }
227 }227 }
@@ -230,7 +230,7 @@ int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance · 2 unmodified lines
230 nextFloors.z += 1.0f;230 nextFloors.z += 1.0f;
231 withinBounds &= (nextCeils.z += 1.0f) <= highest;231 withinBounds &= (nextCeils.z += 1.0f) <= highest;
232 } else {232 } else {
233 withinBounds &= (nextFloors.z -= 1.0f) >= HEIGHT_BORDER;233 withinBounds &= (nextFloors.z -= 1.0f) >= HeightfieldBuffer::HEIGHT_BORDER;
234 nextCeils.z -= 1.0f;234 nextCeils.z -= 1.0f;
235 }235 }
236 }236 }
@@ -337,19 +337,19 @@ int HeightfieldHeightVisitor::visit(MetavoxelInfo& info) { · 100 unmodified lines
337 const uchar* src = (const uchar*)contents.constData();337 const uchar* src = (const uchar*)contents.constData();
338 int size = glm::sqrt((float)contents.size());338 int size = glm::sqrt((float)contents.size());
339 int unextendedSize = size - HeightfieldBuffer::HEIGHT_EXTENSION;339 int unextendedSize = size - HeightfieldBuffer::HEIGHT_EXTENSION;
340 int highest = HEIGHT_BORDER + unextendedSize;340 int highest = HeightfieldBuffer::HEIGHT_BORDER + unextendedSize;
341 relative *= unextendedSize / info.size;341 relative *= unextendedSize / info.size;
342 relative.x += HEIGHT_BORDER;342 relative.x += HeightfieldBuffer::HEIGHT_BORDER;
343 relative.z += HEIGHT_BORDER;343 relative.z += HeightfieldBuffer::HEIGHT_BORDER;
344 344
345 // find the bounds of the cell containing the point and the shared vertex heights345 // find the bounds of the cell containing the point and the shared vertex heights
346 glm::vec3 floors = glm::floor(relative);346 glm::vec3 floors = glm::floor(relative);
347 glm::vec3 ceils = glm::ceil(relative);347 glm::vec3 ceils = glm::ceil(relative);
348 glm::vec3 fracts = glm::fract(relative);348 glm::vec3 fracts = glm::fract(relative);
349 int floorX = qMin(qMax((int)floors.x, HEIGHT_BORDER), highest);349 int floorX = qMin(qMax((int)floors.x, HeightfieldBuffer::HEIGHT_BORDER), highest);
350 int floorZ = qMin(qMax((int)floors.z, HEIGHT_BORDER), highest);350 int floorZ = qMin(qMax((int)floors.z, HeightfieldBuffer::HEIGHT_BORDER), highest);
351 int ceilX = qMin(qMax((int)ceils.x, HEIGHT_BORDER), highest);351 int ceilX = qMin(qMax((int)ceils.x, HeightfieldBuffer::HEIGHT_BORDER), highest);
352 int ceilZ = qMin(qMax((int)ceils.z, HEIGHT_BORDER), highest);352 int ceilZ = qMin(qMax((int)ceils.z, HeightfieldBuffer::HEIGHT_BORDER), highest);
353 float upperLeft = src[floorZ * size + floorX];353 float upperLeft = src[floorZ * size + floorX];
354 float lowerRight = src[ceilZ * size + ceilX];354 float lowerRight = src[ceilZ * size + ceilX];
355 float interpolatedHeight = glm::mix(upperLeft, lowerRight, fracts.z);355 float interpolatedHeight = glm::mix(upperLeft, lowerRight, fracts.z);
@@ -593,6 +593,10 @@ void PointBuffer::render(bool cursor) { · 237 unmodified lines
593 _buffer.release();593 _buffer.release();
594}594}
595 595
596const int HeightfieldBuffer::HEIGHT_BORDER = 1;
597const int HeightfieldBuffer::SHARED_EDGE = 1;
598const int HeightfieldBuffer::HEIGHT_EXTENSION = 2 * HeightfieldBuffer::HEIGHT_BORDER + HeightfieldBuffer::SHARED_EDGE;
599
596HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale,600HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale,
597 const QByteArray& height, const QByteArray& color) :601 const QByteArray& height, const QByteArray& color) :
598 _translation(translation),602 _translation(translation),