Jump to content
  • Advertisement

OpenGL OpenGL 2D GUI system question

This topic is 449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all,

i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.

now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.

1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?

2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.

3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).

lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,

Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.

IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.

thank you, and looking forward to positive replies.

Share this post

Link to post
Share on other sites

No no no, you can pass pixel coords to vertex shader and there recalculate actual vertex clip position on screen not to mention y axis is flipped so you start from y=0 at bottom left corner


So how to do that in shader well first of all you pass vertex pixel coords then you pass screen size in pixels then you make something like nv.x = v.x / screen_width to get percentage of screen coords this should give you a number from 0..1 then you need to multiple it by 0.5 and subtract it by -1??? I really forgot how to do this anyway you need to end with a number between -1 and 1 or was it 0.5 as I said I'm on the phone now and can't actually check that for code let others think

Share this post

Link to post
Share on other sites

Store the coordinates in screen coordinates. Use this to check against mouse position.

Set up a VAO, and VBO with a rectangle only 1.0f by 1.0f, and 0.0f as the depth. You can also disable depth before rendering, and enabling it again after.

Translate the model matrix (intialized to identity matrix) by (xPos, yPos, 0.0f).

Scale the model matrix by (width, height, 1.0f).

Multiply the projection matrix (use ortho for 2D that doesn't ever need to simulate depth) by the model matrix. Then send that one resulting matrix to the shader.

These are the steps I use, the first one ensures that the mouse checks pass. The others ensure that the sprite renders at the same position.

Edited by Yxjmir

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!