2011-03-24 161 views
6

OpenGL ES 2.0中不包含四元組,因爲它們在1.1中。使用OpenGL ES 2.0提供的可編程流水線,我怎樣才能將紋理(最初是一個PNG文件)繪製成4個定義的點?如何使用OpenGL ES 2將紋理繪製爲四邊形?

我需要一個VBO嗎?我是否需要計算透視矩陣?所有這些對我來說都是混亂的......使用的平臺是iPhone。

回答

9

我找到了答案。即使對於這樣一個簡單的任務,也必須使用透視矩陣和着色器。 This answer幫我做到了。

這裏是this book我使用的代碼,以功能es*()

// 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。

+0

爲什麼不是1,2,3和3,4,1? – rmp251 2014-03-27 00:52:11

相關問題