سؤال

I'm trying to render an array of VBOs but the results are not right. I am saving them into an array when I draw them the first time using the GLPaint (OpenGLES v2) setup. Here is the sample project I have if you want to help me out. https://drive.google.com/file/d/0B0pG5vRVzBTzUTZPYWNoenhkcWs/edit?usp=sharing

////// Store VBOs when drawing this is done in renderLineFromPoint (from GLPaint)
- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{

static GLfloat*     vertexBuffer = NULL;
static NSUInteger   vertexMax = 64;
NSUInteger          vertexCount = 0,
count,
i;

[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);

// Convert locations from Points to Pixels
CGFloat scale = self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;

// Allocate vertex array buffer
if(vertexBuffer == NULL)
    vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));

// Add points to the buffer so there are drawing points every X pixels
count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);

for(i = 0; i < count; ++i)
{
    if(vertexCount == vertexMax)
    {
        vertexMax = 2 * vertexMax;
        vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
    }

    vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
    vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
    vertexCount += 1;
}

glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);

// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);

// Display the buffer
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];

// Store VBO for undo
VBOHolder *vboHolder = [[VBOHolder alloc]init];
vboHolder.vertexCount = vertexCount;
vboHolder.vbo = vertexBuffer;
[vboHolderArray addObject:vboHolder];

}

////// Here is the problem: I clear the screen and then try to re-render the VBOs
-(void)renderSavedVertexBufferes
{

  for (VBOHolderModel *vboh in vboHolderArray)
  {
    if (vboh.vertexCount == 0)
    {
        continue;
    }

    NSUInteger vertexCount = vboh.vertexCount;

    glBindBuffer(GL_ARRAY_BUFFER, vboId);
    glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vboh.vbo, GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);

    // Render the vertex array
    glVertexPointer(2, GL_FLOAT, 0, vboh.vbo);
    glDrawArrays(GL_POINTS, 0, vertexCount);

    // Display the buffer
    glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER];

  }
}


////// VBOHolderModel looks like this, it wraps the vbo so it can be put in NSMutableArray
@interface VBOHolderModel : NSObject
{
   GLfloat *vbo;
   NSUInteger vertexCount;

}

It's basically an undo... when pressing undo it should remove only the 3 because it was the last stroke, but it erases it all and draws the dots shown in the second image.

enter image description here

enter image description here

هل كانت مفيدة؟

المحلول

I found the answer to this question in the lines below

// Allocate vertex array buffer
if(vertexBuffer == NULL)
vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));

The problems is that the stored Vertex Arrays are all the same! It was only allocating memory once on init when vertexBuffer is null. Some of the other code has change a bit to, if you want the updates, let me know.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top