View Full Version : how to map normalized device coordinates/world space to screen coordinates in 2D?

hopjopper

06-03-2016, 07:51 PM

I am trying to map the NDC to screen coordinates in 2D , what is the formula for this?

i dont know matrix mathematics so if you can give me the Cartesian formula or method

just to be precise i am trying to go from vertices i give opengl to screen coordinates i.e -1.0,0.0 etc to screen coordinates 200,300 etc in 2D space

GClements

06-04-2016, 03:26 AM

just to be precise i am trying to go from vertices i give opengl to screen coordinates i.e -1.0,0.0 etc to screen coordinates 200,300 etc in 2D space

Vertices which you give to OpenGL are first converted to clip coordinates, either by multiplying them by the model-view and projection matrices (for the fixed-function pipeline), or by whatever transformations the vertex shader implements (gl_Position is in clip coordinates).

After clipping, clip coordinates are converted to NDC by dividing by W. NDC are converted to window coordinates by the viewport transformation:

Xw = (xndc+1)*(width/2)+x

yw = (yndc+1)*(height/2)+y

where x,y,width,height are the parameters to glViewport().

Note that window coordinates are relative to the lower-left corner of the window.

hopjopper

06-07-2016, 10:40 PM

thanks for that information

also if it is possible if you would be able to tell me how i would also go in reverse? if i have screen coordinates how would i go to ndc?

GClements

06-08-2016, 05:55 AM

if i have screen coordinates how would i go to ndc?

xndc = (xw-x)/(width/2)-1

yndc = (yw-y)/(height/2)-1

hopjopper

06-08-2016, 06:01 PM

thanks for that lol i obviously missed something quiet simple, but thanks nevertheless

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.