簡體   English   中英

OpenGL ES,在圓上添加紋理

[英]OpenGL ES, add texture to circle

我在OpenGL ES中繪制一個圓,添加紋理時,它被“添加”了4次而不是1次(請參見下圖)。

在此處輸入圖片說明

換句話說,就是我的照片x4。 我想要的是以圓圈中的一張圖片為中心的核武器符號。

下面是我的方法(在Circle類中),用於使用紋理繪制圓

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);

}

目前,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;
    }
}

編輯:添加了Circle類的構造函數

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);
}

您的問題是您的uv從-1變為1。 確保它們從0變為1。

竇的最低值為-1,最高為1。

公式將是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM