DDA Line Drawing Algorithm has errors

痞子三分冷 提交于 2019-12-13 09:37:36

问题


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

回答1:


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.



回答2:


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).



来源:https://stackoverflow.com/questions/31806853/dda-line-drawing-algorithm-has-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!