This is the render to texture version:
glPushMatrix(); glScalef(1.0f, -1.0f, 1.0f); glCullFace(GL_FRONT); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); //bind the terrain texture tc->Bind(HMAP_TX); //need to inverse cull direction for the reflection glCullFace(GL_FRONT); //draws the terrain, filled tri''s, DRAW_EVERYTHING //indicates that both the under and above water sectsions //should be draw - no clip planes used so far QUADTerrain->Draw(Frustum, DRAW_TEXTURE_FILL, DRAW_EVERYTHING); glCullFace(GL_BACK); glPopMatrix(); //take a snapshot glViewport(0, 0, 512, 512); glBindTexture(GL_TEXTURE_2D, ReflectionTx); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 512, 512, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0 , 0, 800, 600); //draw the terrain tc->Bind(HMAP_TX); QUADTerrain->Draw(Frustum, DRAW_TEXTURE_FILL, DRAW_EVERYTHING); glBindTexture(GL_TEXTURE_2D, ReflectionTx); glEnable(GL_BLEND); glColor4f(1.0f, 1.0f, 1.0f, 0.4f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //bla-bla bla-bla bla - no clue what goes here except for these 4 lines glTexGenf(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenf(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenf(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenf(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); //these lines are probably very incorrect...//glTexGenfv(GL_S, GL_TEXTURE_GEN_MODE, GLCamera->GetPosition());//glTexGenfv(GL_T, GL_TEXTURE_GEN_MODE, GLCamera->GetPosition()); //enabling these messes up the texture//glEnable(GL_TEXTURE_GEN_S);//glEnable(GL_TEXTURE_GEN_T); //draw the water glBegin(GL_POLYGON); glTexCoord2f(1, 0); glVertex3f(0, 20, 1024); glTexCoord2f(0, 0); glVertex3f(1024, 20, 1024); glTexCoord2f(0, 1); glVertex3f(1024, 20, 0); glTexCoord2f(1, 1); glVertex3f(0, 20, 0); glEnd();
And here''s the stencil version, this one''s a bit longer:
//taken from NeHe''s reflections tutorial. as far as i //understand this should represent the plane equation //for the reflecting surface (which should be (0, 60, 0)), //but then I get no reflection double eqr[] = {0.0f, -1.0f, 0.0f, 0.0f}; glCullFace(GL_BACK); tc->Bind(HMAP_TX); //draws only the underwater part - no clip planes used so far QUADTerrain->Draw(Frustum, DRAW_TEXTURE_FILL, DRAW_BELOW); //render water surface to stencil glColorMask(0, 0, 0, 0); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 1, 0xffffffff); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); tc->Bind(TX_WATER); glBegin(GL_POLYGON); glTexCoord2f(0, 0); glVertex3f(0, 60, 0); glTexCoord2f(0, 1); glVertex3f(0, 60, 1024); glTexCoord2f(1, 1); glVertex3f(1024, 60, 1024); glTexCoord2f(1, 0); glVertex3f(1024, 60, 0); glEnd(); glEnable(GL_DEPTH_TEST); glColorMask(1, 1, 1, 1); glStencilFunc(GL_EQUAL, 1, 0xffffffff); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glEnable(GL_CLIP_PLANE0); glClipPlane(GL_CLIP_PLANE0, eqr); glPushMatrix(); glScalef(1.0f, -1.0f, 1.0f); glCullFace(GL_BACK); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); //mmkay - the messy part - according to vincoof''s instructions one part is missing here glDepthFunc(GL_EQUAL); glEnable(GL_BLEND); glCullFace(GL_FRONT); tc->Bind(HMAP_TX); //draw the whole lot, although jsut drawing anything //that remains above water level should suffice QUADTerrain->Draw(Frustum, DRAW_TEXTURE_FILL, DRAW_EVERYTHING); glCullFace(GL_BACK); glPopMatrix(); glDisable(GL_CLIP_PLANE0); glDisable(GL_STENCIL_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_TEXTURE_2D); tc->Bind(TX_WATER); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(1, 1, 1, .4); glBegin(GL_POLYGON); glTexCoord2f(0, 0); glVertex3f(0, 60, 0); glTexCoord2f(0, 1); glVertex3f(0, 60, 1024); glTexCoord2f(1, 1); glVertex3f(1024, 60, 1024); glTexCoord2f(1, 0); glVertex3f(1024, 60, 0); glEnd(); glDisable(GL_BLEND); //draw everything above the water tc->Bind(HMAP_TX); QUADTerrain->Draw(Frustum, DRAW_TEXTURE_FILL, DRAW_ABOVE);
RipTorn - maybe you wouldn''t mind sharing the part of your code where you calculate the tex coords (using glFrustum) - as I getmore idea of what''s really going on, I''ll try and optimize the thing. The whole concept isn''t at all that difficult - I understand how it works, but there''s those few things that just don''t make sense...
Crispy