gpt4 book ai didi

objective-c - iPad [opengl-es] 的画图应用程序线条不正确

转载 作者:行者123 更新时间:2023-11-28 17:42:34 26 4
gpt4 key购买 nike

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

enter image description here

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

- (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 使用以下代码片段

- (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];

最佳答案

我有一个类似的缺失点问题,它与我的 CAEAGLLayer 的 drawableProprties 有关。

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

关于objective-c - iPad [opengl-es] 的画图应用程序线条不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7551534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com