简体   繁体   English

批处理OpenGL精灵

[英]Batching OpenGL sprites

i have been learning OpenGL and have been able to create a successful 2d drawing system using triangle strips. 我一直在学习OpenGL,并且能够使用三角带创建成功的2D绘图系统。 I wrote a particle generator to test batching the geometry and everything works well, i'm able to render 30k+ vertices at 60 fps on an iPhone 5. I use degenerative triangles to connect the particles and draw them at once. 我编写了一个粒子生成器来测试几何的批处理,并且一切工作正常,我能够在iPhone 5上以60 fps的速度渲染30k +顶点。我使用退化三角形连接粒子并立即绘制它们。 What i am trying to accomplish is batch rendering without using degenerative triangles as that would reduce the amount of data being sent to the GPU by 1/3 the amount which would be huge. 我要完成的是不使用退化三角形的批量渲染,因为那样会使发送到GPU的数据量减少1/3,这将是巨大的。

I am attempting to use glDrawElements with triangles to draw 2 sprites with the following code 我正在尝试使用带有三角形的glDrawElements通过以下代码绘制2个精灵

//the vertices for the square (2d)
GLfloat square[] = {
    50, 50,  //bottom left
    100, 50, //bottom right
    50, 100, //top left
    100, 100, //top right

    150, 200,  //bottom left
    200, 150, //bottom right
    150, 200, //top left
    200, 200 //top right
};

//texture coords
GLfloat tex[] = {
    0,0,
    1,0,
    0,1,
    1,1,

    0,0,
    1,0,
    0,1,
    1,1
};

GLubyte indices[] =
{
    0,2,3, 
    0,3,1,

    0,2,3,
    0,3,1
};

//actual drawing code
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, square);
glVertexAttribPointer(ATTRIB_TEX, 2, GL_FLOAT, GL_FALSE, 0, tex);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, image);

glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices);

This code draws the first image without issue, but the second image is distorted. 此代码绘制第一个图像没有问题,但第二个图像变形。 I have been looking online but have been unable to figure out how to make this work correctly. 我一直在网上寻找,但一直无法弄清楚如何使其正常工作。

First of all, you're currently using the same indices for both quads, so the second image shouldn't even be visible at all. 首先,您当前对两个四边形使用相同的索引,因此第二张图像甚至根本不可见。

Other than that your vertex data for the second quad is messed, you have the point (150, 200) two times, the first one should probably be (150, 150) . 除了第二个四边形的顶点数据混乱以外,您还获得了两次(150, 200)点,第一个可能应该是(150, 150)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM