OpenGL通过单击鼠标来绘制三角形

kzipqqlq  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(242)

我想画一个这样的三角形

我想我必须更改我的代码的这一部分

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

我的鼠标按键代码是这样的

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

}

我要怎么做?

ztigrdn8

ztigrdn81#

您需要针对3个点的一些全局变量和一个告诉您实际编辑哪个点的索引……

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

渲染器将更改为

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

现在,我不使用GLFW编写代码,但您需要将onClick事件更改为如下所示:

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

这是我在我的编辑器中使用的标准编辑代码。有关更多信息,请参阅:

我不确定q1,因为我不是用GLFW编码的,有可能你可以用不同的表达式直接提取鼠标左键的状态。q0不需要是静态的,但在这种情况下,它应该是全局的...也有可能GLFW也具有这样的状态,在这种情况下,您可以类似于q1来提取它,并且它不再需要全局或静态...

相关问题