SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 130
  2.  
  3. attribute vec4 vertexIn;
  4. attribute vec2 textureIn;
  5. varying vec2 textureOut;
  6. void main(void)
  7. {
  8.     gl_Position = vertexIn;
  9.     textureOut = textureIn;
  10. }
  11.      
  12. #version 130
  13.  
  14. varying vec2 textureOut;
  15. uniform sampler2D tex_y;
  16. uniform sampler2D tex_u;
  17. uniform sampler2D tex_v;
  18. void main(void)
  19. {
  20.     vec3 yuv;
  21.     vec3 rgb;
  22.     yuv.x = texture2D(tex_y, textureOut).r;
  23.     yuv.y = texture2D(tex_u, textureOut).r - 0.5;
  24.     yuv.z = texture2D(tex_v, textureOut).r - 0.5;
  25.     rgb = mat3(1.0, 1.0, 1.0,
  26.         0.0, -0.39465, 2.03211,
  27.         1.13983, -0.58060, 0.0) * yuv;
  28.     gl_FragColor = vec4(rgb, 1.0);
  29. }
  30.      
  31. static const GLfloat ver[] = {
  32.     -1.0f,-1.0f,
  33.      1.0f,-1.0f,
  34.     -1.0f, 1.0f,
  35.      1.0f, 1.0f
  36. };
  37.  
  38.  
  39. static const GLfloat tex[] = {
  40.     0.0f, 1.0f,
  41.     1.0f, 1.0f,
  42.     0.0f, 0.0f,
  43.     1.0f, 0.0f
  44. };
  45.  
  46. void OpenGLArea::init()
  47. {
  48.     std::cout << "OpenGLArea init" << std::endl;
  49.  
  50.     set_size_request(640, 360);
  51.     Singleton::instance()->getStream("cam1").mediaStream->ffmpegDecoder->setVideoReceiver(this);
  52. }
  53.  
  54. void OpenGLArea::receiveVideo(unsigned char **videoBuffer, int frameWidth, int frameHeight)
  55. {
  56.     this->frameWidth = frameWidth;
  57.     this->frameHeight = frameHeight;
  58.     //Before first render, datas pointer isn't even created yet
  59.     if (!firstFrameReceived)
  60.     {
  61.         buffer[0] = new unsigned char[frameWidth * frameHeight];     //Y
  62.         buffer[1] = new unsigned char[frameWidth * frameHeight / 4]; //U
  63.         buffer[2] = new unsigned char[frameWidth * frameHeight / 4]; //V
  64.         firstFrameReceived = true;
  65.     }
  66.     else
  67.     {
  68.         memcpy(buffer[0], videoBuffer[0], frameWidth * frameHeight);
  69.         memcpy(buffer[1], videoBuffer[1], frameWidth * frameHeight / 4);
  70.         memcpy(buffer[2], videoBuffer[2], frameWidth * frameHeight / 4);
  71.     }
  72.     //glDraw();
  73. }
  74.  
  75. void OpenGLArea::glInit()
  76. {
  77.     int frameWidth = 640;
  78.     int frameHeight = 360;
  79.     glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  80.  
  81.     Shader vertex_shader(ShaderType::Vertex, "vertex.shader");
  82.     Shader fragment_shader(ShaderType::Fragment, "fragment.shader");
  83.  
  84.     program = new Program();
  85.     program->attach_shader(vertex_shader);
  86.     program->attach_shader(fragment_shader);
  87.     program->link();
  88.  
  89.     glGenTextures(3, texs);//TODO: delete texture
  90.  
  91.     //Y
  92.     glBindTexture(GL_TEXTURE_2D, texs[0]);
  93.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frameWidth, frameHeight, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
  94.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  95.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  96.     //U
  97.     glBindTexture(GL_TEXTURE_2D, texs[1]);
  98.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frameWidth / 2, frameHeight / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
  99.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  101.     //V
  102.     glBindTexture(GL_TEXTURE_2D, texs[2]);
  103.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frameWidth / 2, frameHeight / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
  104.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  105.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  106.  
  107. }
  108.  
  109. void OpenGLArea::glDraw()
  110. {
  111.     program->use();
  112.  
  113.     glVertexAttribPointer(A_VER, 2, GL_FLOAT, 0, 0, ver);
  114.     glEnableVertexAttribArray(A_VER);
  115.  
  116.     glVertexAttribPointer(T_VER, 2, GL_FLOAT, 0, 0, tex);
  117.     glEnableVertexAttribArray(T_VER);
  118.  
  119.     unis[0] = glGetAttribLocation(program->get_id(), "tex_y");
  120.     unis[1] = glGetAttribLocation(program->get_id(), "tex_u");
  121.     unis[2] = glGetAttribLocation(program->get_id(), "tex_v");
  122.  
  123.     glActiveTexture(GL_TEXTURE0);
  124.     glBindTexture(GL_TEXTURE_2D, texs[0]);
  125.     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frameWidth, frameHeight, GL_RED, GL_UNSIGNED_BYTE, buffer[0]);
  126.     glUniform1i(unis[0], 0);
  127.  
  128.     glActiveTexture(GL_TEXTURE0 + 1);
  129.     glBindTexture(GL_TEXTURE_2D, texs[1]);
  130.     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frameWidth / 2, frameHeight / 2, GL_RED, GL_UNSIGNED_BYTE, buffer[1]);
  131.     glUniform1i(unis[1], 1);
  132.  
  133.     glActiveTexture(GL_TEXTURE0 + 2);
  134.     glBindTexture(GL_TEXTURE_2D, texs[2]);
  135.     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frameWidth / 2, frameHeight / 2, GL_RED, GL_UNSIGNED_BYTE, buffer[2]);
  136.     glUniform1i(unis[2], 2);
  137.  
  138.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  139.  
  140.  
  141. }
  142.  
  143. class GLWindow : public Gtk::Window
  144. {
  145.   public:
  146.     GLWindow()
  147.     {
  148.  
  149.         vbox = new Gtk::VBox;
  150.         drawing_area = new OpenGLArea();
  151.  
  152.         vbox->pack_start(*drawing_area, true, true);
  153.         add(*vbox);
  154.     }
  155.  
  156.   private:
  157.     Gtk::Button *button;
  158.     Gtk::VBox *vbox;
  159.     OpenGLArea *drawing_area;
  160. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top