2016-09-06 2 views
0

Ricoh Theta app과 같은 등각 투사 이미지 뷰어를 개발해야합니다.안드로이드 OpenGL ES 텍스처 반구

Open GL ES (1.0, 필요하면 2.0으로 변경할 수 있음)를 사용하여 Android에서이 작업을 수행하고 있습니다. 내가의 좌표를 사용하고 같은

물론
public class HalfSphere { 

    // --------------------------------------------------------------------------------------------- 
    // region Attributes 

    private final int[] mTextures = new int[1]; 

    float[][] mVertices; 
    int mNbStrips; 
    int mNbVerticesPerStrips; 

    private final List<FloatBuffer> mVerticesBuffer = new ArrayList<>(); 
    private final List<ByteBuffer> mIndicesBuffer = new ArrayList<>(); 
    private final List<FloatBuffer> mTextureBuffer = new ArrayList<>(); 

    // endregion 
    // --------------------------------------------------------------------------------------------- 



    // --------------------------------------------------------------------------------------------- 
    // region Constructor 

    public HalfSphere(int nbStrips, int nbVerticesPerStrips, float radius) { 

     // Generate the vertices: 
     mNbStrips = nbStrips; 
     mNbVerticesPerStrips = nbVerticesPerStrips; 
     mVertices = new float[mNbStrips * mNbVerticesPerStrips][3]; 

     for (int i = 0; i < mNbStrips; i++) { 

      for (int j = 0; j < mNbVerticesPerStrips; j++) { 

       mVertices[i * mNbVerticesPerStrips + j][0] = (float) (radius * Math.cos(j * 2 * Math.PI/mNbVerticesPerStrips) * Math.cos(i * Math.PI/mNbStrips)); 
       mVertices[i * mNbVerticesPerStrips + j][1] = (float) (radius * Math.sin(i * Math.PI/mNbStrips)); 
       mVertices[i * mNbVerticesPerStrips + j][2] = (float) (radius * Math.sin(j * 2 * Math.PI/mNbVerticesPerStrips) * Math.cos(i * Math.PI/mNbStrips)); 
      } 
     } 

     // Populate the buffers: 
     for(int i = 0; i < mNbStrips - 1; i++) { 

      for(int j = 0; j < mNbVerticesPerStrips; j++) { 

       byte[] indices = { 
         0, 1, 2, // first triangle (bottom left - top left - top right) 
         0, 2, 3 // second triangle (bottom left - top right - bottom right) 
       }; 

       float[] p1 = mVertices[i * mNbVerticesPerStrips + j]; 
       float[] p2 = mVertices[i * mNbVerticesPerStrips + (j + 1) % mNbVerticesPerStrips]; 
       float[] p3 = mVertices[(i + 1) * mNbVerticesPerStrips + (j + 1) % mNbVerticesPerStrips]; 
       float[] p4 = mVertices[(i + 1) * mNbVerticesPerStrips + j]; 

       float[] quad = { 
         p1[0], p1[1], p1[2], 
         p2[0], p2[1], p2[2], 
         p3[0], p3[1], p3[2], 
         p4[0], p4[1], p4[2] 
       }; 

       mVerticesBuffer.add(floatArrayToFloatBuffer(quad)); 
       mTextureBuffer.add(floatArrayToFloatBuffer(quad)); 
       mIndicesBuffer.add(byteArrayToByteBuffer(indices)); 
      } 
     } 
    } 

    // endregion 
    // --------------------------------------------------------------------------------------------- 



    // --------------------------------------------------------------------------------------------- 
    // region Draw 

    public void draw(final GL10 gl) { 

     // bind the previously generated texture. 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, this.mTextures[0]); 

     // Point to our buffers. 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

     // Set the face rotation, clockwise in this case. 
     gl.glFrontFace(GL10.GL_CW); 

     for(int i = 0; i < mVerticesBuffer.size(); i++) { 

      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer.get(i)); 
      gl.glTexCoordPointer(3, GL10.GL_FLOAT, 0, mTextureBuffer.get(i)); 

      gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 6, GL10.GL_UNSIGNED_BYTE, mIndicesBuffer.get(i)); // GL_TRIANGLE_STRIP/GL_LINE_LOOP 
     } 

     // Disable the client state before leaving. 
     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    } 

    // endregion 
    // --------------------------------------------------------------------------------------------- 



    // --------------------------------------------------------------------------------------------- 
    // region Utils 

    public void loadGLTexture(GL10 gl, Bitmap texture) { 
     // Generate one texture pointer, and bind it to the texture array. 
     gl.glGenTextures(1, this.mTextures, 0); 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, this.mTextures[0]); 

     // Create nearest filtered texture. 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); 
     gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 

     // Use Android GLUtils to specify a two-dimensional texture image from our bitmap. 
     GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0); 
     texture.recycle(); 
    } 

    public FloatBuffer floatArrayToFloatBuffer(float[] array) { 

     ByteBuffer vbb = ByteBuffer.allocateDirect(array.length * 4); 
     vbb.order(ByteOrder.nativeOrder()); // use the device hardware's native byte order 
     FloatBuffer fb = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer 
     fb.put(array); // add the coordinates to the FloatBuffer 
     fb.position(0);  // set the buffer to read the first coordinate 

     return fb; 
    } 

    public ByteBuffer byteArrayToByteBuffer(byte[] array) { 

     ByteBuffer vbb = ByteBuffer.allocateDirect(array.length * 4); 
     vbb.order(ByteOrder.nativeOrder()); // use the device hardware's native byte order 
     vbb.put(array); // add the coordinates to the FloatBuffer 
     vbb.position(0);  // set the buffer to read the first coordinate 

     return vbb; 
    } 

    // endregion 
    // --------------------------------------------------------------------------------------------- 
} 

는, 질감이 제대로 적용되지 않습니다 :

는 지금,이 코드 (this answer 기준) 반구를 작성 관리해야 내 정점들. 누군가 그것을 정확하게하는 방법을 보나요? 또한 사용자가 팬 할 때 텍스처를 "이동"할 수 있어야합니다.

편집 : codetiger가 제안한대로 lat/180 및 lon/360을 수행 한 다음 [0..1]로 정규화했습니다. 자, 패닝을 추가하려고합니다. (수직) 위도에 패닝 때

longitude panning

을하지만,하지 :

enter image description here

단순히 추가 해요 값을 0..1시 사이 (수평) 경도에 패닝 때 작동 사용자가 팬합니다. 나는 here 주어진 수식을 사용하려고 시도했지만 성공하지 못했습니다. 어떤 생각? 도움이된다면

, 그건 내가 (리코 세타 응용 프로그램을 얻을) 원하는 무엇 : 구에게 360도 영역을하기 위해

enter image description here

답변

0

, 당신은 아래의 라인을 교체 할 수 있습니다.

mVertices[i * mNbVerticesPerStrips + j][0] = (float) (radius * Math.cos(j * 2 * Math.PI/mNbVerticesPerStrips) * Math.cos(2 * i * Math.PI/mNbStrips)); 
mVertices[i * mNbVerticesPerStrips + j][1] = (float) (radius * Math.sin(2 * i * Math.PI/mNbStrips)); 
mVertices[i * mNbVerticesPerStrips + j][2] = (float) (radius * Math.sin(j * 2 * Math.PI/mNbVerticesPerStrips) * Math.cos(2 * i * Math.PI/mNbStrips)); 

유일한 변화 대신 Math.PI/mNbStrips

그리고 이미지를 회전의 두 번째 각도 2 * Math.PI/mNbStrips을 사용하면 사용하여 영역을 회전 할 수

gl.glRotatef(angle, 1.0f, 0.0f, 0.0f); 

업데이트 : 올바른 텍스처를 얻으려면 구면의 좌표, 사용할 수있는 표준 왜곡 구형 텍스처 (lat/180, lon/360) 및 [0..1]이되도록 정규화합니다. 여기에 언급 된 바와 같이 https://stackoverflow.com/a/10395141/409315

+0

감사합니다.하지만 "반원 축소"(= 반구를 멀리 표시) 할 수 있도록 반구가 필요합니다. 내가 구의 "밖으로"나왔다면, 결과는 내가 원하는 것이 아닙니다. 질문은 실제로 : 내 반구에 텍스처를 적용하는 방법입니까? –

+0

네,이 방법으로 작동합니다. 하지만 이제 위도 패닝에 문제가 있습니다 (내 수정 참조). –

+0

사실, 투영 된 투사 이미지의 경우 위도로 패닝 할 수 없다고 생각합니다. 이렇게하면 왜곡이 생깁니다. – codetiger