title: what is wrong about the I420 render from ffmpeg?
body: <p>I use glfw render YUV from ffmpeg;the Y is ok(only use data Y ,and frag texture2D Y is ok ,the color is Grayscale).but when I add U,V ;the display show pink and green; I try to change frag shader or the imgtexture ,there have no use .</p>
  <pre><code>#include &lt;glad/glad.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  #include&lt;sstream&gt;
  #include&lt;iostream&gt;
  #include&lt;stdio.h&gt;
  
  #include &lt;stdio.h&gt;
  
  // settings
  const unsigned int SCR_WIDTH = 544;
  const unsigned int SCR_HEIGHT = 960;
  const int len = 544 * 960 * 3/2;
  BYTE YUVdata [len];//
  BYTE Ydata [544 * 960];//
  BYTE Udata [272 * 480];//
  BYTE Vdata [272 * 480];//
  unsigned int VBO = 0;
  unsigned int VAO = 0;
  unsigned int EBO = 0;
  unsigned int texturePIC = 0;
  int shaderProgram = 0;
  
  GLuint texIndexarray[3];
  GLuint texUniformY = 99;
  GLuint texUniformU = 99;
  GLuint texUniformV = 99;
  
  void LoadPicture()
  {
  
  
      glGenTextures(3, texIndexarray);
  
      glBindTexture(GL_TEXTURE_2D, texIndexarray[0]);
      
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
      glBindTexture(GL_TEXTURE_2D, texIndexarray[1]);
      
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glBindTexture(GL_TEXTURE_2D, texIndexarray[2]);
      
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  
      glValidateProgram(shaderProgram);
  
      texUniformY = glGetUniformLocation(shaderProgram, &quot;dataY&quot;);//2
      texUniformU = glGetUniformLocation(shaderProgram, &quot;dataU&quot;);//0
      texUniformV = glGetUniformLocation(shaderProgram, &quot;dataV&quot;);//1
  
      
      FILE* fp = fopen(&quot;./output544_960.yuv&quot;,&quot;rb+&quot;);//I420
      int returns  =fread(YUVdata,1,len,fp);
      int w = 544;
      int h = 960;
      int ysize = w*h;
      int uvsize = w * h / 4;
  
      void* uptr = &amp;YUVdata[ysize];
      void* vptr = &amp;YUVdata[ysize * 5 / 4];
  
      memcpy(Ydata,YUVdata,ysize);
      memcpy(Udata, uptr,uvsize);
      memcpy(Vdata, vptr,uvsize);
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, texIndexarray[0]);
      
      glTexImage2D(GL_TEXTURE_2D, 0 , GL_RED, w, h ,0, GL_RED,GL_UNSIGNED_BYTE ,Ydata);
      glUniform1i(texUniformY, texIndexarray[0]);               
  
  
      glActiveTexture(GL_TEXTURE1);
      glBindTexture(GL_TEXTURE_2D, texIndexarray[1]);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/2, h/2, 0, GL_RED, GL_UNSIGNED_BYTE,Udata );
  
      glUniform1i(texUniformU, texIndexarray[1]);
  
  
      glActiveTexture(GL_TEXTURE2);
      glBindTexture(GL_TEXTURE_2D, texIndexarray[2]);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w/2, h/2, 0, GL_RED, GL_UNSIGNED_BYTE,Vdata);
      glUniform1i(texUniformV, texIndexarray[2]);
  
  }
  
  
  void render()
  {
      glBindVertexArray(VAO);
      glUseProgram(shaderProgram);
      glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
      //glDrawArrays(GL_TRIANGLE_FAN,0,4);
      glUseProgram(0);
      glBindVertexArray(0);
  }
  
  void initmodule()
  {
      
      float vertexs[] = {
          
          1.0f,  1.0f, 0.0f,  1.0f, 0.0f,   
          1.0f, -1.0f, 0.0f,  1.0f, 1.0f,   
          -1.0f, -1.0f, 0.0f,  0.0f, 1.0f,   
          -1.0f,  1.0f, 0.0f,  0.0f, 0.0f    
      
      
      };
      
      unsigned int indexs[] = {
          0,1,3,
          1,2,3,
      };
  
      
      glGenVertexArrays(1,&amp;VAO);
      glBindVertexArray(VAO);
  
      
  
      glGenBuffers(1, &amp;VBO);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      
      glBufferData(GL_ARRAY_BUFFER,sizeof(vertexs), vertexs, GL_STATIC_DRAW);
  
      
      glGenBuffers(1,&amp;EBO);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexs),indexs,GL_STATIC_DRAW);
      
      LoadPicture();
  
      glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(float),(void*)0);
      
      glEnableVertexAttribArray(0);
      
      glVertexAttribPointer(1,2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
      
      glEnableVertexAttribArray(1);
  
      
      glBindBuffer(GL_ARRAY_BUFFER,0);
  
      
      glBindVertexArray(0);
  
  
  
  }
  
  void initshader(const char* verpath,const char* fragpath)
  {
      
      std::string VerCode(&quot;&quot;);
      std::string fregCode(&quot;&quot;);
      
      std::ifstream  vShaderFile;
      std::ifstream  fShaderFile;
  
      vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
      fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  
      try
      {
          vShaderFile.open(verpath);
          fShaderFile.open(fragpath);
  
          std::stringstream vsstream, fsstream;
          vsstream &lt;&lt; vShaderFile.rdbuf();
          fsstream &lt;&lt; fShaderFile.rdbuf();
          VerCode = vsstream.str();
          fregCode = fsstream.str();
      
      }
      catch (const std::exception&amp;)
      {
          std::cout &lt;&lt; &quot;read file error&quot; &lt;&lt; std::endl;
      }
  
      const char* vshader = VerCode.c_str();
      const char* fshader = fregCode.c_str();
  
      
      unsigned int vertexID = 0, fragID = 0;
      char infoLog[512];
      int  successflag = 0;
      vertexID = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexID,1,&amp;vshader,NULL );
      glCompileShader(vertexID);
      
      glGetShaderiv(vertexID,GL_COMPILE_STATUS,&amp;successflag);
      if (!successflag)
      {
          glGetShaderInfoLog(vertexID,512,NULL,infoLog);
          std::string errstr(infoLog);
          std::cout &lt;&lt; &quot;v shader err&quot;&lt;&lt;infoLog;
      }
      //frag
      fragID = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fragID, 1, &amp;fshader, NULL);
      glCompileShader(fragID);
      
      glGetShaderiv(fragID, GL_COMPILE_STATUS, &amp;successflag);
      if (!successflag)
      {
          glGetShaderInfoLog(fragID, 512, NULL, infoLog);
          std::string errstr(infoLog);
          std::cout &lt;&lt; &quot;f shader err&quot;&lt;&lt;infoLog;
      }
      
      shaderProgram = glCreateProgram();
      glAttachShader(shaderProgram,vertexID);
      glAttachShader(shaderProgram,fragID);
  
      glBindAttribLocation(shaderProgram, 0, &quot;aPos&quot;);
      glBindAttribLocation(shaderProgram, 1, &quot;texCoord&quot;);
  
      glLinkProgram(shaderProgram);
      glGetProgramiv(shaderProgram,GL_LINK_STATUS,&amp;successflag);
      if (!successflag)
      {
          glGetShaderInfoLog(shaderProgram, 512, NULL, infoLog);
          std::string errstr(infoLog);
          std::cout &lt;&lt; &quot;link error&quot;;
      }
  
      
      glDeleteShader(vertexID);
      glDeleteShader(fragID);
  }
  void processInput(GLFWwindow *window)
  {
      if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
      {
          
          glfwSetWindowShouldClose(window, true);
      }
  }
  
  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(544, 960, &quot;LearnOpenGL&quot;, NULL, NULL);
      if (window == NULL)
      {
          printf(&quot;&quot;);
          
          glfwTerminate();
          return -1;
      }
      
      glfwMakeContextCurrent(window);
  
      
      glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  
      
      if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
      {
          printf(&quot;&quot;);
          return -1;
      }
      initshader(&quot;vertexShader.glsl&quot;, &quot;fragmentShader.glsl&quot;);//
      initmodule();
  
  
      
      while (!glfwWindowShouldClose(window))
      {
          
          processInput(window);
  
          glClearColor(0.0f,0.0f,0.0f,1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
          render();
      
          
          glfwSwapBuffers(window);
          
          glfwPollEvents();
      }
  
      
      glfwTerminate();
      return 0;
  }
  </code></pre>
  <p>I get the Y data ,and run the code is ok;the color is gray;but when I add the U ,the color is Light green；and when i add the V is pink and green;</p>
  <pre><code>    #version 330 core
  layout(location = 0) out vec4 FragColor;
  in vec2 TexCoord;
  uniform sampler2D dataY;
  uniform sampler2D dataU;
  uniform sampler2D dataV;
  vec3 yuv;
  vec3 rgb;
  void main()
  {
  
  
     yuv.x = texture2D(dataY, TexCoord).r-0.0625;
     yuv.y = texture2D(dataU, TexCoord).r-0.5;
     yuv.z = texture2D(dataV, TexCoord).r-0.5;
  
     rgb = mat3(1,              1,      1,     
              0,       -0.18732, 1.8556,    
              1.57481, -0.46813,      0) * yuv;   
      FragColor = vec4(rgb.x, rgb.y,rgb.z,1); 
  };
  </code></pre>