c - OpenGL 截取背景屏幕截图而不是执行

标签 c opengl

#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。这将运行直到用户退出程序,此时循环将退出并且您可以销毁窗口。

关于c - OpenGL 截取背景屏幕截图而不是执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48803729/

相关文章:

java - Bat文件自动更新空气 native 应用程序而不显示cmd

c - fprint 不能在循环中工作

python - 如何使用 opengl 和 pyglet 设置裁剪平面

c++ - 在 Emscripten 和 Qt 之间共享 OpenGL 代码

c - 如何使用 printf() 实现 gotoxy()

c++ - 原生显卡功能

c - 为什么我无法输入所需的输入数量?

java - 将 2d 窗口点转换为 3d 点

c++ - 使用 OpenSceneGraph 创建和导出球体

c++ - OpenGL,使用浮点颜色渲染到纹理,无需裁剪值