【问题标题】:DDA Line Drawing Algorithm has errorsDDA 画线算法有错误
【发布时间】:2015-08-04 10:28:52
【问题描述】:

为什么我收到错误提示“未定义 setPixel”?

#include <windows.h>    
#include <stdio.h>    
#include <math.h>    
#include <stdlib.h>    
#include<GL/glut.h>

inline int round(const float a)
{
    return int (a+0.5);
}

void init(void)
{
    glClearColor(0.0f,0.0f,1.0f,1.0f);
    gluOrtho2D(0.0,200.0,0.0,200.0);
    glMatrixMode(GL_PROJECTION);
}

void LineSegment(int xa, int ya,int xb,int yb)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f,0.0f,0.0f);    

    printf("Enter the initial value");
    scanf("%d%d",&xa,&ya);

    printf("Enter the final value");
    scanf("%d%d",&xb,&yb);

    int dx=xb-xa;
    int dy=yb-ya;
    int steps,k;
    float xIncrement,yIncrement,x=xa,y=ya;
    if(fabs(dx)>fabs(dy))
        steps=fabs(dx);
    else
        steps=fabs(dy);

    xIncrement=dx/(float)steps;
    yIncrement=dy/(float)steps;
    setPixel(round(x),round(y));
    for(k=0;k<steps;k++);
    {
        x += xIncrement;
        y += yIncrement;
        setPixel(round(x),round(y));
    }
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
    glutCreateWindow("DDA Line Algorithm");
    glutDisplayFunc(LineSegment);
    init();
    glutMainLoop();
    return 0;
}

【问题讨论】:

  • 你在哪一行得到错误?

标签: c opengl opengl-3


【解决方案1】:

因为 OpenGL 或 GLUT 中没有 setPixel 方法,而且据我从您的代码中看到的,您也没有定义一个。 OpenGL 处理渲染图元,如点、线、三角形等,但不直接在屏幕上设置单个像素。由于不清楚您想要实现什么建议:

  • 如果您想在 OpenGL 中绘制一条线,请使用适当的方法,例如 glBegin(GL_LINES), etc.(尽管它们已被弃用且不应再使用。)或 glDrawArrays(GL_LINES, ...
  • 如果目标是实现 dda 软件光栅化器,那么您可能必须将像素写入纹理,然后显示此纹理。

【讨论】:

    【解决方案2】:

    因为您还没有在任何地方定义setPixel。这不是 OpenGL 调用。您需要自己编写它,它应该在缓冲区上设置像素(如果您使用双缓冲),然后您将其用作glDrawPixels() 的参数,或使用glVertex2i(x,y) 调用显示缓冲区。您可以查看herehere 两种方法的示例。

    另外,您的LineSegment 功能已损坏。在 OpenGL 中,您调用glutDisplayFunc 来指定一个调用尽可能快 的函数来渲染显示。但是,在此函数中,您调用 scanf() 来提示用户输入数据 - 这已损坏。您应该在开始时提示它们一次,然后将该数据传递给函数(一旦调用glutMainLoop,它将尽可能频繁地运行)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多