【问题标题】:Draw a polygon from mouse clicks通过鼠标点击绘制多边形
【发布时间】:2016-05-08 19:22:15
【问题描述】:

我正在尝试在屏幕上绘制一个多边形,其顶点由鼠标单击确定。 左键单击将顶点添加到多边形,右键单击会将最后一个顶点添加到多边形,将其连接到第一个并创建形状。

我目前有两个向量,一个用于 x 坐标,一个用于 y 坐标,我正在循环创建一个线循环的向量。向量中的 -1 确定多边形的结束和新多边形的开始。这是一个随后在显示函数中调用的函数。

最终我必须扫描转换这些多边形,然后使用 Sutherland Hodgman 算法将它们剪辑到用户定义的窗口中,但我什至无法显示多边形。

glBegin(GL_LINE_LOOP);
for (int i = 0; i < xCo.size(); i++)
{
    if (xCo[i + 1] != -1)
    {
        glVertex2f(xCo[i], yCo[i]);
        glVertex2f(xCo[i + 1], yCo[i + 1]);
    }
    else
    {
        glVertex2f(xCo[i + 1], yCo[i + 1]);
        glVertex2f(xCo[0], yCo[0]);
    }
}
glEnd();
glFlush();
xCo.clear();
yCo.clear();

【问题讨论】:

    标签: c++ opengl


    【解决方案1】:

    使用structs 而不是单独的数组和float 比较:

    #include <glm/glm.hpp>
    
    typedef vector< glm::vec2 > Poly;
    void drawPoly( const Poly& poly )
    {
        if( poly.size() == 1 )
            glBegin( GL_POINTS );
        else
            glBegin( GL_LINE_STRIP );
    
        for( const auto& pt : poly )
        {
            glVertex2f( pt.x, pt.y );
        }
    
        glEnd();
    }
    

    在上下文中:

    #include <GL/glut.h>
    #include <glm/glm.hpp>
    #include <vector>
    
    typedef std::vector< glm::vec2 > Poly;
    void drawPoly( const Poly& poly )
    {
        if( poly.size() == 1 )
            glBegin( GL_POINTS );
        else
            glBegin( GL_LINE_STRIP );
    
        for( const auto& pt : poly )
        {
            glVertex2f( pt.x, pt.y );
        }
    
        glEnd();
    }
    
    typedef std::vector< Poly > Polys;
    Polys polys( 1 );
    void mouse( int button, int state, int x, int y )
    {
        if( GLUT_UP == state && GLUT_LEFT_BUTTON == button )
        {
            polys.back().push_back( glm::vec2( x, y ) );
            glutPostRedisplay();
        }
        if( GLUT_UP == state && GLUT_RIGHT_BUTTON == button )
        {
            polys.back().push_back( polys.back().front() );
            polys.push_back( Poly() );
            glutPostRedisplay();
        }
    }
    
    void display()
    {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        double w = glutGet( GLUT_WINDOW_WIDTH );
        double h = glutGet( GLUT_WINDOW_HEIGHT );
        glOrtho( 0, w, h, 0, -1, 1 );
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    
        glColor3ub( 255, 255, 255 );
        for( const auto& poly : polys )
        {
            drawPoly( poly );
        }
    
        glutSwapBuffers();
    }
    
    int main( int argc, char **argv )
    {
        glutInitWindowSize( 640, 480 );
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
        glutCreateWindow( "GLUT" );
        glutDisplayFunc( display );
        glutMouseFunc( mouse );
        glutMainLoop();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2013-04-20
      相关资源
      最近更新 更多