Fitting entity to the screen, based size (in pixel)

Posted By: Emre

Fitting entity to the screen, based size (in pixel) - 03/24/20 07:35

Hi everyone!

As some of you know, i'm working on wallpaper project. Now i need some help.

I have to use an entity as a wallpaper, instead of "panel" or "view entity". Because I have to use shader effects, especially post process. I use the vec_to_screen function to fit the wallpaper on the screen like this;

[Linked Image]

But this way causes some problems. Is there an easier way to do this? I want to fit wallpaper on the screen without moving the object in the x direction.

You can download and take a look the sample from here.


Thanks in advance!
Posted By: Quad

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 08:42

There should be a way using, fov, screensize and entity size, but not exactly sure how.
Did you try view entities or bmap_process with panels though?
Posted By: Emre

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 08:49

Originally Posted by Quad
There should be a way using, fov, screensize and entity size, but not exactly sure how.
Did you try view entities or bmap_process with panels though?

Hi Quad. Unfortunately, i'm still using A7. A7 doesn't support bmap_process. Even if it so still i have to use world entity for post process effects. Panels and view entities are not affected by post processing shaders.
Posted By: alibaba

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 09:38

I think I may have the solution. First, setting the ISOMETRIC flag is mandatory I think, since you're working with flat surfaces such that the x position causes no problems. Then we need a fixed camera arc which I calculated* to roughly 53. With this arc, the screen dimensions are as big as the entity/picture dimensions. Lastly we just calculate the new scale directly. I commented the needed lines.



*The actual calculation can be found in the manual (under "arc") with the formula: width = view.size_x * 2 * tan(view.arc/2).
If we calculate the arc with that, we get an exact solution of 53.130102354156.

Code
///////////////////////////////
#include <acknex.h>
#include <default.c>

///////////////////////////////

ENTITY* slayt_ent;

#define image_type skill1
#define landscape 1
#define portrait 0

action slayt_ent_act()
{
	my.ambient=100;
	wait(1); //wait for the morphing
	var scale;
	var width = my.max_y * 2; //the actual width and height
	var height = my.max_z * 2;
	while(1)
	{
		if(width > screen_size.x)  //depending on what needs to fit into the screen
			scale = screen_size.x/width;
		if(height > screen_size.y)
			scale = screen_size.y/height;
		vec_fill(my.scale_x,scale);		
		wait(1);
	}
}


int level=0;
function level_load_sample()
{
	if(level==0)//landscape sample
	{
		if(slayt_ent!=NULL)
		{
			ent_remove(slayt_ent);
		}
		slayt_ent=ent_create("slaytent.png",vector(100,0,0),slayt_ent_act);//create slayt_entity (slaytent.png)
		ent_morph(slayt_ent,"wall_landscape.jpg");// change wallpaper
		slayt_ent.image_type=landscape;
		level=1;
		return;
	}
	if(level==1)// portrait sample
	{
		if(slayt_ent!=NULL)
		{
			ent_remove(slayt_ent);
		}
		slayt_ent=ent_create("slaytent.png",vector(100,0,0),slayt_ent_act);//create slayt_entity (slaytent.png)
		ent_morph(slayt_ent,"wall_portrait.jpg");// change wallpaper
		slayt_ent.image_type=portrait; 
		level=0;
		return;
	}
}

function main()
{
	fps_max=75;
	video_mode=9;
	video_screen=2;
	wait(3);
	level_load("");
	set(camera,ISOMETRIC); //needed to have a flat projection
	camera.arc = 53; //the estimated arc for the same height/screensize dimension
	
	level_load_sample();
	on_enter= level_load_sample;
	
	while(1)
	{
		draw_text("Press Enter to change wallpaper",screen_size.x-500,20,COLOR_RED);
		wait(1);
	}
}

Posted By: Emre

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 10:09

Wow! Works like a charm! Thank you so much, alibaba!
Posted By: alibaba

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 10:58

You're welcome! Glad to have been of help.
Posted By: txesmi

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 12:53

I would suggest using the camera's top, botton, left, and right members for a full control of the isometric projection.

Other different way would be computing the projection of the quad directly in clip space into the vertex shader. Starting with a fullscreen square (-1:-1/1:1), it only needs to be multiplied by the proportions. Something like this:
Code
outPos.xy = inPos.xy * vImageSize.xy / vViewportSize.xy;
outPos.xy += 2.0f * vOffset.xy / vViewportSize.xy;
outPos.z = 0.5f; // depth into the view fustrum 0<->1
outPos.w = 1.0f;
Posted By: Emre

Re: Fitting entity to the screen, based size (in pixel) - 03/24/20 13:59

Got it! Thank you!
© 2024 lite-C Forums