0

I'm trying to render a texture to the bottom left corner of the screen, so something like:

glViewPort(0, 0, width/4, height/4);

I'm having trouble performing that rendering. I draw a quad and do some basic stuff. I've tried all variants of rendering to a quad as discussed in these links and others on SO:

How to draw a texture as a 2D background in OpenGL ES 2.0?

OpenGL ES 2.0 Rendering with a Texture

My vertex and fragment shaders are as follows:

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\
";

And my rendering code is this:

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

The vertex and fragment shaders compile and the glProgram links successfully. All I'm getting is a white rectangle in the bottom left of my screen (on top of the already rendered scene, of course). Am I missing something here?

Community
  • 1
  • 1
kevlar
  • 1,110
  • 3
  • 17
  • 30
  • 1
    What do you mean mytexture_ has been checked? Checked by what? Post texture loading code please. – Tim Jun 20 '12 at 00:43
  • I've saved the texture as an image to the photo album and verified that it's the correct image. – kevlar Jun 20 '12 at 01:01
  • Tried glGetError? What is 'framebuffer' in your case? – Tim Jun 21 '12 at 05:08
  • I'm not sure it's the problem, but have you tried passing z values (even 0) for a_position, and using a `vec2` for `a_texCoord`. Also make sure all tests (depth, scissor, etc.) are off. If `glGetError()` is 0, it's probably a test or clipping. – Todd Christensen Apr 19 '16 at 20:25

2 Answers2

0

Maybe you forgot to set your minification filter? Saving the texture as an image would probably not alert you of sampling errors like this.

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

Tim
  • 35,413
  • 11
  • 95
  • 121
  • I have those set up almost exactly like in the link :( Thanks for the suggestion though. – kevlar Jun 21 '12 at 05:02
  • @kevlar I think you should nonetheless post your texture loading code, you're problem does probably not lie in the code you posted. – rotoglup Jun 21 '12 at 06:33
0

You should change

attribute vec4 a_position;
attribute vec4 a_texCoord;

to

attribute vec2 a_position;
attribute vec2 a_texCoord;

and

gl_Position = a_position;             

to

gl_Position = vec4(a_position,0.0,1.0);
darius
  • 837
  • 9
  • 22