我的朋友在opengl-es中有一个绘制纹理圆的问题,他不能在此站点上发布他的报价,他要我帮他做,他将跟进并通过我的回复帐户。
因此,如果这件事没有错,或者没有违反网站的条款,请提供帮助。
他的话:
如何绘制纹理圆opengl-es?
这是他的代码:
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();
}
}
请帮助他解决这个问题。
答案 0 :(得分:0)
创建顶点坐标和纹理坐标的数组。第一点是中心点。其他点在圆上:
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);
}
为顶点坐标定义纹理坐标的缓冲区。例如:
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
)的数组:
gl.glVertexPointer(3, 0, vertBuff);
gl.glTexCoordPointer(2, 0, texBuff);
另请参阅Java Code Examples for org.lwjgl.opengl.GL11.glVertexPointer()
为顶点坐标和纹理坐标启用客户端状态(glEnableClientState
):
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
绑定纹理并启用二维纹理(glEnable
):
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
绘制圆形
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points+2);
另请参阅Java Code Examples for org.lwjgl.opengl.GL11.glDrawArrays()