GLPaint 画笔效果

发布于 2025-01-07 17:57:11 字数 3951 浏览 2 评论 0原文

我正在开发一个基于 GLPaint 的绘图 iPhone 应用程序。我想要得到如图 1 所示的画笔效果。但我只能得到类似于image2的效果。我想要的效果是在白色背景上绘制时沿着画笔绘制一条线。 屏幕截图

预先感谢您。

与 GLPaint 代码没有太大区别;这里我给出了我认为应该改变的 glBlendFunc() 方法。

 if ((self = [super initWithCoder:coder])) {

CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

eaglLayer.opaque = YES;

// In this application, we want to retain the EAGLDrawable contents after a call to presentRenderbuffer.

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

if (!context || ![EAGLContext setCurrentContext:context]) {

[self release];

return nil;

}

// Create a texture from an image

// First create a UIImage object from the data in a image file, and then extract the Core Graphics image

brushImage = [UIImage imageNamed:@"criclewww.png"].CGImage;

// Get the width and height of the image

width = CGImageGetWidth(brushImage);

height = CGImageGetHeight(brushImage);

// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,

// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.

// Make sure the image exists

if(brushImage) {

// Allocate  memory needed for the bitmap context

brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));

// Use  the bitmatp creation function provided by the Core Graphics framework. 

brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);

// After you create the context, you can draw the  image to the context.

CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);

// You don't need the context at this point, so you need to release it to avoid memory leaks.

CGContextRelease(brushContext);

// Use OpenGL ES to generate a name for the texture.

glGenTextures(1, &brushTexture);

// Bind the texture name. 

glBindTexture(GL_TEXTURE_2D, brushTexture);

// Set the texture parameters to use a minifying filter and a linear filer (weighted average)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Specify a 2D texture image, providing the a pointer to the image data in memory

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);

// Release  the image data; it's no longer needed

            free(brushData);

}

// Set the view's scale factor

self.contentScaleFactor = 1.0;



// Setup OpenGL states

glMatrixMode(GL_PROJECTION);

CGRect frame = self.bounds;

CGFloat scale = self.contentScaleFactor;

// Setup the view port in Pixels

glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);

glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);

glMatrixMode(GL_MODELVIEW);

glDisable(GL_DITHER);

glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);

    glEnable(GL_BLEND);

// Set a blending function appropriate for premultiplied alpha pixel data

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        //(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_POINT_SPRITE_OES);

glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);

glPointSize(width / kBrushScale);

// Make sure to start with a cleared buffer

needsErase = YES;

// Playback recorded path, which is "Shake Me"

recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]];

if([recordedPaths count])

[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2];

}

    self.backgroundColor = [UIColor clearColor];

return self;

I am working on a drawing iPhone app based on GLPaint. I want to get the brush effect as you see in the image1. But i could only get a effect similar to image2. The effect i want is to get a line along with the brush when drawn on white background. Screen shots

Thank you in advance.

Nothing much differ from GLPaint code; Here I have given the glBlendFunc() method that i think should change.

 if ((self = [super initWithCoder:coder])) {

CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

eaglLayer.opaque = YES;

// In this application, we want to retain the EAGLDrawable contents after a call to presentRenderbuffer.

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

if (!context || ![EAGLContext setCurrentContext:context]) {

[self release];

return nil;

}

// Create a texture from an image

// First create a UIImage object from the data in a image file, and then extract the Core Graphics image

brushImage = [UIImage imageNamed:@"criclewww.png"].CGImage;

// Get the width and height of the image

width = CGImageGetWidth(brushImage);

height = CGImageGetHeight(brushImage);

// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,

// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.

// Make sure the image exists

if(brushImage) {

// Allocate  memory needed for the bitmap context

brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));

// Use  the bitmatp creation function provided by the Core Graphics framework. 

brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);

// After you create the context, you can draw the  image to the context.

CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);

// You don't need the context at this point, so you need to release it to avoid memory leaks.

CGContextRelease(brushContext);

// Use OpenGL ES to generate a name for the texture.

glGenTextures(1, &brushTexture);

// Bind the texture name. 

glBindTexture(GL_TEXTURE_2D, brushTexture);

// Set the texture parameters to use a minifying filter and a linear filer (weighted average)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Specify a 2D texture image, providing the a pointer to the image data in memory

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);

// Release  the image data; it's no longer needed

            free(brushData);

}

// Set the view's scale factor

self.contentScaleFactor = 1.0;



// Setup OpenGL states

glMatrixMode(GL_PROJECTION);

CGRect frame = self.bounds;

CGFloat scale = self.contentScaleFactor;

// Setup the view port in Pixels

glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);

glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);

glMatrixMode(GL_MODELVIEW);

glDisable(GL_DITHER);

glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);

    glEnable(GL_BLEND);

// Set a blending function appropriate for premultiplied alpha pixel data

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        //(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_POINT_SPRITE_OES);

glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);

glPointSize(width / kBrushScale);

// Make sure to start with a cleared buffer

needsErase = YES;

// Playback recorded path, which is "Shake Me"

recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]];

if([recordedPaths count])

[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2];

}

    self.backgroundColor = [UIColor clearColor];

return self;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文