c++ 如何使用OpenGL在2D模式下渲染完美的線框矩形?
編輯:只是這樣你知道:我還沒有完全解決這個問題,目前我使用的是0.5px的偏移,似乎有效,但正如其他人所說,這不是“正確”的解決方案.所以我正在尋找真正的交易,鑽石出口規則解決方案根本沒有工作.
我相信這可能是顯示卡的錯誤,但如果是,那麼任何專業的程式員都應該有這樣的防彈解決方案呢?
編輯:我現在買了一個新的nvidia卡(以前有ATI卡),我仍然遇到這個問題.我也在許多遊戲中看到同樣的錯誤.所以我想這是不可能修復一個乾淨的方式?
這是錯誤的形象:
你如何克服這個問題?如果可能,最好是非著色器解決方案.我試圖設定第一行的偏移量,當我自己繪製4個單獨的行,而不是使用線框模式,但沒有很好的工作:如果矩形大小改變,它有時看起來完美的矩形,但有時甚至比我的修復之前更糟.
這是我渲染四邊形的方式:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_QUADS); glVertex2f(...); glVertex2f(...); glVertex2f(...); glVertex2f(...); glEnd(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
是的,我知道我可以使用頂點陣列或VBO,但這不是重點.
我也試過GL_LINE_LOOP,但它沒有修復這個錯誤.
編輯:一個解決方案,迄今為止工作:ofollow,noindex" target="_blank">Opengl pixel perfect 2D drawing by Lie Ryan:
Note that OpenGL coordinate space has no notion of integers,
everything is a float and the “centre” of an OpenGL pixel is really at
the 0.5,0.5 instead of its top-left corner. Therefore, if you want a
you really had to draw a
line from 0.5,0.5 to 10.5,10.5.
This will be especially apparent if you turn on anti-aliasing, if you
have anti-aliasing and you try to draw from 50,0 to 50,100 you may see
a blurry 2px wide line because the line fell in-between two pixels.
這不是一個bug,這正是遵循規範.沒有繪製一行的最後一個畫素,以防止使用以下線段進行過度繪製,這會導致混合問題.解決方案:傳送最後一個頂點兩次.
程式碼更新
// don't use glPolygonMode, it doesn't // do what you think it does glBegin(GL_LINE_STRIP); glVertex2f(a); glVertex2f(b); glVertex2f(c); glVertex2f(d); glVertex2f(a); glVertex2f(a); // resend last vertex another time, to close the loop glEnd();
BTW:你應該學習如何使用頂點陣列.立即模式(glBegin,glEnd,glVertex呼叫)已經從OpenGL-3.x核心和之後移除.
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/10320332/how-to-render-perfect-wireframed-rectangle-in-2d-mode-with-opengl