用鼠标在 OpenGL GLUT 中绘制多边形
Draw a polygon in OpenGL GLUT with mouse
我想在 openGL Glut 中通过鼠标交互绘制一个多边形,每次左键单击都会成为一个顶点,并且会在每个顶点之间绘制一条线。单击鼠标右键时,多边形将关闭从最后一个顶点到第一个顶点绘制一条线。我想到了这个,但它似乎不起作用。
void draw_polygon(int button, int state, int x, int y) {
bool right_pushed = 0;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
p1.x = x;
p1.y = 480 - y;
//if right is clicked draw a line to here
first.x = x;
first.y = 480 - y;
}
while (right_pushed == false) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
p2.x = x;
p2.y = 480 - y;
}
GLfloat dx = p2.x - p1.x;
GLfloat dy = p2.y - p1.y;
GLfloat x1 = p1.x;
GLfloat y1 = p1.y;
GLfloat step = 0;
if (abs(dx) > abs(dy)) {
step = abs(dx);
}
else {
step = abs(dy);
}
GLfloat xInc = dx / step;
GLfloat yInc = dy / step;
for (float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
p1.x = p2.x;
p1.y = 480 - y;
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
right_pushed = 1;
p2.x = first.x;
p2.y = first.y;
dx = p2.x - p1.x;
dy = p2.y - p1.y;
x1 = p1.x;
y1 = p1.y;
step = 0;
if (abs(dx) > abs(dy)) {
step = abs(dx);
}
else {
step = abs(dy);
}
xInc = dx / step;
yInc = dy / step;
for (float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
}
}
glEnd();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("windows");
glutDisplayFunc(display);
glutMouseFunc(draw_polygon);//
init();
glutMainLoop();
return 0;
}
我也在尝试找出如何添加一个功能,当我从菜单中 select 时,我可以从创建这个多边形到以我可以 select 的方式编辑它vertex ,左右移动,形状随之变化
你必须把鼠标事件和绘图函数分开。
在鼠标事件中,您应该只收集输入。我建议为此使用 std::vector
。如果按下鼠标左键,以下函数会向 std::vector
添加一个点。如果按下右键,多边形被标记为关闭。如果再次按下左键,多边形将被清除并重新开始。
#include <vector>
#include <array>
int vp_width = 640;
int vp_height = 480;
std::array<int, 2> currentPt;
std::vector<std::array<int, 2>> pts;
bool closed = false;
void draw_polygon(int button, int state, int x, int y)
{
currentPt = std::array<int, 2>{x, vp_height-y};
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if ( closed )
pts.clear(); // restart if last action was close
closed = false;
pts.push_back( currentPt );
}
if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
closed = true;
}
在鼠标移动事件函数中,您可以跟踪当前鼠标位置:
void mouse_move(int x, int y)
{
currentPt = std::array<int, 2>{x, vp_height-y};
glutPostRedisplay();
}
在您的主循环中,您可以连续绘制当前点之间的线。下面的函数在点列表之间画线。如果设置了 "cloesd" 标志,则多边形被关闭。否则绘制从列表中的最后一个点到当前鼠标位置的线。
void display(void)
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
if ( !pts.empty() )
{
glBegin(GL_LINE_STRIP);
for ( auto &pt : pts )
glVertex2f( (float)pt[0], (float)pt[1] );
auto &endPt = closed ? pts.front() : currentPt;
glVertex2f( (float)endPt[0], (float)endPt[1] );
glEnd();
}
//glFlush();
glutSwapBuffers();
}
int main()
{
.....
glutDisplayFunc(display);
glutPassiveMotionFunc (mouse_move);
glutMouseFunc(draw_polygon);
glMatrixMode( GL_PROJECTION );
glOrtho(0.0f, (float)vp_width, 0.0f, (float)vp_height, -1.0, 1.0);
.....
}
预览:
我想在 openGL Glut 中通过鼠标交互绘制一个多边形,每次左键单击都会成为一个顶点,并且会在每个顶点之间绘制一条线。单击鼠标右键时,多边形将关闭从最后一个顶点到第一个顶点绘制一条线。我想到了这个,但它似乎不起作用。
void draw_polygon(int button, int state, int x, int y) {
bool right_pushed = 0;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
p1.x = x;
p1.y = 480 - y;
//if right is clicked draw a line to here
first.x = x;
first.y = 480 - y;
}
while (right_pushed == false) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
p2.x = x;
p2.y = 480 - y;
}
GLfloat dx = p2.x - p1.x;
GLfloat dy = p2.y - p1.y;
GLfloat x1 = p1.x;
GLfloat y1 = p1.y;
GLfloat step = 0;
if (abs(dx) > abs(dy)) {
step = abs(dx);
}
else {
step = abs(dy);
}
GLfloat xInc = dx / step;
GLfloat yInc = dy / step;
for (float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
p1.x = p2.x;
p1.y = 480 - y;
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
right_pushed = 1;
p2.x = first.x;
p2.y = first.y;
dx = p2.x - p1.x;
dy = p2.y - p1.y;
x1 = p1.x;
y1 = p1.y;
step = 0;
if (abs(dx) > abs(dy)) {
step = abs(dx);
}
else {
step = abs(dy);
}
xInc = dx / step;
yInc = dy / step;
for (float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
}
}
glEnd();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("windows");
glutDisplayFunc(display);
glutMouseFunc(draw_polygon);//
init();
glutMainLoop();
return 0;
}
我也在尝试找出如何添加一个功能,当我从菜单中 select 时,我可以从创建这个多边形到以我可以 select 的方式编辑它vertex ,左右移动,形状随之变化
你必须把鼠标事件和绘图函数分开。
在鼠标事件中,您应该只收集输入。我建议为此使用 std::vector
。如果按下鼠标左键,以下函数会向 std::vector
添加一个点。如果按下右键,多边形被标记为关闭。如果再次按下左键,多边形将被清除并重新开始。
#include <vector>
#include <array>
int vp_width = 640;
int vp_height = 480;
std::array<int, 2> currentPt;
std::vector<std::array<int, 2>> pts;
bool closed = false;
void draw_polygon(int button, int state, int x, int y)
{
currentPt = std::array<int, 2>{x, vp_height-y};
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if ( closed )
pts.clear(); // restart if last action was close
closed = false;
pts.push_back( currentPt );
}
if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
closed = true;
}
在鼠标移动事件函数中,您可以跟踪当前鼠标位置:
void mouse_move(int x, int y)
{
currentPt = std::array<int, 2>{x, vp_height-y};
glutPostRedisplay();
}
在您的主循环中,您可以连续绘制当前点之间的线。下面的函数在点列表之间画线。如果设置了 "cloesd" 标志,则多边形被关闭。否则绘制从列表中的最后一个点到当前鼠标位置的线。
void display(void)
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
if ( !pts.empty() )
{
glBegin(GL_LINE_STRIP);
for ( auto &pt : pts )
glVertex2f( (float)pt[0], (float)pt[1] );
auto &endPt = closed ? pts.front() : currentPt;
glVertex2f( (float)endPt[0], (float)endPt[1] );
glEnd();
}
//glFlush();
glutSwapBuffers();
}
int main()
{
.....
glutDisplayFunc(display);
glutPassiveMotionFunc (mouse_move);
glutMouseFunc(draw_polygon);
glMatrixMode( GL_PROJECTION );
glOrtho(0.0f, (float)vp_width, 0.0f, (float)vp_height, -1.0, 1.0);
.....
}
预览: