我使用OpenCV捕獲攝像頭數據並將它們顯示爲GL窗口的紋理,這很好。在OpenGL中顯示紋理頂部的2D形狀
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image->imageData);
但我想也疊加在此之上的一些基本形狀,MD2模式,保留的背景,並在同一個窗口本身的質感。
這是我的函數傳遞到glutDisplayFunc()
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// Set Projection Matrix
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 0);
// Setup the view // Switch to Model View Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
// Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
// start drawing the Background texture
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
glFlush();
glutSwapBuffers();
}
編輯 新秩序
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
glBegin(GL_QUADS); //Begin quadrilateral coordinates
// Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
編輯: 新訂單和座標:
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// Trapezoid to be displayed on top of texture
glVertex3f(0.2f, 0.3f, -0.6f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.4f, -0.5f, -0.5f);
glVertex3f(-0.4f, -0.5f, -0.5f);
glEnd(); //End quadrilateral coordinates
////
glEnable(GL_TEXTURE_2D); // start drawing the background texture
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
上述結果做和禁用gluOrtho2D()
燦你告訴我們什麼是錯的?我看到的只是一個需要額外代碼來加載和顯示模型的工作程序。 – NickLH
+1即時回覆。我得到的只是背景紋理,這些2D形狀不可見。就這樣。 :) – coder9
啊,你正在繪製梯形圖後的背景紋理。因此它正在繪製它們。反轉繪圖順序,它應該工作。 – NickLH