2016-03-01 89 views
0

我想設置我的頂點和紋理座標,使得我有一個覆蓋整個OpenGL窗口的Quad(2個三角形)。我想從我的相機接收圖像時更新紋理。OpenGL ES 2.0紋理貼圖在樹莓派上填充Quad

它的工作原理除了我的流紋理永遠不會填充整個屏幕,並且紋理像素在預期紋理之外重複使用(請參見下面的圖像)。

如何設置我的頂點和紋理座標,以便我的紋理填充四邊形而不重複?

這是我如何生成我的質地:

glGenTextures(1, &m_dataFrame); 
    glBindTexture(GL_TEXTURE_2D, m_dataFrame); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, frame.bits()); 
    // CUSTOM 
    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_CLAMP_TO_EDGE); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

初步嘗試:

// Setup the vertices and texture coordinates 
float scaler = 1.0f; 
static const GLfloat squareVertices[] = { 
    -(1.0f/scaler), -(1.0f/scaler), 0.0f, // First triangle bottom left 
    (1.0f/scaler), -(1.0f/scaler), 0.0f, 
    -(1.0f/scaler), (1.0f/scaler), 0.0f, 
    (1.0f/scaler), -(1.0f/scaler), 0.0f, // Second triangle bottom right 
    (1.0f/scaler), (1.0f/scaler), 0.0f, 
    -(1.0f/scaler), (1.0f/scaler), 0.0f, 
}; 
static const GLfloat textureVertices[] = { 
    -1.0f, -1.0f, 
    1.0f, -1.0f, 
    -1.0f, 1.0f, 
    1.0f, -1.0f, 
    1.0f, 1.0f, 
    -1.0f, 1.0f, 
}; 

這將導致如下圖: enter image description here

// Setup the vertices and texture coordinates 
float x = 1.0f; 
float y = 1.0f; 
static const GLfloat squareVertices[] = { 
    -(x*2), -(y*2), 0.0f, // First triangle bottom left 
    (x*2), -(y*2), 0.0f, 
    -(x*2), (y*2), 0.0f, 
    (x*2), -(y*2), 0.0f, // Second triangle bottom right 
    (x*2), (y*2), 0.0f, 
    -(x*2), (y*2), 0.0f, 
}; 
static const GLfloat textureVertices[] = { 
    -1.0f, -1.0f, 
    1.0f, -1.0f, 
    -1.0f, 1.0f, 
    1.0f, -1.0f, 
    1.0f, 1.0f, 
    -1.0f, 1.0f, 
}; 

這將導致以下圖片: enter image description here

+0

機會是你的紋理座標設置被打破。但是你並沒有在那裏發現代碼。 –

回答

2

這是我用來渲染Raspberry Pi上的紋理四元組(在Swift中,但語法類似於您用於C的語法)。設置紋理參數:

glEnable(GLenum(GL_TEXTURE_2D)) 
    glGenTextures(1, &cameraOutputTexture) 
    glActiveTexture(GLenum(GL_TEXTURE0)); 
    glBindTexture(GLenum(GL_TEXTURE_2D), cameraOutputTexture) 
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST) 
    glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_NEAREST) 

上傳質地:

glActiveTexture(GLenum(GL_TEXTURE0)) 
    glBindTexture(GLenum(GL_TEXTURE_2D), cameraOutputTexture) 
    glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGB, 1280, 720, 0, GLenum(GL_RGB), GLenum(GL_UNSIGNED_BYTE), buffers[Int(currentBuffer)].start) 

,並使其:

glClear(GLenum(GL_COLOR_BUFFER_BIT)) 
shader.use() 

let squareVertices:[GLfloat] = [ 
    -1.0, -1.0, 
    1.0, -1.0, 
    -1.0, 1.0, 
    1.0, 1.0, 
] 

let textureCoordinates:[GLfloat] = [ 
    0.0, 1.0, 
    1.0, 1.0, 
    0.0, 0.0, 
    1.0, 0.0, 
] 

glVertexAttribPointer(positionAttribute, 2, GLenum(GL_FLOAT), 0, 0, squareVertices) 
glVertexAttribPointer(textureCoordinateAttribute, 2, GLenum(GL_FLOAT), 0, 0, textureCoordinates) 

glUniform1i(GLint(textureCoordinateAttribute), 1) 

glDrawArrays(GLenum(GL_TRIANGLE_STRIP), 0, 4) 
eglSwapBuffers(display, surface) 

我可能會旋轉圖像在相機佔旋轉,我作爲輸入源使用,所以如果看到圖像渲染顛倒,您可能需要調整頂點或座標。