【问题标题】:How to draw a texture into a quad with OpenGL ES 2?如何使用 OpenGL ES 2 将纹理绘制成四边形?
【发布时间】:2011-03-24 16:41:26
【问题描述】:

OpenGL ES 2.0 中没有像 1.1 中那样包含四边形。使用 OpenGL ES 2.0 提供的可编程管道,我怎样才能将纹理(最初是 png 文件)简单地绘制成 4 个定义的点?

我需要 VBO 吗?我需要计算透视矩阵吗?所有这些对我来说都是一团糟......使用的平台是iPhone。

【问题讨论】:

    标签: iphone textures opengl-es-2.0


    【解决方案1】:

    我找到了答案。即使对于这样一个简单的任务,也必须使用透视矩阵和着色器。 This answer 帮我做到了。

    这是我使用的代码,函数 es*() 来自 this book

    // generate an orthographic matrix
    esMatrixLoadIdentity( &projectionMatrix );
    esOrtho(&projectionMatrix, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    // generate a model view
    esMatrixLoadIdentity(&modelviewMatrix);
    // compute the final MVP
    esMatrixMultiply(&modelviewProjectionMatrix, &modelviewMatrix, &projectionMatrix);
    // setting up the viewport
    glViewport(0, 0, width, height);
    // binding
    glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glClearColor(0, 0, 0, 0);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );   
    glEnable(GL_TEXTURE_2D);
    glActiveTexture(0);
    glBindTexture(GL_TEXTURE_2D, videoTexture);
    
    // setting up shader
    useProgram(projectionProgram);
    
    // updating uniform values
    GLint uMVPIndex      = indexUniform(projectionProgram, "mvpMatrix");
    GLint uTextureIndex  = indexUniform(projectionProgram, "textureImg");
    glUniformMatrix4fv( uMVPIndex, 1, GL_FALSE, (GLfloat*) &modelviewProjectionMatrix.m[0][0] );
    glUniform1i(uTextureIndex, 0);  
    
    // updating attribute values
    GLint aPositionIndex = indexAttribute(projectionProgram, "position");
    GLint aTextureIndex  = indexAttribute(projectionProgram, "inputTextureCoordinate");
    
    // drawing quad
    static int strideVertex  = 2*sizeof(GLfloat); // 2D position
    static int strideTexture = 2*sizeof(GLfloat); // 2D texture coordinates
    
    // beginning of arrays
    const GLvoid* vertices  = (const GLvoid*) &screenVertices[0];
    const GLvoid* textures  = (const GLvoid*) &texCoordsFullScreen [0];
    
    // enabling vertex arrays  
    glVertexAttribPointer(aPositionIndex, 2, GL_FLOAT, GL_FALSE, strideVertex, vertices);
    glEnableVertexAttribArray(aPositionIndex);
    glVertexAttribPointer(aTextureIndex, 2, GL_FLOAT, GL_FALSE, strideTexture, textures);
    glEnableVertexAttribArray(aTextureIndex);
    
    // drawing
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    // resetting matrices
    esMatrixLoadIdentity(&projectionMatrix);
    esMatrixLoadIdentity(&modelviewMatrix);
    esMatrixLoadIdentity(&modelviewProjectionMatrix);
    // resetting shader
    releaseProgram(projectionProgram);
    // unbinding texture
    glBindTexture(GL_TEXTURE_2D, 0);
    

    【讨论】:

      【解决方案2】:

      代替顶点为 1、2、3、4 的四边形,分别绘制顶点为 1、2、4 和 4、2、3 的两个三角形。

      【讨论】:

      • 为什么不是 1,2,3 和 3,4,1?
      【解决方案3】:

      【讨论】:

      • 谢谢,我知道这段代码,但它只是在帧缓冲区中绘制纹理并显示它,它没有几何变换......
      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多