Post by DJLinux on Jun 8, 2011 4:27:06 GMT -5
save it in your "Basic4GL\Programs" folder or you wan't see the texture from
"Basic4GL\Programs\Textures\floor01.jpg"
Unbelievable but true the dynamic light map is only 8x8 pixels.
Joshy
"Basic4GL\Programs\Textures\floor01.jpg"
Unbelievable but true the dynamic light map is only 8x8 pixels.
Joshy
const SIZE as integer = 8
const STEPS as single = 1.0 / SIZE
type SURFACE
v(3,2) as single
m0(2) as single
m1(2) as single
'm2(2) as single
sd as single
td as single
end type
dim lighting as integer = 1
dim Surfaces(5) as SURFACE
dim SurfaceTexture as integer
dim light_pos(2) as single
dim buffer(SIZE*SIZE-1) as integer
dim LightTexture as integer
function CreateSurface(v0() as single,v1() as single,v2() as single,v3() as single) as SURFACE
dim s as SURFACE
s.v(0) = v0:s.v(1) = v1:s.v(2) = v2:s.v(3) = v3
' x axis in texture space
s.m0 = s.v(3)-s.v(0):s.sd=sqrt(s.m0*s.m0):s.m0=normalize(s.m0)
' y axis in texture space
s.m1 = s.v(1)-s.v(0):s.td=sqrt(s.m1*s.m1):s.m1=normalize(s.m1)
return s
end function
sub CreateScene()
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDisable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
glColor4f(1.0, 1.0, 1.0, 1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
dim ratio as single = WindowWidth()
ratio = ratio / WindowHeight()
gluPerspective(45.0, ratio, 0.1, 200.0)
glMatrixMode(GL_MODELVIEW)
glActiveTexture(GL_TEXTURE1)
glEnable(GL_TEXTURE_2D)
LightTexture = glGenTexture()
glBindTexture(GL_TEXTURE_2D, LightTexture)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
glActiveTexture(GL_TEXTURE0)
glEnable(GL_TEXTURE_2D)
SurfaceTexture = LoadMipMapTexture("Textures\floor01.jpg")
glBindTexture(GL_TEXTURE_2D, SurfaceTexture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
Surfaces(0)=CreateSurface(vec3(-1.0,-1.0, 3.0),vec3(-1.0,-1.0, 1.0),vec3( 1.0,-1.0, 1.0),vec3( 1.0,-1.0, 3.0))
Surfaces(1)=CreateSurface(vec3( 1.0, 1.0,-1.0),vec3( 1.0,-1.0,-1.0),vec3(-1.0,-1.0,-1.0),vec3(-1.0, 1.0,-1.0))
Surfaces(2)=CreateSurface(vec3(-1.0, 1.0, 1.0),vec3(-1.0, 1.0,-1.0),vec3( 1.0, 1.0,-1.0),vec3( 1.0, 1.0, 1.0))
Surfaces(3)=CreateSurface(vec3( 1.0,-1.0, 1.0),vec3( 1.0,-1.0,-1.0),vec3(-1.0,-1.0,-1.0),vec3(-1.0,-1.0, 1.0))
Surfaces(4)=CreateSurface(vec3(-1.0, 1.0, 1.0),vec3(-1.0, 1.0,-1.0),vec3(-1.0,-1.0,-1.0),vec3(-1.0,-1.0, 1.0))
Surfaces(5)=CreateSurface(vec3( 1.0,-1.0, 1.0),vec3( 1.0,-1.0,-1.0),vec3( 1.0, 1.0,-1.0),vec3( 1.0, 1.0, 1.0))
end sub
sub DynamicLightMap(&s as SURFACE)
dim d as single
dim t as single
dim ts as single
dim tt as single
dim i as integer
dim j as integer
dim k as integer
dim p(2) as single
dim tdtm1(2) as single
dim tdt as single
dim sdt as single
for i = 0 to SIZE-1
ts = 0.0
tdt = s.td * tt
tdtm1 = tdt * s.m1
tdtm1 = tdtm1+s.v(0)-light_pos
for j = 0 to SIZE-1
sdt = s.sd * ts
p = s.m0 * sdt + tdtm1
d = p * p
' fill the light map
if (d < 1.0) then
buffer(k+j)=196
else
buffer(k+j)=196/d
end if
ts = ts + steps
next
k=k+SIZE
tt = tt + steps
next
glTexImage2D(GL_TEXTURE_2D,0,1,SIZE,SIZE,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,buffer)
end sub
sub RenderScene()
dim i as integer
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0, 0.0, -5.0)
glActiveTexture(GL_TEXTURE0)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, SurfaceTexture)
glActiveTexture(GL_TEXTURE1)
if(lighting) then glEnable(GL_TEXTURE_2D):end if
for i = 0 to 5
if(lighting) then
DynamicLightMap(Surfaces(i))
end if
glBegin(GL_QUADS)
glMultiTexCoord2f(GL_TEXTURE0, 0,0)
glMultiTexCoord2f(GL_TEXTURE1, 0,0)
glVertex3fv(Surfaces(i).v(0))
glMultiTexCoord2f(GL_TEXTURE0, 0,1)
glMultiTexCoord2f(GL_TEXTURE1, 0,1)
glVertex3fv(Surfaces(i).v(1))
glMultiTexCoord2f(GL_TEXTURE0, 1,1)
glMultiTexCoord2f(GL_TEXTURE1, 1,1)
glVertex3fv(Surfaces(i).v(2))
glMultiTexCoord2f(GL_TEXTURE0, 1,0)
glMultiTexCoord2f(GL_TEXTURE1, 1,0)
glVertex3fv(Surfaces(i).v(3))
glEnd()
next
glDisable(GL_TEXTURE_2D)
glActiveTexture(GL_TEXTURE0):glDisable(GL_TEXTURE_2D)
glColor3f(1,1,0)
glBegin(GL_QUADS)
glVertex3f(light_pos(0) - 0.02, light_pos(1) + 0.02, light_pos(2))
glVertex3f(light_pos(0) - 0.02, light_pos(1) - 0.02, light_pos(2))
glVertex3f(light_pos(0) + 0.02, light_pos(1) - 0.02, light_pos(2))
glVertex3f(light_pos(0) + 0.02, light_pos(1) + 0.02, light_pos(2))
glEnd()
glColor4f(1.0, 1.0, 1.0, 1.0)
end sub
CreateScene()
dim light_rot as single
while True
if inkey$()="l" then
lighting=1-lighting
end if
light_pos(0) = cos(light_rot) * 0.9
light_pos(1) = sin(light_rot*2) * 0.8
light_pos(2) = 1 + sin(light_rot)*2
light_rot = light_rot + 0.01
RenderScene()
SwapBuffers()
Sleep(20)
Wend