OStack程序员社区-中国程序员成长平台

标题: 为 Paint 应用程序缩放 OpenGL ES 内容时 iPhone 像素失真 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:09
标题: 为 Paint 应用程序缩放 OpenGL ES 内容时 iPhone 像素失真

我对 openGL ES 有一个奇怪的问题。我正在为 iphone 开发一个绘画应用程序 [从 GLPaint 应用程序中获取引用]。在我的代码中,我使用了一个包含轮廓图像的 ImageView ,我将 [CAEAGLLayer] 绘画 View 用于填充颜色。我正在根据用户触摸的位置在屏幕上绘制线条来填充轮廓图像中的颜色。我正在使用“renderLineFromPoint”函数在两点之间绘制一条线,使用“touchesMoved”来获取起点和终点。

- (void) renderLineFromPointCGPoint)start toPointCGPoint)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] respondsToSelectorselector(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];


}

现在我正在使用捏合手势缩放绘画 View :
 UIPinchGestureRecognizer *twoFingerPinch = 
[[[UIPinchGestureRecognizer alloc] initWithTarget:self actionselector(twoFingerPinch] autorelease];
[self addGestureRecognizer:twoFingerPinch];



- (void)twoFingerPinchUIPinchGestureRecognizer *)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);



}

}

上面的代码可以正常工作,但是在绘制 View 上的缩放线后,像素失真 [ 不是视网膜显示 ]。

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

提前谢谢你,



Best Answer-推荐答案


这一点也不奇怪。当您使用 OpenGL 绘制时,您是以固定分辨率绘制的,该分辨率就是您的 OpenGL 上下文的大小。您很可能正在创建一个与屏幕分辨率相同的上下文。

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

关于为 Paint 应用程序缩放 OpenGL ES 内容时 iPhone 像素失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079664/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4