Comments (9)
for any poor soul wandering upon this post in confusion,
the baryPosition output uses barycentric coordinates for the x and y components. The z component is the scalar factor for ray.
That is,
1.0 - baryPosition.x - baryPosition.y = actual z barycentric coordinate
if you compute the point inside the triangle that corresponds to those barycentric coordinates, you'll find the exact same answer as if you did:
origin + direction * baryPosition.z
tl;dr:
[ v0.x v1.x v2.x ] [ baryPosition.x ]
[ v0.y v1.y v2.y ] * [ baryPosition.y ]
[ v0.z v1.z v2.z ] [ (1.0 - baryPosition.x - baryPosition.y) ]
is equal to
origin + direction * baryPosition.z
from glm.
Well, I feel it is exactly the way it should work, but should not it be documented?
I've noticed that sum of baryPosition coordinates is not 1 but really could not understand what the hell is going on until I've read the above explanation.
EDIT: as of version 0.9.6.1:
In fact barycentric coordinates are [(1 - bary.x - bary.y); bary.x; bary.y], which is a bit counter-intuitive.
So intersection result is as follows:
v0 * (1 - bary.x - bary.y) + v1 * bary.x + v2 * bary.y
= origin + direction * bary.z
= intersection coordinates (cartesian)
Also, triangle vertices must go counter-clockwise when looking along the ray for them to intersect.
(That is, in context of OpenGL, only front face of triangle intersects the ray, not the back one.)
from glm.
Can this be considered closed?
from glm.
Barycentric coordinates are still not explained in docs.
Should I check that my observations still hold (except for backface culling, #194) and post a 1-line pull request?
Edit: Actually, I am not sure how to edit docs properly. No relevant code changed (except removal of culling), so my observations still hold.
from glm.
If somebody is interested to improve / fix this code, I think a pull request is the best approach. :)
from glm.
This issue should be fixed in master branch for GLM 0.9.9 release.
Thanks for reporting,
Christophe
from glm.
It looks like now the barycentric z coordinate is never assigned, instead the value passed for it is merely divided by a determinant? (https://github.com/g-truc/glm/blob/master/glm/gtx/intersect.inl#L89)
Also, the test case doesn't actually use the z coordinate for the triangle vertices which makes it impossible to catch this "bug" (?) with it: https://github.com/g-truc/glm/blob/master/test/gtx/gtx_intersect.cpp#L12
I guess you can always calculate it from the other two coordinates, in which case a little documentation about that would be very appreciated.
Or am I missing something?
Kind regards, Jonathan.
from glm.
thank you @nlguillemot for your explanation
from glm.
@nlguillemot
Note that when calling glm::intersectRayTriangle(orig, dir, v0, v1, v2, baryPosition),
intersection = orig + baryPosition.z * dir
should in theory be equal to
intersection = v0 + (baryPosition.x * (v1 - v0) + baryPosition.y * (v2 - v0))
However, in practice, the latter is more accurate than the former.
In fact, I was playing with a 3D Gaussian function the other day and as we all know, this function usually converge to 0 pretty quickly. So, I tried to draw a contour line around it at mouse position using glm::intersectRayTriangle and it turned out that using the latter allowed me to draw the contour line further and more accurately than the former (using the resulting y-coordinate of the intersection to generate it).
So, if floating-point precision is an issue, use the latter.
Otherwise, since it's faster to compute, use the former.
from glm.
Related Issues (20)
- Build (tests) fails with -Werror due to a lot of warnings HOT 4
- Build fails with clang on GLM_DEPRECATED GLM_FUNC_DECL construct HOT 4
- Please restore glm.pc HOT 1
- GLM shared doesnt export DLL symbols HOT 3
- Compiler warnings (error: unsafe buffer access)
- are there any gdb pretty printers for the glm library? HOT 1
- is glm::cubic broken?
- CUDA compilation error: dot and vector multiplication functions use STL and cause compilation error.
- Wrong vector addition result inside a Cuda kernel. HOT 1
- 1.0.1: build fails with `GLM_TEST_ENABLE=ON` HOT 3
- Where can I find the implementation code for `glm::mat4 * glm::vec4`? HOT 1
- Compile Error MSVC
- Update fetch content CMake example to point to correct tag
- Enhance glm::dualquat and singular value decomposition for square matrix
- AVX2 on glm::vec4 and not on glm::vec3
- 'quat_cast': identifier not found when using glm as C++20 module
- Issue: Example Commands in README.md, in 'Build and Install' section, Label: Documentation
- What is glm-light?
- glm-1.0.1-light identifies itself as version 1.0.0
- Official build2 Package HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from glm.