[SOLVED] vec_to_screen with the view as bmap on panel

Posted By: Reconnoiter

[SOLVED] vec_to_screen with the view as bmap on panel - 11/30/15 12:45

Hi,

I have trouble with converting the 3d pos of an object to the 2d pos for draw_text. The text is not drawn direct at the object but instead is drawn with an offset. Is that cause I show the VIEW on a panel bmap which is smaller than the VIEW's size?

Code:
//do only once
small_view.bmap = bmap_createblack(450, 450, 24);

...
objectview_panel.bmap = small_view.bmap; //to show it ontop of the interface	
vec_set(draw_vec, ent_obj.x);	
vec_to_screen(draw_vec, small_view); // to screen coordinates
str_for_num(draw_str, ent_obj.HEALTH);
draw_text(draw_str, draw_vec.x, draw_vec.y, vector(255, 255, 255));



For solution see end page 1 / page 2
Posted By: Anonymous

Re: vec_to_screen with the view as bmap on panel - 11/30/15 18:32

The function does the calculation based on the clipping Frustum(Whatever the hell that word is).

So yes, you should be able to line it up, by adjusting the draw vec by the offset of the view-panel size. Figure out the xy difference ex 25%x25% then you can vec_scale or vec_normalize(draw_vec,0.75); before the call to draw_text

Edit - The above would only work if you target bmap is anchored at pos [0,0]. Otherwise it will take more to adjust the text.
Basically I would assume the engine sets VIEW pos [0,0] x VEIW size. So to adjust you have to offset by the difference of targetmap pos and size.
Posted By: Reconnoiter

Re: vec_to_screen with the view as bmap on panel - 12/01/15 11:37

Thanks it works better, not perfect yet but a big improvement (it still gives to much offset with objects that are more to the north/top or to the south/bottom);

Code:
objectview_panel.bmap = small_view.bmap; //to show it ontop of the interface	
vec_set(draw_vec, ent_obj.x);	
vec_to_screen(draw_vec, small_view); // to screen coordinates

draw_vec.x /= screen_size.x / objectview_panel.size_x;
draw_vec.y /= screen_size.y / objectview_panel.size_y;
draw_vec.x += objectview_panel.pos_x;
draw_vec.y += objectview_panel.pos_y;

str_for_num(draw_str, ent_obj.HEALTH);
draw_text(draw_str, draw_vec.x, draw_vec.y, vector(255, 255, 255));



ps: a bit unrelated to this thread but you really should give yourself more credit, your a smart guy
Posted By: Anonymous

Re: vec_to_screen with the view as bmap on panel - 12/01/15 17:49

Well this is one of those places where using a logic fails to a good math solution. Many other users are better at complex math ideas.
There are factors I can't know for your post.
Camera ISOMETRIC or NOT?
VEIW top down?
PANEL target same aspect as screen resolution?

Also you operate on vec.x/y with math, they are vars, and pixel locations are pure int, there is no x =5.74; This can be cause of some normal offset. And additively and cause a good amount of offset.

Here I'd be forced to use one of my very specific, guess and test solutions.
Find entity is above or below the accurate zone at bmap center.
Plug in a constant as a modifier and adjust it by a modifier.

Code:
draw_vec.y+=...........;
// end vec adjustment
if( draw_vec.y < panel.y+(panel_size.y/2)) // above center
{
  draw_vec.y(+-)= 5*(a modifier based on dist center-to-top)
}
// invert 
if( draw_vec.y > panel.y+(panel_size.y/2)) // below center
{
  draw_vec.y(+-)= 5*(a modifier based on dist center-to-top)
}



Mal
p.s. Very kind of you.
Posted By: Reconnoiter

Re: vec_to_screen with the view as bmap on panel - 12/01/15 18:48

What the heck, this compensation code works crazy grin (got to it through trial and error, check it with different resolutions):

Code:
draw_vec.x /= screen_size.x / fullscreen_image_pan[0].size_x; //size_x/size_y are both 450
draw_vec.y /= screen_size.x / fullscreen_image_pan[0].size_y; //but screen reso are e.g. 1000x600
draw_vec.x += fullscreen_image_pan[0].pos_x;
draw_vec.y += fullscreen_image_pan[0].pos_y * (screen_size.x/screen_size.y);



Note that for draw_vec.y I am using screen_size.x too.
We make a strange team, haha.

Info: this VIEW is not isometric, aspect is default (/ not touched), VIEW is top down, panel has same aspect as screen reso I think.
Posted By: Anonymous

Re: vec_to_screen with the view as bmap on panel - 12/02/15 00:06

If it works then good. I am sorry not to supply more support , but I don't even have the engine installed anymore.

Code:
// Shrink the draw_vec
vec_mul(draw_vec,vector(fullscreen_image_pan[0].size_x/small_view.size_x,fullscreen_image_pan[0].size_y/small_view.size_y,0)); 
// find diff of screen pos[0,0] and image corner
vec_set(vec_temp,vec_sub(vector(screen_size.x,screen_size.y,0),vector(fullscreen_image_pan[0].pos_x,fullscreen_image_pan[0].pos_y,0)));
// move shrunk vec to use image corner as if screen pos[0,0]
vec_add(draw_vec,vec_temp);



This is pure guessing, The math or idea might be wrong. But may work.

OF COURSE... You have a working solution, just this is just something to play with. IF IT AIN'T BROKEN DON'T FIX IT!

EDIT - This has to be broken code as view_size and image_size should be the same based on the manual..
So then the MUL second vec would be view_size_x/screen_size.x --/-- size.y/size.y, with view_size and image_size exchangeable

Ok
Mal


Posted By: Reconnoiter

Re: vec_to_screen with the view as bmap on panel - 12/02/15 10:56

Oops sry I said "check it with different resolutions" but I meant "(I) checked it with different resolutions".

But I just double checked it and it isn't entirely perfect but it seems to be fine for now, with only big offset with weird ratio's where e.g. y reso is bigger than x reso.

ty for the replies
Posted By: txesmi

Re: vec_to_screen with the view as bmap on panel - 12/02/15 16:01

Hi,
Since the view is at x0:y0 (otherway it will not be rendered correctly over the render target) you only need to add the panel position to the result of vec_to_screen. No more calculations are needed. vec_to_screen takes into account view position and size already.

Salud!



Posted By: Reconnoiter

Re: vec_to_screen with the view as bmap on panel - 12/02/15 16:06

Do you have an example?, for me that doesn't work. (I am probably making some newbie mistake)

Code:
vec_set(draw_vec, ent_obj.x);
vec_to_screen(draw_vec, house_largeview); // to screen coordinates
draw_vec.x += my_viewpan.pos_x;
draw_vec.y += my_viewpan.pos_y;

Posted By: txesmi

Re: vec_to_screen with the view as bmap on panel - 12/02/15 16:11

sure

Code:
#include <acknex.h>

void main ()
{
	video_mode = 8;
	wait(1);
	level_load ( "" );
	ENTITY *ent = ent_create ( SPHERE_MDL, nullvector, NULL );
	ent->x = 400;
	
	VIEW *cam = view_create ( 1 );
	cam->bg = pixel_for_vec ( COLOR_BLACK, 100, 888 );
	cam->bmap = bmap_createblack ( 450, 450, 24 );
	cam->size_x = 450;
	cam->size_y = 450;
	cam->flags |= SHOW;
	
	PANEL *pan = pan_create ( "flags=SHOW;", 1 );
	pan->bmap = cam->bmap;
	pan->pos_x = screen_size.x - 450;
	pan->pos_y = screen_size.y - 450;

	while ( !key_esc )
	{
		ent->y = fcos ( total_ticks*5, 100 );
		ent->z = fsin ( total_ticks*5, 100 );
		VECTOR vec;
		vec_set ( &vec, &ent->x );
		vec_to_screen ( &vec, cam );
		vec.x += pan->pos_x;
		vec.y += pan->pos_y;
		draw_text ( "X", vec.x, vec.y, COLOR_RED );
		wait(1);
	}
	
	pan_remove ( pan );
	bmap_remove ( cam->bmap );
	ptr_remove ( cam );
	ent_remove ( ent );
	
	sys_exit ( NULL );
}

Posted By: Anonymous

Re: vec_to_screen with the view as bmap on panel - 12/02/15 16:37

^--- I see, you manually set the VIEW_size == image_bmap_size.

The manual states that VIEW_size == render_target.bmp, so Reconnoiter's last code should work... Correct? As View size should automatically be set to image size.


@Reconnoiter sorry for my misunderstanding and time wasting.
@Txesmi Thanks for solving this. And why all the C++ style dereferences and referencing, tare you studying C/C++ too ?
Posted By: txesmi

Re: vec_to_screen with the view as bmap on panel - 12/02/15 16:49

Originally Posted By: Malice
The manual states that VIEW_size == render_target.bmp, so Reconnoiter's last code should work... Correct? As View size should automatically be set to image size.

Manual says but size is not set. I remember upon a time it was.

@Malice
because I am worth it grin
out of jokes, I got used a couple of years ago and I continued disabling pointer detection.
Posted By: Anonymous

Re: vec_to_screen with the view as bmap on panel - 12/02/15 17:04

Quote:
Manual says but size is not set. I remember upon a time it was.

One of our manuals is lying... Both in text and function.
Quote:
If no size is given in a panel definition, the panel is set to the size of the panel bitmap. If neither a size nor a bitmap is given, the panel is set to the size of its largest element (f.i. a button).''' If no size is given in a view definition, the view is set to the size of its render target. '''

^Online manual.

Quote:
I got used a couple of years ago and I continued disabling pointer detection.


Lol yes! I did the change years back as well, but ended up having to go back when answering questions in this actual Section.
Posted By: Reconnoiter

Re: vec_to_screen with the view as bmap on panel - 12/02/15 19:34

@txesmi, works, tnx!
(anyone reading this and seeing only part of VIEW on the panel.bmap, check that you don't set the pos_x/pos_y of the VIEW, I was doing this wrong too blush )
Posted By: txesmi

Re: vec_to_screen with the view as bmap on panel - 12/02/15 22:57

glad of being helpful
© 2024 lite-C Forums