#include <stdio.h>
#include <math.h>
#include<time.h>
#include<stdlib.h>
#include <GL/glut.h>
clock_t t;
double X1, Y1, X2, Y2;
void delay(int number_of_seconds)
{
// Converting time into milli_seconds
int milli_seconds = 1000 * number_of_seconds;
// Stroing start time
clock_t start_time = clock();
// looping till required time is not acheived
while (clock() < start_time + milli_seconds)
;
}
float round_value(float v)
{
return floor(v + 0.5);
}
void MP(void)
{
double x,y,p;
x=X1;
y=Y1;
double dx=(X2-X1);
double dy=(Y2-Y1);
p=2*dy-dx;
double steps;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int k;
/* For every step, find an intermediate vertex */
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
while(x<X2)
{
if(p>=0)
{
glVertex2d(round_value(x), round_value(y));
y=y+1;
p=p+2*dy-2*dx;
}
else
{
glVertex2d(round_value(x), round_value(y));
p=p+2*dy;
}
x=x+1;
}
glEnd();
glFlush();
}
void LineDDA(void)
{
glClear(GL_COLOR_BUFFER_BIT);
double dx=(X2-X1);
double dy=(Y2-Y1);
double steps;
float xInc,yInc,x=X1,y=Y1;
/* Find out whether to increment x or y */
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int k;
/* For every step, find an intermediate vertex */
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
glVertex2d(round_value(x), round_value(y));
}
glEnd();
glFlush();
}
void Init()
{
/* Set clear color to white */
glClearColor(1.0,1.0,1.0,0);
/* Set fill color to black */
glColor3f(0.0,0.0,0.0);
/* glViewport(0 , 0 , 640 , 480); */
/* glMatrixMode(GL_PROJECTION); */
/* glLoadIdentity(); */
gluOrtho2D(0 , 640 , 0 , 480);
}
void main(int argc, char **argv)
{
char ch='n';
int choice;
/* Initialise GLUT library */
glutInit(&argc,argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
do
{
printf("Enter two end points of the line to be drawn:\n");
printf("\n************************************");printf("\nEnter Point1( X1 , Y1):\n");
scanf("%lf%lf",&X1,&Y1);
printf("\n************************************");
printf("\nEnter Point1( X2 , Y2):\n");
scanf("%lf%lf",&X2,&Y2);
glFlush();
/* Create the window with title "DDA_Line" */
printf("!----------------Menu---------------!\n");
printf("!---------------1.DDA---------------!\n");
printf("!------------2.Brehanham------------!\n");
scanf("%d",&choice);
glutCreateWindow("Compare b/w DDA and Bresenham");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
t=clock();
if(choice==1)
glutDisplayFunc(LineDDA);
else if(choice==2)
glutDisplayFunc(MP);
else
printf("\nWrong choice");
t=clock()-t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("Algorithm took %f seconds to execute \n", time_taken);
printf("Question?\n");
scanf(" %c",&ch);
delay(3000);
glutDestroyWindow(1);
}while(ch=='Y');
/* Keep displaying until the program is closed */
glutMainLoop();
}
我正在尝试实现算法之间的比较。 (中点和DDA)。但是当我尝试打开窗口时,它会截取背景而不是实际算法。如何刷新帧缓冲区以避免这种情况?我把glFlush放在多个地方,但这似乎没有办法。 任何帮助都将深表感谢。
答案 0 :(得分:0)
恐怕你误解了如何使用GLUT。
每次要求窗口重绘时,glutMainLoop
执行的操作都会调用您的集glutDisplayFunc
。
你描述的是&#34;截取背景的截图&#34;实际上是#34;我的窗户没有被重新粉刷&#34;。
前进的方法如下:
glutDisplayFunc
设置为您自己的功能,将存储的输入呈现为屏幕,同时牢记用户选择的绘图算法。glutMainLoop
。这将一直运行,直到用户退出程序,此时循环将退出,您可以销毁窗口。