为 Paint 应用程序缩放 OpenGL ES 内容时 iPhone 像素失真
<p>我对 openGL ES 有一个奇怪的问题。我正在为 iphone 开发一个绘画应用程序 [从 GLPaint 应用程序中获取引用]。在我的代码中,我使用了一个包含轮廓图像的 ImageView ,我将 绘画 View 用于填充颜色。我正在根据用户触摸的位置在屏幕上绘制线条来填充轮廓图像中的颜色。我正在使用“renderLineFromPoint”函数在两点之间绘制一条线,使用“touchesMoved”来获取起点和终点。<br><pre><code>- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end{
</code></pre><br>//根据用户触摸的位置在屏幕上画一条线<br><pre><code>static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;
;
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
// Convert locations from Points to Pixels
//CGFloat scale = self.contentScaleFactor;
CGFloat scale;
if ()
{
scale=self.contentScaleFactor;
}
else{
//scale = 1.000000;
if ([ respondsToSelector:@selector(scale)] == YES && [ 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 = start.x + (dx) * ((GLfloat)i / (GLfloat)count);
vertexBuffer = 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);
;
}
</code></pre><br>现在我正在使用捏合手势缩放绘画 View :<br><pre><code> UIPinchGestureRecognizer *twoFingerPinch =
[[ initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
;
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
if( == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = ;
}
if ( == UIGestureRecognizerStateBegan ||
== UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[.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 - );
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale(, newScale, newScale);
self.transform = transform;
lastScale = ;// Store the prev
textureScale = lastScale;
;
CGRect frame = self.bounds;
NSLog(@"Scale %f Last scale %f width %f Height %f", newScale , lastScale, frame.size.width * newScale, frame.size.height * newScale);
}
}
</code></pre><br>上面的代码可以正常工作,但是在绘制 View 上的缩放线后,像素失真 [ 不是视网膜显示 ]。<br><br>有没有办法根据缩放比例重绘opengl es的内容。<br><br>提前谢谢你,</p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p>这一点也不奇怪。当您使用 OpenGL 绘制时,您是以固定分辨率绘制的,该分辨率就是您的 OpenGL 上下文的大小。您很可能正在创建一个与屏幕分辨率相同的上下文。<br><br>如果您希望能够在不插入图像的情况下放大,则必须创建比屏幕更大的 GL 上下文。例如,如果您希望能够放大到 200%,则必须创建一个像素数是屏幕像素数两倍的上下文。</p>
<p style="font-size: 20px;">关于为 Paint 应用程序缩放 OpenGL ES 内容时 iPhone 像素失真,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9079664/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9079664/
</a>
</p>
页:
[1]