簡體   English   中英

opengles顯示圖像的問題

[英]Problem with opengles to show an image

我使用xcode Opengl App模板創建示例。 我是opengles的新手,不得不嘗試重寫ES1Renderer.m中的'render'方法。我嘗試創建一個紋理並在屏幕上顯示它,但沒有顯示。 有人可以幫幫我嗎? 我不知道如何解決它:

- (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];
}

遺憾的是,Xcode提供的OpenGL模板在某些時候發生了變化 - 當前代碼(從Xcode 3.2.5開始,使用'OpenGL ES Application'模板創建iOS應用程序)不再提供單獨的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];
}

結果完全像你想要的那樣起作用。 猜測,是否有可能:

  • 你設置了除標識之外的其他東西作為投影矩陣,導致你的幾何圖形被裁剪,因為它被置於z = 0?
  • 您是否已經忽略了放棄嘗試ES 2渲染,導致意外結果,因為紋理渲染等任務與ES 2的硬連線方式不同,與ES1相同?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM