1

enter image description here

I want to draw a triangle like this

I think I have to change these part of my codes

    glBegin(GL_LINES);
    glVertex2f(point[0][0], point[0][1]);
    glVertex2f(point[1][0], point[1][1]);
    glEnd();

and my mouse button down codes are like this

if (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT)
{
    inputMode = InputMode::DRAGGING; // Dragging starts

    point[0][0] = xw;   point[0][1] = yw; // Start point
    point[1][0] = xw;   point[1][1] = yw; // End point

}

How I have to do?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Concerning your exposed code: For a triangle, you need 3 lines, don't you? Alternatively, you could consider a `GL_LINE_STRIP`. (I mean: before you cannot render a triangle it's a bit useless to think about how to interact with it.) – Scheff's Cat Sep 23 '22 at 06:55

1 Answers1

2

You need some global variables for 3 points and one index that is telling you which point you actually edit ...

float point[3][2];
int ix=0;

the render change to

glBegin(GL_LINE_LOOP); // or GL_TRIANGLE
glVertex2fv(point[0]);
glVertex2fv(point[1]);
glVertex2fv(point[2]);
glEnd();

now I do not code in GLFW but you need to change the onclick events to something like:

static bool q0 = false; // last state of left button
bool q1 = (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT); //actual state of left button
if ((!q0)&&(q1)) // on mouse down
    {
    if (ix==0) // init all points at first click
      {
      point[0][0]=xw; point[0][1]=yw;
      point[1][0]=xw; point[1][1]=yw;
      point[2][0]=xw; point[2][1]=yw;
      }
    }
if (q1) // mouse drag
    {
    point[ix][0]=xw; point[ix][1]=yw;
    }
if ((q0)&&(!q1)) // mouse up
    {
    point[ix][0]=xw; point[ix][1]=yw;
    ix++;
    if (ix==3)
      {
      ix=0;
      // here finalize editation for example
      // copy the new triangle to some mesh or whatever...
      }
    }
q0=q1; // remember state of mouse for next event

This is my standard editation code I use in my editors for more info see:

I am not sure about the q1 as I do not code in GLFW its possible you could extract the left mouse button state directly with different expression. the q0 does not need to be static but in such case it should be global... Also its possible the GLFW holds such state too in which case you could extract it similarly to q1 and no global or static is needed for it anymore...

Spektre
  • 49,595
  • 11
  • 110
  • 380