# How to position/transform vertices for 2D UI in shaders?

I am building a 3D engine and have a rendering abstraction that focuses on writing shaders. Most my 3D shaders have gl_Position output like:

gl_Position = projection * view * model * vec4(position, 1);

I would like to draw some 2D UI on top of all the 3D draw calls. I am having a hard time understanding how to do this.

Let's say I wanted to draw a HUD player life bar that is fixed to the bottom left corner of the screen. I can imagine drawing a quad. and maybe applying a texture to it. I would have 4 vertices for the quad to feed to the shader. How would I change the gl_Position calculation to handle drawing only to 2D screen space? Would I update the transformation to only apply the projection matrix?

gl_Position = projection * vec4(position, 0, 1);

How to determine the local coordinates of the quad verts? When the screen scales or resolution varies, How to ensure the quad stays positioned to the lower bottom screen? How to ensure the quad dimensions stay the same ratio?

For 2D UI you typically achieve this by using an orthographic projection matrix, and setting view and model to identity, which collapses the typical transform:

gl_Position = projection * view * model * vec4(position, 1);


To:

gl_Position = projection * vec4(position, 1);


So yes, this approach is correct.

To lay out the UI elements, there are a number of possible approaches, but one simple and commonly used one is to set up your ortho matrix like so:

glOrtho (0, width, height, 0, -1, 1);


Or if you prefer:

glm::ortho (0, width, height, 0, -1, 1);


And you can then lay out your UI using screen co-ordinates. note 1

What might not be immediately obvious is that width and height here need not be the width and height of your window (or screen); they can be any arbitrary values. So you could, for example, decide to use width 640 and height 480 and then lay out your UI as if you had a 640x480 screen, and the ortho matrix will automatically scale and position all UI elements for you.

640x480 obviously assumes a 4:3 aspect ratio, which - on most computers from the last 15-off years - you and your users probably don't have, so just adjust the numbers to the correct aspect ration; e.g. for a 16:9 aspect ratio you might use 800x450 instead.

note 1: If this causes your UI to be upside-down onscreen, simply reverse the bottom and top params, like so: (0, width, 0, height, -1, 1).
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.24240200221538544, "perplexity": 1661.0164751983684}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-45/segments/1603107878633.8/warc/CC-MAIN-20201021205955-20201021235955-00047.warc.gz"}