我試圖渲染貼圖到屏幕的左下角,所以像:的OpenGL ES 2.0:渲染紋理屏幕沒有模型視圖或投影矩陣
glViewPort(0, 0, width/4, height/4);
我有執行該渲染的麻煩。我畫一個四邊形,做一些基本的東西。我已經試過渲染的所有變體爲在這些鏈接和其他人討論SO四:
How to draw a texture as a 2D background in OpenGL ES 2.0?
OpenGL ES 2.0 Rendering with a Texture
我的頂點和片段着色器如下:
const char * VERTEX_SHADER =
" \n\
attribute vec4 a_position; \n\
attribute vec4 a_texCoord; \n\
\n\
\n\
#ifdef GL_ES \n\
varying mediump vec2 v_texCoord; \n\
#else \n\
varying vec2 v_texCoord; \n\
#endif \n\
\n\
void main() \n\
{ \n\
gl_Position = a_position; \n\
v_texCoord = a_texCoord.xy; \n\
} \n\
";
const char * FRAGMENT_SHADER =
" \n\
#ifdef GL_ES \n\
precision lowp float; \n\
#endif \n\
\n\
varying vec2 v_texCoord; \n\
uniform sampler2D u_texture; \n\
\n\
void main() \n\
{ \n\
gl_FragColor = texture2D(u_texture, v_texCoord); \n\
} \n\
";
而我的渲染代碼是這樣的:
static const GLfloat squareVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
static const GLfloat textureVertices[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
// ...
glUseProgram(myProgram_);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewPort(0, 0, width/4, height/4); // width and height are defined elsewhere
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, myTexture_); // myTexture_ has been successfully loaded and checked
// Update uniform values
glUniform1i(textureUniformLocation_, 0);
// Update attribute values.
glEnableVertexAttribArray(a_position_location_);
glEnableVertexAttribArray(a_texcoord_location_);
glVertexAttribPointer(a_position_location_, 2, GL_FLOAT, 0, 0, squareVertices);
glVertexAttribPointer(a_texcoord_location_, 2, GL_FLOAT, 0, 0, textureVertices;
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
頂點和片段着色器編譯並且glProgram鏈接成功。我所得到的只是屏幕左下角的一個白色矩形(當然,在已經渲染的場景的頂部)。我在這裏錯過了什麼嗎?
你是什麼意思mytexture_已被檢查?檢查什麼?請貼上紋理加載代碼。 – Tim
我已將紋理作爲圖像保存到相冊中,並驗證它是正確的圖像。 – kevlar
試過glGetError?你的情況是什麼'framebuffer'? – Tim