OpenGL ES 2.0 - Textured Quad

前端 未结 1 1102
抹茶落季
抹茶落季 2021-01-03 16:56

Whenever I attempt to render a textured quad, I end up with a triangular section of the texture distorted: .

The texture is a PNG created in GIMP, and I\'ve tried

相关标签:
1条回答
  • 2021-01-03 17:24

    It looks like your texture coordinates may be wrong (notice the texture appears to be wrapping around the left side).

    Here is a snippet of code that I've used in the past:

    const float quadPositions[] = {  1.0,  1.0, 0.0, 
                                    -1.0,  1.0, 0.0, 
                                    -1.0, -1.0, 0.0, 
                                    -1.0, -1.0, 0.0, 
                                     1.0, -1.0, 0.0, 
                                     1.0,  1.0, 0.0 };
    const float quadTexcoords[] = { 1.0, 1.0, 
                                    0.0, 1.0, 
                                    0.0, 0.0, 
                                    0.0, 0.0, 
                                    1.0, 0.0, 
                                    1.0, 1.0 };
    
    // stop using VBO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    
    // setup buffer offsets
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), quadPositions);
    glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), quadTexcoords);
    
    // ensure the proper arrays are enabled
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glEnableVertexAttribArray(ATTRIB_TEXCOORD0);
    
    // draw
    glDrawArrays(GL_TRIANGLES, 0, 2*3);
    

    That should draw a two triangles at z=0. You'll want to setup your projection from -1 to 1 in width and height.

    Edit:

    Here's a working version of your code:

    const GLfloat texices[] = { 0, 0,
                                0, 1,
                                1, 1,
                                1, 0 };
    
    
    const GLfloat vertices[] = { -1, -1, 0,  // bottom left corner
                                 -1,  1, 0,  // top left corner
                                  1,  1, 0,  // top right corner
                                  1, -1, 0}; // bottom right corner
    
    const GLubyte indices[] = { 0, 2, 1,     // first triangle (bottom left - top left - top right)
                                0, 3, 2 };
    
    
    // ensure no VBOs or IBOs are bound
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);    
    
    glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), vertices);
    glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), texices);
    
    // ensure the proper arrays are enabled
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glEnableVertexAttribArray(ATTRIB_TEXCOORD0);
    
    glDisable(GL_CULL_FACE);
    
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
    
    0 讨论(0)
提交回复
热议问题