[程式] ABOUT STENCIL BUFFER Question
有個問題想請教板上的各位前輩。
小弟我寫 MFC & OpenGL 程式,
目前想做一個複雜多邊形區域內交集物體之選取功能,
但GOOGLE很久就是沒找到相關功能技術討論文章。
目前先以Tessellation方式畫出複雜多邊形,
然後接下來我要把複雜多邊形的區域紀錄到Stencil Buffer中,
但問題來了,
我寫了Unit Test Code去檢查我的Stencil Buffer紀錄情況,
發現結果不是我想像的。
我以全白色的QUAD覆蓋在整個繪圖區中,
並且透過Stencil Test來只畫出複雜多邊形區域。
繪圖結果如下圖連結,黃色Stipple區+白色區便是完整的複雜多邊形區域,
各位可以發現,通過Stencil Test區域只有小於繪圖區的一個矩形區塊。
https://picasaweb.google.com/112165426532756160107/StencilQz#5627339704475169794
我不懂為何會變成如此結果,目前百思不得其解。
是因為Stencil Buffer我必須要在Resize時去同步它的長與寬嗎?
但我記得不用呀(Stencil雖然沒有很熟但以前做過沒碰到這個問題)。
現在先以製作Stencil Mask & 執行Stencil Test時的Projection下手來Debug,
但一樣還沒找出錯誤(已確認兩個時間點Projection完全一樣)。
可否請板上的高手來替我解惑呢?
感謝各位大大不吝分享。
下方是我的相關Test Code
-----------------------------------------------------
// 2d projection
gluOrtho2D(-Ortho2dRange.x, Ortho2dRange.x, -Ortho2dRange.y, Ortho2dRange.y);
....
....
// do polygon tessellation
glColor3f(1.0, 1.0, 0.5);
gluTessBeginPolygon(glTessel, NULL);
gluTessBeginContour(glTessel);
for(size_t i=0 ; i<num ; i++)
gluTessVertex(glTessel, TessPolygon[i].m_Vector, TessPolygon[i].m_Vector);
gluTessEndContour(glTessel);
gluTessEndPolygon(glTessel);
// make mask of the polygon tessellation on the stencil buffer
// write nothing to color buffer
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// start record to the stencil buffer
gluTessBeginPolygon(glTessel, NULL);
gluTessBeginContour(glTessel);
for(size_t i=0 ; i<num ; i++)
gluTessVertex(glTessel, TessPolygon[i].m_Vector, TessPolygon[i].m_Vector);
gluTessEndContour(glTessel);
gluTessEndPolygon(glTessel);
// reset color buffer writing
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// draw test quad to find out the area of the stencil mask
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex2i(-Ortho2dRange.x, -Ortho2dRange.y);
glVertex2i(Ortho2dRange.x, -Ortho2dRange.y);
glVertex2i(Ortho2dRange.x, Ortho2dRange.y);
glVertex2i(-Ortho2dRange.x, Ortho2dRange.y);
glEnd();
glDisable(GL_STENCIL_TEST);
-----------------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.228.167
推
07/09 21:31, , 1F
07/09 21:31, 1F
→
07/09 21:33, , 2F
07/09 21:33, 2F
→
07/09 21:34, , 3F
07/09 21:34, 3F
※ 編輯: riveranb 來自: 61.230.228.167 (07/09 21:43)
推
07/09 21:41, , 4F
07/09 21:41, 4F
→
07/09 21:43, , 5F
07/09 21:43, 5F
推
07/09 21:45, , 6F
07/09 21:45, 6F
→
07/09 22:08, , 7F
07/09 22:08, 7F
→
07/09 22:08, , 8F
07/09 22:08, 8F
→
07/09 22:08, , 9F
07/09 22:08, 9F
→
07/09 22:10, , 10F
07/09 22:10, 10F
→
07/09 22:10, , 11F
07/09 22:10, 11F
推
07/09 22:11, , 12F
07/09 22:11, 12F
→
07/09 22:11, , 13F
07/09 22:11, 13F
→
07/09 22:12, , 14F
07/09 22:12, 14F
→
07/09 22:12, , 15F
07/09 22:12, 15F
推
07/09 22:14, , 16F
07/09 22:14, 16F
→
07/09 22:14, , 17F
07/09 22:14, 17F
→
07/09 22:14, , 18F
07/09 22:14, 18F
→
07/09 22:15, , 19F
07/09 22:15, 19F
→
07/09 22:15, , 20F
07/09 22:15, 20F
→
07/09 22:15, , 21F
07/09 22:15, 21F
→
07/09 22:16, , 22F
07/09 22:16, 22F
→
07/09 22:22, , 23F
07/09 22:22, 23F
※ 編輯: riveranb 來自: 61.230.228.167 (07/09 22:23)
→
07/09 22:24, , 24F
07/09 22:24, 24F
推
07/09 22:26, , 25F
07/09 22:26, 25F
→
07/09 22:34, , 26F
07/09 22:34, 26F
推
07/10 03:06, , 27F
07/10 03:06, 27F
推
07/10 03:30, , 28F
07/10 03:30, 28F
→
07/10 03:30, , 29F
07/10 03:30, 29F
→
07/10 03:33, , 30F
07/10 03:33, 30F
→
07/10 03:34, , 31F
07/10 03:34, 31F
→
07/10 03:34, , 32F
07/10 03:34, 32F
→
07/10 07:03, , 33F
07/10 07:03, 33F
→
07/10 07:04, , 34F
07/10 07:04, 34F
→
07/10 07:04, , 35F
07/10 07:04, 35F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):