iphone – 使用OpenGL在iPad上绘制应用程序
我正在使用OpenGL为iPad创建一个绘图应用程序(文本).我已经看过苹果的例子GLPaint,而我的应用程序现在基于该代码.我的应用程序应该只是为了绘制文字,而不是绘画.
那么我的应用程序工作,我可以写一些文本.但写作并不是很好,写作并不好玩.绘图路径不顺利,因为我从一个点到另一个点画一条线,是有角度的.而路径无处不在,宽度相同.我的想法是:当你写得很快时,线条比写入缓慢时更薄.它应该是一样的经验,像一个真正的笔写作. 如何使路径看起来更顺畅?如何根据写作速度来改变线条的宽度? 在这里你可以看到我的意思: 解决方法
平滑绘图的最佳方法是使用bezeir曲线.这是我的代码这是我在苹果开发网站上发现的一个修改版本,但我不记得原来的链接:
CGPoint drawBezier(CGPoint origin,CGPoint control,CGPoint destination,int segments) { CGPoint vertices[segments/2]; CGPoint midPoint; glDisable(GL_TEXTURE_2D); float x,y; float t = 0.0; for(int i = 0; i < (segments/2); i++) { x = pow(1 - t,2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x; y = pow(1 - t,2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y; vertices[i] = CGPointMake(x,y); t += 1.0 / (segments); } //windowHeight is the height of you drawing canvas. midPoint = CGPointMake(x,windowHeight - y); glVertexPointer(2,GL_FLOAT,vertices); glDrawArrays(GL_POINTS,segments/2); return midPoint; } 这将以三点为基础.控制是中点,您需要返回.新的中点将与之前的不同.另外,如果你通过上面的代码,它只会画一半.下一个笔画将填写.这是必需的.我的代码调用这个函数(以上是在C中,这在Obj-C中): //Invert the Y axis to conform the iPhone top-down approach invertedYBegCoord = self.bounds.size.height - [[currentStroke objectAtIndex:i] CGPointValue].y; invertedYEndCoord = self.bounds.size.height - [[currentStroke objectAtIndex:i+1] CGPointValue].y; invertedYThirdCoord = self.bounds.size.height - [[currentStroke objectAtIndex:i+2] CGPointValue].y; //Figure our how many dots you need count = MAX(ceilf(sqrtf(([[currentStroke objectAtIndex:i+2] CGPointValue].x - [[currentStroke objectAtIndex:i] CGPointValue].x) * ([[currentStroke objectAtIndex:i+2] CGPointValue].x - [[currentStroke objectAtIndex:i] CGPointValue].x) + ((invertedYThirdCoord - invertedYBegCoord) * (invertedYThirdCoord - invertedYBegCoord))) / pointCount),1); newMidPoint = drawBezier(CGPointMake([[currentStroke objectAtIndex:i] CGPointValue].x,invertedYBegCoord),CGPointMake([[currentStroke objectAtIndex:i+1] CGPointValue].x,invertedYEndCoord),CGPointMake([[currentStroke objectAtIndex:i+2] CGPointValue].x,invertedYThirdCoord),count); int loc = [currentStroke count]-1; [currentStroke insertObject:[NSValue valueWithCGPoint:newMidPoint] atIndex:loc]; [currentStroke removeObjectAtIndex:loc-1]; 该代码将基于倒置的iPad点获得中点,并将“控件”设置为当前点. 这将平滑边缘.现在关于线宽,你只需要找到该图的速度.找到你的线的长度是最简单的.这很容易使用组件数学.我没有任何代码,但here是来自物理网站的组件数学的底稿.或者你可以简单地划分(上面)计数一些数字,以找出你需要多大的行(计数使用组件数学). 我将点数据存储在名为currentStroke的数组中,以防不明显. 这应该是你需要的. 编辑: 要存储点,你应该使用touchesBegin和touchesEnd: - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { self.currentStroke = [NSMutableArray array]; CGPoint point = [ [touches anyObject] locationInView:self]; [currentStroke addObject:[NSValue valueWithCGPoint:point]]; [self draw]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint point = [ [touches anyObject] locationInView:self]; [currentStroke addObject:[NSValue valueWithCGPoint:point]]; [self draw]; } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint point = [ [touches anyObject] locationInView:self]; [currentStroke addObject:[NSValue valueWithCGPoint:point]]; [self draw]; } 这几乎是整个绘图应用程序.如果您使用GL_Paint,那么您已经在使用该系统构建的点精灵. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |