c++ - OpenGL 中一致的手绘线

标签 c++ opengl glut

我知道如何在 opengl 中徒手绘制,但我希望鼠标移动速度过快时不会出现间隙。这是我的:

void myMovedMouse(int mouseX, int mouseY)
{
    int x = mouseX;
    int y = IMAGE_Y - mouseY - 1;
    //int brushSize = 20;
    //glRecti(x, y, x + brushSize, y + brushSize);
    drawDot(x, y);
    glFlush();
}

//in main
glutDisplayFunc(myDisplay);
glutMotionFunc(myMovedMouse);

我也尝试过使用 GL_LINE_LOOP,当然那没有用。

最佳答案

将鼠标位置附加到 std::vector 并使用 GL_LINE_STRIP:

#include <GL/glut.h>
#include <vector>

std::vector< int > points;
void mouse( int button, int state, int x, int y )
{
    if( state == GLUT_DOWN )
        points.clear();
    points.push_back( x );
    points.push_back( y );
    glutPostRedisplay();
}

void motion( int x, int y )
{
    points.push_back( x );
    points.push_back( y );
    glutPostRedisplay();
}

void display()
{
    glClearColor( 0, 0, 0, 1 );
    glClear(GL_COLOR_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();

    glBegin( GL_LINE_STRIP );
    glColor3ub( 255, 0, 0 );
    for( size_t i = 0; i < points.size(); i += 2 )
    {
        glVertex2i( points[i+0], points[i+1] );
    }
    glEnd();

    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutCreateWindow( "GLUT" );
    glutMouseFunc( mouse );
    glutMotionFunc( motion );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

关于c++ - OpenGL 中一致的手绘线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648327/

相关文章:

c++ - 将 AoS 转换为 SoA 时处理组合爆炸

c++ - OpenGL Scaling/Translation 坐标系或语句顺序错误

c++ - 四元数和翻译

swift - 在 Swift 项目中使用 OpenGL

opengl - 在OpenGL GLUT程序中动态查找屏幕宽度和高度

c++ - 在循环中显示 glutDisplayFunc 函数

c++ - Opengl/Glut - 使倾斜变得平滑

c++ - 为什么在通过添加字符串文字和 char 进行初始化时不打印字符串

c++ - 这可以用 C++ 中的模板来完成吗?

c++ - 递归和函数 c++