如何使用OpenGL ES 2将纹理绘制成四边形? [英] How to draw a texture into a quad with OpenGL ES 2?

查看:177
本文介绍了如何使用OpenGL ES 2将纹理绘制成四边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

四元组不包含在OpenGL ES 2.0中,因为它们在1.1中。使用OpenGL ES 2.0提供的可编程管道,我怎样才能简单地将纹理(最初是一个png文件)绘制成4个定义的点?

Quads are not included in OpenGL ES 2.0 as they were in 1.1. With the programmable pipeline offered by OpenGL ES 2.0, how can I simply draw a texture (originally a png file) into 4 defined points?

我需要一个VBO吗?我需要计算透视矩阵吗?所有这些对我来说都很混乱...使用的平台是iPhone。

Do I need a VBO? Do I need to compute a perspective matrix? All these things are messy for me... The platform used is the iPhone.

推荐答案

我找到了答案。即使对于这样一个简单的任务,也必须使用透视矩阵和着色器。 这个答案帮我做了它。

I found the answer. It is mandatory to use a perspective matrix and a shader, even for such a simple task. This answer helped me to do it.

这是我使用的代码,函数 es *()来自这本书

Here is the code I used, with functions es*() from this book:

// 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);

这篇关于如何使用OpenGL ES 2将纹理绘制成四边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆