首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何绘制纹理圆opengl-es?

如何绘制纹理圆opengl-es?
EN

Stack Overflow用户
提问于 2019-07-06 15:02:53
回答 1查看 882关注 0票数 1

我的朋友在opengl-es中绘制纹理圆有问题,他不能在这个网站上发布他的方程,他要求我为他做这件事,他会跟进帖子并通过我的帐户回复。

所以,如果这件事没有错,或者这并没有违反网站的条款,请帮助。

他的公式是:

如何绘制纹理圆opengl-es?

这是他的代码:

代码语言:javascript
复制
package com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

class TextureBall {
    private FloatBuffer vertexBuffer; // Buffer for vertex-array
    private FloatBuffer texBuffer;    // Buffer for texture-coords-array (NEW)
    private int points=360;
    private float vertices[]={0.0f,0.0f,0.0f};
    private FloatBuffer vertBuff;

    float[] texCoords = { // Texture coords for the above face (NEW)
            0.0f, 1.0f,  // A. left-bottom (NEW)
            1.0f, 1.0f,  // B. right-bottom (NEW)
            0.0f, 0.0f,  // C. left-top (NEW)
            1.0f, 0.0f   // D. right-top (NEW)
    };
    int[] textureIDs = new int[1];   // Array for 1 texture-ID (NEW)
    public TextureBall() {

    // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder()); // Use native byte order
        vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
        vertexBuffer.put(vertices);         // Copy data into buffer
        vertexBuffer.position(0);           // Rewind


    }
    // Draw the shape
    public void draw(GL10 gl) {


        vertices=new float[(points+1)*3];
        for(int i=3;i<(points+1)*3;i+=3){
            double rad=(i*360/points*3)*(3.14/180);
            vertices[i]=(float)Math.cos(rad);
            vertices[i+1]=(float) Math.sin(rad);
            vertices[i+2]=0;
        }
        ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff=bBuff.asFloatBuffer();
        vertBuff.put(vertices);
        vertBuff.position(0);





        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  // Disable texture-coords-array (NEW)
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisable(GL10.GL_CULL_FACE);
    };

    // Load an image into GL 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 "res\drawable\nehe.png"
        InputStream istream = context.getResources().openRawResource(R.drawable.nehe);
        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();
    }
}

请帮他解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-06 20:00:51

创建顶点坐标和纹理坐标的数组。第一个点是中心点。其他点在圆上:

代码语言:javascript
复制
vertices  = new float[(points+2) * 3];
texCoords = new float[(points+2) * 2];

vertices[0] = vertices[1] = vertices[2] = 0;
texCoords[0] = texCoords[1] = 0.5;

for(int i=0; i <= points; i ++ ){

    double rad = (i * 360.0 / points) * (3.14/180);
    vertices[(i+1)*3+0] = (float)Math.cos(rad);
    vertices[(i+1)*3+1] = (float)Math.sin(rad);
    vertices[(i+1)*3+2] = 0;

    texCoords[(i+1)*2+0] = 0.5 + 0.5 * Math.cos(rad);
    texCoords[(i+1)*2+1] = 0.5 - 0.5 * Math.sin(rad);
} 

定义纹理坐标的缓冲区,就像定义顶点坐标一样。例如:

代码语言:javascript
复制
ByteBuffer bTBuff = ByteBuffer.allocateDirect(texCoords .length*4);
bTBuff.order(ByteOrder.nativeOrder());
texBuff = bBuff.asFloatBuffer();
texBuff.put(texCoords );
texBuff.position(0);

另请参阅Java Code Examples for java.nio.FloatBuffer

定义顶点数据(glVertexPointer)和纹理坐标(glTexCoordPointer)的数组:

代码语言:javascript
复制
gl.glVertexPointer(3, 0, vertBuff); 
gl.glTexCoordPointer(2, 0, texBuff);

另请参阅Java Code Examples for org.lwjgl.opengl.GL11.glVertexPointer()

为顶点坐标和纹理坐标启用客户端状态(glEnableClientState):

代码语言:javascript
复制
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

绑定纹理并启用二维纹理(glEnable):

代码语言:javascript
复制
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]); 
gl.glEnable(GL10.GL_TEXTURE_2D);

另请参阅Java Code Examples for org.lwjgl.opengl.GL11.glEnable()

使用基本类型GL_TRIANGLE_FAN绘制,用glDrawArrays绘制圆形

代码语言:javascript
复制
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points+2);

另请参阅Java Code Examples for org.lwjgl.opengl.GL11.glDrawArrays()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56911955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档