Render to Texture: Read on.....

BlitzMax Forums/BlitzMax Programming/Render to Texture: Read on.....

TartanTangerine (was Indiepath)(Posted 2006) [#1]
*It does not require any tweaking of any official modules*

And yes it works in DirectX and OpenGL (Win/OSX and probably Linux).

*** NOW WITH GREYSCALE RENDER : http://www.blitzbasic.com/Community/posts.php?topic=56099

What does it do? It lets you create images that can be used as render targets. Basically anything you can draw to screen you can now draw to an image.

If you don't know what to use it for then you don't need it.

So what do you want to do?

1) Moan about some feature that it does not support?
2) Let me post the module and get on with my business?

Your choice...

Email me if you want a copy of the module.


Big&(Posted 2006) [#2]
Post the mod :)


Scott Shaver(Posted 2006) [#3]
+1


deps(Posted 2006) [#4]
Post mod, ignore moaning, do bugfixes if a nasty one is found.

And thank you verry much! :)


TartanTangerine (was Indiepath)(Posted 2006) [#5]
I'll release it tomorrow, I got too much going on right now.

The following example code creates an empty image, when you click the mouse it renders a white square and a Purple Circle to the image and attaches it to the mouse.
Strict

Import Indiepath.Render2Texture

Graphics 640,480

tRender.Initialise()

Local myImage:TImage = tRender.Create(128,128)


While Not KeyHit(KEY_ESCAPE)
  Cls
  If MouseHit(1)
  	tRender.TextureRender_Begin(myImage)
		Cls
		SetColor 255,255,255
    		DrawRect 0,0,40,40
		SetColor 255,0,255
		DrawOval 10,10,20,20
	tRender.TextureRender_End()
  EndIf

  SetColor 255,255,255

  tRender.BackBufferRender_Begin()
	  DrawImage myImage,MouseX(),MouseY()
  tRender.BackBufferRender_End()

  Flip

Wend
	
EndGraphics()



GW(Posted 2006) [#6]
Nice,
how much faster is it than grabbing a pixmap?


TartanTangerine (was Indiepath)(Posted 2006) [#7]
Imagine you just learned how to run...


jkrankie(Posted 2006) [#8]
great, i was hoping someone would do something like this. cheers : )


Robert Cummings(Posted 2006) [#9]
hmmm, what uses are there?


TartanTangerine (was Indiepath)(Posted 2006) [#10]
Oh Mr Cummings, you of all people should know - I suspect you are taking the Michael.

For the people who are actually interested :

The benefit of Render to Texture is that everything stays in Video Mem, no more copying from the back buffer to an image buffer (in memory) back to the backbuffer.

With Render to Texture you can do fullscreen realtime effects such as:-

- Fullscreen blurs
- Feedback effects
- Bloom filters
- TV-Style Scene Transitions
- motion blur
- Real time depth of field for you parallax scrollers
- Combining render-to-texture and bump mapping to do heat shimmer or refraction effects.


Amon(Posted 2006) [#11]
Sounds cool. Please release. :)


Robert Cummings(Posted 2006) [#12]
Sounds good... bet it's well slow though - I await the amazing demo :)


ImaginaryHuman(Posted 2006) [#13]
Excellent and very handy for lots of purposes. Looking forward to it, thanks for the hard work.


TartanTangerine (was Indiepath)(Posted 2006) [#14]
ROFL - Me, demo, amazing, LOL!


Fetze(Posted 2006) [#15]
Great, I've waited for something like that =)
Just one question: What system-minimum is able to render to a texture?


Dreamora(Posted 2006) [#16]
That are great news indiespath :-)


TartanTangerine (was Indiepath)(Posted 2006) [#17]
I just did some tests on speed and I think you'd be interested in the results.

The code below gave these results (i've commented them) :

Render 1 Million Random Blocks to Backbuffer and Flip (Cycle1): 2739 Millisecs (DirectX) 1887 (OpengGL)
Render 1 Million Blocks to Texture then Draw Image to Backbuffer and Flip (Cycle1) : 2721 Millisecs (DirectX) 1867 (OpenGL)
Render 1 Million Random Blocks to Backbuffer and Flip (Cycle2): 2667 Millisecs (DirectX) 1867 (OpenGL)
Render 1 Million Blocks to Texture then Draw Image to Backbuffer and Flip (Cycle2) : 2670 Millisecs(DirectX) 1868 (OpenGL)



Now unless I've made some stupid error in my test code I reckon that's pretty damn fast don't you Mr Cummings..


Dreamora(Posted 2006) [#18]
Thats really great. That if we would render straight to backbuffer just with the aditional use of an "effect image" we could missuse for other things :-)


TartanTangerine (was Indiepath)(Posted 2006) [#19]
I've made a few changes, the BlitzMax DirectX "SetViewport" command does not actually set the Viewport, it sets clip-planes. I've added a *REAL* viewport command.

Just doing the docs and I'll upload it. Maybe :)


TartanTangerine (was Indiepath)(Posted 2006) [#20]
Made another change the the OpenGL View port, the Y axis WAS flipped.

Email me if you want a copy of the module.

Bye...


Robert Cummings(Posted 2006) [#21]
Damn that sounds plenty quick. Don't understand why copyrect is slow in Blitz3d?


TartanTangerine (was Indiepath)(Posted 2006) [#22]
Problem with CopyRect, from my understanding, is that its pulling the data from the VideoMemory to System Memory and then back again when you convert the image to a texture.

What this is doing is creating a texture in Videomemory and rendering directly to it.

I do have one question : Say I rendered a screen to the Texture and the background is black, How do I make the black stuff transparent?


TartanTangerine (was Indiepath)(Posted 2006) [#23]
I'll do a bloom filter example when I've picked my daughter up from school...


ImaginaryHuman(Posted 2006) [#24]
I can definitely see a lot of uses for this. I have emailed you Mr Indie for the module ;-)


ImaginaryHuman(Posted 2006) [#25]
Hey, does this mean you can have an effective `backbuffer` (well, a texture) which is bigger than the screen resolution? THAT is a useful feature!

Does it only support RGBA8888 textures btw?


TartanTangerine (was Indiepath)(Posted 2006) [#26]
Yes RGBA8888 Pixel Format.

MipMaps and Filtered flags don't work yet in DirectX neither does MipMap display selection.


altitudems(Posted 2006) [#27]
Sounds excellent! You've made some great modules.

Thank you!


Fetze(Posted 2006) [#28]
I've already asked but didn't get an answer:
What system-minimum do I need to support "Render2Texture"?
It won't run on old graphiccards, will it? :/


TartanTangerine (was Indiepath)(Posted 2006) [#29]
If your card supports DX7 then it should support Render To Texture. Cards released prior to DX7 will probably not support it. You could always use OpenGL as the fallback...

Do a device caps to see if your card does indeed support it.


TartanTangerine (was Indiepath)(Posted 2006) [#30]
Here is a small example : http://indiepath.com/public/Example1.rar

Here is the code for the example
Strict

Import Indiepath.Render2Texture

HideMouse()
'SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,32

tRender.Initialise()

AutoMidHandle(1)

Local logo:Timage = LoadImage("bmax160_2.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
Local myImage:TImage = tRender.Create(256,128,MIPMAPPEDIMAGE|FILTEREDIMAGE)


tRender.TextureRender_Begin(myImage)
	Cls
tRender.TextureRender_End()

SetBlend(ALPHABLEND)

Local ii:Float = 0

While Not KeyHit(KEY_ESCAPE)
	
	SetBlend(ALPHABLEND)
	SetAlpha(1)
	tRender.TextureRender_Begin(myImage)
	Cls	
		SetRotation(ii)
		Local sc:Float = (Sin(MilliSecs()/10))+1.5
		SetScale(sc,sc)
		DrawImage logo,320,240
	tRender.TextureRender_End()


	tRender.BackBufferRender_Begin()

		SetRotation(ii)
	 	DrawImage logo,320,240
		SetBlend(ALPHABLEND)
		SetMaskColor(0,0,0)
		SetRotation(0)
		Local ap# = 0.2
		SetScale(1,1)
		For Local a:Int = 1 To 10
			Local Xoffset:Int = a * 60
			Local yoffset:Int = a * 40
			SetAlpha(ap#)
			DrawImageRect(myImage,-Xoffset,-Yoffset,640+(XOffset*2),480+(yOffset*2))
			ap:-0.01
		Next


	tRender.BackBufferRender_End()

	
	If MouseHit(1) tRender.SetMipMap(myImage,1)
	
	Flip
	ii:+1
	
Wend



Haramanai(Posted 2006) [#31]
Can I strech the texture? Can I change the color?
Can I...
This for me looks like it must be part of the poly.mod.


TartanTangerine (was Indiepath)(Posted 2006) [#32]
@Haramanai

Do what you want, it's treated just as a normal image.

You can even render your rendered texture to another texture if you so desire!

This will not be part of the Poly mod because it's got nothing to do with the Poly module :)


TartanTangerine (was Indiepath)(Posted 2006) [#33]
Anyway I gotta go I got a big press announcement to make re: igLoader :D


TartanTangerine (was Indiepath)(Posted 2006) [#34]
Now uses MipMapped, Filtered and other TExture flags...

Also has Manual MipMap level selection for effects such as 3D accelerated fullscreen blur.


Robert Cummings(Posted 2006) [#35]
I think this is a great module but if I recall, mark said he didn't use Render to Texture for a very good reason, and that was it was really slow on some cards?

I would be very cautious about releasing a casual game with render to texture but enthusiastic for more hardcore titles.


xlsior(Posted 2006) [#36]
With Render to Texture you can do fullscreen realtime effects such as:-


Hmmmm. very interesting -- I wonder if my fade-to-black&white routine would see any speed increase with this. Sounds like it should.


TartanTangerine (was Indiepath)(Posted 2006) [#37]
@Rob, I've build it some error checking so your application will know if the client machine is compatible.

I've had to rebuilt the Image creation routines as the offical module was unable to build textures with the required caps. Maybe that is why Mark did not include it.

Have a look at this article and try to remember it's 7 years old! : http://www.gamasutra.com/features/19991112/pallister_pfv.htm


smilertoo(Posted 2006) [#38]
where was the mod posted?


ImaginaryHuman(Posted 2006) [#39]
I emailed for the mod, no sign of it :?


TartanTangerine (was Indiepath)(Posted 2006) [#40]
no email :(


Fetze(Posted 2006) [#41]
Will you post the module here or will I have to send a mail too?


TartanTangerine (was Indiepath)(Posted 2006) [#42]
No email no Module...


Fetze(Posted 2006) [#43]
...why? ôo
But well, okay, I'll mail. Just interested why you don't just post it.


jamesmintram(Posted 2006) [#44]
Hi, I tried out the module today and think its brilliant, the only thing I have a problem with ( which i think you mentioned above ) is the ability to mask colours out.
For example the code below:


Import indiepath.render2texture

Graphics 800, 600
trender.initialise ()

img:timage = trender.create ( 400, 400,, 0, 0, 0 )

		
trender.texturerender_begin ( img )
	
	SetClsColor ( 0, 0, 0 )
	Cls
	
	SetColor ( 200, 100, 300 )	
	DrawRect ( 10, 10, 150, 150 )
trender.texturerender_end ()


Repeat
	trender.backbufferrender_begin ()
		Cls
		
		SetColor ( 200, 0, 255 )
		DrawRect ( 10, 10, 500, 500 )
		
		DrawImage ( img, 100, 100 )
	trender.backbufferrender_end ()
	
	Flip
Until AppTerminate () = 1	



On my machine there is no transparency where the black pixels are, which is a problem for me.

Is there something im missing? Or is it not possible to do what I want?


ImaginaryHuman(Posted 2006) [#45]
I did send an email but apparently you didn't get it, oh well


Dreamora(Posted 2006) [#46]
@jamesmintram: You forgot to set the the blend mode to mask


jamesmintram(Posted 2006) [#47]
Ok, thanks man. I will try that out when I get back to my cabin.


TartanTangerine (was Indiepath)(Posted 2006) [#48]
@AngelDaniel, emailed ya.

@Dreamora, oh is that how you do it :)

@James, You really should try and keep your texture sizes ^2 like 256x256 or 512x512 or even 512x256. If you don't do this you just end up wasting video memory.


ImaginaryHuman(Posted 2006) [#49]
Received, thanks


salric(Posted 2006) [#50]
Tim,

I've sent two emails now - have you recieved them? Which email address is the best to contact you with?

??


jamesmintram(Posted 2006) [#51]
I tried what Dreamora suggested to no avail, heres the code I ran.


Import indiepath.render2texture

Graphics 800, 600
trender.initialise ()

setblend ( MASKBLEND )

img:timage = trender.create ( 400, 400,, 0, 0, 0 )

		
trender.texturerender_begin ( img )
	
	SetClsColor ( 0, 0, 0 )
	Cls
	
	SetColor ( 200, 100, 300 )	
	DrawRect ( 10, 10, 150, 150 )
trender.texturerender_end ()


Repeat
	trender.backbufferrender_begin ()
		Cls
		
		SetColor ( 200, 0, 255 )
		DrawRect ( 10, 10, 700, 700 )
		
		DrawImage ( img, 100, 100 )
	trender.backbufferrender_end ()
	
	Flip
Until AppTerminate () = 1	




jkrankie(Posted 2006) [#52]
does this work on mac yet?

cheers
charlie


TartanTangerine (was Indiepath)(Posted 2006) [#53]
It's always worked in the Mac, I've sent copies to those Mac users that have requested it.


Dreamora(Posted 2006) [#54]
@jamesmintram: And what is the actual problem? (beside that, color range is 0-255 not 300 ;))

the image is drawn correctly. As you haven't changed the mask color for it, it will only see black as mask color which you haven't used at any point on the texture


jamesmintram(Posted 2006) [#55]
The 300 should be a 200, typo, but it still seemd to work on my system.

Anyway there is black in img, i create it, clear it all to black and then draw a rect onto a section of it.

When img is drawn further on there are is no transparency where the black pixels are in the image.

Maybe im not seeing something here, if you could post and example which works on your system, i could then see if its me or if its an issue with my machine.

Cheers


Dreamora(Posted 2006) [#56]
Seems like there is definitely an error in it, so I'm for missinforming. Normally the setblend maskblend should work and as example 3 shows, for alpha it even seems to work.

Strict
Import indiepath.render2texture

Graphics 800, 600
trender.initialise ()


SetMaskColor(200,200,200)
Local img:TImage = trender.Create ( 400, 400, MASKEDIMAGE, 200, 200, 200 )

	
trender.texturerender_begin ( img )
	
	SetClsColor ( 200, 200, 200 )
	Cls
	
	SetColor ( 200, 100, 200 )	
	DrawRect ( 10, 10, 150, 150 )
trender.texturerender_end ()

SetBlend ( MASKBLEND )
Repeat  
	
	trender.backbufferrender_begin ()
		
		Cls
		
		SetColor ( 200, 0, 255 )
		DrawRect ( 10, 10, 700, 700 )
		
		SetColor ( 255, 255, 255)
		DrawImage ( img, 100, 100 )
	trender.backbufferrender_end ()
	
	Flip
Until AppTerminate () = 1	


As you see, even using BMs internal setmaskcolor does not help to get around this.


TartanTangerine (was Indiepath)(Posted 2006) [#57]
I've looked into this further and it from what I can see BMAX sets the MaskColor when it loads/creates the image - it's not dynamic.

I'm trying to find a dynamic way of doing it without reading every damn texel and changing it.


ckob(Posted 2006) [#58]
the irrlicht module allows for rendering to textures and it works very well, indiepath you should check it out.


TartanTangerine (was Indiepath)(Posted 2006) [#59]
Does it allow for alpha masking?


jamesmintram(Posted 2006) [#60]
I believe so, the only problem being is the fact you would not be able to use max2d commands with it as it is a complete 3d engine based on opengl/directx. Which defeats the point of your mod (im guessing).


TartanTangerine (was Indiepath)(Posted 2006) [#61]
The problem is the same as loading a .BMP file and using that as a sprite with alpha. Someone must know how to change the blend mode so that black becomes Alpha, and I don't mean light blend.


TartanTangerine (was Indiepath)(Posted 2006) [#62]
Apologies to those who have emailed me and have not received the module yet. I've got a lot of work on at the moment and I will get something out to you all soon.

Tim.


TartanTangerine (was Indiepath)(Posted 2006) [#63]
Sorted the problem!!!!

I've introduced a new cls command that allows the ClsColor to have alpha!!!


TartanTangerine (was Indiepath)(Posted 2006) [#64]
OSX compiled, will send out later.


RiK(Posted 2007) [#65]
Sorry to bump an old thread but any chance of a copy of the module?


TartanTangerine (was Indiepath)(Posted 2007) [#66]
modules.indiepath.com


RiK(Posted 2007) [#67]
Found the forum ok but no download area.

Have I missed something?

Earlier in the thread you've asked people to email you but there's no email address in you profile which makes that a tad difficult.. :)


TartanTangerine (was Indiepath)(Posted 2007) [#68]
@Rik, then do some research, I have many sites and many ways to contact me.


Grey Alien(Posted 2007) [#69]
http://www.indiepath.com/contact.php


RiK(Posted 2007) [#70]
Err, thanks Grey.

@ indiepath, err, thanks... I think?

Did I do/say something to offend somehow?

Just asking as I'd only asked a question. Didn't think that deserved such a curt or sarcastic response, especially when the links in your sig are either broken or just a holding page.. Wouldn't it have been easier to just poste the link to where I could contact you as Grey Alien kindly did?

Hey, I was just trying to support your module thats all.. :(


RiK(Posted 2007) [#71]
Hmm... so it seems Indiepath's module is not an option then..

Has anyone else got a (OpenGL) Render to Texture solution?

It's only for my own noodlings at home but I wouldn't mind paying a sensible amount for a decent solution that works.


Perturbatio(Posted 2007) [#72]
.


tonyg(Posted 2007) [#73]
Hmm... so it seems Indiepath's module is not an option then..


What was the reply when you contacted Indiepath?
Did you post anything on his forums?
There was a Highgfx module from Diablo but it went 'wrong' and was removed. I got something working myself but the changes made in 1.24 meant I would have to start again and life is too short.
What is it you want it for again? Maybe there is an alternative solution.


jkrankie(Posted 2007) [#74]
can't you do this with minib3d? or is that different?

Cheers
Charlie


RiK(Posted 2007) [#75]
What was the reply when you contacted Indiepath?


I mailed via the link grey found but didn't get a reply.

Did you post anything on his forums?


I'd love to but having registered it requires admin approval prior to posting. Interestingly there are users who registered *after* me that did get approved but for some reason I didn't, further fuelling my paranoia stated above...

What is it you want it for again? Maybe there is an alternative solution.

Just noodling with some feedback type effects really, and to allow me to draw to off-screen render targets to use for some message displays etc.


tonyg(Posted 2007) [#76]
If you can find HighGfx then it might help the OGL side and still work. It's well documented that there is a lot of interest in getting RTT included in the official code but Mark isn't interested.


Grey Alien(Posted 2007) [#77]
He's probably getting married or on honeymoon around now...or just hiding ;-)


RiK(Posted 2007) [#78]
tonyg: Thanks, Found the original thread but unfortunately the D/L link is dead.

Anyone happen to have a copy floating around anywhere?


jkrankie(Posted 2007) [#79]
I have a version here i could send you RiK. Is that your moniker on yakyak too, i could send you a pm if it is.

Cheers
Charlie


RiK(Posted 2007) [#80]
Thanks Charlie..

Yup, same RiK.

email is rik[AT]olpin[DOT]net


SofaKng(Posted 2007) [#81]
Is there a new (eg. working) render-to-texture module available for BlitzMax?

@JKrankie: I've sent you an e-mail :)


RiK(Posted 2007) [#82]
Still trying to track down a solution here, no luck so far...