bobjamin
bobjamin

Reputation: 265

Need help to find bug in code with textures in Open GL ES 2.0 using shaders

Ive been using Open GL ES 2.0 Programming guide and am following chapter 9 about textures but ive set up the same pixel array (2x2- Red Green Blue Yellow) and it only shows red for the whole triangle. Cant seem to find the problem, i think my shaders are fine too please help my code follows.

- (void)setupGL
   {
    [EAGLContext setCurrentContext:self.context];

    [self loadShaders];

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_SRC_COLOR);

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gRectVertexData), gRectVertexData, GL_STATIC_DRAW);

// 2 x 2 Image, 3 bytes per pixel(R, G, B) 
GLubyte pixels[12] =
{
    255,0,0, // Red 
    0,255,0, // Green
    0,0,255, // Blue
    0,255,255 // Yellow 
};
    // Use tightly packed data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    // Generate a texture object 
glGenTextures(1, &texture[0]);
    // Bind the texture object 
glBindTexture(GL_TEXTURE_2D, texture[0]);
    // Load the texture
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, 2,2, 0, GL_RGB,
                 GL_UNSIGNED_BYTE, pixels);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    GLint SamplerLoc = glGetUniformLocation(_program, "Texture");
glUniform1i(SamplerLoc, 0);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));

glBindVertexArrayOES(0);

}

I have this in my load shaders part

glBindAttribLocation(_program, ATTRIB_VERTEX, "position");
glBindAttribLocation(_program, ATTRIB_NORMAL, "normal");
glBindAttribLocation(_program, ATTRIB_TEX, "TexCoordIn");

then my vertex shader and fragment shader are as follows

attribute vec4 position;
attribute vec3 normal;
attribute vec2 TexCoordIn;

varying lowp vec4 colorVarying;
varying vec2 TexCoordOut;

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
  vec3 eyeNormal = normalize(normalMatrix * normal);
  vec3 lightPosition = vec3(0.0, 0.0, 1.0);
  vec4 diffuseColor = vec4(0.8, 0.4, 1.0, 1.0);

  float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));

  colorVarying = diffuseColor*nDotVP;

  gl_Position = modelViewProjectionMatrix * position;
  TexCoordOut = TexCoordIn;
}

The Fragment Shader

varying lowp vec4 colorVarying;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;

void main()
{
lowp vec4 col = texture2D(Texture, TexCoordOut);
col.x *= colorVarying.x;
col.y *= colorVarying.x;
col.z *= colorVarying.x;
col.a = 1.0;
    gl_FragColor =  col;
}

Upvotes: 2

Views: 943

Answers (2)

Jeremy
Jeremy

Reputation: 26

Change your call

glBindAttribLocation(_program, ATTRIB_TEX, "TexCoordIn");

to use the GLKVertexAttribTexCoord0 instead of your ATTRIB_TEX enum.

This is an annoying gotcha in the OpenGL Game project. The enum members they use in their glBindAttribLocation calls for positions and normals happen to have the same values as the GLKVertexAttribPosition and GLKVertexAttribNormal enums, but this is just a coincidence.

Check out the post at headnoodles for step by step instructions. The example uses GLKit but everything else should be the same.

Upvotes: 1

djmj
djmj

Reputation: 5544

What happens if you just output your texture coordinates?

Do you see the gradient from [0-1]?

Upvotes: 0

Related Questions