我有一個尺寸爲320x320的正方形圖像,我從中創建了一個OpenGL紋理。我使用最基本的Vertext和Fragment着色器,我想在整個視圖中顯示紋理。視圖(從許多OpenGL iOS示例中的UIView派生而來的EAGLView)的大小也是320x320。爲什麼我的iPhone OpenGL-ES紋理不包含視口?
問題是,圖像繪製在左上角,只佔整個視圖的50%左右。它不包括100%的觀點。我不知道爲什麼?
這裏是我的代碼:
position = glGetAttribLocation(m_shaderProgram, "position");
inputTextureCoordinate = glGetAttribLocation(m_shaderProgram, "inputTextureCoordinate");
inputImageTexture = glGetUniformLocation(m_shaderProgram, "inputImageTexture");
static const GLfloat textureCoordinates[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
static const GLfloat imageVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
[EAGLContext setCurrentContext:context];
glViewport(0, 0, backingWidth, backingHeight); // These are 320, 320
glUseProgram(m_shaderProgram);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, sourceTextureID); // The texture is also of size 320x320
glUniform1i(inputImageTexture, 2);
glVertexAttribPointer(position, 2, GL_FLOAT, 0, 0, imageVertices);
glVertexAttribPointer(textureCoordinate, 2, GL_FLOAT, 0, 0, textureCoordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
Vertext着色器。
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
片段着色器。
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
void main()
{
gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
}
你考慮過iphone 4和2x尺寸因子嗎? (嘗試渲染它兩倍大,以彌補視網膜顯示) – Pochi 2012-08-13 08:25:29
我嘗試了不同的尺寸,而不是320.但結果是一樣的。我在這裏只是用320來舉例。 – abix 2012-08-13 08:48:28
你的意思是你已經試過改變視口嗎? cuz應該明確地使它看起來更大或更小 – Pochi 2012-08-13 08:51:19