缩放Paint应用程序的OpenGL ES内容时,iPhone像素失真 [英] Iphone pixel distortion while zooming OpenGL ES content for Paint app

查看:90
本文介绍了缩放Paint应用程序的OpenGL ES内容时,iPhone像素失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对openGL ES有一个奇怪的问题.我正在开发iPhone的绘画应用程序[从GLPaint应用程序获取参考].在我的代码中,我使用的是imageView,其中包含轮廓图,并在其中放置了[CAEAGLLayer] paintView来填充颜色.我根据用户触摸的位置通过在屏幕上绘制线条来填充轮廓图像中的颜色.我正在使用"renderLineFromPoint"功能,使用"touchesMoved"在两点之间绘制一条线,以获取起点和终点.

I have a strange problem with openGL ES. I’m working on a paint app for iphone [taking reference from GLPaint app] .In my code I'm using an imageView which contain outline image on which I am puting [CAEAGLLayer] paintView for filling colors. I am filling colors in outline image by drawings lines onscreen based on where the user touches. I'm using the "renderLineFromPoint" function to draw a line between two points using "touchesMoved" for getting starting point and ending point.

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end{

//根据用户触摸的位置在屏幕上绘制一条线

// Drawings a line onscreen based on where the user touches

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


}

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

Now I am zooming paint view using pinch gesture:

 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上的线条缩放后像素变形了[不是视网膜显示].

Above code is working but after zooming lines on the paintView has distorted pixels [ not retina display ].

有什么方法可以根据缩放比例重绘opengl es的内容.

Is there any way to redraw content of opengl es based on zoom scale.

先谢谢您

推荐答案

一点都不奇怪.当您使用OpenGL绘制时,您将以固定的分辨率进行绘制,而该分辨率就是您的OpenGL上下文的大小.您最有可能创建一个屏幕分辨率的上下文.

It is not weird at all. When you draw to OpenGL, you are drawing at a fixed resolution and that resolution is what size your OpenGL context is. You are most likely creating a context that is the resolution of your screen.

如果要在不插入图像的情况下进行放大,则必须创建比屏幕更大的GL Context.例如,如果您希望能够放大到200%,则必须创建一个具有两倍于屏幕像素数的上下文.

You will have to create your GL Context larger then the screen if you want to be able to zoom in without interpolating the image. For example, if you want to be able to zoom in to 200%, you will have to create a context with twice the number of pixels as your screen.

这篇关于缩放Paint应用程序的OpenGL ES内容时,iPhone像素失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆