【问题标题】:Drawing a linestrip with opengl1 by looping through a linked list of vectors通过循环向量链表使用opengl1绘制线条
【发布时间】:2020-05-01 10:38:33
【问题描述】:

我之前问过question关于将点击的坐标附加到单链表,以便循环遍历链表,将顶点放在点击的位置并将它们连接起来

给出的答案解决了我的链表溢出问题,但后来我遇到了显示功能的问题,例如当我循环遍历链表并将这些坐标添加到glVertex2i(); 时,它不会绘制线条当我在图形窗口上多次单击时。

我试图看看删除while 循环后会发生什么,但这会导致分段错误。

这些是结构体。

typedef struct vector{int x;int y;}Vector;
typedef struct VectorList{Vector X; struct VectorList*next; }node_v;

我已经声明了这些

Vector P;
node_v * prev;

并借助my previous question的答案,用点击的坐标初始化Vector P并附加到向量链表node_v * prev;

static void _display_CB()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(0,window_width,window_height,0,-1,1);

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3ub(0,0,0);
    glBegin(GL_LINE_STRIP);
    while(prev){
        glVertex2i( prev->X.x, prev->X.y);
        prev=prev->next;
    }

    glEnd();
    glFlush();
  glutSwapBuffers();
glutPostRedisplay();
}

应该在_display_CB() 中进行哪些更改以使程序能够绘制如图中的线条?

【问题讨论】:

    标签: c opengl linked-list glut opengl-compat


    【解决方案1】:

    prev 是列表的头部。当您执行elem = elem->next; 时,您实际上将列表的头部更改为列表的尾部。保留列表的头部并使用局部变量elem 来遍历列表。
    此外,正交投影矩阵glOrtho应设置为当前投影矩阵(GL_PROJECTION):

    void _display_CB() {
    
        node_v * elem = prev;
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, window_width, window_height, 0, -1, 1);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glClear(GL_COLOR_BUFFER_BIT);
    
        glColor3ub(0, 0, 0);
        glBegin(GL_LINE_STRIP);
        while(elem){
            glVertex2i(elem->X.x, elem->X.y);
            elem = elem->next;
        }
        glEnd();
    
        glutSwapBuffers();
        glutPostRedisplay();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多