gpt4 book ai didi

c - Opengl 中的边界填充算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:52 27 4
gpt4 key购买 nike

算法进入递归阶段。它在多边形上但在特定位置(X=397,Y=98)打印出蓝线。颜色不会在像素处打印,然后进入同一像素及其相邻像素的递归。

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

typedef struct pixel{ GLubyte red, green, blue; } pixel;



void boundaryfill(float x,float y, pixel fill, pixel boundary)
{
pixel c;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &c);
// printf("%d,%d,%d",(int)c.red,(int)c.green,(int)c.blue);
if ((c.red!=boundary.red)&&(c.red!=boundary.blue)&&(c.green!=boundary.green)&&(c.green!=fill.green)&&(c.blue!=fill.blue)&&(c.red!=fill.red)&&\
(x<=400))//&&(y<=100)&&(y>=50)&&(x>=200))
{

glBegin(GL_POINTS);
glColor3ub(fill.red,fill.green,fill.blue);
glVertex2f(x,y);
glEnd();
glFlush();
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &c);
printf("\nCOlOR %d,%d,%d",(int)c.red,(int)c.green,(int)c.blue);
printf("\nX=%f,Y=%f",x,y)

//
boundaryfill(x+1,y,fill,boundary);
boundaryfill(x-1,y,fill,boundary);
boundaryfill(x,y+1,fill,boundary);
boundaryfill(x,y-1,fill,boundary);

}

}

void mydisplay()
{
glBegin(GL_POLYGON);
glColor3ub(10,10,10);
glVertex2f(200,50);
glVertex2f(200,100);
glVertex2f(400,100);
glVertex2f(400,50);
glEnd();
glFlush();
pixel fill,boundary;
fill.red=0;
fill.green=0;
fill.blue=255;
boundary.red=255;
boundary.green=255;
boundary.blue=255;
boundaryfill(300,75,fill,boundary);
glEnd();
glFlush();
}

void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(540,320);
glutCreateWindow("my first attempt");
glClearColor(1.0f,1.0f,1.0,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(mydisplay);

gluOrtho2D(0.0,400.0,0.0,400.0);
glutMainLoop();


}

最佳答案

我怀疑问题是由于无限递归导致堆栈溢出。是什么阻止了程序永远只调用 boundaryfill

此外,您在 boundaryfill 中缺少对 glEnd 的调用,但我怀疑这不是崩溃的原因。

关于c - Opengl 中的边界填充算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22496405/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com