OpenGL ES顶点阵列对象和奇怪的工件

蜻蜓式

我正在渲染一个场景,只要方向发生变化,就必须重新构建该场景,以适当填充屏幕。该场景渲染了一些规则的彩色四边形的顶点数组对象和一个纹理化的顶点数组对象数组。所有这些绘图元素(包括所有着色器以及与之相关的顶点,索引等数组)都包含在一个类中,因此我每次都吹掉该类并在旋转时执行新的alloc / init。

首次创建场景时,一切看起来都很好。但是,由于某种原因并且没有明显的模式,在随后的任何旋转中都可能会出现怪异的三角形对于特定场景,每次加载时似乎都是确定性的。但是,从一个场景到另一个场景,有时会出现伪影,有时一切看起来都很可爱。

我无法弄清楚这是我如何分解内存(即使在ARC中,我也需要释放其中的一部分)还是这是我创建VAO的方式。但是,由于仅当我拆下场景并对其进行重建时才会出现问题,然后再通过这种思考过程开始旋转轮子。

我有很多代码,所以这里是一些(希望)相关的代码。

这是彩色方形VAO的一些绘图代码。我正在向NSMutableArray添加顶点,因为我不知道会有多少个四边形。

- (void) buildNode : (NSDictionary *) nodeDictionary
            bounds : (BoundsGL) bounds
{
    if (!nodeDictionary)
    {
        return;
    }

    NSString *jobidstr = [nodeDictionary objectForKey:kNodeJobidKey];
    NSInteger jobid = jobidstr == NULL ? -1 : [jobidstr intValue];

    NSString *colorHexStr = [nodeDictionary objectForKey:kNodeColorKey];
    ColorGL color = HexidecimalStringToColorGL(colorHexStr);


    //
    // indices

    [_coloredQuadIndices addObject:[NSNumber numberWithInt:_coloredQuadVertices.count]];
    [_coloredQuadIndices addObject:[NSNumber numberWithInt:_coloredQuadVertices.count+1]];
    [_coloredQuadIndices addObject:[NSNumber numberWithInt:_coloredQuadVertices.count+2]];
    [_coloredQuadIndices addObject:[NSNumber numberWithInt:_coloredQuadVertices.count+2]];
    [_coloredQuadIndices addObject:[NSNumber numberWithInt:_coloredQuadVertices.count+1]];
    [_coloredQuadIndices addObject:[NSNumber numberWithInt:_coloredQuadVertices.count+3]];


    //
    // vertices

    GLfloat x2 = bounds.pos.x + bounds.size.w;
    GLfloat y2 = bounds.pos.y + bounds.size.h;

    PointGL blp = {bounds.pos.x, bounds.pos.y};
    PointGL brp = {x2, bounds.pos.y};
    PointGL tlp = {bounds.pos.x, y2};
    PointGL trp = {x2, y2};

    ColoredVertex bl = { .pos = blp, .color = color };
    ColoredVertex br = { .pos = brp, .color = color };
    ColoredVertex tl = { .pos = tlp, .color = color };
    ColoredVertex tr = { .pos = trp, .color = color };

    NSValue *tlv = [NSValue valueWithBytes:&tl objCType:@encode(ColoredVertex)];
    NSValue *blv = [NSValue valueWithBytes:&bl objCType:@encode(ColoredVertex)];
    NSValue *trv = [NSValue valueWithBytes:&tr objCType:@encode(ColoredVertex)];
    NSValue *brv = [NSValue valueWithBytes:&br objCType:@encode(ColoredVertex)];
    [_coloredQuadVertices addObject:tlv];
    [_coloredQuadVertices addObject:blv];
    [_coloredQuadVertices addObject:trv];
    [_coloredQuadVertices addObject:brv];
}

一些代码来为纹理四边形创建VAO:

#import "TexturedQuadVAO.h"
#import "Texture.h"
#import "ShaderUtil.h"

@interface TexturedQuadVAO()
{
    GLuint _textureUniformLocation;
}

@property (strong, nonatomic) Texture *texture;

@end


@implementation TexturedQuadVAO

@synthesize vaoid = _vaoid;

@synthesize vertexBuffer = _vertexBuffer;
@synthesize vcount = _vcount;
@synthesize vsize = _vsize;

@synthesize indexBuffer = _indexBuffer;
@synthesize icount = _icount;
@synthesize isize = _isize;

@synthesize texture = _texture;

- (id) initWithData : (TexturedVertex *) vertices
   numberOfVertices : (NSUInteger) vcount
       verticesSize : (size_t) vsize
            indices : (GLushort *) indices
    numberOfIndices : (NSUInteger) icount
        indicesSize : (size_t) isize
           viewSize : (CGSize) viewSize
               text : (NSString *) text
        textureSize : (SizeGL) textureSize
    foregroundColor : (UIColor *) fgcolor
    backgroundColor : (UIColor *) bgcolor
textureUniformLocation : (GLuint) textureUniformLocation
{
    self = [super init];

    if (self)
    {
        //
        // geometry

        self.vcount = vcount;
        self.vsize = vsize;

        self.icount = icount;
        self.isize = isize;


        //
        // texture

        self.texture = [[Texture alloc] init];
        UIFont *font = [UIFont fontWithName:@"Arial" size:24];
        SizeGL tSize = {textureSize.w * 2.5, textureSize.h * 2.5};
        UIImage *img = createTextImage(viewSize, text, tSize, font, fgcolor, bgcolor);

        [self.texture setupTextureWithImage:img];


        //
        // for shaders
        _textureUniformLocation = textureUniformLocation;


        [self createVertexArrayObject:vertices indices:indices];
    }

    return self;
}

- (void) createVertexArrayObject : (TexturedVertex *) vertices
                         indices : (GLushort *) indices
{
    checkGLError(@"  TexturedQuadVAO createVertexArrayObject ");

    // vertex array object

    glGenVertexArraysOES(1, &_vaoid);
    glBindVertexArrayOES(_vaoid);


    //
    // vertices buffer

    GLuint vb;
    glGenBuffers(1, &vb);
    self.vertexBuffer = vb;
    glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, self.vsize, vertices, GL_STATIC_DRAW);   // copy data into buffer object

    // position vertex attribute
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (const GLvoid *) offsetof(TexturedVertex, pos));

    // color vertex attribute
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(TexturedVertex), (const GLvoid *) offsetof(TexturedVertex, color));

    // textures vertex attribute
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (const GLvoid  *) offsetof(TexturedVertex, texcoord));


    //
    // index data buffer

    GLuint ib;
    glGenBuffers(1, &ib);
    self.indexBuffer = ib;
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.isize, indices, GL_STATIC_DRAW); // copy index data into buffer object


    //
    // clear binds

    glBindVertexArrayOES(0);        // ok to unbind for now, and bind when we want to render
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    checkGLError(@"2 TexturedQuadVAO createVertexArrayObject ");
}

- (void) render
{
    checkGLError(@"  TexturedQuadVAO ");

    glBindVertexArrayOES(_vaoid);
    checkGLError(@"2 TexturedQuadVAO ");

    glActiveTexture(GL_TEXTURE0);
    checkGLError(@"3 TexturedQuadVAO ");

    [self.texture bind];
    checkGLError(@"4 TexturedQuadVAO ");

    glUniform1i(_textureUniformLocation, 0);
    checkGLError(@"5 TexturedQuadVAO ");

    //glDrawElements(GL_TRIANGLE_STRIP, self.vsize/sizeof(GLshort), GL_UNSIGNED_SHORT, 0);
    glDrawElements(GL_TRIANGLE_STRIP, self.vsize/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
    checkGLError(@"6 TexturedQuadVAO ");

    [self.texture unbind];
    checkGLError(@"7 TexturedQuadVAO ");

    glBindVertexArrayOES(0);
    checkGLError(@"8 TexturedQuadVAO ");
}

- (void) tearDown
{
    [self.texture deleteTexture];

    glDeleteBuffers(1, &_vertexBuffer);
    glDeleteBuffers(1, &_indexBuffer);
    glDeleteVertexArraysOES(1, &_vaoid);
}

@end

我的拆卸代码:

- (void) tearDown
{
    // VAOs

    for (int i=0; i<_texturedQuadArray.count; i++)
    {
        TexturedVAO *vao = [_texturedQuadArray objectAtIndex:i];
        [vao tearDown];
        vao = nil;
    }
    [_texturedQuadArray removeAllObjects];

    if (_coloredQuadVAO)
    {
       [_coloredQuadVAO tearDown];
       _coloredQuadVAO = nil;
    }

    if (_coloredQuadOutlinesVAO)
    {
       [_coloredQuadOutlinesVAO tearDown];
       _coloredQuadOutlinesVAO = nil;
    }

    // shaders

    if (_shaders.colorShader)
    {
       glDeleteProgram(_shaders.colorShader);
       _shaders.colorShader = 0;
    }

    if (_shaders.textureShader)
    {
       glDeleteProgram(_shaders.textureShader);
       _shaders.textureShader = 0;
    }

    checkGLError(@"Main tearDown");
}

我搜寻了网络并尝试了所有我能想到的一切,以在最近的几天里找出该问题,并且该问题开始变得无处不在。任何想法将不胜感激!

更新:我发现了问题!

正如我所怀疑的那样,我花了很多天试图在所有错误的地方寻找东西,这简直是微不足道的!

罪魁祸首是这条线:

glDrawElements(GL_TRIANGLE_STRIP, self.vsize/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

在看到这条混乱的计算结果之前,我在这条直线上浏览了无数次,然后立即将其更改为:

glDrawElements(GL_TRIANGLE_STRIP, self.icount, GL_UNSIGNED_SHORT, 0);

即我改为计算实际的索引数。

蜻蜓式

如我所料,我花了很多天试图在所有错误的地方寻找东西,这简直是微不足道的!

罪魁祸首是TexturedQuadVAOs的第二组代码中的这一行:

glDrawElements(GL_TRIANGLE_STRIP, self.vsize/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

在看到这条混乱的计算结果之前,我在这条直线上浏览了无数次,然后立即将其更改为:

glDrawElements(GL_TRIANGLE_STRIP, self.icount, GL_UNSIGNED_SHORT, 0);

即我改为计算实际的索引数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

OpenGL顶点阵列对象用法

来自分类Dev

OpenGL顶点阵列对象用法

来自分类Dev

具有纹理ID的OpenGL ES交错顶点阵列

来自分类Dev

Opengl:如何为由顶点阵列制成的模型制作纹理?

来自分类Dev

OpenGL ES顶点/索引

来自分类Dev

OpenGL实例数组奇怪的顶点位置

来自分类Dev

OpenGL实例数组奇怪的顶点位置

来自分类Dev

OpenGL索引和阵列纹理

来自分类Dev

OpenGL索引和阵列纹理

来自分类Dev

使用顶点缓冲区对象的最低OpenGL ES版本

来自分类Dev

android opengl-es中的顶点数组对象

来自分类Dev

OpenGL ES移动对象

来自分类Dev

OpenGL ES移动对象

来自分类Dev

使用soubroutine的opengl工件

来自分类Dev

OpenGL生成顶点属性

来自分类Dev

OpenGL 打包顶点属性

来自分类Dev

OpenGL-glDrawElements与顶点数组对象

来自分类Dev

动态包装OpenGL顶点缓冲对象的数据

来自分类Dev

OpenGL复制顶点缓冲区对象

来自分类Dev

OpenGL绘制顶点缓冲区对象

来自分类Dev

OpenGL顶点和波前obj的索引

来自分类Dev

OpenGL球体顶点和UV坐标

来自分类Dev

OpenGL纹理奇怪的颜色

来自分类Dev

OpenGL:奇怪的普通渲染

来自分类Dev

OpenGL 奇怪的颜色

来自分类Dev

将OpenGL glBegin()移植到OpenGL ES

来自分类Dev

OpenGL glDrawElements传输到OpenGL ES glDrawElements

来自分类Dev

将OpenGL ES Framebuffer移植到OpenGL

来自分类Dev

自定义和通用顶点着色器属性在OpenGL和OpenGL ES中的使用