Iphone OpenGL渲染问题

Iphone OpenGL渲染问题,iphone,objective-c,opengl-es,Iphone,Objective C,Opengl Es,(也在苹果开发者论坛上提问) 我似乎有一个OpenGL的渲染问题。我的应用程序应该在用户用手指在屏幕上画一条线。因此,touchesMoved调用下面的draw函数,并传递一个包含点数据的NSMutableArray currentStroke 我遇到的问题是,为每条线绘制的最后一个三角形(每条线以touchesend结尾)闪烁。我认为这与渲染缓冲区有关。。。如果我从touchesend调用draw函数两次,闪烁就会消失。我只是觉得这是一个非常非常巧妙的解决方案。有人知道是什么引起了闪烁,并找到

(也在苹果开发者论坛上提问)

我似乎有一个OpenGL的渲染问题。我的应用程序应该在用户用手指在屏幕上画一条线。因此,touchesMoved调用下面的draw函数,并传递一个包含点数据的NSMutableArray currentStroke

我遇到的问题是,为每条线绘制的最后一个三角形(每条线以touchesend结尾)闪烁。我认为这与渲染缓冲区有关。。。如果我从touchesend调用draw函数两次,闪烁就会消失。我只是觉得这是一个非常非常巧妙的解决方案。有人知道是什么引起了闪烁,并找到了解决办法吗

- (void)draw {

glLoadIdentity();

//Configuring OpenGL
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);



//Texture Loading
GLuint      texture[1];
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);

//Configuring Image for OpenGL Texture: Switch off mipmap; Set linear scaling
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

//Set image to texture
NSString *path = [[NSBundle mainBundle] pathForResource:@"at2" ofType:@"png"];
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:texData];
    if (image == nil)
        NSLog(@"No Image");

    GLuint width = CGImageGetWidth(image.CGImage);
    GLuint height = CGImageGetHeight(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc( height * width * 4 );
    CGContextRef context2 = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast |kCGBitmapByteOrder32Big );
    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( context2, CGRectMake( 0, 0, width, height ) );
    CGContextTranslateCTM( context2, 0, height - height );
    CGContextDrawImage( context2, CGRectMake( 0, 0, width, height ), image.CGImage );

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    CGContextRelease(context2);

    free(imageData);
    [image release];
    [texData release];

static const GLfloat texCoords[] = {
        0.0, 1.0,
        1.0, 1.0,
        1.0, 0.0,
        0.0, 0.0
 };




glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

GLfloat vertices[8];
GLfloat xadjust;
GLfloat yadjust;

//Iterate through points and create triangles

if ( (currentStroke != nil) && ([currentStroke count] > 1)) {

  for (int i=0; i<([currentStroke count]-1); i++) {
    vertices[0] = [[currentStroke objectAtIndex:i] CGPointValue].x;
    vertices[1] = self.frame.size.height - [[currentStroke objectAtIndex:i] CGPointValue].y;
    vertices[2] = [[currentStroke objectAtIndex:i+1] CGPointValue].x;
    vertices[3] = self.frame.size.height - [[currentStroke objectAtIndex:i+1] CGPointValue].y;

    //Minimum width of triangle
    xadjust = -4;


    //Minimum height of triangle, increase of y aspect of triangle is short
    if ((vertices[3]-vertices[1])<1&&(vertices[3]-vertices[1])>=0) {
      yadjust = 2;
    } else if ((vertices[3]-vertices[1])>-1&&(vertices[3]-vertices[1])<=0){ 
      yadjust = -3;
    } else  {
      yadjust = 2;
    }

    vertices[4] = [[currentStroke objectAtIndex:i] CGPointValue].x+xadjust;
    vertices[5] = self.frame.size.height - [[currentStroke objectAtIndex:i] CGPointValue].y+yadjust;
    vertices[6] = [[currentStroke objectAtIndex:i+1] CGPointValue].x+xadjust;
    vertices[7] = self.frame.size.height - [[currentStroke objectAtIndex:i+1] CGPointValue].y+yadjust;

    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);

    glVertexPointer(2, GL_FLOAT, 0, vertices);

    glEnableClientState(GL_VERTEX_ARRAY);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


  }




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


  glDisable(GL_BLEND);
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);

}
-(无效)绘图{
glLoadIdentity();
//配置OpenGL
glEnable(GL_纹理_2D);
glEnable(GL_混合物);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去GL_SRC_ALPHA);
//纹理加载
胶合纹理[1];
glGenTextures(1,&纹理[0]);
glBindTexture(GL_TEXTURE_2D,纹理[0]);
//为OpenGL纹理配置图像:关闭mipmap;设置线性缩放
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);
glTexParameteri(GL_纹理2D、GL_纹理MAG_过滤器、GL_线性);
//将图像设置为纹理
NSString*path=[[NSBundle mainBundle]pathForResource:@“at2”类型:@“png”];
NSData*texData=[[NSData alloc]initWithContentsOfFile:path];
UIImage*image=[[UIImage alloc]initWithData:texData];
如果(图像==nil)
NSLog(@“无图像”);
GLuint width=CGImageGetWidth(image.CGImage);
GLuint height=CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
void*imageData=malloc(高度*宽度*4);
CGContextRef context2=CGBitmapContextCreate(图像数据、宽度、高度、8,4*宽度、颜色空间、KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextClearRect(context2,CGRectMake(0,0,宽度,高度));
CGContextTranslateCm(context2,0,高度-高度);
CGContextDrawImage(context2,CGRectMake(0,0,宽度,高度),image.CGImage);
GLTEXAGE2D(GL_纹理_2D,0,GL_RGBA,宽度,高度,0,GL_RGBA,GL_无符号_字节,图像数据);
CGContextRelease(context2);
免费(图像数据);
[图片发布];
[数据发布];
静态常数GLfloat texCoords[]={
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
0.0, 0.0
};
glEnableClientState(GL_顶点_数组);
glEnableClientState(GL_普通_阵列);
glEnableClientState(GL_纹理_坐标_阵列);
GLfloat顶点[8];
乙二醛;
glyadjust;
//迭代点并创建三角形
如果((当前行程!=nil)&([currentStroke count]>1)){

对于(inti=0;i-1&(顶点[3]-顶点[1])来说,与问题没有直接关系,但是每次画图时都会加载图像,不是吗?(或者可能不会,但我对objective-c的语法感到困惑)如果这样做,那就不是一件好事。尤其是initWithData:texData和glTexImage2D应该只在draw()之外执行一次

对于闪烁,它通常是一个双缓冲问题,但在iPhone上它总是一个双缓冲

不过,我想你应该在真正的抽签之前给glBindRenderbufferOES打电话。

哦,我想我知道了

for (int i=0; i<([currentStroke count]-1); i++)
    vertices[2] = [[currentStroke objectAtIndex:i+1] CGPointValue].x;
for(int i=0;i超出范围

对于(int i=0;i 2…

当我发现它时,应该发布它,但晚发布总比不发布好。通过将KeaglDrawablePropertyRetaineBacking设置为true,它停止闪烁。当我只使用一次glClear()时,应用程序也会闪烁(必须使用两次),但设置保留背景属性也解决了这个问题。我仍然不完全确定为什么会发生闪烁。这显然是一个双缓冲区问题,因为它每隔一帧闪烁一次。

哦,我将其移到绘图方法中进行错误检查,但忘了将其移出。起初我担心这是一个纹理问题。Th在初始页面创建时,e图像加载发生在之前。我尝试在调用之前抛出glBindRenderbufferOES,但它似乎没有做任何事情。。。