How to grab a part of screen fast with render2texture?

Started by takis76, August 19, 2018, 18:50:44

Previous topic - Next topic

takis76

You were faster.
You created the effect I want and I was just made the effect too.
But Yours is better because you are changing and the ambience color too.

In my example I used and Col's ClearTo function to change the darkness and the color of the ambience.
Here was my version before you posted yours which is very nice too.


SuperStrict

Import "renderimage.bmx"
Include "Clear_To.bmx"

'SetGraphicsDriver GLMax2DDriver()
SetGraphicsDriver(D3D9Max2DDriver(), 1)
Local Graph:TGraphics = Graphics(800, 600, 0, 60)

Local Small_Flare:TImage = LoadImage("Torch_Flare_1.png", MASKEDIMAGE)
Local Tree:TImage = LoadImage("Tree.png", MASKEDIMAGE)
Local Visible_Screen:TRenderImage = CreateRenderImage(Graph, 800, 600) 'Game Visible Screen
Local Flare_Layer:TRenderImage = CreateRenderImage(Graph, 800, 600) 'Flare Layer

MidHandleImage(Small_Flare)

While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
' Render all of your game into a texture instead of to the backbuffer
' - this saves using GrabImage to create a TImage from the backbuffer :- you are drawing into a texture, just need to DrawImage the render-texture later
Setrenderimage(Flare_Layer)
'Cls
ClearTo(Graph, 30, 30, 30, 255)
SetBlend(LIGHTBLEND)
SetColor(255, 25, 25)
DrawImage(Small_Flare, 150, 100)
SetColor(255, 255, 255)
DrawImage(Small_Flare, MouseX(), MouseY())
SetBlend(ALPHABLEND)


Setrenderimage(Visible_Screen)
Cls

SetBlend SOLIDBLEND
SetColor(255, 255, 255)
DrawImage(Tree, 0, 0)

' set to render to the backbuffer
Setrenderimage(Null)
Cls

DrawImage(Visible_Screen, 0, 0)

SetBlend SHADEBLEND
DrawImage(Flare_Layer, 0, 0)
SetBlend ALPHABLEND | MASKBLEND
   
Flip 1
Wend


Anyway the conclusion is that I understood a (little) how this render2texture library works. I will need to make lots of changes in my game.

I don't know if render2texture work for creating the underwater effect. But I managed to compile the shader framerwork which I like to do experiments with this too and try to implement an underwater effect and other effects too.
I will open a new topic about shaders framework. (I don't know nothing about shaders by the way). And I don't know if I will need to write and shader script :P
This topic will be big.

Thank you very much. My game will become very beautiful and ofcourse your names will be in my credits.

Derron

Albeit it is "solved" for now:

Col's example (and maybe yours) has(ve) assumingly (not able to test now - so only judging by looking at the code) the problem of "additive blends". The code renders multiple flares "lightblended", so colors might "add" (bright + bright = brighter).

Lightblend:


To avoid that you somehow need to join flares in a "non additive way" - "semi additive way" (brightness limited by the brightest torch-center). So for all "to mix" pixels the resulting color should equal to the brightest of the "to mix"-pixels (100,100,100 + 200,200,200 = 200,200,200).
This is not easily doable with lightblend/shadeblend/alphablend.
I can imagine you can play around with having lights and a maximum brightness of "128,128,128" (or "0.5" times white). But this results in lights not being able to be "lighter" than a certain level. To then get rid of that limitation you would need to draw the torch-map-image (containing all lights) multiple times. This allows a "50% transparent torch image" to light up the scene multiple times - each increasing brightness by a "low" amount.


@ shaders
With shaders I assume the light effect would be created in a more "dynamical" way.


bye
Ron

TomToad

When combining lights together, LIGHTBLEND is what you want.  It is how light works in the real world.  Shine two lights on the same surface, and the result is brighter than either single light.  If it only results in the brightest, then there would be no need for two lamps in a room, you would only need the brightest one.  There is a point when a surface is fully lit and additional light will not make any difference.  On a computer, fully lit is when the light source is at 255.  Adding another light on top of that will be clipped and have no additional affect. 

When illuminating a scene, you want to use SHADEBLEND.  Why would you do that when it isn't the way the world works?  When you shine a light on a spot, it brightens that area, not darken the rest of the room.  Unfortunately, you can't replicate that with a computer.  Imagine you are in a completely dark room with a blue wall.  Even though the wall has all the properties needed to make the wall blue, you cannot see it because there is no light to reflect from it.  Shine a white light on the wall, and now you see blue wherever the light hits.

This wouldn't work on a computer.  How would you simulate a completely dark room?  Just render everything in black.  Setcolor 0,0,0.  The problem here is that the image now loses all the properties that made it that image.  Shine a light on it with LIGHTBLEND, it will still be a black image.  Use ALPHABLEND or SOLIDBLEND, the image now takes on the properties of the light. That is why it looks wrong.

So when illuminating your scene on the computer, you do the opposite of nature.  You render your scene fully lit, then darken the areas that are not illuminated.  That's what SHADEBLEND does.  Render your scene to a texture, fully lit, with Render2Texture.  Render all your lights on the backbuffer using LIGHTBLEND.  Then render the texture onto the lights with SHADEBLEND.  Now everything looks as it suppose to. Well, at least as close as you can get with Max2D, you could probably get better results using shaders.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Derron

Thanks for this explanation - that (pun intended) shed some light ;-)

For certain effects you might still want two "lights" to mix in a non-natural way (so bright + bright =/= 2*bright).


bye
Ron

takis76

Yes I have recognize this effect. When one spot light overlaps another one the second one will not be added with the first one.
How can I fix that with code?
Also I have seen the light volume of the light is not so bright.
For example in my code the red light wasn't so bright.


TomToad

The lights appear dim because of the image you chose for the torch.  Wherever the light is 100% white, your image will be fully illuminated there.  Your torch image is only 100% in the very center and drops off from there.  Try the image I have included below, which is 100% white for most of the image, and only drops off at the edges.  You will see that the areas are much better illuminated.  As for the red being so dim, it is because your image doesn't have very much red to begin with.  A light cannot illuminate a color that doesn't exist.  try the room image below in place of your tree image and you will see that even the red is bright.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Derron

@ red light without red in the world
This is what I tried to explain with "special effects". You might want to tint stuff (white becomes red - as if red-magic-fog is floating around) you need to wrap your head around some functions and try combinations to make it work (eg. colors not being full-bright (255,0,0 - or 255,255,255) or full opacque (setalpha 0.5)).

Eg the "LIGHTBLEND" mode can do wonders if you want to have a hover effect on buttons: just set the blend mode to "LIGHTBLEND" and draw the button again after you draw the button in a normal way. Use a low "SetAlpha 0.x" value to just lighten up the button a bit. Use a "SetAlpha 0.2 + 0.1*Sin(Millisecs()/5)" style approach and it slightly pulsates. Easy-peasy effect without the need of additional "hover"-sprites/images.

Similar could be said about the light effect: for fire you would use a slight tint (setcolor x,y,z instead of white) - and the add a pulsating or flickering variation of the numbers. So you could create a "fire torch lightning"-tint-effect. If you scaled the torch image slowly (same millisecs-approach with SetScale x,y) you could be creating a fire-torch-effect which - I assume so - could look pretty intriguing. But as first step you should solve the initial issue of "lighting up your scene".


bye
Ron