How to input video (frames) into a GLSL shader

人走茶凉 提交于 2019-12-06 07:38:21

Let's clarify a few things that needs to happen before the loop:

  • Set the pixel storage mode with glPixelStorei();
  • Generate only one texture with glGenTextures(), because at every iteration of the loop its content will be replaced with new data;
  • Compile the shader with glCompileShader(), use glCreateShader() to create a shader object, invoke glCreateProgram()to create a program, call glAttachShader() to attach the shader object to the new program, and finally glLinkProgram() to make everything ready to go.

That said, every iteration of the loop must:

  • Clear the color and depth buffer;
  • Load the modelview matrix with the identity matrix;
  • Specify the location where the drawing is going to happen glTranslatef();
  • Retrieve a new frame from the video;
  • Enable the appropriate texture target, bind it and then transfer the frame to the GPU with glTexImage2D();
  • Invoke glUseProgram() to activate your GLSL shader;
  • Draw a 2D face using GL_QUADS or whatever;
  • Disable the program with glUseProgram(0);
  • Disable the texture target with glDisable(GL_TEXTURE_YOUR_TEXTURE_TARGET);

This is more or less what needs to be done.

By the way: here's my OpenCV/OpenGL/Qt application that retrieves frames from the camera and displays it in a window. No shaders, though.

Good luck!

You don't need to use a framebuffer to send textures to the shader. Once you've got texture 0 selected as the active texture and 0 set as the value of the uniform sampler2D in your shader, every time you call glBindTexture(), it will set the sampler2D to whichever texture you've specified in the function parameter. So no, you don't need to relink or recompile your shader each time you want to change texture.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!