Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Here are my shaders...

 GLbyte vShaderStr[] = "attribute vec4 vPosition;   \n"
 "attribute vec2 texPosition; \n"
 "attribute vec4 inColor;   \n"
 "varying vec4 fragColor;   \n"
 "varying vec2 v_texCoord;     \n"
 "void main()                 \n"
 "{                           \n"
 "   gl_Position = vPosition; \n"
 "   fragColor = inColor; \n"
 "   v_texCoord = texPosition; \n"
 "}                           \n";

GLbyte fShaderStr[] =
  "precision mediump float;                            \n"
  "varying vec2 v_texCoord;                            \n"
  "uniform sampler2D s_texture;                        \n"
  "void main()                                         \n"
  "{                                                   \n"
  "  gl_FragColor = texture2D( s_texture, v_texCoord );\n"
  "}                                                   \n";

And here is my code...

GLfloat gObj[18] = {
  0.5f, 0.5f, 0.0f,
  -0.5f, -0.5f, 0.0f,
  0.5f, -0.5f, 0.0f,
  0.5f, 0.5f, 0.0f,
  -0.5f, 0.5f, 0.0f,
  -0.5f, -0.5f, 0.0f
}
...
GLuint textureId;

GLubyte bytePix[4 * 3] ={
        255, 0, 0,
        0, 255, 0,
        0,0, 255,
        255,255,0
};
GLfloat texC[] = {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f
    };
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, bytePix);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, gObj);
glVertexAttribPointer(textCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, texC);

glUniform1i(sampler, 0 );
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(textCoordLoc);
glDrawArrays(GL_TRIANGLES, 0, 6);

But when I run the app I see the following...

Output

This seems wrong, how can I make it so each quadrant of the square is the color in the texture. Of course I could always be misunderstanding textures in general.

share|improve this question

closed as too localized by msell, Nicol Bolas, Josh Petrie, Anko, Byte56 Jun 6 '13 at 18:02

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 2 down vote accepted

You have six vertices (correctly) but only four texture coordinate pairs, so the last two are undefined. You must specify matching texture coordinates for each vertex, including the repeated ones.

share|improve this answer
    
Thats what I thought and I did finally figure it out but it was based of some sample code so that is what threw me. The sample code used DrawElement instead and reused verts. There also was something to be said about the relation to texture cords order and vertex cords order, as well as, text cords order vs orientation. But that is probably too broad here. –  Jackie Jun 6 '13 at 18:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.