几个月前我开始学习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()
{
//TEMP!
l.update();
}
等级
private int size;
private Color[] colorList;
private TriangleList triangleList;
public Level(Bitmap b)
{
size = b.getWidth()/16;
colorList = new Color[size];
for(int i = 0;i<size;i++)
{
int pixColor = b.getPixel(i*16, 1);
float r = (pixColor&0x00FF0000)>>16;
float g = (pixColor&0x0000FF00)>>8;
float blue = (pixColor&0x000000FF);
colorList[i] = new Color(r,g,blue);
}
triangleList = new TriangleList(size,colorList);
colorList = null;
}
public void update()
{
triangleList.draw();
}
TriangleList
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);
}
}
它没有给我一个错误,但没有画出来。
答案 0 :(得分:1)
您的代码中存在一些问题。首先,您不需要一个ShortBuffers数组作为索引列表缓冲区。
private FloatBuffer vertexBuffer;
private ShortBuffer indicesBuffer;
你应该将所有数据紧密地打包到vertexBuffer和indicesBuffer中并且只做一次绘制调用。 这是一个准备数据的示例,用于绘制形成矩形的2个三角形:
// 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);
同样删除渲染函数中的循环,只使用一个绘制调用:
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);
}
如果还有不清楚的地方,请告诉我,我会尝试澄清。