OpenGL rendering does not work correctly with OpenCV using GLSL shaders -
i have coded small opengl program using opengl , glsl shaders. here's screen of application :
now, objective mix opengl frame , 1 webcam in unique frame using opencv. before glsl shaders, tried make simple program drawing colored triangle basic opengl functions (glbengin, glvertex, etc.).
static int initgl() { if (!glfwinit()) return (-1); if (!glfwopenwindow(500, 500, 8, 8, 8, 0, 24, 0, glfw_window)) { glfwterminate(); return (-1); } glenable(gl_projection); glloadidentity(); gluperspective(45.0f, 500.0f / 500.0f, 0.1f, 100.0f); glulookat(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); return (0); } static void drawgl() { glbegin(gl_triangles); glcolor3ub(255, 0, 0); glvertex3f(-1.0f, 0.0f, 0.0f); glcolor3ub(0, 255, 0); glvertex3f(0.0f, 1.0f, 0.0f); glcolor3ub(0, 0, 255); glvertex3f(1.0f, 0.0f, 0.0f); glend(); } int main(void) { initgl(); cvcapture *capture = cvcapturefromcam(cv_cap_any); if (!capture) { fprintf(stderr, "error: capture null \n"); getchar(); return (-1); } while (1) { glclear(gl_color_buffer_bit); glenable(gl_modelview); iplimage *frame = cvqueryframe(capture); if (!frame) { fprintf(stderr, "error: frame null...\n"); getchar(); break; } gldrawpixels(frame->width, frame->height, gl_rgb, gl_unsigned_byte, frame->imagedata); drawgl(); glfwswapbuffers(); if ( (cvwaitkey(10) & 255) == 27 ) break; } cvreleasecapture(&capture); cvdestroywindow("mywindow"); return (exit_success); }
here's render :
as can see result correct. now, wanted try first program using glsl shaders opencv below when application launched have black screen. tried several tests , problem seems begin when call gluseprogram function (so when want bind program shader -> in code corresponds program->bind() call). here use gldrawpixels function load video frame. think it's not function call if want use glsl shaders. here's piece of c++ code :
[...] /*my glsl shaders initialization*/ [...] int main(int ac, char **av) { bool isalive = true; unsigned int vaoid = 0; sdl_event event; gluint textureid = 0; shaderprogram *program = null; if (!glfwinit()) return (-1); if (!glfwopenwindow(width, height, 8, 8, 8, 0, 24, 0, glfw_window)) { glfwterminate(); return (-1); } glenable(gl_depth_test); //viewport initialization glviewport(0, 0, width, height); //vertex declaration vertexdeclaration *vdeclar = initvertexdeclaration(); //glew init component glewinit(); //vbo initialization vertexbuffer *vbuffer = initvbo(); //shaders initialization program = initshaders("triangle-pf.vert", "triangle-pf.frag"); //texture initialization textureid = loadbmptexture("box.bmp"); //vao initialization initvao(vaoid, vbuffer, textureid, vdeclar); //screen capture initialization cvcapture *capture = cvcapturefromcam(cv_cap_any); if (!capture) { fprintf(stderr, "error: capture null \n"); getchar(); return (-1); } //main loop while (isalive == true) { //eventlistener(&event, &isalive); if (glfwgetkey(glfw_key_esc)) isalive = false; glcleardepth(1.0f); glclearcolor(0.13f, 0.12f, 0.13f, 1.0f); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); //--------------------------------------------------- iplimage *frame = cvqueryframe(capture); if (!frame) { fprintf(stderr, "error: frame null...\n"); getchar(); break; } //--------------------------------------------------- program->bind(); //projection matrix glm::mat4 projectionmatrix = glm::perspective(45.0f, 500.0f / 500.0f, 0.1f, 100.0f); //view matrix glm::mat4 viewmatrix = glm::lookat(glm::vec3(0.0f, 0.0f, 8.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); //model matrix glm::mat4 modelmatrix = glm::mat4(1.0f); modelmatrix = glm::translate(modelmatrix, glm::vec3(0.0f, 0.0f, 0.0f)); modelmatrix = glm::rotate(modelmatrix, angle, glm::vec3(1.0f, 1.0f, 1.0f)); modelmatrix = glm::scale(modelmatrix, glm::vec3(1.0f, 1.0f, 1.0f)); //prepare matrix glm::mat4 modelviewmatrix = viewmatrix * modelmatrix; glm::mat3 normalmatrix = glm::mat3(glm::vec3(modelviewmatrix[0]), glm::vec3(modelviewmatrix[1]), glm::vec3(modelviewmatrix[2])); glm::mat4 modelviewprojectionmatrix = projectionmatrix * modelviewmatrix; glm::vec4 lightpositionvec1 = viewmatrix * glm::vec4(lightposition1[0], lightposition1[1], lightposition1[2], lightposition1[3]); //send source light properties program->setuniform("lightinfos[0].la", glm::vec3(0.000000f, 0.000000f, 0.000000f)); program->setuniform("lightinfos[0].ld", glm::vec3(0.800000f, 0.800000f, 0.800000f)); program->setuniform("lightinfos[0].ls", glm::vec3(1.000000f, 1.000000f, 1.000000f)); program->setuniform("lightinfos[0].le", glm::vec3(0.200000f, 0.200000f, 0.200000f)); /*program->setuniform("lightinfos[1].la", glm::vec3(0.000000f, 0.000000f, 0.000000f)); program->setuniform("lightinfos[1].ld", glm::vec3(0.800000f, 0.800000f, 0.800000f)); program->setuniform("lightinfos[1].ls", glm::vec3(0.000000f, 1.000000f, 1.000000f)); program->setuniform("lightinfos[1].le", glm::vec3(0.200000f, 0.200000f, 0.200000f));*/ //send model materials properties program->setuniform("materialinfos.ka", glm::vec3(0.000000f, 0.000000f, 0.000000f)); program->setuniform("materialinfos.kd", glm::vec3(1.000000f, 1.000000f, 1.000000f)); program->setuniform("materialinfos.ks", glm::vec3(1.000000f, 1.000000f, 1.000000f)); program->setuniform("materialinfos.ke", glm::vec3(0.200000f, 0.000000f, 0.200000f)); program->setuniform("materialinfos.shininess", 10.0f); //send light position program->setuniform("lightinfos[0].position", lightpositionvec1); //send matrix program->setuniform("projectionmatrix", projectionmatrix); program->setuniform("normalmatrix", normalmatrix); program->setuniform("modelviewmatrix", modelviewmatrix); program->setuniform("modelmatrix", modelmatrix); program->setuniform("mvp", modelviewprojectionmatrix); gldrawpixels(frame->width, frame->height, gl_rgb, gl_unsigned_byte, frame->imagedata); //vao binding glbindvertexarray(vaoid); //render meshes gldrawarrays(gl_triangles, 0, vbuffer->getsize()); glbindvertexarray(0); program->release(); angle += 0.50f; glflush(); glfwswapbuffers(); } unsigned int vboid = vbuffer->gethandle(); gldeletebuffers(1, &vboid); gldeletevertexarrays(1, &vaoid); return (0); }
so think problem comes gldrawpixels cannot used glsl shaders. tried several possible ways without success. maybe have send video frame buffer directly pixel shader ? in case, how can ? i'm lost. can me?
the way set use image data texture. this link might starting point. can render quad in background, fills entire screen, , uses texture determine color of each pixel. advantage on gldrawpixels()
if scale window (and quad), image video scaled accordingly.
if need to, can use texture in shader when rendering quad: fragment (pixel) shader gets called each pixel, , use sampler color texture. can modify color according lighting needs.
what draw on top of you.
Comments
Post a Comment