Page 1 of 3

Level and screen projections (now with resize handling)

Posted: Fri May 03, 2019 7:54 am
by KeksDose
Use this zscript library I've made to deal with screen and world projections. It allows you to:

1. Find an actor's screen position (maybe to draw text over a target, like HudMessageOnActor)
2. Click a screen location to fire a bullet in that direction
3. Determine if a location is in your view

in both opengl and old software render modes. You don't need to know any of the involved maths, and it runs very very fast, too.

Examples included are free aiming (kinda like in Metroid Prime 3), drawing actors' bounding boxes and... a candle with some text over it. Just load the pk3 to test this. Here's an easy example:

Code: Select all

// Setup:
proj.CacheResolution();
proj.CacheFov(players [consoleplayer].fov);
proj.OrientForRenderOverlay(event);
proj.BeginProjection();

// Now you can project as much as you like, just like this:
proj.ProjectWorldPos(mo.vec3offset(0, 0, mo.height));

if(proj.IsInFront()) {
    let draw_pos = proj.ProjectToScreen();
    
    Screen.DrawText(
        smallfont,
        Font.CR_ICE,
        draw_pos.x,
        draw_pos.y,
        "achachachachach");
}
(You shouldn't view the free aim as a complete mod. It'll cause multiplayer issues. It merely demonstrates it works.)

You may do whatever you like with this. There's a lil note in the libeye directory. If you also include that, we're golden.

Feel free to post suggestions, questions about usage, or issues you encountered.

Re: Level and screen projections (+ free aiming)

Posted: Fri May 03, 2019 8:13 am
by m8f
How does it compare to the similar functionality from Gutamatics? Does the coordinate results differ much?

This library seems to be easier to use.

Re: Level and screen projections (+ free aiming)

Posted: Fri May 03, 2019 9:13 am
by Nash
Siiiiiiiick, thanks for this!

Already found a bug: it doesn't handle screen size properly. :D


Re: Level and screen projections (+ free aiming)

Posted: Fri May 03, 2019 2:24 pm
by KeksDose
@Nash: Looks like it's too early to thank me. I'll be working on it.

@m8f: Thanks! I hope modders will find this easy to use.

Both appear to output the same coordinates. I skipped a step concerning some depth business, cuz in gzd, it amounts to virtually no change. znear and zfar may ring a bell. I don't see deprojection or software render in Gutamatics, so I can't judge that.

Performance may be of interest. I tested my library to produce only 4% of the vm calls Gutamatics does, and it runs at least 3.5 times as fast.

Re: Level and screen projections (+ free aiming)

Posted: Sat May 04, 2019 12:15 am
by StroggVorbis
I've seen your video about it in the WIP thread. Does that mean it can be used as a base for GoldenEye/Perfect Dark/TimeSplitters like aiming? :O

It also reminded me of the console in Bethesda's games, where you can click on entities and it would display a unique ID which you can use to do all sorts of stuff.

Re: Level and screen projections (+ free aiming)

Posted: Sat May 04, 2019 1:39 pm
by KeksDose
Aye. Implementing such a thing properly and meaningfully sounds tough. I have doubts we'll see such a mod, since its easier to simply look with your mouse.

Concerning screen resize, I know what must be done. Given other projects and testing required, I'd estimate an update to this within Sunday in two weeks.

Re: Level and screen projections (+ free aiming)

Posted: Sat May 04, 2019 5:52 pm
by Nash
I have a suggestion:

Option to check if projection is not blocked by solid geometry or other actors. So if you can't see the thing, it will not project.

Re: Level and screen projections (+ free aiming)

Posted: Sun May 05, 2019 12:55 am
by Gutawer
KeksDose wrote: Both appear to output the same coordinates. I skipped a step concerning some depth business, cuz in gzd, it amounts to virtually no change. znear and zfar may ring a bell. I don't see deprojection or software render in Gutamatics, so I can't judge that.

Performance may be of interest. I tested my library to produce only 4% of the vm calls Gutamatics does, and it runs at least 3.5 times as fast.
Yeah, I never bothered to implement the software renderer or deprojection into Gutamatics, I hadn't really worked out the maths for the latter at the time and was never interested in bothering with the former. I'm also not massively interested in adding more in that area to it because I'd honestly rather spend my time coding it into gzdoom natively, since I remember Graf saying that's what would be preferrable (and also obviously because this now exists).

I'm also not surprised this runs much faster - mine spends a lot of time in VM calls doing manual matrix multiplication which is bound to be much slower than the dot product matrix multiplication you're doing. Gutamatics was more of an experiment that ended up influencing much better matrix/quaternion classes in the game engine project that I started (and need to finish, lol).

Re: Level and screen projections (+ free aiming)

Posted: Sun May 05, 2019 1:50 pm
by KeksDose
Strange situation when the main attraction of a library wasn't its focus. I had more to say but I forgot, so do your best on your projects!

@Nash: Plenty of considerations to be made here. I'll see what can be done.

Re: Level and screen projections (+ free aiming)

Posted: Mon May 06, 2019 11:16 am
by kodi
Thanks a ton for this! I tried but couldn't make heads or tails of your PM in the end. Awesome to have my deprojection 'accurized' now! :D

Re: Level and screen projections (+ free aiming)

Posted: Thu May 09, 2019 10:29 am
by Nash
Another suggestion: option to enable distance scaling with projected image. Probably would be easier done if the projections were drawn using Shape2D...

Re: Level and screen projections (+ free aiming)

Posted: Sat May 11, 2019 5:11 am
by KeksDose
I'll let the projector return the view distance, then you can rescale the screen (or shape) yourself for drawing.

@kodi: I think I should've asked beforehand what you already have. If it works now and everybody agrees, that's good.

Re: Level and screen projections (+ free aiming)

Posted: Sun May 12, 2019 8:24 am
by kodi
You said you were going to release it, so I just waited patiently. Had much else to work on too!

Re: Level and screen projections (+ free aiming)

Posted: Sun May 19, 2019 4:04 pm
by KeksDose
You and me both. As scheduled, here is the update for resize handling... (well, at least it was Sunday when I started typing♪)

With the Le_Viewport struct, you can bring a projected position into view if it has been resized with screenblocks and status bar scaling. For this, I named two spaces: Viewport is what you see, while the scene is what gets shifted by the status bar.

I only updated the bounding box viewer and opengl text candle to use this handling at the moment. Scopes for the free aim are a mess to figure out, so aiming will be weird with the status bar, till I sort it out.

For now, you can turn around and play with screenblocks to see how the new checks work. If you hold alt fire, you will see the viewport and scene bounds. Here's how to use it:

Code: Select all

// Assuming proj is a projector you set up like in the OP. This brings the projected
// position into your resized viewport at your window resolution.
Le_Viewport viewport;
viewport.FromHud();

proj.ProjectWorldPos(mo.pos + (0, 0, 0.5 * mo.height));
let draw_pos = viewport.SceneToWindow(proj.ProjectToNormal());

// (Insert some draw stuff here)      
I'm not forgetting about visibility or scaling. For now, I'm fixing up Guncaster (rather, I've been doing so for a while, and now we're crunching again). I should also document this. One step at a time.

Re: Level and screen projections (now with resize handling)

Posted: Mon May 20, 2019 9:17 am
by m8f
Will there be a git repository for libeye? One may want to submodule it.