I am developing a drawing application. I tried to do this with CoreGraphics / Quartz 2D, and the curve drawing algorithm is pretty slow. Therefore, we decided to switch to OpenGL ES. I have never had OpenGL experience, so I found the glPaint example from apple and started playing with it.
I changed the way I erasemade the white background. How I got stuck with brushes and blending. In the example, Apple uses a white on black texture for the brush (first in the figure below). But this did not work for me (I played with different blending modes). So I decided to use different brushes, but I did not find a suitable way. I found some questions about stackoverflow, but they all went unanswered. Here is a photograph (from another question, thanks to Kevin Baymers ).
Results http://www.straandlooper.com/GLPaint.png
So, the question is how to implement the beat as โdesiredโ in the picture. And how to mix 2 strokes closer to real life experience (blue over yellow = dark green).
Thank.
There is current code (bit changed from glPaint) for the brush (from the method initWithFrame:
if(brushImage) {
brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, width, width, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glEnable( GL_COLOR_MATERIAL );
glEnable(GL_TEXTURE_2D);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable(GL_BLEND);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
}
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;
glOrthof(0, frame.size.width, 0, frame.size.height, -1, 1);
glViewport(0, 0, frame.size.width, frame.size.height);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_BLEND);
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
self.brushScale = 3;
self.brushStep = 3;
self.brushOpacity = (1.0 / 1.5);
glPointSize(width / brushScale);
needsErase = YES;
[self erase];