Posted: 12th May 2003 0:14
I don't know if this is usefull to anyone, or if there are better ways already to do simple transparency effects, but this is easy and works quite fast. Try it out and use it if you want to. ;D

This is DBPro only.


+ Code Snippet
sync on : sync rate 0
set display mode 1024,768,32
randomize timer()

rem create green texture
for x=0 to 128
for y=0 to 128
ink rgb(rnd(100),200+rnd(50),rnd(100)),0
dot x,y
next y
next x
get image 1,0,0,128,128

rem create opacity map
for x=0 to 128
for y=0 to 128
x#=x : y#=y
ink rgb((sin((x#/128.00)*180.00)*sin((y#/128.00)*180.00))*255,(sin((x#/128.00)*180.00)*sin((y#/128.00)*180.00))*255,(sin((x#/128.00)*180.00)*sin((y#/128.00)*180.00))*255),0
dot x,y
next y
next x
get image 2,0,0,128,128

ink rgb(255,255,255),0




rem make cube object
make object sphere 1,10
texture object 1,1

rem make matrix
make matrix 1,1000,1000,10,10




rem position object
move camera 20
position object 1,camera position x(),camera position y(),camera position z()
move camera -20


rem vars
time=timer()

transparencytype=0
percentage#=100.00



rem LOOP

do

text 0,0,str$(screen fps())
text 0,20,str$(percentage#)
text 0,60,"Press 't' for transparency effect"
text 0,80,"Press 'o' for opacity mapping"
text 0,100,"Press up and down to change percentage"

rem time stuff
dt#=(timer()-time)/1000.0
time=timer()

rem rotate object
yrotate object 1,wrapvalue(object angle y(1)+(120*dt#))

if transparencytype=0
text 0,40,"TRANSPARENCY EFFECT"
if downkey()=1
percentage#=percentage#-(50.0*dt#)
if percentage#100.00 then percentage#=100.00
settransparency(1,1,percentage#)
endif
if upkey()=1
percentage#=percentage#+(50.0*dt#)
if percentage#100.00 then percentage#=100.00
settransparency(1,1,percentage#)
endif
endif

if transparencytype=1
text 0,40,"OPACITY MAPPING"
if downkey()=1
percentage#=percentage#-(50.0*dt#)
if percentage#100.00 then percentage#=100.00
setopacitymappingon(1,1,2,percentage#)
endif
if upkey()=1
percentage#=percentage#+(50.0*dt#)
if percentage#100.00 then percentage#=100.00
setopacitymappingon(1,1,2,percentage#)
endif
endif

if inkey$()="t" then transparencytype=0 : settransparency(1,1,percentage#)
if inkey$()="o" then transparencytype=1 : setopacitymappingon(1,1,2,percentage#)


sync
loop









rem THE FUNCTIONS

function settransparency(object,image,percentage as float)
make memblock from image image,image
for x=1 to memblock dword(image,0)
for y=1 to memblock dword(image,4)
write memblock byte image,((((y-1)*memblock dword(image,4))+(x-1))*4)+15,int(percentage*2.55)
next y
next x
make image from memblock image,image
set object transparency object,1
endfunction



function setopacitymappingon(object,image,opacitymap,percentage as float)
make memblock from image image,image
make memblock from image opacitymap,opacitymap
part#=percentage/100.00
for x=1 to memblock dword(image,0)
for y=1 to memblock dword(image,4)
pos=((((y-1)*memblock dword(image,4))+(x-1))*4)+15
write memblock byte image,pos,part#*memblock byte(opacitymap,pos-3)
next y
next x
make image from memblock image,image
set object transparency object,1
endfunction






Kevil
Posted: 12th May 2003 2:15
Again, excellent work Kevil. Seems you're a pretty talented coder

This is VERY useful.
Posted: 12th May 2003 2:24
Found a bug though. If you try and view a reflective surface through an object ghosted using this method, you can just see the backdrop. Not sure if this is DBP or your code though.
Posted: 12th May 2003 2:34
I think it is a DBPro bug, because I have seen it before with trees. Now where was that.
http://www.darkbasicpro.com/apollo/view.php?t=9953&b=1

Maybe it's the same problem?
Anyway, the only thing I did is change the image data, so that couldn't be the problem I guess.

Kevil
Posted: 12th May 2003 2:37
^^ and another, it can't handle images larger than 128x128, but by the looks of it, it should be easily fixable.
Posted: 12th May 2003 11:20
Hmmm, I think it has to do with the opacity mapping function, right? Because the opacity image needs to have the same size as the other image. I'll fix it, give me a minute .

Kevil
Posted: 12th May 2003 11:50
Ok, it's fixed this way. And now I hope that the memblocks used will not come in the way with the memblock you use yourself. And I also deleted the memblocks now after usage.

+ Code Snippet
sync on : sync rate 0
set display mode 1024,768,32
randomize timer()

rem create green texture
for x=0 to 128
for y=0 to 128
ink rgb(rnd(100),200+rnd(50),rnd(100)),0
dot x,y
next y
next x
get image 1,0,0,128,128

rem create opacity map
for x=0 to 128
for y=0 to 128
x#=x : y#=y
ink rgb((sin((x#/128.00)*180.00)*sin((y#/128.00)*180.00))*255,(sin((x#/128.00)*180.00)*sin((y#/128.00)*180.00))*255,(sin((x#/128.00)*180.00)*sin((y#/128.00)*180.00))*255),0
dot x,y
next y
next x
get image 2,0,0,128,128

ink rgb(255,255,255),0




rem make cube object
make object sphere 1,10
texture object 1,1

rem make matrix
make matrix 1,1000,1000,10,10




rem position object
move camera 20
position object 1,camera position x(),camera position y(),camera position z()
move camera -20


rem vars
time=timer()

transparencytype=0
percentage#=100.00



rem LOOP

do

text 0,0,str$(screen fps())
text 0,20,str$(percentage#)
text 0,60,"Press 't' for transparency effect"
text 0,80,"Press 'o' for opacity mapping"
text 0,100,"Press up and down to change percentage"

rem time stuff
dt#=(timer()-time)/1000.0
time=timer()

rem rotate object
yrotate object 1,wrapvalue(object angle y(1)+(120*dt#))

if transparencytype=0
text 0,40,"TRANSPARENCY EFFECT"
if downkey()=1
percentage#=percentage#-(50.0*dt#)
if percentage#<0.00 then percentage#=0.00
if percentage#>100.00 then percentage#=100.00
settransparency(1,1,percentage#)
endif
if upkey()=1
percentage#=percentage#+(50.0*dt#)
if percentage#<0.00 then percentage#=0.00
if percentage#>100.00 then percentage#=100.00
settransparency(1,1,percentage#)
endif
endif

if transparencytype=1
text 0,40,"OPACITY MAPPING"
if downkey()=1
percentage#=percentage#-(50.0*dt#)
if percentage#<0.00 then percentage#=0.00
if percentage#>100.00 then percentage#=100.00
setopacitymappingon(1,1,2,percentage#)
endif
if upkey()=1
percentage#=percentage#+(50.0*dt#)
if percentage#<0.00 then percentage#=0.00
if percentage#>100.00 then percentage#=100.00
setopacitymappingon(1,1,2,percentage#)
endif
endif

if inkey$()="t" then transparencytype=0 : settransparency(1,1,percentage#)
if inkey$()="o" then transparencytype=1 : setopacitymappingon(1,1,2,percentage#)


sync
loop









rem THE FUNCTIONS

function settransparency(object,image,percentage as float)
make memblock from image image+100,image
for x=1 to memblock dword(image+100,0)
for y=1 to memblock dword(image+100,4)
write memblock byte image+100,((((y-1)*memblock dword(image+100,4))+(x-1))*4)+15,int(percentage*2.55)
next y
next x
make image from memblock image,image+100
delete memblock image+100
set object transparency object,1
endfunction



function setopacitymappingon(object,image,opacitymap,percentage as float)
make memblock from image image+100,image
make memblock from image opacitymap+50,opacitymap
part#=percentage/100.00
opacsize=memblock dword(opacitymap+50,0)*memblock dword(opacitymap+50,4)*4
for x=1 to memblock dword(image+100,0)
for y=1 to memblock dword(image+100,4)
pos=((((y-1)*memblock dword(image+100,4))+(x-1))*4)+15
write memblock byte image+100,pos,part#*memblock byte(opacitymap+50,(pos-(int(pos/opacsize)*opacsize))-3)
next y
next x
make image from memblock image,image+100
delete memblock image+100
delete memblock opacitymap+50
set object transparency object,1
endfunction
Posted: 12th May 2003 13:34
This is ecellent work, nice 1 man
Posted: 12th May 2003 14:27
I'm shocked! He is coping my ideas! I've made probably the same code for creating mesh matrix few months before! Then I've made such transparency effect and then I've made real time shadows. I'm sure that his next program will be something like "shadow generator"
Posted: 12th May 2003 14:31
:-s .
LOL, I promise I will not make a shadow generator.
I've never seen your code before. Maybe because I'm new to DBPro.

Kevil
Posted: 12th May 2003 14:40
@neteraser

Kevil is doing this as a learning curve, calm down. I don't really care who does the work, both of you have produced some pretty useful stuff.
Posted: 12th May 2003 14:58
> I've never seen your code before
I know no1 (except DMiTR0S) have seen my code. But I didn't said that you've copied it. I was simply shocked, because you have exacly the same ideas as I and DMiTR0S.

> I'm new to DBPro
You are very talented programmist in that case!

BTW, MeshMatrix(R) and (C) NetEraser(R) (net-eraser@mail.ru(R))
Posted: 12th May 2003 15:09
Thanx .
Rob is right. I'm doing this to learn memblocks and I think I understand them well now. They can be so useful.

BTW do you have a demo with that shadow generator? Sounds interesting .


MeshMatrix(R) and (C) NetEraser(R) (net-eraser@mail.ru(R))


Ok.... What does it mean????.

Kevil
Posted: 16th May 2003 9:52
it doesnt matter what i do, both versions return a memblock position out of range on line 141 as soon as i press a key T_T
Posted: 16th May 2003 11:11
Impossible?!

Kevil
Posted: 16th May 2003 14:32
I kid you not :-s I tried both the sources and copy pasted exactally n everything
Posted: 16th May 2003 14:53
You must be using DBC then, because else I don't know what's wrong.

Kevil
Posted: 16th May 2003 14:55
nope, im using darkbasic pro with patch 4 -_-
Posted: 16th May 2003 17:24
Ok, that means you have no memory . LOL
I honestly don't know what could be wrong. Seems to work for most people.

Kevil