Tôi đang phát triển ứng dụng vẽ tranh. Tôi đã cố gắng làm điều đó với thuật toán CoreGraphics/Quartz 2D và vẽ đường cong khá chậm. Vì vậy, chúng tôi đã quyết định chuyển sang OpenGL ES. Tôi chưa bao giờ có bất kỳ trải nghiệm OpenGL nào, vì vậy tôi đã tìm thấy ví dụ glPaint từ táo và bắt đầu chơi với nó.Ứng dụng vẽ iPhone (dựa trên glPaint). Trộn với nền trắng
Tôi đã thay đổi phương thức erase
làm nền trắng. Làm thế nào tôi bị mắc kẹt với bàn chải và pha trộn. Trong ví dụ, Apple sử dụng texture "white on black" cho brush (đầu tiên trên pic bên dưới). Nhưng nó không hiệu quả đối với tôi (tôi chơi với các chế độ hoà trộn khác nhau). Vì vậy, tôi đã quyết định sử dụng bàn chải khác nhau, nhưng tôi đã không tìm thấy cách thích hợp. Tôi đã tìm thấy một số câu hỏi về luồng ngăn xếp, nhưng tất cả chúng đều chưa được trả lời. Đây là một hình ảnh (từ một câu hỏi khác, nhờ vào Kevin Beimers). Results http://www.straandlooper.com/GLPaint.png
Vì vậy, câu hỏi là cách thực hiện nét như "mong muốn" trong ảnh. Và làm thế nào để pha trộn 2 nét gần gũi hơn với trải nghiệm thực tế đời sống (màu xanh trên màu vàng = màu xanh đậm).
Cảm ơn.
Có mã hiện tại (bit sửa đổi từ glPaint) cho bàn chải (từ initWithFrame
phương pháp:.
// 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, width, 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);
// Make the current material colour track the current color
glEnable(GL_COLOR_MATERIAL);
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
// Set a blending function to use
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Enable blending
glEnable(GL_BLEND);
// Multiply the texture colour by the material colour.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
//Set up OpenGL states
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);
// Alpha blend each "dab" of paint onto background
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_SRC_COLOR, GL_ONE);
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);
//Make sure to start with a cleared buffer
needsErase = YES;
[self erase];
Cảm ơn bạn rất nhiều vì đã trả lời rất chi tiết! Tôi đã thử giải pháp của bạn và nó hoạt động. – OgreSwamp
Tôi mới cho opengl nếu có thể bạn có thể chia sẻ mã – dineshprasanna