开发者

Set UIImage as a background for 2D OpenGL ES on iPhone

once again I'm asking for help after quite a bit of research.

I need to create a view where the user can place an image to the background and draw lines/dots(touch events) on top of it and then save the "sketch" by pressing save button.

So after research I decide to pick up this code and build the thing on top of it because it already does half of what I want(it does 开发者_如何转开发the drawing).

The sample I have is using OpenGL for drawing and basically I don't care if it is OpenGL or CoreGraphics as soon as it does it.

The problem I have is how to put an image as a background of EAGLView I have in this sample code. My research gave me only suggestions for OpenGL experienced developers but not the working code snippet/solution. If somebody can help me with this I would be very appreciate.

What I need is just a sample of how to put a UIImage to EAGLView background so then the user can draw(already have the code) on top of it and save the result.


One usually doesn't mix OpenGL with ordinary UI... views. Also drawing a background image using OpenGL is trivial:

First you need to load the Image into a texture. In GLPaint a image file is loaded as brush-texture https://github.com/omeryavuz/glpaint/blob/master/Classes/PaintingView.m function initWithCoder

To draw a background, the first thing you draw after framebuffer clear is a fullscreen quad with that texture. If you build upong GLPaint, then the projection and modelview matrix and the vertex array state are set properly already. So it boils down to

GLfloat vert[] = {0,0, frame.size.width,0, frame.size.width,frame.size.height, 0,frame.size.height};
GLfloat tex[] = {0,0, 1,0, 1,1, 0,1};
GLuint indexes[] = {0, 1, 2, 2, 3, 0};

glBindTexture(GL_TEXTURE_2D, backgroundTexture);
glEnable(GL_TEXTURE_2D);    

glVertexPointer(2, GL_FLOAT, 0, vert);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, indexes);


In PaintingView.m, on line 89, set eaglLayer.opaque = NO;.

In your viewController, put a UIImageView or whatever behind the paintingView.

Note: This will probably decrese performance.

Note: It might not initially work; the OpenGL layer may overwrite itself with some sort of default background color before rendering a frame. EDIT: Line 304 in PaintingView.m: Try setting the color to glClearColor(1.0, 1.0, 1.0, 0.0);. I am not sure this works, and don't have time to test this. If it doesn't work, wait till Brad Larson comes around, sees your question, and answers it perfectly ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜