OpenGL ES,为圆圈添加纹理

时间:2014-04-03 12:24:24

标签: java android opengl-es

我在OpenGL ES中绘制一个圆圈,并在添加纹理时添加"添加" 4次而不是1次(见下图)。

enter image description here

换句话说,那是我的照片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);
}

1 个答案:

答案 0 :(得分:0)

你的问题是你的uv从-1变为1。确保它们从0到1。

窦的最低值为-1,而最高值为1。

公式将是

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