我已經編寫使用OpenGL和GLSL着色器小OpenGL圖像正常工作。這裏是我的應用程序的屏幕:OpenGL渲染不使用OpenCV的GLSL着色器
現在,我的目標是混合我的OpenGL的框架,並從我的攝像頭在一個獨特的框架使用了OpenCV的一個。在使用GLSL着色器做這件事之前,我嘗試製作一個非常簡單的程序,用一些基本的OpenGL函數(glBengin,glVertex等)繪製彩色三角形。
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 is 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 is 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);
}
這裏的渲染:
正如你所看到的結果是正確的。現在,我想嘗試我的第一個程序使用GLSL着色器與OpenCV像下面一樣,但是當我的應用程序啓動時,我有一個黑屏。我嘗試了幾次測試,當我調用glUseProgram函數時,問題似乎開始了(所以當我想綁定程序着色器 - >在我的代碼中它對應於程序 - > bind()調用)。但在這裏我使用glDrawPixels函數來加載我的視頻幀。如果我想使用GLSL着色器,我認爲這不是一個好的函數。這是我的一個C代碼:
[...]
/*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 is 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 is 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);
}
所以我認爲問題來自glDrawPixels,它不能與GLSL着色器一起使用。我嘗試了幾種可能的方式,但沒有取得任也許我必須將視頻幀緩衝區直接發送到像素着色器?在這種情況下,我該怎麼做?我真的迷失了。有人可以幫助我嗎?
「混合我的OpenGL的框架,並從我的攝像頭在一個獨特的框架使用了OpenCV的一個,」這是什麼意思?你不能只在背景中渲染一個四邊形,並使用OpenCV中的圖像數據作爲紋理,然後在頂部繪製其他3D對象?我不明白你爲什麼要使用着色器。着色器應該做什麼? – 2013-05-04 13:10:24
我使用着色器來計算一些無法用基本的Opengl用法渲染的燈光效果。我只想渲染我的立方體,並將我的實時視頻作爲背景(如上面三角形的示例)。這就像視頻框架(來自我的網絡攝像頭)和其他框架與立方體的並置。你明白我的意思嗎? – user1364743 2013-05-04 13:27:55
是的,這就是所謂的增強現實。你是否需要將這些燈光效果應用於背景或3D對象? – 2013-05-04 13:37:58