2014-12-04 191 views
0

我正在學習如何在OpenGL中紋理四邊形。這個想法是讓圖像在整個四邊形上重複,但是我正在拉伸圖像並且形成四邊形的兩個三角形是可見的。用平鋪圖像渲染四邊形?

我錯過了什麼?代碼如下所示

void GLViewer::initializeGL() 
{ 
    float vertices[] = 
    { 
     -0.5, 0.0, 0.0, 
     0.5, 0.0, 0.0, 
     0.5, 0.5, 0.0 
    }; 

    // Setup viewport 
    glViewport(0, 0,this->width(), this->height()); 

    // OpenGL configuration 
    glClearColor(0.0f,0.0f,0.0f,1.0f); 

    // Setup shaders 
    m_triangleShader.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl"); 
    m_triangleShader.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl"); 
    m_triangleShader.link(); 

    glGenVertexArrays(1, &m_VAO); 
    glBindVertexArray(m_VAO); 

    glPointSize(10); 

    glGenBuffers(1, &m_VBO); 
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glBindVertexArray(0); 

    float quad[] = 
    { 
     -1.0, -1.0, 
     1.0, -1.0, 
     -1.0, 1.0, 
     1.0, 1.0 
    }; 

    float uv[] = 
    { 
     0.0, 0.0, 
     1.0, 0.0, 
     1.0, 1.0, 
     0.0, 1.0 
    }; 

    m_backgroundShader.addShaderFromSourceFile(QGLShader::Vertex, ":/vbackground.glsl"); 
    m_backgroundShader.addShaderFromSourceFile(QGLShader::Fragment, ":/fbackground.glsl"); 
    m_backgroundShader.link(); 
    glGenVertexArrays(1, &m_backgroundVAO); 
    glBindVertexArray(m_backgroundVAO); 

    glGenBuffers(1, &m_quadVBO); 
    glBindBuffer(GL_ARRAY_BUFFER, m_quadVBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); 

    glGenBuffers(1, &m_uvVBO); 
    glBindBuffer(GL_ARRAY_BUFFER, m_uvVBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(uv), uv, GL_STATIC_DRAW); 
    glEnableVertexAttribArray(1); 
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); 

    glGenTextures(1, &m_textureID); 
    glBindTexture(GL_TEXTURE_2D, m_textureID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    QImage image(":/background.png"); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 
       0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits()); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glBindVertexArray(0); 
} 

void GLViewer::paintGL() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glDepthMask(GL_TRUE); 
    glEnable(GL_DEPTH_TEST); 

    m_backgroundShader.bind(); 
    glBindVertexArray(m_backgroundVAO); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, m_textureID); 
    glUniform1i(glGetUniformLocation(m_backgroundShader.programId(),"tex"),0); 
    glDrawArrays(GL_TRIANGLE_STRIP,0,4); 

    m_triangleShader.bind(); 
    glBindVertexArray(m_VAO); 
    glDrawArrays(GL_TRIANGLES, 0, 3); 

    update(); 
} 

回答

0

爲什麼你期望你的紋理被你正在使用的代碼重複(以及多少次)?

但是,代碼與您的觀察一致。您正在使用[0,1]範圍內的texcoords,因此如果在採樣紋理之前您沒有調整texcoords,則應該只能看到圖像被固定到四邊形。你可以通過你迄今尚未粘貼的着色器來做到這一點,可能。如果您希望紋理沿v沿u尺寸平鋪出現n-時間平鋪,m時間,則v需要分別使用[0,n]v範圍使用[0,m]

第二個問題是您的texcoords與您的幾何圖形不匹配。這可能會導致您形容爲「形成四邊形的兩個三角形可見」。你的兩個trianges不堆積在紋理空間四你所期望的,你應該換的最後兩行:

float uv[] = 
{ 
    0.0, 0.0, 
    1.0, 0.0, 
    1.0, 1.0,  /* should be 0.0, 1.0 */ 
    0.0, 1.0  /* should be 1.0, 1.0 */ 
}; 
+0

其實我可以強制平鋪未做着色器的東西,如果我只是讓我的'uv'座標大於範圍[0,0] x [1,1]。 – BRabbit27 2014-12-04 19:46:37