title: OpenGL Render to a texture
body: <p><a href="https://i.stack.imgur.com/kZSfs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kZSfs.png" alt="enter image description here"></a>How can I render to a texture using framebuffer objects and use that texture as an input for my shader.</p>
  
  <p>I need to perform this for programmable blending.</p>
  
  <p>I am able to create a FBO and texture individually but not able to render to that texture and attach my frame buffer object.</p>
  
  <p>I need to render to texture because I want to pass this texture to my shader so that I am able to write my own programmable blending equation.</p>
  
  <pre><code>#include &lt;glad/glad.h&gt;
  #include &lt;GLFW\glfw3.h&gt; 
  #include &lt;iostream&gt;
  #include "shader.h"
  
  
  #include "std_image.h"
  
  using namespace std;
  
  void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  {
      glViewport(0, 0, width, height);
  }
  
  int main()
  {
      glfwInit();
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  
      GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
      if (window == NULL)
      {
          cout &lt;&lt; "Failed to create GLFW window" &lt;&lt;endl;
          glfwTerminate();
          return -1;
      }
      glfwMakeContextCurrent(window);
  
      if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
      {
          cout &lt;&lt; "Failed to initialize GLAD" &lt;&lt;endl;
          return -1;
      }
      glViewport(0, 0, 800, 600);
      glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  
      ///////////////////////////////////////////////////////////////////////////////////
  
      Shader ourShader("Shader.vs", "Shader.fs");
  
      Shader ourShader2("Shader.vs", "Shader2.fs");
  
      unsigned int FBO;
      glGenFramebuffers(1, &amp;FBO);
      glBindFramebuffer(GL_FRAMEBUFFER, FBO);
  
      unsigned int attachment_index_color_texture = 0;
      glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachment_index_color_texture, GL_UNSIGNED_BYTE, 0);
  
      glBindFramebuffer(GL_FRAMEBUFFER, 0);    //unbind framebuffer
  
  
  
  
  
  
  
      float vertices[] = {
          // positions          // colors           // texture coords
          0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
          0.5f, -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 0.0f,   // bottom right
          -0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   0.0f, 0.0f,   // bottom left
          -0.5f,  0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   0.0f, 1.0f    // top left 
      };
      unsigned int indices[] = {
          0, 1, 3, // first triangle
          1, 2, 3  // second triangle
      };
  
      float vertices2[] = {
          // positions          // colors           // texture coords
          0.75f,  0.75f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 1.0f,   // top right
          0.75f, 0.0f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
          0.0f, 0.0f, 0.0f,  0.0f,1.0f, 0.0f,   0.0f, 0.0f,   // bottom left
          0.0f,  0.75f, 0.0f,  0.0f, 1.0f, 0.0f,   0.0f, 1.0f    // top left 
      };
      unsigned int indices2[] = {
          0, 1, 2, // first triangle
          0, 2, 3  // second triangle
      };
  
  
      unsigned int VBO[2],VAO[2],EBO[2];
  
      glGenVertexArrays(2, VAO);
      glGenBuffers(2, VBO);
      glGenBuffers(2, EBO);
  
      glBindVertexArray(VAO[0]);
      glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
  
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
      glEnableVertexAttribArray(2);
  
      glBindVertexArray(0);
  
      glBindVertexArray(VAO[1]);
      glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[1]);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
  
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
      glEnableVertexAttribArray(2);
  
      glEnable(GL_BLEND);
      glBlendFunc(GL_ONE, GL_ONE);
  
  
      while (!glfwWindowShouldClose(window))
      {
          glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
  
  
  
          ourShader.use();
  
          glBindVertexArray(VAO[0]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
          ourShader2.use();
  
          glBindVertexArray(VAO[1]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
          glfwSwapBuffers(window);
          glfwPollEvents();
      }
      /*glDeleteVertexArrays(1, &amp;VAO);
      glDeleteBuffers(1, &amp;VBO);
      glDeleteBuffers(1, &amp;EBO);*/
  
      glfwTerminate();
      return 0;
  }
  </code></pre>
  
  <p>Edit 1:</p>
  
  <p>I am now attaching my texture to fbo, also rendering on my textrue as shown below:</p>
  
  <pre><code>while (!glfwWindowShouldClose(window))
      {
          glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
  
  
  
          ourShader.use();
          glBindVertexArray(VAO[0]);
          unsigned int FBO, color, depth;
  
          glGenFramebuffers(1, &amp;FBO);
          glGenTextures(1, &amp;color);
  
  
          glBindFramebuffer(GL_FRAMEBUFFER, FBO);
          glBindTexture(GL_TEXTURE_2D, color);
          glGenRenderbuffers(1, &amp;depth);
  
          glTexImage2D(GL_TEXTURE_2D,
              0,
              GL_RGBA,
              800, 600,
              0,
              GL_RGBA,
              GL_UNSIGNED_BYTE,
              NULL);
  
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
          glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color, 0);
  
          GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
          glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
  
          if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
              return false;
  
          glBindFramebuffer(GL_FRAMEBUFFER, FBO);
          glViewport(0, 0, 800, 600); // Render on the whole framebuffer, complete from the lower left corner to the upper right
  
  
          glBindFramebuffer(GL_FRAMEBUFFER, 0);
  
  
          ourShader2.use();
  
          glBindVertexArray(VAO[1]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
  
  
          glfwSwapBuffers(window);
          glfwPollEvents();
      }
  </code></pre>
  
  <p>One issue left is how can i pass my texture "color" in my shader.</p>
  
  <p>Thanks for help</p>
  
  <p>Edit2:</p>
  
  <p>I am attaching my whole code, can u please check why i am not getting anything on screen.</p>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;glad/glad.h&gt;
  #include &lt;GLFW\glfw3.h&gt; 
  #include &lt;iostream&gt;
  #include "shader.h"
  
  
  #include "std_image.h"
  
  using namespace std;
  
  void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  {
      glViewport(0, 0, width, height);
  }
  
  int main()
  {
      glfwInit();
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  
      GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
      if (window == NULL)
      {
          cout &lt;&lt; "Failed to create GLFW window" &lt;&lt;endl;
          glfwTerminate();
          return -1;
      }
      glfwMakeContextCurrent(window);
  
      if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
      {
          cout &lt;&lt; "Failed to initialize GLAD" &lt;&lt;endl;
          return -1;
      }
      glViewport(0, 0, 800, 600);
      glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  
      ///////////////////////////////////////////////////////////////////////////////////
  
      Shader ourShader("Shader.vs", "Shader.fs");
  
      Shader ourShader2("Shader.vs", "Shader2.fs");
  
  
  
      //glBindFramebuffer(GL_FRAMEBUFFER, 0);    //unbind framebuffer
  
  
  
  
  
      float vertices[] = {
          // positions          // colors           // texture coords
          0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
          0.5f, -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 0.0f,   // bottom right
          -0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   0.0f, 0.0f,   // bottom left
          -0.5f,  0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   0.0f, 1.0f    // top left 
      };
      unsigned int indices[] = {
          0, 1, 3, // first triangle
          1, 2, 3  // second triangle
      };
  
      float vertices2[] = {
          // positions          // colors           // texture coords
          0.75f,  0.75f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 1.0f,   // top right
          0.75f, 0.0f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
          0.0f, 0.0f, 0.0f,  0.0f,1.0f, 0.0f,   0.0f, 0.0f,   // bottom left
          0.0f,  0.75f, 0.0f,  0.0f, 1.0f, 0.0f,   0.0f, 1.0f    // top left 
      };
      unsigned int indices2[] = {
          0, 1, 2, // first triangle
          0, 2, 3  // second triangle
      };
  
  
      unsigned int VBO[2],VAO[2],EBO[2];
  
      glGenVertexArrays(2, VAO);
      glGenBuffers(2, VBO);
      glGenBuffers(2, EBO);
  
      glBindVertexArray(VAO[0]);
      glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
  
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
      glEnableVertexAttribArray(2);
  
      glBindVertexArray(0);
  
      glBindVertexArray(VAO[1]);
      glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[1]);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
  
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
      glEnableVertexAttribArray(2);
  
      glEnable(GL_BLEND);
      glBlendFunc(GL_ONE, GL_ONE);
  
  
      while (!glfwWindowShouldClose(window))
      {
          glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
  
  
          ourShader.use();
  
  
          glBindVertexArray(VAO[0]);
          unsigned int FBO, color, depth;
  
          glGenFramebuffers(1, &amp;FBO);
          glGenTextures(1, &amp;color);
  
  
          glBindFramebuffer(GL_FRAMEBUFFER, FBO);
          glBindTexture(GL_TEXTURE_2D, color);
          glGenRenderbuffers(1, &amp;depth);
  
          glTexImage2D(GL_TEXTURE_2D,
              0,
              GL_RGBA,
              800, 600,
              0,
              GL_RGBA,
              GL_UNSIGNED_BYTE,
              NULL);
  
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
          glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, color, 0);
  
          unsigned int decalTexLocation;
          decalTexLocation = glGetUniformLocation(ourShader.ID, "DecalTex");
  
          glUniform1i(decalTexLocation, 0);
          glActiveTexture(GL_TEXTURE0 + 0);
          glBindTexture(GL_TEXTURE_2D, color);
  
  
          GLenum DrawBuffers[2] = { GL_COLOR_ATTACHMENT0 };
          glDrawBuffers(1, &amp;DrawBuffers[0]); // "1" is the size of DrawBuffers
  
          if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
              return false;
  
          glViewport(0, 0, 800, 600); // Render on the whole framebuffer, complete from the lower left corner to the upper right
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
  
          glBindFramebuffer(GL_FRAMEBUFFER, 0);
  
  
          ourShader2.use();
  
          glBindVertexArray(VAO[1]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
  
  
          glfwSwapBuffers(window);
          glfwPollEvents();
      }
      /*glDeleteVertexArrays(1, &amp;VAO);
      glDeleteBuffers(1, &amp;VBO);
      glDeleteBuffers(1, &amp;EBO);*/
  
      glfwTerminate();
      return 0;
  }
  </code></pre>
  
  <p>Fragment Shader1:</p>
  
  <pre><code>#version 330 core
  
  out vec4 FragColor;
  
  in vec3 ourColor;
  
  void main()
  {
    // vec4 dst = getCurrentColorAtOutputFragmentPosition();
     FragColor = vec4(ourColor, 1.0);
  }
  </code></pre>
  
  <p>Fragment Shader2:</p>
  
  <pre><code>#version 330 core
  
  out vec4 FragColor;
  
  uniform sampler2D DecalTex;
  
  in vec3 ourColor;
  
  void main()
  {
    vec2 coords = gl_TexCoord[0].xy;
    vec3 normalColor = texture2D(DecalTex, coords).rgb; //original color
  
  
     FragColor = vec4(normalColor.x,normalColor.y,normalColor.z, 1.0);
  }
  </code></pre>
  
  <p>Vertex Shader:</p>
  
  <pre><code>#version 330 core
  layout (location = 0) in vec3 aPos;
  layout (location = 1) in vec3 aColor;
  layout (location = 2) in vec2 aTexCoord;
  
  out vec3 ourColor;
  out vec2 TexCoord;
  
  void main()
  {
      gl_Position = vec4(aPos, 1.0);
      ourColor = aColor;
      TexCoord = aTexCoord;
  }
  </code></pre>
  
  <p>Edit 3:</p>
  
  <p>Int the below code i am rendering 2 rectangles, one of a texture using FBO(Frame Buffer Object) and second on screen.</p>
  
  <p>I am able to render both on screen.</p>
  
  <p>I have written 2 fragment shader and 2 vertex shader.</p>
  
  <p>One pair of fragment and vertex shader is use for rendering on texture and other is used for rendering on screen.</p>
  
  <p>My main file:</p>
  
  <pre><code>#include &lt;glad/glad.h&gt;
  #include &lt;GLFW\glfw3.h&gt; 
  #include &lt;iostream&gt;
  #include "shader.h"
  
  
  #include "std_image.h"
  
  using namespace std;
  
  void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  {
      glViewport(0, 0, width, height);
  }
  
  int main()
  {
      glfwInit();
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  
      GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
      if (window == NULL)
      {
          cout &lt;&lt; "Failed to create GLFW window" &lt;&lt;endl;
          glfwTerminate();
          return -1;
      }
      glfwMakeContextCurrent(window);
  
      if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
      {
          cout &lt;&lt; "Failed to initialize GLAD" &lt;&lt;endl;
          return -1;
      }
      glViewport(0, 0, 800, 600);
      glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  
      ///////////////////////////////////////////////////////////////////////////////////
  
      Shader ourShader("Shader.vs", "Shader.fs");
  
      Shader ourShader2("Shader2.vs", "Shader2.fs");
  
  
  
      //glBindFramebuffer(GL_FRAMEBUFFER, 0);    //unbind framebuffer
  
  
  
  
  
      float vertices[] = {
          // positions          // colors           // texture coords
          0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
          0.5f, -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 0.0f,   // bottom right
          -0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   0.0f, 0.0f,   // bottom left
          -0.5f,  0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   0.0f, 1.0f    // top left 
      };
      unsigned int indices[] = {
          0, 1, 3, // first triangle
          1, 2, 3  // second triangle
      };
  
      float vertices2[] = {
          // positions          // colors           // texture coords
          1.0,  1.0, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 1.0f,   // top right
          1.0, 0.0f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
          0.0f, 0.0f, 0.0f,  0.0f,1.0f, 0.0f,   0.0f, 0.0f,   // bottom left
          0.0f,  1.0, 0.0f,  0.0f, 1.0f, 0.0f,   0.0f, 1.0f    // top left 
      };
      unsigned int indices2[] = {
          0, 1, 2, // first triangle
          0, 2, 3  // second triangle
      };
  
  
      unsigned int VBO[2],VAO[2],EBO[2];
  
      glGenVertexArrays(2, VAO);
      glGenBuffers(2, VBO);
      glGenBuffers(2, EBO);
  
      glBindVertexArray(VAO[0]);
      glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[0]);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
  
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
      glEnableVertexAttribArray(2);
  
      glBindVertexArray(0);
  
      glBindVertexArray(VAO[1]);
      glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO[1]);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
  
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
      glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
      glEnableVertexAttribArray(2);
  
      /*glEnable(GL_BLEND);
      glBlendFunc(GL_ONE, GL_ONE)*/;
  
      unsigned int color;
      glGenTextures(1, &amp;color);
      glBindTexture(GL_TEXTURE_2D, color);
  
      glTexImage2D(GL_TEXTURE_2D,
          0,
          GL_RGBA,
          800, 600,
          0,
          GL_RGBA,
          GL_UNSIGNED_BYTE,
          NULL);
  
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
      unsigned int FBO;
      glGenFramebuffers(1, &amp;FBO);
      glBindFramebuffer(GL_FRAMEBUFFER, FBO);
  
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
  
  
  
  
      while (!glfwWindowShouldClose(window))
      {
  
          ourShader.use();
  
          glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
  
  
          glBindVertexArray(VAO[0]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
  
          glBindFramebuffer(GL_FRAMEBUFFER, 0);
  
  
          ourShader2.use();
  
          int texturehandle = glGetUniformLocation(ourShader.ID, "DecalTex");
          glActiveTexture(GL_TEXTURE0);
          glBindTexture(GL_TEXTURE_2D, color);
          glUniform1i(texturehandle, 0);
  
          glBindVertexArray(VAO[1]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  
  
  
  
          glfwSwapBuffers(window);
          glfwPollEvents();
      }
  
  
      glfwTerminate();
      return 0;
  }
  </code></pre>
  
  <p>Veretx Shader 1:</p>
  
  <pre><code>#version 330 core
  layout (location = 0) in vec3 aPos;
  layout (location = 1) in vec3 aColor;
  layout (location = 2) in vec2 aTexCoord;
  
  out vec3 ourColor;
  out vec2 TexCoord;
  
  void main()
  {
      gl_Position = vec4(aPos, 1.0);
      ourColor = aColor;
      TexCoord = aTexCoord;
  }
  </code></pre>
  
  <p>Fragment Shader 1</p>
  
  <pre><code>#version 330 core
  
  out vec4 FragColor;
  
  in vec3 ourColor;
  
  void main()
  {
    // vec4 dst = getCurrentColorAtOutputFragmentPosition();
     FragColor = vec4(ourColor, 1.0);
  }
  </code></pre>
  
  <p>Vertex Shader 2</p>
  
  <pre><code>#version 330 core
  layout (location = 0) in vec3 aPos;
  layout (location = 1) in vec3 aColor;
  layout (location = 2) in vec2 aTexCoord;
  
  out vec3 ourColor;
  out vec2 myUV;
  
  
  void main()
  {
      gl_Position = vec4(aPos, 1.0);
      ourColor = aColor;
      myUV = vec2(aPos.x,aPos.y);
  }
  </code></pre>
  
  <p>Fragment Shader 2</p>
  
  <pre><code>#version 330 core
  
  out vec4 FragColor;
  
  uniform sampler2D DecalTex;
  
  in vec3 ourColor;
  in vec2 UV;
  
  void main()
  {
     vec2 outUV = vec2(0.26,0.26);
     vec4 myFragColor = texture2D(DecalTex,outUV);
     //vec4 myFragColor = texture2D(DecalTex,UV);
      //FragColor = vec4(ourColor,1.0);
  
      vec4 tempFragColor = vec4(ourColor, 1.0);
  
      //FragColor = myFragColor;
  
      FragColor = myFragColor + tempFragColor;
  
      //FragColor = (1.0,1.0,1.0,1.0);//tempFragColor;
  }
  </code></pre>
  
  <p>Here the issue is that when i use the line:</p>
  
  <pre><code>vec4 myFragColor = texture2D(DecalTex,UV);
  </code></pre>
  
  <p>I am not able to get any value in myFragColor, as a result normal rendering occurs.</p>
  
  <p>but if i change to </p>
  
  <pre><code>vec2 outUV = vec2(0.26,0.26);
         vec4 myFragColor = texture2D(DecalTex,outUV);
  </code></pre>
  
  <p>I am able to get value of destination pixel and hence rending is done according to my need.</p>
  
  <p>Can u please tell why i am not able from:</p>
  
  <pre><code>vec4 myFragColor = texture2D(DecalTex,UV);
  </code></pre>
  
  <p>Edit 4</p>
  
  <p>Now i am able to access my destination pixel, but only 1 issue is left:</p>
  
  <p>first rectangles co-ordinates (-0.5,-0.5) &lt;-----> (0.5,0.5)</p>
  
  <p>Second Rectangles co - ordinates (0,0) &lt;----> (1,1)</p>
  
  <p>Area of intersection (0,0) &lt;----> (0.5,0.5)  --------> This should be yellow (Red + Green)</p>
  
  <p>But area (0.26,0.26) &lt;-------> (0.74,0.74)    ------> this is comming out yellow, very strange any reason..?</p>
  
  <p>Latest Fragment Shader:</p>
  
  <pre><code>#version 330 core
  
  out vec4 FragColor;
  
  uniform sampler2D DecalTex;
  
  in vec3 ourColor;
  in vec2 myUV;
  
  void main()
  {
  
  
     vec4 myFragColor = texture2D(DecalTex,myUV);
      FragColor = vec4(ourColor,1.0);
  
      vec4 tempFragColor = vec4(ourColor, 1.0);
  
  
      FragColor = myFragColor + tempFragColor;
  
  }
  </code></pre>
  
  <p>Expected Result:</p>
  
  <p><a href="https://i.stack.imgur.com/0FPqT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0FPqT.png" alt="Expected Result"></a></p>