2014-04-03 3 views
1

OpenGL ES에서 원을 그렸습니다. 텍스처를 추가 할 때 1이 아닌 4 번 "추가"됩니다 (아래 이미지 참조). 즉OpenGL ES, 원에 텍스처 추가

enter image description here

은, 내 사진 X4입니다. 내가 원했던 것은 원 안에 하나의 그림으로 중심이 된 핵 기호입니다. 다음은

이 순간 텍스처

public void drawTexture(GL10 gl) { 
    gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation 
    gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face 
    gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display) 

    // Enable vertex-array and define its buffer 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Enable texture-coords-array 
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer); // Define texture-coords 


    // Draw the primitives from the vertex-array directly 

    gl.glPushMatrix(); 
    gl.glTranslatef(0.0f, 0.0f, 1.0f); 
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, numberOfVertices); 
    gl.glPopMatrix(); 

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisable(GL10.GL_CULL_FACE); 

} 

를 사용하여 원을 그리는 (Circle 클래스에서) 나의 방법의 texBuffer는 VertexBuffer에 동일하다 (어쩌면 문제?)가해야하기 때문에 같은 양의 점을 가지고 있고 어떤 점이 꼭지점이 아닌지 모르겠습니다.

다음은 텍스쳐를로드하기위한 나의 방법과 텍스쳐에 대한 정점을 설정하는 방법입니다.

public void loadTexture(GL10 gl, Context context) { 
    gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); // Bind to texture ID 
    // Set up texture filters 
    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); 

    // Construct an input stream to texture image 
    InputStream istream = context.getResources().openRawResource(R.drawable.nuke); 
    Bitmap bitmap; 
    try { 
     // Read and decode input as bitmap 
     bitmap = BitmapFactory.decodeStream(istream); 
    } finally { 
     try { 
      istream.close(); 
     } catch (IOException e) { 
     } 
    } 

    // Build Texture from loaded bitmap for the currently-bind texture ID 
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 
    bitmap.recycle(); 
} 

private void setUpTextureVertices(float radius) { 
    float theta = (float) (2 * Math.PI/(numberOfVertices-1)); 
    float c = (float) Math.cos(theta); 
    float s = (float) Math.sin(theta); 
    float x = radius; 
    float y = 0; 

    for (int i = 0; i < numberOfVertices; i++) { 
     texVertices[i][0] = x; 
     texVertices[i][1] = y; 
     float t = x; 
     x = (c * x - s * y + 1) * 0.5f; 
     y = (s * t + c * y + 1) * 0.5f; 
    } 
} 

private void setUpVertices(float radius) { 
    float theta = (float) (2 * Math.PI/(numberOfVertices-1)); 
    float c = (float) Math.cos(theta); 
    float s = (float) Math.sin(theta); 
    float x = radius; 
    float y = 0; 

    for (int i = 0; i < numberOfVertices; i++) { 
     vertices[i][0] = x; 
     vertices[i][1] = y; 
     float t = x; 
     x = c * x - s * y; 
     y = s * t + c * y; 
    } 
} 

편집 :

public Circle() { 

    setUpVertices(1.0f); 
    // Setup vertex-array buffer. Vertices in float. A float has 4 bytes 
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4 * 2); 
    vbb.order(ByteOrder.nativeOrder()); // Use native byte order 
    vertexBuffer = vbb.asFloatBuffer(); // Convert byte buffer to float 

    // Loop through the vertices and put them in the vertexbuffer 
    for (int i = 0; i < numberOfVertices; i++) { 
     for (int j = 0; j <= 1; j++) { 
      vertexBuffer.put(vertices[i][j]); // Copy data into buffer 
     } 
    } 
    vertexBuffer.position(0); // Rewind 

    setUpTextureVertices(1.0f); 

    // Setup texture-coords-array buffer, in float. An float has 4 bytes 
    ByteBuffer tbb = ByteBuffer.allocateDirect(vertices.length * 4 * 2); 
    tbb.order(ByteOrder.nativeOrder()); 
    texBuffer = tbb.asFloatBuffer(); 
    // Loop through the vertices and put them in the vertexbuffer 
    for (int i = 0; i < numberOfVertices; i++) { 
     for (int j = 0; j <= 1; j++) { 
      texBuffer.put(texVertices[i][j]); // Copy data into buffer 
     } 
    } 
    texBuffer.position(0); 
} 

답변

0

당신의 문제가 당신의 자외선은 -1에서 1로 이동한다는 것입니다 Circle 클래스의 생성자를 추가했습니다. 0에서 1로 이동해야합니다. 가장 높은

x = (c * x - s * y +1) *0.5f; 
y = (s * t + c * y +1)*0.5f; 
+0

이 만 질감 또는 전체 원의 정점가 될 것이다 1.

공식에가는 동안

부비동의 가장 낮은 값은 -1? 이 유일한 차이점이있는 텍스처 정점을 설정하는 방법을해야합니까? – simtaxman

+0

이것은 텍스처의 "정점"에만 해당됩니다. 텍스처 공간의 좌표를 UV라고 부릅니다 (그래서 glTexCoordPointer 함수 호출에서 전달하는 버퍼). 정점 (복수 정점)은 위치 (XYZ)와 UV로 구성됩니다. – MichaelCMS

+0

오키 (Okey)는 결과가 다소 껄끄 럽기 때문에 하나의 그림 만 렌더링 된 것처럼 보이지만 그림이 재조합 된 것을 볼 수 없었습니다. 내 질문에 코드를 보완합니다. – simtaxman