0

I'm making a 2D drawing canvas in OpenGL that allows you to draw shapes on the drawing canvas by simply selecting which shape you want to draw.

I have created the entire interface for the 2D drawing canvas and also added in a mouse function that reads a screen size of 1000x800 that works perfectly fine also, however with some squares that I have drawn out, I want to be able to change the colour of them depending on which is click (Highlighting them basically).

Here's my drawSquare function that draws the 4 points to make a square:

void drawSquare(GLfloat length, GLfloat x, GLfloat y, GLfloat outline)
    {
        // x1,y1 is the top left-hand corner coordinate
        // and so on...
        GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    x1 = x - length / 2;
    y1 = y + length / 2;
    x2 = x + length / 2;
    y2 = y + length / 2;
    x3 = x + length / 2;
    y3 = y - length / 2;
    x4 = x - length / 2;
    y4 = y - length / 2;

    // ACTUAL SQUARE OBJECT
    glColor3f(0.0, 1.0, 1.0); // Colour: Cyan
    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);     // vertex for BLUE SQUARES
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd();

    if (outline == true)
    {
        // SQUARE OUTLINE
        glColor3f(0.0, 0.0, 0.0);   // Colour: Black
        glBegin(GL_LINE_LOOP);
        glLineWidth(2);
        glVertex2f(x1, y1);     // vertex for OUTLINE
        glVertex2f(x2, y2);
        glVertex2f(x3, y3);
        glVertex2f(x4, y4);
        glEnd();
    }
    glFlush();
}

And to draw it, I simply call the function in my display() function using drawSquare(100, 50, 350,true);.

But if I want to be able to highlight each square when clicked, how would I do this?

Ben Tanos
  • 51
  • 2
  • 8
  • You should look to this https://www.opengl.org/archives/resources/faq/technical/selection.htm – Cherkesgiller Nov 02 '16 at 20:40
  • simply by after rendering whole scene you render the selected stuff again but with different color (make sure depth test does not screw that up). If you need help with the selection itself see [simple C++ Drag&Drop example](http://stackoverflow.com/a/20924609/2521214) the architecture of the code should be almost the same as your editors ... unless you are using different techniques ofcoarse. For complex shapes and many entities gfx approach is better see edit1 in [Improving performance of click detection on a staggered column isometric grid](http://stackoverflow.com/a/35917976/2521214) – Spektre Nov 03 '16 at 08:48
  • btw I am not sure if ti is wise to call `glFlush` after each entity ... I call it usually after the whole scene is pended for render. It may or may not be a performance problem on some implementations. – Spektre Nov 03 '16 at 08:56
  • I've kind of got it working, I made an if statement that checks its location when I click, and if its true, I made it exit the application to test if the statement works properly, and it does. Now I want to call the drawSquare() function again with a different colour when the statement is true, how would I do this? – Ben Tanos Nov 03 '16 at 17:08
  • that does not sound right ... why exit anything? On mouse event just compute the actual mouse position and the selection state and store the result into some "global" variables like `float mx,my;` for mouse position and `int sel_tp=-1,sel_ix=-1;` for selected item type and index in the list of objects. Then in your render routine after all the items are rendered render selected item again with different color if `sel_tp>=0`. You can even draw the mouse cursor or what ever. The selection computation code is in booth links I provided in previous comment... – Spektre Nov 07 '16 at 08:56
  • simpler would be determine selection from mouse position inside rendering the square but for your editor purposes I recommend not to do this as it will bring you problems with other functionality you would need later – Spektre Nov 07 '16 at 09:01

0 Answers0