作者riveranb (River)
看板GameDesign
標題[程式] ABOUT STENCIL BUFFER Question
時間Sat Jul 9 21:22:52 2011
有個問題想請教板上的各位前輩。
小弟我寫 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
推 NDark:為什麼你不用 select buffer 07/09 21:31
→ riveranb:那個我已經做完了..SelectBuffer只能做矩形選取不是嗎? 07/09 21:33
→ riveranb:我目前要做的是complex(concave) shape selection 07/09 21:34
※ 編輯: riveranb 來自: 61.230.228.167 (07/09 21:43)
推 NDark:那個天才告訴你他只能做矩形的? 07/09 21:41
→ riveranb:那要如何設定複雜多邊形區域? 07/09 21:43
→ riveranb:不好意思該文件看完了,但似乎沒找到我要的資訊? 07/09 22:08
→ riveranb:select-buffer由gluPickMatrix 設定選取區.. 07/09 22:08
→ riveranb:因此我一直認為Select-Buffer只能做矩形選取 07/09 22:08
→ riveranb:比較可能用到的技術可能就是unique color render這方面 07/09 22:10
→ riveranb:但這個似乎跟Select Buffer無太多正相關? 07/09 22:10
推 NDark:gluPickMatrix 只是決定點選點大小而已. 07/09 22:11
→ NDark:我通常都設1x1 07/09 22:11
→ riveranb:對..所以gluPickMatrix只能決定mxn的區域不是嗎 07/09 22:12
→ riveranb:這跟我要的是arbitrary shape area的定義似乎不相同 07/09 22:12
推 NDark:被選取物又不限一定要MxN 07/09 22:14
→ NDark:你的點選筆觸為什麼要是多邊形的? 07/09 22:14
→ riveranb:你沒弄懂我的意思..我想單一次設定就達到目標 07/09 22:14
→ riveranb:如果要用gluPickMatrix做的話..那不是要做mxn次的1x1點? 07/09 22:15
→ riveranb:如果我誤會你的意思那不好意思 07/09 22:15
→ riveranb:客戶需求 -__- 我也不想做這種麻煩的功能 07/09 22:15
→ NDark:"複雜多邊型選取功能" 目的是 "選一個多邊形" 沒錯吧? 07/09 22:16
→ riveranb:不對..是選取"與複雜形狀有交集"的物體 07/09 22:22
※ 編輯: riveranb 來自: 61.230.228.167 (07/09 22:23)
→ riveranb:原文講的不夠清楚, 已更新 07/09 22:24
推 NDark:還是看不懂你的需求XD 07/09 22:26
→ riveranb:那就先想像成可以用圓形來選取形狀內交集到的物體..ok嗎? 07/09 22:34
推 epephanylo:類似挖冰其林的動作嘛? 07/10 03:06
推 ddavid:他是想說,當他的例如滑鼠遊標是可以不規則形狀的,而且這 07/10 03:30
→ ddavid:形狀上的每一點指著的東西「全部」都要選得到。是這樣吧? 07/10 03:30
→ ddavid:而且他是要確實的形狀,所以不能使用外切大方框來簡化。 07/10 03:33
→ ddavid:總之,他要的不是「被選物可不規則」,而是「選取用的遊標 07/10 03:34
→ ddavid:可不規則外形」吧,我想。 07/10 03:34
→ riveranb:樓上正解 07/10 07:03
→ riveranb:其實如果有人知到我的stencil test為什麼錯也請告訴我 07/10 07:04
→ riveranb:目前我正苦惱stencil哪裡做錯了..囧 07/10 07:04