2011-11-11 62 views
2

我使用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()

enter image description here

+1

燦你告訴我們什麼是錯的?我看到的只是一個需要額外代碼來加載和顯示模型的工作程序。 – NickLH

+0

+1即時回覆。我得到的只是背景紋理,這些2D形狀不可見。就這樣。 :) – coder9

+0

啊,你正在繪製梯形圖後的背景紋理。因此它正在繪製它們。反轉繪圖順序,它應該工作。 – NickLH

回答

1

檢查文檔爲gluOrtho2D()

gluOrtho2D設置了一個兩維正交觀看區域。這等同於調用glOrtho近= -1,遠= 1

你把你的梯形,在Z = -5,這樣你的近裁剪平面背後:

glBegin(GL_QUADS); 
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(); 

嘗試 - 0.5爲你的Z座標,或-1和0

編輯之間至少東西:例子:

#include <GL/glut.h> 

int win_w = 0; 
int win_h = 0; 
GLuint texObj = 0; 

void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // set up projection 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    double halfWidth = (win_w/2.0);; 
    double halfHeight = (win_h/2.0); 
    glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -10, 10); 

    // set up modelview 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    // render textured quad 
    glEnable(GL_TEXTURE_2D); 
    glColor3ub(255,255,255); 
    glPushMatrix(); 
     glScalef(halfWidth * 0.9, halfHeight * 0.9, 1.0); 
     glBegin(GL_QUADS); 
      glTexCoord2f(0,0); 
      glVertex2f(-1,-1); 
      glTexCoord2f(1,0); 
      glVertex2f(1,-1); 
      glTexCoord2f(1,1); 
      glVertex2f(1,1); 
      glTexCoord2f(0,1); 
      glVertex2f(-1,1); 
     glEnd(); 
    glPopMatrix(); 
    glDisable(GL_TEXTURE_2D); 

    // draw quad on top 
    glColor3ub(255,0,0); 
    glPushMatrix(); 
     // move quad back a bit so it's above Z=0 
     glTranslatef(0,0,-1); 
     glScalef(50,50,50); 
     glBegin(GL_QUADS); 
      glVertex2f(-1,-1); 
      glVertex2f(1,-1); 
      glVertex2f(1,1); 
      glVertex2f(-1,1); 
     glEnd(); 
    glPopMatrix(); 

    glFlush(); 
    glutSwapBuffers(); 
} 

void reshape(int w, int h) 
{ 
    win_w = w; 
    win_h = h; 
    glViewport(0, 0, w, h); 
} 

GLuint getTexture() 
{ 
    // 2x2 texture 
    unsigned char bytes[] = 
    { 
      0, 0, 255,  255, 255, 255, 
     255, 0, 0,  0, 255, 0, 
    }; 

    GLuint texID; 
    glGenTextures(1, &texID); 
    glBindTexture(GL_TEXTURE_2D, texID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes); 
    return texID; 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 

    glutInitWindowSize(800,600); 
    glutCreateWindow("Texture"); 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 

    texObj = getTexture(); 
    glutMainLoop(); 
    return 0; 
} 
+0

+1信息和建議。做完你所說的話後,我最終得到了上述結果。請看我編輯部分下的屏幕截圖。 – coder9

+0

@ coder9:增加了一個示例程序。 – genpfault

+0

非常感謝!質地是顛倒的,我會盡力解決這個問題! – coder9

1

確保禁用絨(glDisable(GL_TEXTURE_2D))如果你正在繪製的東西,不應該被紋理喜歡你的梯形後。

否則,最後設置的紋理座標將應用於所有新的頂點,在一些奇怪的點中對紋理進行採樣。

+0

也沒有工作,我試着在紋理的glTexCoord2f()行之前在梯形和glEnable(GL_TEXTURE_2D)之前調用glDisable(GL_TEXTURE_2D),它仍然顯示只有紋理 – coder9

+0

@ coder9:確保你把它叫做glBegin(...)-glEnd()塊的_outside_BTW:你不應該使用即時模式(glBegin(...)-glEnd())。過時和緩慢,而且在使用gluBuild2DMipmaps時也沒什麼用處,因爲OpenGL-2可以使用任意尺寸的紋理圖像,只需禁用mipmaping並僅上傳0級。 – datenwolf

+0

@datenwolf嗨。 「只上傳0級」是什麼意思?你能發表一些骨架代碼嗎? – coder9