DDA线描算法有错误 [英] DDA Line Drawing Algorithm has errors
问题描述
为什么我在使用此代码时显示未定义setPixel未定义"错误?
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;
}
推荐答案
因为您没有在任何地方定义setPixel
.这不是OpenGL调用.您需要自己编写,并且应该在缓冲区上设置像素(如果您使用的是双缓冲),然后将其用作glDrawPixels()
的参数,或者使用glVertex2i(x,y)
调用显示缓冲区.您可以在此处和此处.
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.
此外,您的LineSegment
功能已损坏.在OpenGL中,您调用glutDisplayFunc
来指定一个称为尽快的函数以渲染显示.但是,在此函数中,您调用scanf()
提示用户输入数据-这已损坏.您应该在开始时提示一次,然后将数据传递到函数中(一旦调用glutMainLoop
,它将尽可能频繁地运行).
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).
这篇关于DDA线描算法有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!