本文介绍了如何加载纹理到与OpenGL ES的一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加载纹理到一个圆圈面临的问题。我的圈子里是用三角形扇的。它提供了一个糟糕的输出。

I am facing problems on loading a texture onto a circle. My circle is made with a triangle fan. It gives a bad output.

原始图像:

Original Image:

结果:

The Result :

我的code:

public class MyOpenGLCircle {

    private int points=360;
    private float vertices[]={0.0f,0.0f,0.0f};
    private FloatBuffer vertBuff, textureBuffer;
    float texData[] = null;


    float theta = 0;
    int[] textures = new int[1];
    int R=1;
    float textCoordArray[] = 
        {
            -R, (float) (R * (Math.sqrt(2) + 1)),
             -R, -R,
            (float) (R * (Math.sqrt(2) + 1)), -R
        };
    public MyOpenGLCircle(){

        vertices = new float[(points+1)*3];
        for(int i=0;i<(points)*3;i+=3)
        {       
                //radius is 1/3 
                vertices[i]=(float) ( Math.cos(theta))/3;
                vertices[i+1]=(float) (Math.sin(theta))/3;
                vertices[i+2]=0;
                theta += Math.PI / 90;

        }
        ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);    
          bBuff.order(ByteOrder.nativeOrder());
          vertBuff=bBuff.asFloatBuffer();
          vertBuff.put(vertices);
          vertBuff.position(0);


        ByteBuffer bBuff2=ByteBuffer.allocateDirect(textCoordArray.length * 4 * 360);
        bBuff2.order(ByteOrder.nativeOrder());
        textureBuffer=bBuff2.asFloatBuffer();
        textureBuffer.put(textCoordArray);
        textureBuffer.position(0);
    }

    public void draw(GL10 gl){ 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glEnable(GL10.GL_TEXTURE_2D);   
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); //4
    gl.glTexCoordPointer(2, GL10.GL_FLOAT,0, textureBuffer); //5
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
     }  

    public void loadBallTexture(GL10 gl, Context context, int resource){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resource);
        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
    }
}

请帮我通过这个

推荐答案

首先,你需要在你的texcoord阵列相同数量的texcoord对你有顶点的元组在你的顶点数组。

For starters, you need to have the same number of texcoord pairs in your texcoord array as you have vertex tuples in your vertex array.

它看起来像你刚刚得到了3对纹理坐标,和360的顶点。

It looks like you've just got 3 pairs of texture coordinates, and 360 vertices.

您需要有一个其中有360纹理坐标作为texcoord阵列。然后,当顶点被绘制,顶点[0]被texcoord [0],顶点[1]得到与texcoord配对[1],等等。

You need to have a texcoord array that has 360 texture coordinates in it. Then when the vertices are drawn, vertex[0] gets texcoord[0], vertex[1] gets paired with texcoord[1], etc.

===编辑===

您只需要定义以类似的方式纹理坐标你如何定义你的顶点:用数学公式在一个循环中。

You just have to define the texture coordinates in a similar manner to how you define your vertices: in a loop using mathematical formulas.

因此​​,例如,你的三角形扇的第一顶点是在该圆的中心。为了您的圆心,你想要的texcoord引用纹理的中心,这是坐标为(0.5,0.5)。

So for example, your first vertex of the triangle fan is at the center of the circle. For the center of your circle, you want the texcoord to reference the center of the texture, which is coordinate (0.5, 0.5).

当你走在边缘,只是觉得哪些纹理坐标映射到圆的那部分。所以,让我们假设你的下一个顶点是圆的最右边顶点,沿相同的Y值作为圆心所在。对于这一个texcoord将是(1.0,0.5),或纹理的上下中间的右边缘。

As you go around the edges, just think about which texture coordinate maps to that part of the circle. So lets assume that your next vertex is the rightmost vertex of the circle, that lies along the same y value as the center of the circle. The texcoord for this one would be (1.0, 0.5), or the right edge of the texture in the vertical middle.

圆的顶部顶点将具有texcoord(0.5,1.0),最左边的顶点是(0.0,0.5),等等。

The top vertex of the circle would have texcoord (0.5, 1.0), the leftmost vertex would be (0.0, 0.5), etc.

您可以使用您的三角填写顶点的其余部分。

You can use your trigonometry to fill in the rest of the vertices.

这篇关于如何加载纹理到与OpenGL ES的一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 06:25