java - OpenGl ES on android fine tuning of ray picking code -
ive implemented ray picking using min3d , code found online when implemented notice off bit times meaning when click on 1 box wrong 1 picked. 1 picked isnt on screen. took quite bit of code implemented in first place main 2 methods call following:
private void raypicking(float x, float y) { //intersection near plane float[] near = new float[4]; glu.gluunproject(x, scene.getviewport()[3] - y, 0f, scene.getgrabber().mmodelview, 0, scene.getgrabber().mprojection, 0, scene.getviewport(), 0, near, 0); if (near[3] != 0) { near[0] = near[0] / near[3]; near[1] = near[1] / near[3]; near[2] = near[2] / near[3]; } number3d near3 = new number3d(near[0], near[1], near[2]); //and far plane float[] far = new float[4]; glu.gluunproject(x, scene.getviewport()[3] - y, 1f, scene.getgrabber().mmodelview, 0, scene.getgrabber().mprojection, 0, scene.getviewport(), 0, far, 0); if (far[3] != 0) { far[0] = far[0] / far[3]; far[1] = far[1] / far[3]; far[2] = far[2] / far[3]; } number3d far3 = new number3d(far[0], far[1], far[2]); box firstpicked = null; box currentmodel = null; number3d currentcoords = null; number3d firstpickedcoords = new number3d(); if (!sceneboxes.isempty()) { //here check each model if tapped , if several models tapped, take closer near clipping plane iterator<box> itr = sceneboxes.iterator(); while (itr.hasnext()) { currentmodel = itr.next(); currentcoords = new number3d(currentmodel.position().x, currentmodel.position().y, currentmodel.position().z); if (picked (far3, near3, currentcoords, 1.2f)) if (firstpicked==null) { firstpicked = currentmodel; firstpickedcoords.setallfrom(currentcoords); } else if (number3d.add(currentcoords, near3).length() < number3d.add(firstpickedcoords, near3).length()) { firstpicked = currentmodel; firstpickedcoords.setallfrom(currentcoords); } } } if (firstpicked != null) // if model picked { string temp = firstpicked.gettitle(); string temp2 = firstpicked.getlink(); log.w("3d touch working "+temp, "3d touch working "+temp); intent intent = new intent(curact.this, webviewer.class); intent.putextra("clkurl", temp2); intent.putextra("clkuid", curr_uid); intent.putextra("rid", curr_rid); startactivity(intent); firstpicked = null; temp = null; temp2 = null; } } private boolean picked(number3d a, number3d b, number3d q, float r) { float ab = (float) math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); float aq = (float) math.sqrt((a.x-q.x)*(a.x-q.x)+(a.y-q.y)*(a.y-q.y)+(a.z-q.z)*(a.z-q.z)); float bq = (float) math.sqrt((b.x-q.x)*(b.x-q.x)+(b.y-q.y)*(b.y-q.y)+(b.z-q.z)*(b.z-q.z)); float p = (ab + aq + bq) / 2; float hh = (float) math.sqrt(p * (p - ab) * (p - aq) * (p - bq)); float h; if (ab!=0) h = 2 * hh / ab; else h = 2*hh; if (aq<h) h = aq; if (bq<h) h = bq; if (h<r)return true; else return false; }
now have tried implementing frustum culling having done slows things way down because didnt optimize , not running natively. if 1 can , maybe give me pointers on how make more accurate awesome. heres call picking methods.
_glsurfaceview.setontouchlistener( new view.ontouchlistener() { @override public boolean ontouch(view arg0, motionevent e) { switch (e.getaction() & motionevent.action_mask) { case motionevent.action_down: startx = e.getx(); starty = e.gety(); trace = 0.0f; time = system.currenttimemillis(); touchmode = drag; break; case motionevent.action_up: // use ray picking when tap wasn't longer 0.5 sec , if didn't move our finger //log.w("this touch y"+e.gety()+" viewport is"+scene.getviewport()[3], "this touch x"+e.getx()); if ((system.currenttimemillis()-time < 500) && (trace<scene.getviewport()[3]*0.075)) raypicking(e.getx(), e.gety()); time = 0; case motionevent.action_pointer_up: touchmode = none; break; } return true; } } );
the bug have sounds making mistake in collision detection, had bug in function had dot products , caused odd results. can post more of collision algorithm?
i implemented ray picking here: http://android-raypick.blogspot.ca/ did not paste code here because there allot of it, feel free compare code code, code tested , working.
Comments
Post a Comment