Friday 15 May 2015

android ndk - Unprojecting Screen coords to world in OpenGL es 2.0 -



android ndk - Unprojecting Screen coords to world in OpenGL es 2.0 -

long time listener, first time caller. have been playing around android ndk , i'm @ point want unproject tap world coordinates can't create work. problem x , y values both near , far points same doesn't seem right perspective projection. in scene draws ok i'm bit confused why wouldn't unproject properly, anyway here code please help

//x , y normalized screen coords ndk_helper::vec4 nearpoint = ndk_helper::vec4(x, y, 1.f, 1.f); ndk_helper::vec4 farpoint = ndk_helper::vec4(x, y, 1000.f, 1.f); ndk_helper::mat4 inverseprojview = this->matprojection * this->matview; inverseprojview = inverseprojview.inverse(); nearpoint = inverseprojview * nearpoint; farpoint = inverseprojview * farpoint; nearpoint = nearpoint *(1 / nearpoint.w_); farpoint = farpoint *(1 / farpoint.w_);

well, after looking @ vector/matrix math code in ndk_helper, isn't surprise. in short: don't utilize it. after scanning through couple of minutes, has obvious mistakes simple typos. , particularly vec4 class useless kind of vector operations need graphics. of operations assume vec4 vector in 4d space, not vector containing homogenous coordinates in 3d space.

if want, can check out here, prepared few face palms:

https://android.googlesource.com/platform/development/+/master/ndk/sources/android/ndk_helper/vecmath.h

for example, implementation of multiplication used in lastly 2 lines of code:

vec4 operator*( const float& rhs ) const { vec4 ret; ret.x_ = x_ * rhs; ret.y_ = y_ * rhs; ret.z_ = z_ * rhs; ret.w_ = w_ * rhs; homecoming ret; }

this multiplies vector in 4d space scalar, wrong if you're operating homogeneous coordinates. explains results seeing.

i suggest either write own vector/matrix library suitable graphics type operations, or utilize 1 of freely available libraries tested, , used others.

btw, specific values using test odd. should not getting same results 2 vectors, it's not had in mind anyway. z coordinate in input vectors, using distances of near , far planes in eye coordinates. apply inverse view-projection matrix vectors, transforms them clip/ndc space world space. input vectors calculation should in clip/ndc space, means z-coordinate values corresponding near/far plane should @ -1 , 1.

opengl-es android-ndk projection ray-picking

No comments:

Post a Comment