[程式] ABOUT STENCIL BUFFER Question

看板GameDesign作者 (River)時間14年前 (2011/07/09 21:22), 編輯推噓8(8027)
留言35則, 4人參與, 最新討論串1/2 (看更多)
有個問題想請教板上的各位前輩。 小弟我寫 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
為什麼你不用 select buffer
07/09 21:31, 1F

07/09 21:33, , 2F
那個我已經做完了..SelectBuffer只能做矩形選取不是嗎?
07/09 21:33, 2F

07/09 21:34, , 3F
我目前要做的是complex(concave) shape selection
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 22:08, , 7F
不好意思該文件看完了,但似乎沒找到我要的資訊?
07/09 22:08, 7F

07/09 22:08, , 8F
select-buffer由gluPickMatrix 設定選取區..
07/09 22:08, 8F

07/09 22:08, , 9F
因此我一直認為Select-Buffer只能做矩形選取
07/09 22:08, 9F

07/09 22:10, , 10F
比較可能用到的技術可能就是unique color render這方面
07/09 22:10, 10F

07/09 22:10, , 11F
但這個似乎跟Select Buffer無太多正相關?
07/09 22:10, 11F

07/09 22:11, , 12F
gluPickMatrix 只是決定點選點大小而已.
07/09 22:11, 12F

07/09 22:11, , 13F
我通常都設1x1
07/09 22:11, 13F

07/09 22:12, , 14F
對..所以gluPickMatrix只能決定mxn的區域不是嗎
07/09 22:12, 14F

07/09 22:12, , 15F
這跟我要的是arbitrary shape area的定義似乎不相同
07/09 22:12, 15F

07/09 22:14, , 16F
被選取物又不限一定要MxN
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
如果要用gluPickMatrix做的話..那不是要做mxn次的1x1點?
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
還是看不懂你的需求XD
07/09 22:26, 25F

07/09 22:34, , 26F
那就先想像成可以用圓形來選取形狀內交集到的物體..ok嗎?
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
其實如果有人知到我的stencil test為什麼錯也請告訴我
07/10 07:04, 34F

07/10 07:04, , 35F
目前我正苦惱stencil哪裡做錯了..囧
07/10 07:04, 35F
文章代碼(AID): #1E65OlMu (GameDesign)
文章代碼(AID): #1E65OlMu (GameDesign)