# From window coordinates to scalled / moved NDC - pseudo code ONLY please

I have decided to bite the bullet and learn how to “transform”.
Found one of the best references in above , older , link.
My objective is super simple
move mouse to a point @ window (space?) , push (left) button and mark such x,y point as center of “zoom circle” _ DONE
drag mouse , with button pushed and use the x,y as “diameter” of the “zoom circle” -DONE

What I now need is to “transform” existing “unity circle” - center @0.0 and diameter of 1 to real location and size selected @ window.

I failed to draw the window circle using GLint, AND I want to learn how to do this magic “transfer” matrices stuff.

Help I like to get from this group is
pseudo code on how to implement such "transform " in 2D “space”.
The examples I am reading are FROM “camera” to “view space” - I sort of want it in reverse.
My “problem” is keeping windows x,y real coordinates in sync with scaled / moved NDC.

This is what I have so far

``````case 11: {
// draw circle
glNewList(list_index, GL_COMPILE);   //
OpenGL_NDC_coordinates();
glNewList(list_index, GL_COMPILE);   //

// test values - "camera" (?)
// transform from  window location
GLint mouse_x_center = 100;
GLint mouse_y_center =  100;
GLint mouse_x_radius = 200;
GLint mouse_y_radius = 200;
// transform HERE (?)
// unity circle - view "space / object (?) " @ window
float x_start = .1; // see offset
float y_start = .0;
float radius = 1.0;

// transform HERE (?)

glPointSize(5); //
// NDC

break;
}``````
``````win_x = viewport_x + viewport_width  * (ndc_x+1)/2;
win_y = viewport_y + viewport_height * (ndc_y+1)/2;
``````

The inverse transformation is

``````ndc_x = 2*(win_x-viewport_x) / viewport_width  - 1;
ndc_y = 2*(win_y-viewport_y) / viewport_height - 1;
``````

The `viewport_*` variables should hold the values of the corresponding arguments to the most recent `glViewport` call.

Note that the origin for window coordinates is in the lower-left, with Y increasing upward. Most UI toolkits will report mouse events relative to the upper-left, with Y increasing downward. To convert mouse coordinates to (OpenGL) window coordinates, use:

``````win_x = 0.5 + mouse_x;
win_y = win_height - 0.5 - mouse_y;
``````

The offset of 0.5 is there so that you get the coordinates of the centre of the pixel under the cursor. This tends to be more useful than the coordinates of one of the corners of the pixel. Change either or both to 0 if you want the coordinates of the upper/left corner, to 1 for the bottom/right.

If transforming sizes (rather than positions), you just want to multiply or divide by `viewport_width/2` and `viewport_height/2`. Bear in mind that a circle in window space is usually an ellipse in NDC and vice versa, i.e. you’ll need separate x/y radii in NDC if you want it to be circular in window coordinates. And if you want it to be an actual circle, you may need to consider the monitor’s physical resolution; monitors don’t necessarily have perfectly square pixels (e.g. my main monitor is 1920x1200 = 16:10, but the physical dimensions of the panel are much closer to 16:9).

Thanks, that is exactly what I was looking for.
In the meantime I found / modified this gross hack working with actual mouse
position. It needs some tweaking - my “center” of the zoom circle keeps “tracking”
the mouse which it should not ! ( I love creating my own bugs!)

But I like your approach better , since I am really after learning to use viewport.

``````     	int width = glutGet( GLUT_WINDOW_WIDTH);
int height = glutGet( GLUT_WINDOW_HEIGHT);
WIN_NDC[index].NDC_x = (float) mouse_x / (width / 2.0f); // This will give a value from 0 - 2
WIN_NDC[index].NDC_y = (float) mouse_y / (height / 2.0f); // This will give a value from 0 - 2
WIN_NDC[index].NDC_x -= 1.0f; // Subtract 1 to get a value from -1 to 1.
WIN_NDC[index].NDC_y -= 1.0f; // Subtract 1 to get a value from -1 to 1.
// WIN_NDC[index].NDC_y = -WIN_NDC.NDC_y; // flip y direction no flipping on radius ??ckquote``````