diff --git a/src/Pudding.cpp b/src/Pudding.cpp index acd6709..09d7186 100644 --- a/src/Pudding.cpp +++ b/src/Pudding.cpp @@ -92,8 +92,12 @@ void Pudding::load_gl_context() glBindAttribLocation(world_program, 0, "in_Position"); glBindAttribLocation(world_program, 1, "vertexUV"); link_shader(world_program); - /* generate the texture that will store the video frame */ + /* generate the texture that will store the video frame, allocate storage for a video frame, bind and edit texture properties */ glGenTextures(1, &video_capture_texture_id); + glBindTexture(GL_TEXTURE_2D, video_capture_texture_id); + glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, capture.get(cv::CAP_PROP_FRAME_WIDTH), capture.get(cv::CAP_PROP_FRAME_HEIGHT)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); log_gl_errors(); } @@ -399,7 +403,7 @@ std::shared_ptr Pudding::texture_from_image_url(const std::string& */ void Pudding::destroy_texture(SDL_Texture* texture) { - // not sure why SDL_Log works here but SDL_LogDebug and SDL_LogInfo don't + /* not sure why SDL_Log works here but SDL_LogDebug and SDL_LogInfo don't */ SDL_Log("destroying texture %p", texture); SDL_DestroyTexture(texture); } @@ -443,24 +447,16 @@ void Pudding::update() capture.read(capture_frame); if (!capture_frame.empty()) { - /* convert opencv matrix to sdl texture */ - // SDL_Texture* texture = SDL_CreateTexture( - // get_renderer(), SDL_PIXELFORMAT_BGR24, SDL_TEXTUREACCESS_STATIC, capture_frame.cols, capture_frame.rows); - // SDL_UpdateTexture(texture, nullptr, static_cast(capture_frame.data), capture_frame.step1()); - // SDL_RenderCopyF(get_renderer(), texture, nullptr, &video_box); - // SDL_DestroyTexture(texture); - glUseProgram(world_program); /* rotate the opencv matrix 180 to work with opengl coords */ cv::flip(capture_frame, capture_frame, -1); + glUseProgram(world_program); /* bind texture to GLSL sampler */ + glBindTexture(GL_TEXTURE_2D, video_capture_texture_id); GLint base_texture_location = glGetUniformLocation(world_program, "baseTexture"); glUniform1i(base_texture_location, 0); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, video_capture_texture_id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* convert opencv matrix to GL texture */ - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, capture_frame.cols, capture_frame.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, capture_frame.ptr()); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, capture_frame.cols, capture_frame.rows, GL_BGR, GL_UNSIGNED_BYTE, capture_frame.ptr()); glDrawArrays(GL_TRIANGLES, 0, 6); SDL_GL_SwapWindow(get_window()); log_gl_errors();