2010-12-04 24 views
4

我正在使用OpenGL,我試圖創建一個具有反射表面的球體。我有它的反映,但反映是不正確的。反射中的物體應該根據曲面的曲線彎曲和變形,而不是直接反射。我沒有使用GL_STENCIL,所以非常感謝。我已經提供了一些代碼,比如創建球體和繪製方法。如果有人需要更多讓我知道。使用帶球體的GL_STENCIL的OpenGL

創作:

sphere = gluNewQuadric(); 
gluQuadricDrawStyle(sphere, GLU_FILL); 
gluQuadricNormals(sphere, GLU_SMOOTH); 
gluSphere(sphere, 1, 100, 100); 
gluDeleteQuadric(sphere); 

圖:

glClearColor (0.0,0.0,0.0,1); 
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 
glLoadIdentity(); 

glTranslatef(0, 0, -10); 

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); //disable the color mask 
glDepthMask(GL_FALSE); //disable the depth mask 
glEnable(GL_STENCIL_TEST); //enable the stencil testing 
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF); 
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); //set the stencil buffer to replace our data 

sphereDraw(); //the mirror surface 

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); //enable the color mask 
glDepthMask(GL_TRUE); //enable the depth mask 

glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF); 
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //set the stencil buffer to keep our next lot of data 

glPushMatrix(); 
glScalef(1.0f, -1.0f, 1.0f); //flip the reflection vertically 
glTranslatef(0,2,-20); //translate the reflection onto the drawing plane 
glRotatef(angle,0,1,0); //rotate the reflection 
//draw object as our reflection 
glPopMatrix(); 

glDisable(GL_STENCIL_TEST); //disable the stencil testing 

glEnable(GL_BLEND); //enable alpha blending 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set the blending function 
sphereDraw(); //draw our bench 
glDisable(GL_BLEND); //disable alpha blending 

//draw object 

由於我是新來使用GL_STENCIL我不知道,如果只是一些小事情,或者做更多的工作,以檢測反射角度。

+0

順便說一句,如果你做的第一件事是開始使用着色器,你的生活可能會變得更容易。模板反射不會因其性質而「彎曲」。 – Kos 2010-12-04 23:48:02

回答

2

您是否考慮過使用reflection/environment mapping

有兩種主要形式。 Spherical environment mapping通常具有預先計算的環境地圖。但是,它可以動態完成。它的主要缺點是它依賴於視圖。

另一個系統是Cubic Environment mapping。 Cubic非常容易設置,並且只需在6個不同的方向(即在立方體的每個面上)渲染6次場景。立方體env映射是視圖獨立的。

還有一個系統位於球形和立方之間。它叫dual paraboloid environment mapping。它的缺點是產生雙拋物面是非常複雜的(像球面),但是(像立方體)它是獨立的視圖。

+0

我已經看過立方圖,但看起來有點多。我問這個問題的原因是因爲一個反射和這個NeHe Tutorial,但我寧願有鏡像比影子。儘管感謝您的評論,但如果我更深入地研究它,這將值得探索。 – ars265 2010-12-04 23:25:50