在ipad上用openGl ES,我可以描画1024*768的图,是通过glTexImage2D把图片的RGB数据(扩充到1024*1024的大小)描画在1020*1024的画布上。但是现在要画640*480的图片。描画多幅后会死机。下面是描画640*480的图片的代码,求指教
const GLfloat spriteVertices[] = {//顶点坐标
    -1.0f, 1.67f,
    -1.0f, -1.0f,
    1.0f, -1.0f,
    1.0f, 1.67f,
};const GLfloat spriteTexcoords[] = {//纹理坐标
    0, 1.0,
    0, 0,
    1, 0,
    1, 1.0,
};//初始化
- (void)initTextureAndBuffer
{
    mDataPtr = (GLubyte *) calloc(1024 * 1024 * 4, sizeof(GLubyte));
    
    glViewport(0, 0, 1024, 768);
glMatrixMode(GL_PROJECTION);    glLoadIdentity();
    glOrthof(-1.0f, 1.0, -1.0f, 1.0f, 0, 0);
glMatrixMode(GL_MODELVIEW);
    
    glVertexPointer(2, GL_FLOAT, 0, spriteVertices);    // 纹理映射在窗口中的对应的顶点信息
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, spriteTexcoords); // 纹理需要被映射部分的坐标
glEnableClientState(GL_TEXTURE_COORD_ARRAY);    glGenTextures(1, &spriteTexture);
    glBindTexture(GL_TEXTURE_2D, spriteTexture);
    
    // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
    // Enable use of the texture
    glEnable(GL_TEXTURE_2D);
}//下面开始一帧帧的描画分辨率为640*480的图片到1024*768的屏幕上
- (void)drawImageWithOpenGL:(UIImage *)image
{
    CGImageRef      spriteImage = image.CGImage;
CGContextRef    spriteContext;
size_t          width, height;    if (spriteImage == NULL) {
        return;
    }
    
    width = CGImageGetWidth(spriteImage);
height = CGImageGetHeight(spriteImage);
    width = 1024;
height = 768;
    
    // CoreGraphics draw
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(spriteImage);
    
    //spriteData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
    memset(mDataPtr, 0, sizeof(mDataPtr));
    spriteContext = CGBitmapContextCreate(mDataPtr, width, height,
                                          8, width * 4, colorSpace,
                                          kCGImageAlphaPremultipliedLast);
    
    CGContextClearRect(spriteContext, CGRectMake(0, 0, 1024, 768));
    CGContextTranslateCTM(spriteContext, 0.0, height);
    CGContextScaleCTM(spriteContext, 1.0, -1.0);
    CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)1024, (CGFloat)768),spriteImage);
    CGContextRelease(spriteContext);
    
    glPushMatrix();
    //glScalef (1.6, 1.6, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, (mDataPtr));
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
[context presentRenderbuffer:GL_RENDERBUFFER_OES]; // present two part draw
    glPopMatrix();
}
现在的问题是,如果我画一张图片的话,没有问题。但是连续画多张的时候会死机,出现的crash信息是argb32_sample_RGB24。并且描画的图片上面还有很宽的黑条。我觉得上面的方法不怎么对,都没有进行缩放,还能显示出来。不知道是不是导致后面死机的原因 

解决方案 »

  1.   

    但是连续画多张的时候会死机,怀疑你内存空间没有处理好,另外有可能的是,有些gl函数调用后,可能还有对应的成对函数你没有调用。
      

  2.   

    内存应该没有什么问题,我通过内存检查工具看,申请的内存很稳定。还有某些gl函数没有成对出现。这个也不太可能。因为我用这种方法画分辨率为1024*768的图片时,都是没有问题的。我怀疑是把640*480放大显示在1024*768上时出的问题。不知道上面的做法对不对