2D drawing with jit.gl Mesh & fit to screen

soundyi's icon

Hi there,
I am looking for a way to draw points in a 2D grid fashion with a [ jit.gl.mesh ], whose position vectors I calculate in a [ jit.gen ] patcher and visualising this „fit to screen“ in a [ jit.world ] … getting stuck with the fit to screen thing.

What I see so far (using the attached patch) is, that if I scale the mesh to some certain degree, I get the effect, so that each of the corner points of the „2D point grid mesh“ are drawn exactly in the corner of the window, which is what I mean with „fit to screen“.

But as I want to be able to change the window size and also the dimension of the „point grid“, I need a proper way to do this - by means of calculating the scale factor taking into account the position bounds of the mesh and the jit.gl.camera properties (using the Pythagorean theorem) or an even more elegant way ;-).

I can imagine that there is a „well known“ approach & formula for this (which I have not come across yet), maybe a [ jit.gen ] patch that does the proper „scaling“ of the position vectors upfront (hence no need to scale the mesh afterwards), or maybe even a shortcut patching technique using some helper jit.gl.? objects.

I tried to "shortcut" via [ jit.gl.node ] and a [jit.gj.videolpane ] (hoping for transform_reset to get the effect done) without success, realising that an "in patcher" [ jit.gl.node ] of course has the same camera projection logic going on as the "main" one in [ jit.world ] and hence a videoplane "down the cords" cannot solve the challenge as I thought in the first place (to cheap that cheat ;-).

I want to explore 2D flow fields and I saw an approach from Frederico (Amazing Max Stuff, https://youtu.be/uGnbA8ExYkU), that has a similar „fit to screen“ technique, but reading his patch, (right now ;-) I cannot distinguish the vector math used for the flow field logic and what is used to get the „fit to screen“ effect.

Any ideas?

Thanks & cheers
soundyi

2D drawing with Mesh + fit to screen.maxpat
Max Patch
2D point grid with jit.gl.mesh, failing fit to screen

Matteo Marson's icon

Hey there,

when dealing with 2D graphics in openGL, the simplest way imo is to use orthographic projection. If you set @ortho 2 for jit.world (or for jit.gl.camera), perspective is no longer taken into account. Hence, you can place the dots on screen giving coordinates in the range [-aspect_ratio; aspect_ratio] for the X (with aspect_ratio = window_size_in_pixels_X / window_size_in_pixels_Y) and in the range [-1;1] for the Y.
This patch automatically adjusts the grid to fit the screen when the window is resized:

Max Patch
Copy patch and select New From Clipboard in Max.

Hope it helps, cheers!

soundyi's icon

Thanks Matteo - that's exactly what I was looking for, it helps perfectly!

So I got it now, even in a "physical realistic" point of view : a pure orthographic projection, where the camera is looking in a parallel manner at the mesh. And as the camera is fully parallel to the (plane) mesh surface, the amount of pixel that apear "fit to screen" correspond to the pixel resolution of the (jit.world) window.

Or in other words : the camera looks in perpendicular / orthogonal angle at the mesh (the 2D surface created by the grid points) and all the "beams" of the cameras "pixel sensor" fall in parallel (this is the "no lense angle" option ) onto the mesh surface ... hence the Z position of the camera can be changed to huge positive values, without any effect, as there is no projection respectively lense angle.

Then the X and Y position of the camera can be used to slide the canvas (flow field) in to the view or out.

Thanks again & cheers