2010-12-16 3 views
0

xcode OpenGL App 템플릿을 사용하여 샘플을 만듭니다. opengles에 익숙하지 않고 ES1Renderer.m에서 'render'메서드를 다시 작성해야합니다. 텍스처를 만들고 화면에 표시하려고했지만 아무 것도 표시되지 않았습니다. 누군가 나를 도울 수 있습니까? '는 OpenGL ES 응용 프로그램과 아이폰 OS 응용 프로그램을 작성, 엑스 코드 3.2.5 기준으로 현재 코드 (- 슬프게도 엑스 코드에서 제공하는 OpenGL을 템플릿 어떤 시점에서 변경이미지 표시를위한 오픈글 문제

- (void)render 
{ 

    int imageW = 16; 
    int imageH = 16; 
    GLubyte *textureData = (GLubyte *) malloc(imageW * imageH << 2); 

    for (int i = 0; i < imageW * imageH << 2; i++) { 
     textureData[i]= 0xff & i; 
    } 

    GLuint textureId; 

    glGenTextures(1, &textureId); 
    glBindTexture(GL_TEXTURE_2D, textureId); 

    // when texture area is small, bilinear filter the closest mipmap 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
        GL_LINEAR ); 
    // when texture area is large, bilinear filter the original 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    // the texture wraps over at the edges (repeat) 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); 

    GLenum err = glGetError(); 
    if (err != GL_NO_ERROR) 
     NSLog(@"Error uploading texture. glError: 0x%04X", err); 

    free(textureData); 


    float x = 10.0f; 
    float y = 10.0f; 
    float z = 0.0f; 

    float scaleX = 1.0f; 
    float scaleY = 1.0f; 
    float scaleZ = 1.0f; 

    int w = imageW /2; 
    int h = imageH /2; 

    const GLfloat squareVertices[] = { 
     -w, -h, 
     w, -h, 
     -w, h, 
     w, h, 
    }; 

    const GLfloat textureCoords[] = { 
     0, 0, 
     1, 0, 
     0, 1, 
     1, 1, 
    }; 

    glEnable(GL_TEXTURE_2D); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
    glEnableClientState(GL_VERTEX_ARRAY); 

    glBindTexture(GL_TEXTURE_2D, textureId); 

    glVertexPointer(2, GL_FLOAT, 0, squareVertices); 
    glTexCoordPointer(2, GL_FLOAT, 0, textureCoords); 

    glPushMatrix(); 
    glTranslatef(x, y, z); 
    glScalef(scaleX, scaleY, scaleZ); 
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 
    glPopMatrix(); 


    glDisable(GL_TEXTURE_2D); 
    glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
    glDisableClientState(GL_VERTEX_ARRAY); 


    NSLog(@"-->"); 
    glDeleteTextures(1, &textureId); 

    // This application only creates a single color renderbuffer which is already bound at this point. 
    // This call is redundant, but needed if dealing with multiple renderbuffers. 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 

답변

2

: 나는 그것을 해결하는 방법을 몰라 '템플리트)는 더 이상 별도의 ES1Renderer.m 및 ES2Renderer.m을 제공하지 않으며, 단순화 된 EAGLView.m을 제공하고 GLTestViewController.m에서 런타임 테스트를 수행합니다. 염두에두고, 나는 수정 GLTestViewController.m의 awakeFromNib 더 이상 ES 2 컨텍스트 얻기 위해 시도 :

- (void)awakeFromNib 
{ 
    EAGLContext *aContext = nil;//[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

    if (!aContext) 
    { 
     aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 
    } 

    if (!aContext) 
     NSLog(@"Failed to create ES context"); 
    else if (![EAGLContext setCurrentContext:aContext]) 
     NSLog(@"Failed to set ES context current"); 

    self.context = aContext; 
    [aContext release]; 

    [(EAGLView *)self.view setContext:context]; 
    [(EAGLView *)self.view setFramebuffer]; 

    if ([context API] == kEAGLRenderingAPIOpenGLES2) 
     [self loadShaders]; 

    animating = FALSE; 
    animationFrameInterval = 1; 
    self.displayLink = nil; 
} 

을 그리고 drawFrame에 코드의 관련 부분을 복사하여 붙여 넣기 :

- (void)drawFrame 
{ 
    [(EAGLView *)self.view setFramebuffer]; 

    // Replace the implementation of this method to do your own custom drawing. 
    static const GLfloat squareVertices[] = { 
     -0.5f, -0.33f, 
     0.5f, -0.33f, 
     -0.5f, 0.33f, 
     0.5f, 0.33f, 
    }; 

    const GLfloat textureCoords[] = { 
     0, 0, 
     1, 0, 
     0, 1, 
     1, 1, 
    }; 
    static float transY = 0.0f; 

    glClearColor(0.5f, 0.5f, 0.5f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 

    int imageW = 16; 
    int imageH = 16; 
    GLubyte *textureData = (GLubyte *) malloc(imageW * imageH << 2); 

    for (int i = 0; i < imageW * imageH << 2; i++) { 
     textureData[i]= 0xff & i; 
    } 

    GLuint textureId; 

    glGenTextures(1, &textureId); 
    glBindTexture(GL_TEXTURE_2D, textureId); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);   
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); 
    free(textureData); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f); 
    transY += 0.075f; 

    glEnable(GL_TEXTURE_2D); 

    glVertexPointer(2, GL_FLOAT, 0, squareVertices); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glTexCoordPointer(2, GL_FLOAT, 0, textureCoords); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

    glDeleteTextures(1, &textureId); 

    [(EAGLView *)self.view presentFramebuffer]; 
} 

결과 당신이 의도 한 것처럼 완전히 작동합니다. 짐작할 수 있겠지만 다음 중 하나 일 수도 있습니다.

  • 영사 매트릭스로 ID 이외의 값을 설정하면 z = 0에 배치되어 지오메트리가 잘릴 수 있습니까?
  • 당신은 ES 2에서 렌더링 시도를 포기하는 것을 제대로 무시했습니다. 텍스쳐 렌더링과 같은 작업이 ES 2에서 ES1과 동일한 방식으로 고정되어 있지 않기 때문에 예기치 않은 결과가 발생 했습니까?