DDA Line Drawing Algorithm has errors

2019-09-21 09:46发布

Why am I getting an error saying 'setPixel not defined' with this code?

#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;
}

2条回答
成全新的幸福
2楼-- · 2019-09-21 09:57

Because there is no setPixel method in OpenGL or GLUT and as far as I can see from your code, you do not define one either. OpenGL deals with rendering primitives like points, lines, triangels etc, but not directly with setting single pixels on the screen. Since it is unclear what you want to achieve some suggestions:

  • If you want to draw a line in OpenGL, use the appropriate methods like glBegin(GL_LINES), etc. (although they are deprecated and should not be used anymore.) or glDrawArrays(GL_LINES, ....
  • If the goal is to implement a dda software rasterizer, then you might have to write the pixels to a texture and then display this texture.
查看更多
我想做一个坏孩纸
3楼-- · 2019-09-21 10:19

Because you haven't defined setPixel anywhere. It's not an OpenGL call. You need to write it yourself, and it should set pixels on a buffer (if you're using double buffering) which you then later use as an argument to glDrawPixels(), or a call to the display buffer using glVertex2i(x,y). You can see an example of both approaches here and here.

Also, your LineSegment function is broken. In OpenGL you call glutDisplayFunc to specify a function which is called as fast as possible to render the display. However, in this function you call scanf() to prompt the user for data - this is broken. You should prompt them once at the start, and then pass that data into the function (which will then run as often as possible once glutMainLoop is called).

查看更多
登录 后发表回答