title: OpenGL Texture drawing as one color
body: <p>I've found similar posts here, but none help me with my problem. I'm just using basic OpenGL, and trying to draw a triangle with a texture applied. But for some reason the triangle is one solid color. This color happens to be the same color as the background of the texture.</p>
  
  <p>This is the texture I'm attempting to draw</p>
  
  <p><a href="https://i.stack.imgur.com/O8ku4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O8ku4.png" alt=""></a></p>
  
  <p>And this is the result when I run the program</p>
  
  <p><a href="https://i.stack.imgur.com/l4Pk3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l4Pk3.png" alt="enter image description here"></a></p>
  
  <p>I've experimented by editing the texture to have some blue in the bottom left (the origin), and it came out like this</p>
  
  <p><a href="https://i.stack.imgur.com/XxxYB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XxxYB.png" alt="enter image description here"></a></p>
  
  <p>My guess is that it's interpolating the red and blue to give me purple. But I have no idea why it'd be interpolating the colors in the first place instead of just drawing the texture like I want.</p>
  
  <p>This is some bits and pieces of the relevant parts of my code:</p>
  
  <p>Enabling things before the main loop</p>
  
  <pre><code>glClearColor(0, 0, 0, 1);
  
  glEnable(GL_BLEND);
  glEnable(GL_TEXTURE);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  </code></pre>
  
  <p>Loading the shaders</p>
  
  <pre><code>auto vertString = getSource("test.vert");
  auto vertSource = vertString.c_str();
  GLint vertLength = vertString.length();
  
  auto fragString= getSource("test.frag");
  auto fragSource = fragString.c_str();
  GLint fragLength = fragString.length();
  
  auto vertShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertShader, 1, &amp;vertSource, &amp;vertLength);
  
  auto fragShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragShader, 1, &amp;fragSource, &amp;fragLength);
  
  //compile shaders
  glCompileShader(vertShader);
  checkShaderCompilation(vertShader, "vertex shader");
  
  glCompileShader(fragShader);
  checkShaderCompilation(fragShader, "fragment shader");
  
  auto program = glCreateProgram();
  
  glAttachShader(program, vertShader);
  glAttachShader(program, fragShader);
  
  glBindAttribLocation(program, 0, "i_position");
  glBindAttribLocation(program, 1, "i_texCoord");
  
  //link program
  glLinkProgram(program);
  checkProgramLinkage(program, "vertex shader", "fragment shader");
  
  glDetachShader(program, vertShader);
  glDetachShader(program, fragShader);
  
  glDeleteShader(vertShader);
  glDeleteShader(fragShader);
  </code></pre>
  
  <p>Loading the Texture</p>
  
  <pre><code>SDL_Surface* surface = IMG_Load("Numel.png");
  
  if (!surface)
  {
      std::cout &lt;&lt; "Unable to load texture." &lt;&lt; std::endl;
      return 0;
  }
  
  auto width = surface-&gt;w;
  auto height = surface-&gt;h;
  GLuint texture = 0;
  
  //generate the id
  glGenTextures(1, &amp;texture);
  glBindTexture(GL_TEXTURE_2D, texture);
  
  auto mode = surface-&gt;format-&gt;BytesPerPixel == 4 ? GL_RGBA : GL_RGB;
  
  glTexImage2D(GL_TEXTURE_2D, 0, mode, surface-&gt;w, surface-&gt;h, false, mode, GL_UNSIGNED_BYTE, surface-&gt;pixels);
  
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  SDL_FreeSurface(surface);
  
  glBindTexture(GL_TEXTURE_2D, 0);
  </code></pre>
  
  <p>Vertex Structure</p>
  
  <pre><code>struct Vertex
  {
      Vector2 position;
      Vector2 texCoord;
  };
  </code></pre>
  
  <p>Generating the Vertex Array</p>
  
  <pre><code>Vertex data[] = {
      -0.5, -0.5, 0, 0,
      0.5, -0.5, 1, 0,
      0.5, 0.5, 1, 1
  };
  
  GLuint vertexBuffer = 0, vertexArray = 0;
  
  glGenBuffers(1, &amp;vertexBuffer);
  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*3, data, GL_STATIC_DRAW);
  
  glGenVertexArrays(1, &amp;vertexArray);
  glBindVertexArray(vertexArray);
  
  glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 2, GL_FLOAT, 0, sizeof(Vertex), (char*)offsetof(Vertex, position));
  
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 2, GL_FLOAT, 0, sizeof(Vertex), (char*)offsetof(Vertex, texCoord));
  </code></pre>
  
  <p>And binding/drawing everything in the main loop</p>
  
  <pre><code>glClear(GL_COLOR_BUFFER_BIT);
  
  //texture
  glBindTexture(GL_TEXTURE_2D, texture);
  
  //shader
  auto colorLoc = glGetUniformLocation(program, "u_color");
  glUniform3f(colorLoc, 0, 0, 1);
  
  auto useTextureLoc = glGetUniformLocation(program, "u_usingTexture");
  glUniform1i(useTextureLoc, 1);
  
  auto textureLoc = glGetUniformLocation(program, "u_sampler");
  glUniform1i(textureLoc, 0);
  
  glUseProgram(program);
  
  //vertex array
  glBindVertexArray(vertexArray);
  
  glEnableVertexAttribArray(0);
  glEnableVertexAttribArray(1);
  glDrawArrays(GL_TRIANGLES, 0, 3);
  
  SDL_GL_SwapWindow(window);
  </code></pre>
  
  <p>This is my vertex shader</p>
  
  <pre><code>#version 420
  
  in vec2 i_position;
  in vec2 i_texCoord;
  
  out vec2 o_texCoord;
  
  void main()
  {
      o_texCoord=i_texCoord;
      gl_Position=vec4(i_position, 0, 1);
  }
  </code></pre>
  
  <p>And my fragment shader</p>
  
  <pre><code>#version 420
  
  uniform vec3 u_color;
  uniform sampler2D u_sampler;
  uniform bool u_usingTexture;
  
  in vec2 i_texCoord;
  
  out vec4 o_color;
  
  void main()
  {
      if(u_usingTexture)
          o_color=texture(u_sampler, i_texCoord);
      else
          o_color=vec4(u_color, 1);
  }
  </code></pre>
  
  <p>I've literally been trying to solve this for days and I have no idea. If anyone could figure out why it's rendering with one color instead of the texture, it'd be very much appreciated. </p>