I’m working on some home-made menu stuff under DX11. I’m creating the rect menus at start all located at position 0,0. Each menu contains some lines of options. Everything is created as transformed vertex (e.g. -1..1 coordinates). Now I want the menu to appear where I click the mouse. So I need to offset the mesh vertices and I’m trying to do it within the vertex shader.

Here the code

The shader constant to store the offset in Rectshow
cbuffer cbMesh : register(b8)
 float4 FactorColor;
 float4 RectShow;

The offsetting at the vertex level
  output.Pos = float4(input.Pos.xy + RectShow.xy, input.Pos.z, 1);
  output.Tex = input.Tex;
  return output;

Within my app the letters positions are calculated this way with Rect.xy being in coordinate [0..Screensize]:

FLOAT tX = Rect->x * 2 / gViewPort.width -1;
FLOAT tY = 1 - Rect->y * 2/ gViewPort.Height;

For each letter and lines I use some tX/tY offsetting during mesh creation.

Now logically I’m calculating the offset from mouse position Pt in coordinate [0..Screensize]:

gCBFont.RectShow.x = Pt.x * 2 / gViewPort.Width - 1;
gCBFont.RectShow.y = (1 - Pt.y * 2 / gViewPort.Height);
gpDC11->UpdateSubresource(gpCBBufferFont, 0, NULL, &gCBFont, 0, 0);
gpDC11->VSSetConstantBuffers(dwSlotFont, 1, &gpCBBufferFont);

if I set arbitrarily RectShow.xy to 0,0 the menu appears at it should at the 0,0 screen location. But when I use the calculated offset I see nothing.

Before doing it this way I was creating the menu upon click directly at the mouse position and it was working fine but it appeared less easy to manage as you need to destroy and recreate the meshes for each click. With this second option I can show/hide the meshes when needed and the menu is created only once. But I have to move it at the mouse position.

I’m missing something here probably. Maybe I should use map/unmap to update the menu meshes instead of the shader but it sounds weird.


1 Answer 1


After some arbitrary value setting finally the mistake was in the offset calculation that must be like this:

gCBFont.RectShow.x = Pt.x / gViewPort.Width *2;
gCBFont.RectShow.y = -Pt.y / gViewPort.Height * 2;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .