【问题标题】:Paint app for ipad [opengl-es] line strokes not proper用于 ipad [opengl-es] 线条笔划的绘图应用程序不正确
【发布时间】:2014-08-03 05:03:29
【问题描述】:

我对 openGL 有一个奇怪的问题。我正在为 iphone 和 ipad 开发绘画应用程序。我正在为我的应用程序使用 opengl-es。在我的应用程序中,我在轮廓图像中填充颜色,根据用户触摸的位置在屏幕上绘制一条线。我只是使用“touchesMoved”函数在两点之间画一条线。因为我希望线条留在屏幕上,所以在我的 renderLineFromPoint 函数中,但由于某种原因,线条的某些点刚刚脱落,看起来完全随机。但是 ipad 模拟器iphone 设备/模拟器 提供了所需的输出。线条描边如图所示。

我正在使用以下代码创建缓冲区:

- (BOOL)createFramebuffer{
// Generate IDs for a framebuffer object and a color renderbuffer
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
// This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
// allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).

[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];


glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);


NSLog(@"Backing Width:%i and Height: %i", backingWidth, backingHeight);


// For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
    NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
    return NO;
}

return YES;

}

我正在为 renderLineFromPoint 使用以下代码 sn-p

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end{
static GLfloat*     vertexBuffer = NULL;
static NSUInteger   vertexMax = 64;

NSUInteger          vertexCount = 0,
                    count,
                    i;

[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

// Convert locations from Points to Pixels
//CGFloat scale = self.contentScaleFactor;
CGFloat scale;
if ([self respondsToSelector: @selector(contentScaleFactor)])
{

    scale=self.contentScaleFactor;

}
else{


//scale = 1.000000;

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
        // RETINA DISPLAY

        scale = 2.000000;
    }
    else {
        scale = 1.000000;
    }

}


NSLog(@"start point %@", NSStringFromCGPoint(start));

NSLog(@"End Point %@", NSStringFromCGPoint(end));



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));


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);

NSLog(@"count %d",count);

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

        NSLog(@"if loop");

            }

    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;
}


NSLog(@"Scale  vertex %f",scale);

//NSLog(@"%i",vertexCount);




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

glDrawArrays(GL_POINTS, 0, vertexCount);


// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}

touchbegan函数代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect              bounds = [self bounds];
UITouch*    touch = [[event touchesForView:self] anyObject];
firstTouch = YES;
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;

}

touchmoved功能代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{  

CGRect              bounds = [self bounds];
UITouch*            touch = [[event touchesForView:self] anyObject];

// Convert touch point from UIView referential to OpenGL one (upside-down flip)
if (firstTouch) {
    firstTouch = NO;
    previousLocation = [touch previousLocationInView:self];
    previousLocation.y = bounds.size.height - previousLocation.y;

} else {
    location = [touch locationInView:self];
    location.y = bounds.size.height - location.y;
    previousLocation = [touch previousLocationInView:self];
    previousLocation.y = bounds.size.height - previousLocation.y;
}

// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];

}

【问题讨论】:

    标签: objective-c ios xcode ipad opengl-es


    【解决方案1】:

    我遇到了类似的缺失点问题,这与我的 CAEAGLLayer 的 drawableProprties 有关。

    在 CAEAGLLayer 的 drawableProperties 中,您是否将 kEAGLDrawablePropertyRetainedBacking 设置为 YES?如果没有,则您的绘图的背景不会在帧之间保留,这可能会导致丢失点和闪烁。

    【讨论】:

    • 嘿 robhasacamera 感谢您的回复,但我已将 kEAGLDrawablePropertyRetainedBacking 设置为“是”。当我调整 CAEaglelayer 的大小时,会出现这个缺失点问题。如果我设置层 768x768 的宽度 n 高度,它可以正常工作,但是当我将高度增加到 768 之外,即使其变为 768x769 时,它会显示一些奇怪的东西,如屏幕截图所示。你知道为什么会这样吗?
    • CAEAGLLayer 开始为 768x768 并在绘制后将其增加到 768x768+n 时是否会出现此问题?或者如果将其初始化为 768x768+n 也会出现此问题?
    • 当我将 CAEagleLayer 初始化为 768x768+n 时出现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多