Android 为什么我的OPEN GL ES纹理未显示
好的,这是代码,它运行得很好,我只是没有看到纹理。我看到一个白色的正方形,大小和我指定的完全一样,只是没有纹理Android 为什么我的OPEN GL ES纹理未显示,android,graphics,opengl-es,Android,Graphics,Opengl Es,好的,这是代码,它运行得很好,我只是没有看到纹理。我看到一个白色的正方形,大小和我指定的完全一样,只是没有纹理 public class MeshRect { MyRect rect; final int VERTEX_SIZE = (2 + 4 + 2) * 4; FloatBuffer vertices; ShortBuffer indices; long MyID; public MeshRect(MyRect rect, long ID) { super();
public class MeshRect {
MyRect rect;
final int VERTEX_SIZE = (2 + 4 + 2) * 4;
FloatBuffer vertices;
ShortBuffer indices;
long MyID;
public MeshRect(MyRect rect, long ID) {
super();
this.rect = rect;
MyID = ID;
Assemble();
}
private void Assemble() {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * VERTEX_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
float x1 = (float) rect.BottomLeftCorner.x;
float x2 = (float) rect.BottomRightCorner.x;
float x3 = (float) rect.TopRightCorner.x;
float x4 = (float) rect.TopLeftCorner.x;
float y1 = (float) rect.BottomLeftCorner.y;
float y2 = (float) rect.BottomRightCorner.y;
float y3 = (float) rect.TopRightCorner.y;
float y4 = (float) rect.TopLeftCorner.y;
vertices.put(new float[] { x1, y1, 1, 1, 1, 1, 0.0f, 1.0f,
x2, y2, 1, 1, 1, 1, 1.0f, 1.0f,
x3, y3, 1, 1, 1, 1, 1.0f, 0.0f,
x4, y4, 1, 1, 1, 1, 0.0f, 0.0f });
// x y r g b A u|s v|t
vertices.flip();
byteBuffer = ByteBuffer.allocateDirect(6 * 2);
byteBuffer.order(ByteOrder.nativeOrder());
indices = byteBuffer.asShortBuffer();
indices.put(new short[] { 0, 1, 2,
2, 3, 0 });
indices.flip();
}
public FloatBuffer getVertices() {
return vertices;
}
public ShortBuffer getIndices() {
return indices;
}
public long getMyID() {
return MyID;
}
public void Draw(int primitiveType, GL10 gl) {
int numVertices = 6;
//set vertex array
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
vertices.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
//set color
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
vertices.position(2);
gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
// set texture coords
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
vertices.position(6);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
//set indices
indices.position(0);
gl.glDrawElements(primitiveType, numVertices, GL10.GL_UNSIGNED_SHORT, indices);
//reset
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
和渲染
public void Render(GL10 gl, long deltaTime) {
//render all
Log.d("Game", "Render");
gl.glViewport(0, 0, Screen.SCREEN_WIDTH, Screen.SCREEN_HEIGHT);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, Screen.WIDTH_SCALE, 0, Screen.HEIGHT_SCALE, 1, -1);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
/**
* so psuedo would look something like this
*
* *.Update();
* gl.glEnable(GL10.GL_TEXTURE_2D);
* Textures.BindTexture(*.getCurrentTextureFileID);
* MeshRects.draw((*.getMyRect) ,GL10.GL_TRIANGLES, gl);
*
* update();
* bind texture;
* draw rect;
*/
ball.Update(deltaTime);
gl.glEnable(GL10.GL_TEXTURE_2D);
texture.bind(gl);
MeshRect mRect = new MeshRect(ball.getMyRect(), 1);
mRect.Draw(GL10.GL_TRIANGLES, gl);
}
以及纹理绑定方法
public void bind(GL10 gl) {
Log.d("Texture", "Bind");
gl.glBindTexture(GL10.GL_TEXTURE_2D, TextureID);
}
我忍不住想我错过了什么。这是我第一次深入到开放式GL的世界,这是我今天通过阅读和测试来自互联网的代码而创建的。没有关于如何设置的简单资源,因此我在学习时一直在制作自己的2D引擎,以便在很长一段时间内可以重复使用它。我没有看到任何纹理设置代码 glTexImage2D在哪里?此外,我没有看到设置过滤等的调用 下面是一个设置纹理的代码段(用C编写,但您需要类似的东西):
你们非常接近,我只需要重新安排几行,再加上我错过的一行。谢谢
GLubyte tex[] = {255, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 255, 0, 0, 255};
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
glBindTexture(GL_TEXTURE_2D, 0);