将纹理图像渲染到四边形不正确

时间:2015-03-18 05:04:09

标签: c++ opengl sdl-2 texture-mapping

我正在尝试渲染图像,但是当程序运行时,我会得到构成四边形的两个三角形的扭曲线条:

的init()

GLfloat vertexData[] =    // Counter Clockwise
    {
        -0.5f, 0.5f, 0.0f,  //  V0 : Top Left
        -0.5f, -0.5f, 0.0f, //  V1 : Bottom Left
        0.5f, -0.5f, 0.0f,  //  v2 : Bottom Right
        0.5f, 0.5f, 0.0f    //  v3 : Top Right
    };


    GLint indexData[] =
    {
        0, 2, 3,
        2, 0, 1
    };

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


    int vSize = (sizeof(vertexData) / sizeof(vertexData[0]));
    int iSize = (sizeof(indexData) / sizeof(indexData[0]));

    // VAO
    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);

    // IBO
    glGenBuffers(1, &iboID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, iSize * sizeof(GLuint), indexData, GL_STATIC_DRAW);

    // VBO
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, vSize * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // Texture
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    SDL_FreeSurface(surface);


    _textureLoc = glGetUniformLocation(shader.getID(), "textureSampler");
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glUniform1i(_textureLoc, 0);

在draw()

glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glBindVertexArray(vaoID);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

0 个答案:

没有答案