Reputation: 1323
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);
}
}
Above code is working but after zooming lines on the paintView has distorted pixels [ not retina display ].
Is there any way to redraw content of opengl es based on zoom scale.
Thanking you in advance,
Upvotes: 2
Views: 913
Reputation: 6668
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.
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.
Upvotes: 3