简体   繁体   中英

Draw texture on touch in openGL iphone

Recently started with OpenGL, I have managed to draw a background image on screen and frame buffer. All works fine. Problem comes when I create new texture and add it to frame buffer.

{

 [EAGLContext setCurrentContext:context]; glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); glViewport(0, 0, backingWidth, backingHeight); // Clear screen glClear(GL_COLOR_BUFFER_BIT); // Render player ship [playerShip drawAtPoint:CGPointMake(160, 240)]; // glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); // [context presentRenderbuffer:GL_RENDERBUFFER_OES]; } 

If I uncomment the above two lines

// glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

// [context presentRenderbuffer:GL_RENDERBUFFER_OES];

I dont get new texture which I am drawing through below code:

NSInteger myDataLength = 20 * 20 * 4; GLubyte *buffer = (GLubyte *) malloc(myDataLength); glReadPixels(location.x-10, location.y-10, 20, 20, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < 20; y++)
{
    for(int x = 0; x < 20 * 4; x++)
    {
        buffer2[(19 - y) * 20 * 4 + x] = buffer[y * 4 * 20 + x];
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 20;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef imageRef = CGImageCreate(20, 20, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
Texture2D *texture = [[Texture2D alloc] initWithImage:myImage];
UIImageWriteToSavedPhotosAlbum(myImage, self, nil, nil);

  glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
[texture drawAtPoint:location];
glPopMatrix();

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

glPushMatrix();
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

I dont know where I am doing mistake. I am new to this.

If you are still rendering the background texture in your code and then render the space ship on top of it, the space ship might fail the depth buffer test.

Have you tried out:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

to clear the z-buffer and then rendering the spaceship?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM