OpenGL ES的,添加纹理圈子 [英] OpenGL ES, add texture to circle

查看:167
本文介绍了OpenGL ES的,添加纹理圈子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画中的OpenGL ES一个圆圈,并添加纹理它是补充,而不是4倍,1(见下图)时。

在换句话说,是我的照片X4。我要的是作为中心在圈内一张图片核弹符号。

下面是我使用一个纹理绘制圆法(在Circle类)

 公共无效drawTexture(GL10 GL){
    gl.glFrontFace(GL10.GL_CCW); //前脸逆时针的方向
    gl.glEnable(GL10.GL_CULL_FACE); //启用剔除脸
    gl.glCullFace(GL10.GL_BACK); //卡尔背面(不显示)    //启用顶点数组,并定义其缓冲区
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); //启用纹理坐标 - 阵列
    gl.glVertexPointer(2,GL10.GL_FLOAT,0,vertexBuffer);
    gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,texBuffer); //定义纹理COORDS
    //直接从顶点数组绘制原语    gl.glPushMatrix();
    gl.glTranslatef(0.0,0.0,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(也许问题?),因为它必须具有点相同数量的,我不知道它指向如果没有顶点

下面是我加载纹理以及在哪里设置我的顶点纹理的方式方法。

 公共无效loadTexture(GL10 GL,上下文的背景下){
    gl.glGenTextures(1,textureIDs,0); //生成纹理ID数组
    gl.glBindTexture(GL10.GL_TEXTURE_2D,textureIDs [0]); //绑定到纹理ID
    //设置纹理过滤器
    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);    //构造一个输入流纹理图像
    InputStream中的IStream = context.getResources()openRawResource(R.drawable.nuke)。
    位图位图;
    尝试{
        //读取和德code输入为位图
        位= BitmapFactory.de codeStream(istream的);
    } {最后
        尝试{
            istream.close();
        }赶上(IOException异常五){
        }
    }    //从加载的位图纹理构建为当前绑定的纹理ID
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,位图,0);
    bitmap.recycle();
}私人无效setUpTextureVertices(浮点半径){
    浮THETA =(浮点)(2 * Math.PI /(numberOfVertices-1));
    浮C =(浮点)Math.cos(THETA);
    浮S =(浮点)Math.sin(THETA);
    浮动X =半径;
    浮Y = 0;    的for(int i = 0; I< numberOfVertices;我++){
        texVertices [I] [0] = X;
        texVertices [I] [1] = Y;
        浮动T = X;
        X =(C * X - S * Y + 1)* 0.5F;
        Y =(S * T + C * Y + 1)* 0.5F;
    }
}私人无效setUpVertices(浮点半径){
    浮THETA =(浮点)(2 * Math.PI /(numberOfVertices-1));
    浮C =(浮点)Math.cos(THETA);
    浮S =(浮点)Math.sin(THETA);
    浮动X =半径;
    浮Y = 0;    的for(int i = 0; I< numberOfVertices;我++){
        顶点[I] [0] = X;
        顶点[I] [1] = Y;
        浮动T = X;
        X = C * X - S * Y;
        Y = S * T + C * Y;
    }
}

编辑:添加的构造函数Circle类

 公开的社交圈(){    setUpVertices(1.0F);
    //设置顶点数组缓冲区。顶点浮动。一个浮点有4个字节
    的ByteBuffer VBB = ByteBuffer.allocateDirect(vertices.length * 4 * 2);
    vbb.order(ByteOrder.nativeOrder()); //使用本地字节顺序
    vertexBuffer = vbb.asFloatBuffer(); //转换字节的缓冲区浮动    //循环遍历顶点并把它们放在vertexbuffer
    的for(int i = 0; I< numberOfVertices;我++){
        为(中间体J = 0; J&下; = 1; J ++){
            vertexBuffer.put(顶点[I] [J]); //将数据复制到缓冲区
        }
    }
    vertexBuffer.position(0); //倒带    setUpTextureVertices(1.0F);    //设置纹理坐标 - 数组缓冲区,在浮动。一个浮动有4个字节
    ByteBuffer的TBB = ByteBuffer.allocateDirect(vertices.length * 4 * 2);
    tbb.order(ByteOrder.nativeOrder());
    texBuffer = tbb.asFloatBuffer();
    //循环遍历顶点并把它们放在vertexbuffer
    的for(int i = 0; I< numberOfVertices;我++){
        为(中间体J = 0; J&下; = 1; J ++){
            texBuffer.put(texVertices [I] [J]); //将数据复制到缓冲区
        }
    }
    texBuffer.position(0);
}


解决方案

您的问题是,你的UV去从-1到1。确保他们从0到1。

有一个窦的最低值是-1,而最高变为1

公式将是

  X =(三* X  -  S * Y +1)* 0.5F;
 Y =(S * T + C * Y +1)* 0.5F;

I'm drawing a circle in OpenGL ES and when adding a texture it is "added" 4 times instead of 1 (see the image below).

In other words, that is my picture x4. What I want is the nuke symbol centered as one picture in the circle.

Below is my method (in the Circle class) for drawing the circle using a texture

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

}

At the moment, the texBuffer is the same as the vertexBuffer (maybe the problem?) since it has to have the same amount of points and I don't know which points if not the vertices.

Below is my method for loading the texture as well as the method where I set up my vertices for the texture.

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

EDIT: Added the constructor for the Circle class

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

解决方案

Your problem is that your uv goes from -1 to 1 . Make sure they go from 0 to 1 .

The lowest value for a sinus is -1 , while the highest goes to 1.

formula would be

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

这篇关于OpenGL ES的,添加纹理圈子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆