Created
June 28, 2018 17:14
-
-
Save hsnks100/64e8198688080cccd5b49cf783b69d52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <mutex> | |
| #include <stb_image.h> | |
| #include <nanogui/nanogui.h> | |
| class VideoView: public nanogui::GLCanvas { | |
| public: | |
| nanogui::GLShader shader; | |
| GLuint textureId; | |
| unsigned char* pixelBuffer; | |
| bool needUpdate; | |
| unsigned int videoWidth, videoHeight; | |
| // constructor | |
| VideoView(Widget *parent, std::string videoPath): nanogui::GLCanvas(parent) { | |
| needUpdate = false; | |
| textureId = 0; | |
| videoWidth = 480; | |
| videoHeight = 320; | |
| setupGL(); | |
| pixelBuffer = new unsigned char[videoWidth*videoHeight*3]; | |
| int w = videoWidth; | |
| int h = videoHeight; | |
| //GLubyte map[*100*4]; | |
| int i,j; | |
| for(i=0; i<h; i++) { | |
| for(j=0; j<w; j++) { | |
| if(j % 2 == 0) { | |
| pixelBuffer[w*i*3 + j*3+0] = 0x00; | |
| pixelBuffer[w*i*3 + j*3+1] = 0x00; | |
| pixelBuffer[w*i*3 + j*3+2] = 0xFF; | |
| } | |
| else { | |
| pixelBuffer[w*i*3 + j*3+0] = 0xFF; | |
| pixelBuffer[w*i*3 + j*3+1] = 0x00; | |
| pixelBuffer[w*i*3 + j*3+2] = 0x00; | |
| } | |
| } | |
| } | |
| } | |
| ~VideoView() { | |
| shader.free(); | |
| } | |
| void setupGL() { | |
| // Setup shader | |
| shader.init( | |
| /* registered name */ | |
| "video_shader", | |
| /* vertex shader */ | |
| "#version 330\n" | |
| "// inputs: should just be the vertices and uv's for a rectangle\n" | |
| "in vec2 position;\n" | |
| "in vec2 texcoord;\n" | |
| "// outputs: interpolated texture coordinates for fragment shader\n" | |
| "out vec2 pass_texcoord;\n" | |
| "void main() {\n" | |
| " pass_texcoord = texcoord;\n" | |
| " gl_Position = vec4(position.xy, 0.0f, 1.0f);\n" | |
| "}\n", | |
| /* fragment shader */ | |
| "#version 330\n" | |
| "in vec2 pass_texcoord;// interpolated texture coordinate\n" | |
| "out vec4 outColor; // output color sampled from texture\n" | |
| "uniform sampler2D passTex;// sampler to read the texture\n" | |
| "void main() {\n" | |
| " outColor = texture(passTex, pass_texcoord);\n" | |
| "}\n" | |
| ); | |
| // Setup geometry. Two triangles. | |
| nanogui::MatrixXu indices(3, 2); | |
| indices.col(0) << 0, 1, 2; | |
| indices.col(1) << 2, 3, 1; | |
| nanogui::MatrixXf positions(2, 4); | |
| positions.col(0) << -1.0f, 1.0f; | |
| positions.col(1) << 1.0f, 1.0f; | |
| positions.col(2) << -1.0f, -1.0f; | |
| positions.col(3) << 1.0f, -1.0f; | |
| nanogui::MatrixXf texcoords(2, 4); | |
| texcoords.col(0) << 0.0f, 0.0f; | |
| texcoords.col(1) << 1.0f, 0.0f; | |
| texcoords.col(2) << 0.0f, 1.0f; | |
| texcoords.col(3) << 1.0f, 1.0f; | |
| shader.bind(); | |
| shader.uploadIndices(indices); | |
| shader.uploadAttrib("position", positions); | |
| shader.uploadAttrib("texcoord", texcoords); | |
| //shader.uploadAttrib("texcoord", texcoords); | |
| // Setup texture | |
| glGenTextures(1, &textureId); | |
| glBindTexture(GL_TEXTURE_2D, textureId); | |
| glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
| //glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
| //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); | |
| } | |
| void tearDownGL() { | |
| shader.free(); | |
| // TODO destroy texture, etc. | |
| } | |
| virtual void drawGL() override { | |
| using namespace nanogui; | |
| glActiveTexture(1); | |
| glUniform1i(shader.uniform("passTex", true), 1 - GL_TEXTURE0); | |
| glBindTexture(GL_TEXTURE_2D, textureId); | |
| glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, videoWidth, videoHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixelBuffer); | |
| float pixels[] = { | |
| 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, | |
| 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f | |
| }; | |
| //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels); | |
| glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, videoWidth, videoHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,pixelBuffer); | |
| //if(needUpdate) { | |
| //needUpdate = false; | |
| //} | |
| shader.bind(); | |
| shader.drawIndexed(GL_TRIANGLES, 0, 2); | |
| } | |
| }; | |
| class GUI: public nanogui::Screen { | |
| public: | |
| GUI(): Screen(Eigen::Vector2i(800, 600), "Video View Test", false) { | |
| using namespace nanogui; | |
| Window *window = new Window(this, "Video"); | |
| window->setPosition(Vector2i(15,15)); | |
| window->setLayout(new GroupLayout()); | |
| VideoView *video = new VideoView(window, "/home/rvg/Projects/Explora/1.m4v"); | |
| video->setSize({480, 320}); | |
| performLayout(); | |
| } | |
| ~GUI() { } | |
| }; | |
| int main(int argc, char *argv[]) { | |
| nanogui::init(); | |
| GUI *app = new GUI(); | |
| app->drawAll(); | |
| app->setVisible(true); | |
| nanogui::mainloop(); | |
| nanogui::shutdown(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment