This is a very very crude example and it doesn't even use the stencil buffer but might give you a little bit of an idea how that sort of reflections work:
option explicit
'$include once: 'GL/glfw.bi'
#include "fbgfx.bi"
type vertex
as single x,y,z
as single r,g,b
end type
type object
as integer num_verts
as vertex pointer vertex_list
end type
sub initialise_opengl(byval wwidth as integer,byval height as integer)
screen 19,32,2,GFX_OPENGL
glMatrixMode GL_PROJECTION '' Select The Projection Matrix
glLoadIdentity '' Reset The Projection Matrix
gluPerspective 45.0, wwidth/height, 1.0, 64000.0 '' Calculate The Aspect Ratio Of The Window
gldepthrange(0.0,1.0)
glMatrixMode GL_MODELVIEW '' Select The Modelview Matrix
glLoadIdentity '' Reset The Projection Matrix
glShadeModel(GL_SMOOTH) '' Enables Smooth Color Shading
glClearColor(0.0, 0.0, 0.0, 0.0) '' This Will Clear The Background Color To Black
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glenable(gl_depth_test)
glcullface(gl_front)
glenable(gl_cull_face)
gldisable(gl_lighting)
glfrontface(gl_cw)
end sub
cube_data:
data 36
data -1,-1,-1,-1,-1,1,-1,1,1
data -1,1,1,-1,1,-1,-1,-1,-1
data -1,-1,1,1,-1,1,1,1,1
data 1,1,1,-1,1,1,-1,-1,1
data 1,-1,1,1,-1,-1,1,1,-1
data 1,1,-1,1,1,1,1,-1,1
data 1,-1,-1,-1,-1,-1,-1,1,-1
data -1,1,-1,1,1,-1,1,-1,-1
data -1,1,-1,-1,1,1,1,1,1
data 1,1,1,1,1,-1,-1,1,-1
data 1,-1,-1,1,-1,1,-1,-1,1
data -1,-1,1,-1,-1,-1,1,-1,-1
function create_cube()as object pointer
dim as object pointer cube=callocate(len(object))
dim as integer i
restore cube_data
read cube->num_verts
cube->vertex_list=callocate(len(vertex)*cube->num_verts)
for i=0 to cube->num_verts-1
read cube->vertex_list[i]->x,cube->vertex_list[i]->y,cube->vertex_list[i]->z
cube->vertex_list[i]->r=rnd
cube->vertex_list[i]->g=rnd
cube->vertex_list[i]->b=rnd
next
return cube
end function
'draw an object, if reflection=-1 then it is drawn upsidedown below the alpha plane
sub draw_object(object as object pointer,x as single,y as single,z as single,reflection as single=1.0)
'in a reflection the winding of tris is reversed
if reflection=-1.0 then
glcullface(gl_back)
else
glcullface(gl_front)
end if
glloadidentity()
gltranslatef(x,y*reflection,z)
dim as integer i
dim as vertex pointer current_vertex
glbegin(gl_triangles)
for i=0 to object->num_verts-1
current_vertex=@object->vertex_list[i]
glcolor3f(current_vertex->r,current_vertex->g,current_vertex->b)
glvertex3f(current_vertex->x,current_vertex->y*reflection,current_vertex->z)
next
glend()
end sub
'draw an alpha blended quad
sub draw_alpha_plane()
glloadidentity()
glcullface(gl_front)
glenable(gl_blend)
glblendfunc(gl_src_alpha,gl_one_minus_src_alpha)
glbegin(gl_quads)
glcolor4f(.4,.4,1.0,0.9)
glvertex3f(-30.0,0.0,-30.0)
glvertex3f(-30.0,0.0, 30.0)
glvertex3f( 30.0,0.0, 30.0)
glvertex3f( 30.0,0.0,-30.0)
glend()
gldisable(gl_blend)
end sub
sub main()
initialise_opengl(640,480)
'make 2 cubes and give them coordinates
dim cube1 as object pointer=create_cube()
dim as single x1=0.0,y1=4.9,z1=-10.0,vy1=0.0
dim cube2 as object pointer=create_cube()
dim as single x2=5.0,y2=2.5,z2=-7.0,vy2=0.0
'setup the camera
dim as single h
glMatrixMode GL_PROJECTION
gltranslatef(0.0,-1.0,0.0)
glMatrixMode GL_MODELVIEW
do
'draw cubes (the ones in the reflection... -1)
draw_object(cube1,x1,y1,z1,-1)
draw_object(cube2,x2,y2,z2,-1)
'draw the surface they're reflected on
draw_alpha_plane()
'draw the actual cubes
draw_object(cube1,x1,y1,z1)
draw_object(cube2,x2,y2,z2)
'control the cubes bouncing
vy1+=.006
y1-=vy1
if y1<1.0 then
y1=2.0-y1
vy1=-vy1
end if
vy2+=.006
y2-=vy2
if y2<1.0 then
y2=2.0-y2
vy2=-vy2
end if
'control the camera
glMatrixMode GL_PROJECTION
gltranslatef(0.2,-0.06*sin(h),0.0)
glrotatef(1.0,0.0,1.0,0.0)
glMatrixMode GL_MODELVIEW
h+=0.05
flip
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
sleep 10
loop while inkey$=""
end sub
main
In this case I am really cheating and it can't handle the objects being rotated or anything but reflections are basically the same objects drawn (sort of the other way round, left becomes right or up becomes down) on the other side of the reflective plane. The stencil buffer can be used with this process to mask the areas of the screen where the reflection would be seen.