2012-06-11 27 views
20

पर निर्देशांक करता है मैं एक एंड्रॉइड एप्लिकेशन बना रहा हूं जो ओपनजीएल ईएस 2.0 का उपयोग करता है और मैंने दीवार में भाग लिया है। मैं विश्व निर्देशांक में स्क्रीन निर्देशांक (जहां उपयोगकर्ता छूता है) को कन्वर्ट करने की कोशिश कर रहा हूं। मैंने GLU.gluUnProject के साथ पढ़ने और खेलने की कोशिश की है, लेकिन मैं या तो गलत कर रहा हूं या बस इसे समझ नहीं पा रहा हूं।एंड्रॉइड ओपनजीएल ईएस 2.0 स्क्रीन विश्व निर्देशांक

यह मेरा प्रयास है ....

public void getWorldFromScreen(float x, float y) { 
    int viewport[] = { 0, 0, width , height}; 

    float startY = ((float) (height) - y); 
    float[] near = { 0.0f, 0.0f, 0.0f, 0.0f }; 
    float[] far = { 0.0f, 0.0f, 0.0f, 0.0f }; 

    float[] mv = new float[16]; 
    Matrix.multiplyMM(mv, 0, mViewMatrix, 0, mModelMatrix, 0); 

    GLU.gluUnProject(x, startY, 0, mv, 0, mProjectionMatrix, 0, viewport, 0, near, 0); 
    GLU.gluUnProject(x, startY, 1, mv, 0, mProjectionMatrix, 0, viewport, 0, far, 0); 

    float nearX = near[0]/near[3]; 
    float nearY = near[1]/near[3]; 
    float nearZ = near[2]/near[3]; 

    float farX = far[0]/far[3]; 
    float farY = far[1]/far[3]; 
    float farZ = far[2]/far[3]; 
} 

संख्या मैं सही नहीं लग रहे हो रही है, इस लिए इस विधि का उपयोग करने के लिए सही तरीका है? क्या यह ओपनजीएल ईएस 2.0 के लिए काम करता है? क्या मुझे इन गणनाओं से पहले मॉडल मैट्रिक्स को एक पहचान मैट्रिक्स बनाना चाहिए (Matrix.setIdentityM (mModelMatix, 0))?

फॉलो अप के रूप में, यदि यह सही है, तो मैं आउटपुट जेड कैसे चुनूं? असल में, मैं हमेशा जानता हूं कि मैं किस दूरी पर विश्व समन्वय चाहता हूं, लेकिन जीएलयू.ग्लूयूप्रोजेक्ट में जेड पैरामीटर निकट और दूर के विमान के बीच कुछ प्रकार का इंटरपोलेशन प्रतीत होता है। क्या यह सिर्फ एक रैखिक इंटरपोलेशन है?

अग्रिम धन्यवाद

उत्तर

21
/** 
    * Calculates the transform from screen coordinate 
    * system to world coordinate system coordinates 
    * for a specific point, given a camera position. 
    * 
    * @param touch Vec2 point of screen touch, the 
     actual position on physical screen (ej: 160, 240) 
    * @param cam camera object with x,y,z of the 
     camera and screenWidth and screenHeight of 
     the device. 
    * @return position in WCS. 
    */ 
    public Vec2 GetWorldCoords(Vec2 touch, Camera cam) 
    { 
     // Initialize auxiliary variables. 
     Vec2 worldPos = new Vec2(); 

     // SCREEN height & width (ej: 320 x 480) 
     float screenW = cam.GetScreenWidth(); 
     float screenH = cam.GetScreenHeight(); 

     // Auxiliary matrix and vectors 
     // to deal with ogl. 
     float[] invertedMatrix, transformMatrix, 
      normalizedInPoint, outPoint; 
     invertedMatrix = new float[16]; 
     transformMatrix = new float[16]; 
     normalizedInPoint = new float[4]; 
     outPoint = new float[4]; 

     // Invert y coordinate, as android uses 
     // top-left, and ogl bottom-left. 
     int oglTouchY = (int) (screenH - touch.Y()); 

     /* Transform the screen point to clip 
     space in ogl (-1,1) */  
     normalizedInPoint[0] = 
     (float) ((touch.X()) * 2.0f/screenW - 1.0); 
     normalizedInPoint[1] = 
     (float) ((oglTouchY) * 2.0f/screenH - 1.0); 
     normalizedInPoint[2] = - 1.0f; 
     normalizedInPoint[3] = 1.0f; 

     /* Obtain the transform matrix and 
     then the inverse. */ 
     Print("Proj", getCurrentProjection(gl)); 
     Print("Model", getCurrentModelView(gl)); 
     Matrix.multiplyMM(
      transformMatrix, 0, 
      getCurrentProjection(gl), 0, 
      getCurrentModelView(gl), 0); 
     Matrix.invertM(invertedMatrix, 0, 
      transformMatrix, 0);  

     /* Apply the inverse to the point 
     in clip space */ 
     Matrix.multiplyMV(
      outPoint, 0, 
      invertedMatrix, 0, 
      normalizedInPoint, 0); 

     if (outPoint[3] == 0.0) 
     { 
      // Avoid /0 error. 
      Log.e("World coords", "ERROR!"); 
      return worldPos; 
     } 

     // Divide by the 3rd component to find 
     // out the real position. 
     worldPos.Set(
      outPoint[0]/outPoint[3], 
      outPoint[1]/outPoint[3]); 

     return worldPos;  
    } 

एल्गोरिथ्म आगे here.

+2

मेरे समाधान सी में हैं ++। बस इस जवाब पर स्कैनिंग, यह मेरे लिए सही लग रहा है। तो, मैं एक उत्तर पोस्ट नहीं करूंगा। :] – TheBuzzSaw

1

समझाया गया है उम्मीद है कि मेरे सवाल (और जवाब) आप बाहर की मदद करनी चाहिए:

How to find absolute position of click while zoomed in

यह न केवल कोड है लेकिन आरेख और आरेख और चित्रों को समझाते हुए :) मुझे इसे समझने के लिए उम्र भी लगा।

1

आईएमएचओ को इस फ़ंक्शन को फिर से कार्यान्वित करने की आवश्यकता नहीं है ... मैंने ईरोल के समाधान के साथ प्रयोग किया और यह काम किया, इसलिए इसके लिए बहुत कुछ धन्यवाद। इसके अलावा, मैं

 Matrix.orthoM(mtrxProjection, 0, left, right, bottom, top, near, far); 

के साथ खेला जाता है और यह 2 डी ओपन ES 2.0 परियोजना मेरी छोटे noob उदाहरण के रूप में अच्छी तरह से ठीक काम करता है: