Android OpenGL ES未绘制
几个月前我开始学习OpenGL ES,但当我试图创建一个绘制多个三角形的类时遇到了一些问题Android OpenGL ES未绘制,android,opengl-es,opengl-es-2.0,Android,Opengl Es,Opengl Es 2.0,几个月前我开始学习OpenGL ES,但当我试图创建一个绘制多个三角形的类时遇到了一些问题 private Level l; private Triangle t; public Game() { Bitmap b = BitmapFactory.decodeResource(MainProgram.res, R.drawable.black); //TEMP ARGUMENT l = new Level(b); } public void update() {
private Level l;
private Triangle t;
public Game()
{
Bitmap b = BitmapFactory.decodeResource(MainProgram.res, R.drawable.black);
//TEMP ARGUMENT
l = new Level(b);
}
public void update()
{
//TEMP!
l.update();
}
水平仪
private int size;
私有颜色[]颜色列表;
私有三角形列表三角形列表;
公共级别(位图b)
{
size=b.getWidth()/16;
颜色列表=新颜色[大小];
对于(int i=0;i>16;
浮点g=(像素颜色&0x0000FF00)>>8;
浮蓝=(像素颜色&0x000000FF);
颜色列表[i]=新颜色(r、g、蓝色);
}
三角形列表=新的三角形列表(大小、颜色列表);
colorList=null;
}
公共无效更新()
{
triangleList.draw();
}
三角形列表
private float[] color;
private FloatBuffer vertexBuffer;
private ShortBuffer[] indicesBuffer;
public TriangleList(int size,Color[] colorList)
{
float[] vertices = null;
short[] indices = null;
if(size == 1)
{
vertices = TriangleSize.stageOne;
indices = TriangleSize.stageOneIndices;
}
color = new float[colorList.length*4];
for(int i = 0;i<colorList.length;i++)
{
color[3*i] = colorList[i].getR();
color[3*i+1] = colorList[i].getG();
color[3*i+2] = colorList[i].getB();
color[3*i+3] = 1.0f;
}
ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
bb = null;
bb = ByteBuffer.allocateDirect(indices.length * 4);
bb.order(ByteOrder.nativeOrder());
indicesBuffer = new ShortBuffer[indices.length/3];
for(int i = 0;i<indices.length;i+=3)
{
bb = ByteBuffer.allocateDirect(6);
ShortBuffer b = bb.asShortBuffer();
b.put(new short[]{indices[i],indices[i+1],indices[i+2]});
b.position(0);
indicesBuffer[i/3] = b;
}
}
public void draw()
{
for(int i = 0;i<color.length/4;i++)
{
int positionHandle = Renderer.shader.getAttribute("vPosition");
int colorHandle = Renderer.shader.getUniform("vColor");
glEnableVertexAttribArray(positionHandle);
glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 0, vertexBuffer);
glUniform4fv(colorHandle, 1, new float[]{1.0f,1.0f,1.0f,1.0f}, 0);
glDrawElements(GL_TRIANGLES, 3,GL_UNSIGNED_SHORT,indicesBuffer[i]);
glDisableVertexAttribArray(positionHandle);
}
}
private float[]颜色;
私有浮动缓冲区顶点缓冲区;
私有ShortBuffer[]表示缓冲区;
公共三角形列表(整数大小,颜色[]颜色列表)
{
float[]顶点=null;
short[]索引=空;
如果(大小==1)
{
顶点=TriangleSize.stageOne;
索引=三角形化。阶段索引;
}
颜色=新浮点[colorList.length*4];
对于(inti=0;i,代码中存在一些问题。首先,您不需要将ShortBuffers数组用作索引列表缓冲区
private FloatBuffer vertexBuffer;
private ShortBuffer indicesBuffer;
您应该将所有数据紧密地打包到vertexBuffer和IndicatesBuffer中,并只执行一个draw调用。
下面是一个准备数据以绘制形成矩形的两个三角形的示例:
// The vertex buffer.
vertexBuffer = ByteBuffer.allocateDirect(4 * 3 * 4) //amount of vertices * amount of coordinates * sizeof(float)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
// initialize byte buffer for the draw list
indicesBuffer = ByteBuffer.allocateDirect(6 * 2) // amount of indices * sizeof(short)
.order(ByteOrder.nativeOrder()).asShortBuffer();
vertexBuffer.position(0);
indicesBuffer.position(0);
float [] vertices = new float[]
{200f, 400f, 100f, //vertex 0
200f, 200f, 100f, //vertex 1
400f, 200f, 100f, //vertex 2
400f, 400f, 100f}; //vertex 3
//indices of vertices above that will be used to draw
short indices[] = new short[] {0, 1, 2, 0, 2, 3};
vertexBuffer.put(vertices);
indicesBuffer.put(indices);
vertexBuffer.position(0);
indicesBuffer.position(0);
还可以删除渲染函数中的循环,并仅使用单个a draw调用:
public void draw()
{
int positionHandle = Renderer.shader.getAttribute("vPosition");
int colorHandle = Renderer.shader.getUniform("vColor");
glEnableVertexAttribArray(positionHandle);
glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 0, vertexBuffer);
glUniform4fv(colorHandle, 1, new float[]{1.0f,1.0f,1.0f,1.0f}, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indicesBuffer.capacity(),
GLES20.GL_UNSIGNED_SHORT, indicesBuffer);
glDisableVertexAttribArray(positionHandle);
}
如果还有什么不清楚的地方,请告诉我,我会尽力澄清
public void draw()
{
int positionHandle = Renderer.shader.getAttribute("vPosition");
int colorHandle = Renderer.shader.getUniform("vColor");
glEnableVertexAttribArray(positionHandle);
glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 0, vertexBuffer);
glUniform4fv(colorHandle, 1, new float[]{1.0f,1.0f,1.0f,1.0f}, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indicesBuffer.capacity(),
GLES20.GL_UNSIGNED_SHORT, indicesBuffer);
glDisableVertexAttribArray(positionHandle);
}