请帮我在EaglView中设置背景图片。这是我的源代码与图像:
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
brushImage = [UIImage imageNamed:@"Particle.png"].CGImage;
width = CGImageGetWidth(brushImage);
height = CGImageGetHeight(brushImage);
if(brushImage) {
brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, width, height, 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);
}
self.contentScaleFactor = 1.0;
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
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);
glBlendFunc(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);`
答案 0 :(得分:4)
不是使用点精灵,而应该只绘制纹理四边形。
GLfloat vert_tex[] = {
0., 0.,
1., 0.,
1., 1.,
0., 1.
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vert_tex);
glTexCoordPointer(2, GL_FLOAT, 0, vert_tex);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_name);
glDrawArrays(GL_QUADS, 0, 4);
EDIT2:
// Erases the screen
- (void) erase
{
[EAGLContext setCurrentContext:context];
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
GLfloat vert_tex[] = {
0., 0.,
frame.size.width * scale, 0.,
frame.size.width * scale, frame.size.height * scale,
0., frame.size.height * scale
};
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vert_tex);
glTexCoordPointer(2, GL_FLOAT, 0, vert_tex);
// Setup the view port in Pixels
glMatrixMode(GL_PROJECTION);
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);
glLoadIdentity();
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
// background_image texture must be initialized with the desired
// content; see initWithCoder of original GLPaint example for
// example code how to load textures.
glBindTexture(GL_TEXTURE_2D, background_image);
// Clear the buffer with background image
glDrawArrays(GL_QUADS, 0, 4);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
在renderLineFromPoint
中,您必须添加一条线以在绘制之前绑定画笔纹理:
// Drawings a line onscreen based on where the user touches
- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
/* ... */
// add this line here v v v v v v v v v v v
glBindTexture(GL_TEXURE_2D, brushTexture);
// ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];