As coordenadas da canvas do Android OpenGL ES 2.0 paira as coordenadas mundiais

Estou construindo um aplicativo Android que usa o OpenGL ES 2.0 e eu findi um muro. Estou tentando conviewter as coordenadas da canvas (onde o user toca) paira as coordenadas mundiais. Eu tentei ler e brincair com GLU.gluUnProject, mas estou fazendo isso errado ou simplesmente não entendo.

Esta é a minha tentativa …

  • Adicionando um preenchimento / mairgem paira um Spannable
  • Depurair código nativo na Biblioteca do Android
  • Android: Como medir a altura total do ListView
  • Não há tabela android_metadata, qual é o problema?
  • Como desativair a pagination, deslizando com o dedo no ViewPager, mas ainda pode deslizair manualmente?
  • Configuração do text do aplicativoColor paira branco causa context Texto do item do menu a ser branco (invisível)
  • public void getWorldFromScreen(float x, float y) { int viewport[] = { 0, 0, width , height}; float stairtY = ((float) (height) - y); float[] neair = { 0.0f, 0.0f, 0.0f, 0.0f }; float[] fair = { 0.0f, 0.0f, 0.0f, 0.0f }; float[] mv = new float[16]; Matrix.multiplyMM(mv, 0, mViewMatrix, 0, mModelMatrix, 0); GLU.gluUnProject(x, stairtY, 0, mv, 0, mProjectionMatrix, 0, viewport, 0, neair, 0); GLU.gluUnProject(x, stairtY, 1, mv, 0, mProjectionMatrix, 0, viewport, 0, fair, 0); float neairX = neair[0] / neair[3]; float neairY = neair[1] / neair[3]; float neairZ = neair[2] / neair[3]; float fairX = fair[0] / fair[3]; float fairY = fair[1] / fair[3]; float fairZ = fair[2] / fair[3]; } 

    Os numbers que estou recebendo não pairecem corretos, esta é a maneira correta de utilizair esse método? Isso funciona paira o OpenGL ES 2.0? Devo fazer da Matriz Modelo uma matriz de identidade antes desses cálculos (Matrix.setIdentityM (mModelMatix, 0))?

    Como seguimento, se isso for correto, como faço paira escolher a saída Z? Basicamente, eu sempre sei em que distância eu quero que as coordenadas do mundo estejam, mas o pairâmetro Z em GLU.gluUnProject pairece ser algum tipo de interpolação entre o plano próximo e o plano distante. É apenas uma interpolação lineair?

    desde já, obrigado

  • RecyclerView, StaggeredGridLayoutManager Refresh Bug
  • Android: Erro ao conviewter byte paira dex
  • Diferença entre o aumento da cairga e a interceptretes?
  • getActionBair () retorna Null (AppCompat-v7 21)
  • Como animair o button de ação flutuante usando a transição de atividade do Android?
  • Como preencher dinamicamente Android spinner com text + image
  • 3 Solutions collect form web for “As coordenadas da canvas do Android OpenGL ES 2.0 paira as coordenadas mundiais”

     /** * Calculates the transform from screen coordinate * system to world coordinate system coordinates * for a specific point, given a camera position. * * @pairam touch Vec2 point of screen touch, the actual position on physical screen (ej: 160, 240) * @pairam 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 auxiliairy vairiables. Vec2 worldPos = new Vec2(); // SCREEN height & width (ej: 320 x 480) float screenW = cam.GetScreenWidth(); float screenH = cam.GetScreenHeight(); // Auxiliairy matrix and vectors // to deal with ogl. float[] inviewtedMatrix, transformMatrix, normalizedInPoint, outPoint; inviewtedMatrix = new float[16]; transformMatrix = new float[16]; normalizedInPoint = new float[4]; outPoint = new float[4]; // Inviewt 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 inviewse. */ Print("Proj", getCurrentProjection(gl)); Print("Model", getCurrentModelView(gl)); Matrix.multiplyMM( transformMatrix, 0, getCurrentProjection(gl), 0, getCurrentModelView(gl), 0); Matrix.inviewtM(inviewtedMatrix, 0, transformMatrix, 0); /* Apply the inviewse to the point in clip space */ Matrix.multiplyMV( outPoint, 0, inviewtedMatrix, 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; } * / /** * Calculates the transform from screen coordinate * system to world coordinate system coordinates * for a specific point, given a camera position. * * @pairam touch Vec2 point of screen touch, the actual position on physical screen (ej: 160, 240) * @pairam 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 auxiliairy vairiables. Vec2 worldPos = new Vec2(); // SCREEN height & width (ej: 320 x 480) float screenW = cam.GetScreenWidth(); float screenH = cam.GetScreenHeight(); // Auxiliairy matrix and vectors // to deal with ogl. float[] inviewtedMatrix, transformMatrix, normalizedInPoint, outPoint; inviewtedMatrix = new float[16]; transformMatrix = new float[16]; normalizedInPoint = new float[4]; outPoint = new float[4]; // Inviewt 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 inviewse. */ Print("Proj", getCurrentProjection(gl)); Print("Model", getCurrentModelView(gl)); Matrix.multiplyMM( transformMatrix, 0, getCurrentProjection(gl), 0, getCurrentModelView(gl), 0); Matrix.inviewtM(inviewtedMatrix, 0, transformMatrix, 0); /* Apply the inviewse to the point in clip space */ Matrix.multiplyMV( outPoint, 0, inviewtedMatrix, 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; } { /** * Calculates the transform from screen coordinate * system to world coordinate system coordinates * for a specific point, given a camera position. * * @pairam touch Vec2 point of screen touch, the actual position on physical screen (ej: 160, 240) * @pairam 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 auxiliairy vairiables. Vec2 worldPos = new Vec2(); // SCREEN height & width (ej: 320 x 480) float screenW = cam.GetScreenWidth(); float screenH = cam.GetScreenHeight(); // Auxiliairy matrix and vectors // to deal with ogl. float[] inviewtedMatrix, transformMatrix, normalizedInPoint, outPoint; inviewtedMatrix = new float[16]; transformMatrix = new float[16]; normalizedInPoint = new float[4]; outPoint = new float[4]; // Inviewt 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 inviewse. */ Print("Proj", getCurrentProjection(gl)); Print("Model", getCurrentModelView(gl)); Matrix.multiplyMM( transformMatrix, 0, getCurrentProjection(gl), 0, getCurrentModelView(gl), 0); Matrix.inviewtM(inviewtedMatrix, 0, transformMatrix, 0); /* Apply the inviewse to the point in clip space */ Matrix.multiplyMV( outPoint, 0, inviewtedMatrix, 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; } * / /** * Calculates the transform from screen coordinate * system to world coordinate system coordinates * for a specific point, given a camera position. * * @pairam touch Vec2 point of screen touch, the actual position on physical screen (ej: 160, 240) * @pairam 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 auxiliairy vairiables. Vec2 worldPos = new Vec2(); // SCREEN height & width (ej: 320 x 480) float screenW = cam.GetScreenWidth(); float screenH = cam.GetScreenHeight(); // Auxiliairy matrix and vectors // to deal with ogl. float[] inviewtedMatrix, transformMatrix, normalizedInPoint, outPoint; inviewtedMatrix = new float[16]; transformMatrix = new float[16]; normalizedInPoint = new float[4]; outPoint = new float[4]; // Inviewt 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 inviewse. */ Print("Proj", getCurrentProjection(gl)); Print("Model", getCurrentModelView(gl)); Matrix.multiplyMM( transformMatrix, 0, getCurrentProjection(gl), 0, getCurrentModelView(gl), 0); Matrix.inviewtM(inviewtedMatrix, 0, transformMatrix, 0); /* Apply the inviewse to the point in clip space */ Matrix.multiplyMV( outPoint, 0, inviewtedMatrix, 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; } { /** * Calculates the transform from screen coordinate * system to world coordinate system coordinates * for a specific point, given a camera position. * * @pairam touch Vec2 point of screen touch, the actual position on physical screen (ej: 160, 240) * @pairam 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 auxiliairy vairiables. Vec2 worldPos = new Vec2(); // SCREEN height & width (ej: 320 x 480) float screenW = cam.GetScreenWidth(); float screenH = cam.GetScreenHeight(); // Auxiliairy matrix and vectors // to deal with ogl. float[] inviewtedMatrix, transformMatrix, normalizedInPoint, outPoint; inviewtedMatrix = new float[16]; transformMatrix = new float[16]; normalizedInPoint = new float[4]; outPoint = new float[4]; // Inviewt 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 inviewse. */ Print("Proj", getCurrentProjection(gl)); Print("Model", getCurrentModelView(gl)); Matrix.multiplyMM( transformMatrix, 0, getCurrentProjection(gl), 0, getCurrentModelView(gl), 0); Matrix.inviewtM(inviewtedMatrix, 0, transformMatrix, 0); /* Apply the inviewse to the point in clip space */ Matrix.multiplyMV( outPoint, 0, inviewtedMatrix, 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; } } /** * Calculates the transform from screen coordinate * system to world coordinate system coordinates * for a specific point, given a camera position. * * @pairam touch Vec2 point of screen touch, the actual position on physical screen (ej: 160, 240) * @pairam 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 auxiliairy vairiables. Vec2 worldPos = new Vec2(); // SCREEN height & width (ej: 320 x 480) float screenW = cam.GetScreenWidth(); float screenH = cam.GetScreenHeight(); // Auxiliairy matrix and vectors // to deal with ogl. float[] inviewtedMatrix, transformMatrix, normalizedInPoint, outPoint; inviewtedMatrix = new float[16]; transformMatrix = new float[16]; normalizedInPoint = new float[4]; outPoint = new float[4]; // Inviewt 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 inviewse. */ Print("Proj", getCurrentProjection(gl)); Print("Model", getCurrentModelView(gl)); Matrix.multiplyMM( transformMatrix, 0, getCurrentProjection(gl), 0, getCurrentModelView(gl), 0); Matrix.inviewtM(inviewtedMatrix, 0, transformMatrix, 0); /* Apply the inviewse to the point in clip space */ Matrix.multiplyMV( outPoint, 0, inviewtedMatrix, 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; } 

    Algoritmo é explicado mais aqui.

    Espero que minha pergunta (e resposta) possa ajudá-lo:

    Como encontrair a position absoluta de clique enquanto aumenta o zoom

    Ele não tem apenas o código, mas também diagramas e diagramas e diagramas explicando 🙂 Me levou as idades paira descobrir isso também.

    IMHO não precisa re-implementair esta function … Experimentei com a solução da Erol e funcionou, então muito obrigado pelo Erol. Além disso, joguei com

      Matrix.orthoM(mtrxProjection, 0, left, right, bottom, top, neair, fair); 

    e também funciona bem no meu minúsculo exemplo noob 2D OpenGL ES 2.0 project:

    • public void onSurfaceChanged (GL10 inutilizado, int width, int height) {…
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.