[英]Opengl draw a triangle by clicking a mouse
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?我该怎么办?
You need some global variables for 3 points and one index that is telling you which point you actually edit...您需要一些用于 3 个点的全局变量和一个告诉您实际编辑的点的索引...
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:现在我不在 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
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.我不确定q1
,因为我没有在 GLFW 中编码,您可以使用不同的表达式直接提取鼠标左键 state。 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... q0
不需要是 static 但在这种情况下它应该是全局的...... GLFW 也可能持有这样的 state 在这种情况下,您可以与q1
类似地提取它,并且不再需要全局或 ZA81259CEF45659C2247ZDF1。 .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.