#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放在了多个位置,但这似乎并不能解决问题。
任何帮助将不胜感激。

最佳答案

恐怕您误解了如何使用GLUT。
glutMainLoop的作用是每次要求窗口重新粉刷时调用您的设置glutDisplayFunc
您所说的“拍摄背景截图”实际上是“我的窗口没有被重新粉刷”。

一种前进的方式如下:


首先收集所有用户输入(坐标和绘图机制),并将它们存储在内存中(最初将使用全局变量)。
接下来,将您的glutDisplayFunc设置为您自己的函数,该函数会将存储的输入呈现到屏幕上,同时牢记用户选择的绘制算法。
最后,输入glutMainLoop。这将一直运行到用户退出程序为止,这时循环将退出,您可以销毁窗口。

09-08 09:55