title: Texture displaying on both triangles forming a square instead of only on one
body: <p>I have tried to put a texture on a square, made out of two triangles, but it isn't displaying on the square and it is on both triangles seperately. I followed many tutorials, but they don't go into much details on how to fix this. It seems, that I need to change my shader, but I don't know how to go about this. Please tell me what to change!</p>
  <p>This is a picture of the window:</p>
  <p><a href="https://campfirebg.eu5.org/upload/linux/OpenGL/image1.png" rel="nofollow noreferrer"><img src="https://campfirebg.eu5.org/upload/linux/OpenGL/image1.png" alt="enter image description here" /></a></p>
  <p>This is my code:</p>
  <pre><code>#define GLEW_STATIC
  #define STB_IMAGE_IMPLEMENTATION
  #include&lt;stdio.h&gt;
  #include&lt;GL/glew.h&gt;
  #include&lt;GLFW/glfw3.h&gt;
  #include&lt;stdbool.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stb/stb_image.h&gt;
  
  GLint uniColor;
  void key_callback();
  
  int main(){ 
     if(!glfwInit()){
        printf(&quot;Failed to initialize glfw!\n&quot;);
        return 1;
     }else{
        printf(&quot;Successfully initialized glfw!\n&quot;);
     }
     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 
     GLFWwindow* window = glfwCreateWindow(1054, 1057, &quot;Title&quot;, NULL, NULL);
     glfwSetKeyCallback(window, key_callback);
     if(!window){
        printf(&quot;Window creation failed!\n&quot;);
        return 1;
     }else{
        printf(&quot;Window created successfully!\n&quot;);
     }
     glfwMakeContextCurrent(window);
     glewExperimental = GL_TRUE;
     if(glewInit() != GLEW_OK){
        printf(&quot;Failed to initialize glew!\n&quot;);
        return 1;
     }else{
        printf(&quot;Successfully initialized glew!\n&quot;);
     } 
     glfwSwapInterval(1);
     float Vertices[] = {
       -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
       -0.5f,  0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f,  0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f
     };
  
     GLuint indices[] = {
        0, 2, 1,
        0, 3, 2
     };
  
     float VerticesSquare[] = {
        -0.5f, 0.5f, 0.0f,
         0.5f, 0.5f, 0.0f,
         0.5f, -0.5f, 0.0f,
        -0.5f, -0.5f, 0.0f
     };
     printf(&quot;GLfloat variable called successfully\n&quot;); 
     GLuint ShaderProgram = glCreateProgram();
     GLuint ShaderObjv = glCreateShader(GL_VERTEX_SHADER);
     GLuint ShaderObjf = glCreateShader(GL_FRAGMENT_SHADER);
     const GLchar* pvs[1];
     const GLchar* pfs[1];
     const char* pVertexShaderText = &quot;#version 330\nlayout(location = 0) in vec3 Position;\nlayout(location = 1) in vec3 incolor;\nlayout(location = 2) in vec2 Tex;\nout vec3 outcolor;\nout vec2 texcoords;\nvoid main(){gl_Position = vec4(Position.x, Position.y, Position.z, 1.0);\noutcolor = incolor;\ntexcoords = Tex;}&quot;;
     const char* pFragmentShaderText = &quot;#version 330\nuniform vec3 color;\nuniform sampler2D tex0;\nin vec3 outcolor;\nin vec2 texcoords;\nout vec4 FragColor;\nvoid main(){FragColor = texture(tex0, texcoords);}&quot;;
     pvs[0] = pVertexShaderText;
     pfs[0] = pFragmentShaderText;
     GLint LenghtsVertex[1];
     GLint LenghtsFragment[1];
     LenghtsVertex[0] = strlen(pVertexShaderText);
     LenghtsFragment[0] = strlen(pFragmentShaderText);
     glShaderSource(ShaderObjv, 1, pvs, LenghtsVertex);
     glShaderSource(ShaderObjf, 1, pfs, LenghtsFragment);
     glCompileShader(ShaderObjv);
     glCompileShader(ShaderObjf);
     GLint successvs;
     glGetShaderiv(ShaderObjv, GL_COMPILE_STATUS, &amp;successvs);
     if (!successvs) {
         GLchar InfoLog1[1024];
         glGetShaderInfoLog(ShaderObjv, sizeof(InfoLog1), NULL, InfoLog1);
         fprintf(stderr, &quot;Error compiling shader type %d: '%s'\n&quot;, GL_VERTEX_SHADER, InfoLog1);
     }
     GLint successfs;
     glGetShaderiv(ShaderObjf, GL_COMPILE_STATUS, &amp;successfs);
     if (!successfs) {
         GLchar InfoLog2[1024];
         glGetShaderInfoLog(ShaderObjf, sizeof(InfoLog2), NULL, InfoLog2);
         fprintf(stderr, &quot;Error compiling shader type %d: '%s'\n&quot;, GL_FRAGMENT_SHADER, InfoLog2);
     }
     glAttachShader(ShaderProgram, ShaderObjv);
     glAttachShader(ShaderProgram, ShaderObjf);
     glLinkProgram(ShaderProgram);
     glValidateProgram(ShaderProgram);
     glUseProgram(ShaderProgram);
     GLint uniColor = glGetUniformLocation(ShaderProgram, &quot;color&quot;);
     GLint tex0uni = glGetUniformLocation(ShaderProgram, &quot;tex0&quot;);
     glUniform3f(uniColor, 1.0f, 0.0f, 0.0f);
     glUniform1i(tex0uni, 0);
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     GLuint VBO;
     GLuint VAO = 0;
     GLuint EBO;
     glGenBuffers(1, &amp;VBO);
     glGenVertexArrays(1, &amp;VAO);
     glGenBuffers(1, &amp;EBO);
     glBindBuffer(GL_ARRAY_BUFFER, VBO);
     glBindVertexArray(VAO);
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
     printf(&quot;Buffer created successfully\n&quot;);
     glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
     printf(&quot;Buffer data set up successfully\n&quot;);
     glEnableVertexAttribArray(0);
     glEnableVertexAttribArray(1);
     glEnableVertexAttribArray(2);
     printf(&quot;Enabling buffer successfull\n&quot;);
     glBindBuffer(GL_ARRAY_BUFFER, VBO);
     printf(&quot;Binding buffer successfull\n&quot;);
     int width, height, num;
     unsigned char* bytes = stbi_load(&quot;image.png&quot;, &amp;width, &amp;height, &amp;num, 0);
     GLuint texture;
     glGenTextures(1, &amp;texture);
     glActiveTexture(GL_TEXTURE0);
     glBindTexture(GL_TEXTURE_2D, texture);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
     float flatcolor[] = {1.0f, 1.0f, 1.0f, 1.0f};
     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, flatcolor);
     printf(&quot;Set parameters successfully\n&quot;);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes);
     glGenerateMipmap(GL_TEXTURE_2D);
     printf(&quot;Texture set successfully\n&quot;);
     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
     printf(&quot;First pointer succeeded\n&quot;);
     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
     printf(&quot;Second pointer succeeded\n&quot;);
     glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
     printf(&quot;Third pointer succeeded\n&quot;);
     printf(&quot;Setting up VectexAttribPointer successfull\n&quot;);
  
     while(!glfwWindowShouldClose(window)){
        glfwPollEvents();  
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
        //glDrawArrays(GL_TRIANGLES, 0, 3); 
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 
        GLenum err;
        if((err = glGetError()) != GL_NO_ERROR){
           printf(&quot;OpenGL error: %d\n&quot;, err);
        }
        glfwSwapBuffers(window);
     } 
     return 0;
  }
  
  
  void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  {
     if(key == GLFW_KEY_R &amp;&amp; action == GLFW_PRESS){
        glUniform3f(uniColor, 1.0f, 0.0f, 0.0f);
     }else if(key == GLFW_KEY_G &amp;&amp; action == GLFW_PRESS){
        glUniform3f(uniColor, 0.0f, 1.0f, 0.0f);
     }else if(key == GLFW_KEY_B &amp;&amp; action == GLFW_PRESS){
        glUniform3f(uniColor, 0.0f, 0.0f, 1.0f);
     }
  }
  </code></pre>