MCPcopy Create free account
hub / github.com/OGRECave/ogre-next / getFirstRayIntersection

Method getFirstRayIntersection

Components/Volume/src/OgreVolumeSource.cpp:133–186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

131 //-----------------------------------------------------------------------
132
133 bool Source::getFirstRayIntersection(const Ray &ray, Vector3 &result, Real scale, size_t maxIterations, Real maxDistance) const
134 {
135 Ray scaledRay(ray.getOrigin() / scale, ray.getDirection());
136 Vector3 start = getIntersectionStart(scaledRay, maxDistance);
137 Vector3 cur = start;
138 Vector3 end = getIntersectionEnd(scaledRay, maxDistance);
139
140 Vector4 startVal = getValueAndGradient(start);
141 Vector3 scaleSampleGradient(startVal.x, startVal.y, startVal.z);
142 Vector3 scaleSampleEnd = start + scaleSampleGradient.normalisedCopy();
143 Real scaleSample = getValue(scaleSampleEnd);
144 Real densityScale = (Real)1.0 / Math::Abs(scaleSample - startVal.w) * (Real)2.0;
145
146 Real densityCur = getValue(cur);
147 Vector3 dir = scaledRay.getDirection().normalisedCopy();
148
149 size_t count = 0;
150 Vector3 prev = Vector3::ZERO, prevPrev = Vector3::ZERO;
151 bool atEnd = false;
152 Real totalLength = (start - end).length();
153 while (Math::Abs(densityCur) > (Real)0.01 && !atEnd)
154 {
155 cur += dir * (Real)-1.0 * (densityCur / densityScale);
156
157 // Increase the scaling a bit if we jump forth and back due to bad depth data.
158 if ((cur - prevPrev).length() < (Real)0.0001)
159 {
160 densityScale *= (Real)2.0;
161 }
162 prevPrev = prev;
163 prev = cur;
164
165 densityCur = getValue(cur);
166
167 // Check if we are out of range
168 if ((start - cur).length() >= totalLength) {
169 atEnd = true;
170 }
171
172 // We have a limit here...
173 count++;
174 if (count > maxIterations)
175 {
176 break;
177 }
178 }
179
180 if (Math::Abs(densityCur) <= (Real)0.01)
181 {
182 result = cur;
183 return true;
184 }
185 return false;
186 }
187
188 //-----------------------------------------------------------------------
189

Callers

nothing calls this directly

Calls 4

AbsFunction · 0.85
getValueFunction · 0.50
getDirectionMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected