Interested in how to create a button, for example, how best to do it.
It is necessary that when you hover it changes color, and when you click something happened event. For example, we have a drawn square (glRect), and we need to hang the hover event on the object and click on the object.

    3 answers 3

    @Alexander Startsev , I recommend to begin to understand how the OpenGL pipeline is arranged.

    The video card does not know anything about any buttons and especially events. And hardly knows about the squares. OpenGL is mainly concerned with feeding the video card lists of vertex coordinates and additional information on how to draw them. Those. all that the library allows you to do is clean the screen and draw a square. Next yourself.

    Get yourself a class or structure that describes the button: coordinates, dimensions, and the state of the button (pressed / not pressed). Create an instance, fill in all the fields. In the drawing cycle, clean the screen and draw a button based on the above data.

    Capture the mouse click event. If we are talking about Windows - through the WM_LBUTTONDOWN message, for example. Then, check whether the click on your button falls by comparing the coordinates of the click with the coordinates and dimensions of the button. If - yes, change the state to "pressed". And on the WM_LBUTTONUP event, similarly check and change the state to “not pressed”. At this time, updating the appearance will diligently deal with the rendering cycle.

    Ps. You can go the boring way and take a ready-made library that will do it all for you (there are options with drawing an interface in Flash, for example). Google by keyword "opengl ui framework".

    • I wish I could have written so easily, I wouldn’t have written here, and I wouldn’t have written on OpenGL - Alexander Startsev
    • one
      @Alexander Startsev, if something is not clear - ask clarifying questions, I will help you figure it out. So my diploma was devoted to drawing windows and buttons on OpenGL in C ++ under Windows. - Nofate ♦

    If this is a Windows application, then the mouse constantly sends its coordinates and button states as messages to the application queue. I think it will not be difficult for you to intercept these messages and impose them on the coordinates of your rectangle.

      In C ++, you need to do this:

      An abstract class is created with an event handler, and classes of graphic objects are created by inheriting from it. OpenGL passes the message to the handler whose object is in the current mouse coordinates. You can also assign a message transfer for other events. It is better to send events in Windows format, so as not to fool with incompatibility.