【问题标题】:OpenGL ES 2.0: Rendering a texture to screen without a ModelView or Projection matrixOpenGL ES 2.0:在没有 ModelView 或 Projection 矩阵的情况下将纹理渲染到屏幕
【发布时间】:2012-06-20 00:26:46
【问题描述】:

我正在尝试将纹理渲染到屏幕的左下角,例如:

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_ 已被检查是什么意思?通过什么检查?请发布纹理加载代码。
  • 我已将纹理作为图像保存到相册并验证它是正确的图像。
  • 试过 glGetError 吗?在您的情况下,“帧缓冲区”是什么?
  • 我不确定是不是问题,但您是否尝试过为 a_position 传递 z 值(甚至为 0),并为 a_texCoord 使用 vec2。还要确保所有测试(深度、剪刀等)都已关闭。如果glGetError() 为 0,则可能是测试或剪辑。

标签: opengl-es-2.0


【解决方案1】:

也许您忘记设置缩小过滤器?将纹理保存为图像可能不会提醒您此类采样错误。

http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

【讨论】:

  • 我的设置与链接中的几乎一模一样 :( 不过感谢您的建议。
  • @kevlar 我认为您仍然应该发布纹理加载代码,您的问题可能不在于您发布的代码。
【解决方案2】:

你应该改变

attribute vec4 a_position;
attribute vec4 a_texCoord;

attribute vec2 a_position;
attribute vec2 a_texCoord;

gl_Position = a_position;             

gl_Position = vec4(a_position,0.0,1.0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2014-10-28
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多