1
我在OpenGL ES中繪製了一個圓圈,添加紋理時,它被「添加」4次而不是1次(請參閱下圖)。OpenGL ES,將紋理添加到圓圈
換句話說,這是我的照片X4。我想要的是核心符號集中在圓圈中的一張圖片中。
下面是我使用貼圖
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);
}
目前畫圓法(在Circle類),該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);
}
這是唯一的紋理或整個圓的頂點?我應該做一個方法來設置紋理頂點,這是唯一的區別嗎? – simtaxman
這僅適用於紋理的「頂點」:)。 Texture空間中的座標稱爲UV(因此您在glTexCoordPointer函數調用中傳遞的緩衝區)。頂點(多個頂點)由位置(XYZ)和UV組成。 – MichaelCMS
Okey,因爲結果相當粗糙,雖然看起來只有一張圖片被渲染,但不可能看到圖片重新組裝的內容。我將補充我的問題中的代碼。 – simtaxman