在为Paint应用程序缩放OpenGL ES内容时Iphone像素失真

时间:2012-01-31 12:40:14

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

我对openGL ES有一个奇怪的问题。我正在研究iphone的绘图应用程序[从GLPaint应用程序中获取参考]。在我的代码中,我使用的是包含轮廓图像的imageView,我在其上放置[CAEAGLLayer] paintView来填充颜色。我根据用户触摸的位置通过屏幕上的图纸线填充轮廓图像中的颜色。我正在使用“renderLineFromPoint”函数在两个点之间绘制一条线,使用“touchesMoved”来获取起点和终点。

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

}




start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;

float dx = end.x - start.x;
float dy = end.y - start.y;
float dist = (sqrtf(dx * dx + dy * dy)/ kBrushPixelStep);

// 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(dist), 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 + (dx) * ((GLfloat)i / (GLfloat)count);
    vertexBuffer[2 * vertexCount + 1] = start.y + (dy) * ((GLfloat)i / (GLfloat)count);

    vertexCount += 1;


}





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


}

现在我正在使用捏合手势缩放绘画视图:

 UIPinchGestureRecognizer *twoFingerPinch = 
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
[self addGestureRecognizer:twoFingerPinch];



- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{



if([recognizer state] == UIGestureRecognizerStateBegan) {
    // Reset the last scale, necessary if there are multiple objects with different scales
    lastScale = [recognizer scale];
}

if ([recognizer state] == UIGestureRecognizerStateBegan || 
    [recognizer state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = [[[recognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 2.0;
    const CGFloat kMinScale = 1.0;

    CGFloat newScale = 1 -  (lastScale - [recognizer scale]); 
    newScale = MIN(newScale, kMaxScale / currentScale);   
    newScale = MAX(newScale, kMinScale / currentScale);
    CGAffineTransform transform = CGAffineTransformScale([self transform], newScale, newScale);
    self.transform = transform;

    lastScale = [recognizer scale];  // Store the prev

    textureScale = lastScale;




    [self setContentScaleFactor:2.0f];


    CGRect frame = self.bounds;


    NSLog(@"Scale %f Last scale %f width %f Height %f", newScale , lastScale, frame.size.width * newScale, frame.size.height * newScale);



}

}

上面的代码正常工作,但在paintView上的缩放线后,像素扭曲了[不是视网膜显示]。

有没有办法根据缩放比例重新绘制opengl的内容。

提前感谢你,

1 个答案:

答案 0 :(得分:3)

根本不奇怪。当您绘制到OpenGL时,您将以固定的分辨率绘制,并且该分辨率是您的OpenGL上下文的大小。您最有可能创建一个屏幕分辨率的上下文。

如果您希望能够在不插入图像的情况下放大,则必须创建大于屏幕的GL上下文。例如,如果您希望能够放大到200%,则必须创建一个像素数为屏幕两倍的上下文。