以下是基本情況:我有一個繪製2D符號的OpenGL VBO(例如多邊形或線條或其他 - 本質上是簡單的精靈)。我已經定義了原點周圍的頂點(0,0),這樣我就可以在屏幕像素中獲得所需的符號大小。使用OpenGL和VBOs繪製未縮放的2D符號(精靈)
我的當前場景已被翻譯和縮放並正投影顯示模型的特定區域。我現在想要繪製符號,使其以特定模型位置爲中心,但不與模型一起縮放(即繪製到VBO中定義的屏幕像素大小)。我寧願這樣做,而不必以編程方式重新填充符號的頂點緩衝區(即,我想使用平移和/或縮放來在屏幕像素中繪製符號,但是在所需位置 - 這是在「模型」座標中定義)。
一個簡單的例子:顯示使用經度和緯度的地圖,並且用戶已經平移和放大到世界的特定區域。用戶點擊感興趣的站點,並且我想在該點顯示「明星」。明星將隨地圖平移,但保持與用戶放大或縮小的大小相同。因此,在任何時候我都知道在哪裏(經度,緯度座標)將「恆星」「中心」,並且我有一個爲所需大小的恆星(以原點爲中心)定義的VBO(以像素座標表示)。
那麼,在這種情況下渲染「未縮放」符號/精靈的「最佳」方式是什麼?
我想我有一個想法,應該怎麼做,但(1)我不確定這個方法是否正確,(2)它看起來沒有像我期望的那樣工作。下面是我的總體思路的一些僞代碼:
void DrawSymbol()
{
...
[Not Shown: Determine the screen coordinates of the symbol's desired model location]
gl.MatrixMode(GL_PROJECTION);
gl.PushMatrix(); // Store the current projection
gl.LoadIdentity(); // Set projection to identity (no projection)
gl.MatrixMode(GL_MODELVIEW);
gl.PushMastrix(); // Store the current model view (was scaled and translated)
gl.LoadIdentity(); // Set model view matrix to identity (1-to-1 with screen coords)
[Not Shown: Translate the model view so that the origin is at the desired screen coordinate found above]
[Not Shown: Draw the VBO for the symbol] // should draw to raw screen pixels, right?
gl.MatrixMode(GL_MODELVIEW);
gl.PopMatrix(); // Set model view matrix to previous value -- should only effect any future renders, right?
gl.MatrixMode(GL_PROJECTION);
gl.PopMatrix(); // Set projection matrix to previous value -- again, should only effect any future renders, right?
...
}
這個(實際上,我嘗試了通過在需要的屏幕位置直接呈現一個簡單的LineStrip而不是使用翻譯VBO我試過 - 只是爲了測試),它似乎沒有工作。不知道爲什麼。這種方法聽起來不錯嗎?
對此提出建議?
嗯......我怎麼在這裏做一個「沒關係」的SO?我意識到我需要在繪製符號/精靈之前重新建立正投影。這似乎現在正在工作。不過,我不確定是否有更好的方法來做到這一點。我應該把問題留下嗎?自己回答(呃 - 仍然覺得不對)? – FTLPhysicsGuy