GLUT中的glReadPixels使用 5

1.使用工具

Code::Blocks

2.代码

用GLUT获取一个点的RGB。
两行代码,glReadPixels 。
读取到的是R,G,B三者的值。

GLfloat color[3];
glReadPixels(xMouse,yMouse,1,1,GL_RGB,GL_FLOAT,&color);
// 打印
cout << color[0] << color[1] << color[2] << endl;

如果帮到你了,点个赞吧!
O(∩_∩)O~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,你需要在 Visual Studio 安装 FreeGLUT 库。可以在官网下载对应版本的库,并按照官方指南进行安装。 接下来,你可以创建一个空的 C++ 项目,并在项目属性添加 FreeGLUT 库的链接。具体步骤如下: 1. 右键点击项目,选择“属性”。 2. 在左侧面板选择“VC++ 目录”。 3. 在“包含目录”添加 FreeGLUT 的 include 文件夹路径。 4. 在“库目录”添加 FreeGLUT 的 lib 文件夹路径。 5. 在“链接器 -> 输入 -> 附加依赖项”添加 FreeGLUT 的 lib 文件名(例如:freeglut.lib)。 完成上述步骤后,你可以开始编写四连通边界填算法和泛滥填充算法的代码。以下是一个简单的示例: ```cpp #include <GL/freeglut.h> void drawPixel(int x, int y) { glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } void boundaryFill4(int x, int y, float* fillColor, float* borderColor) { float interiorColor[3]; glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, interiorColor); if (interiorColor[0] != borderColor[0] || interiorColor[1] != borderColor[1] || interiorColor[2] != borderColor[2]) { drawPixel(x, y); glFlush(); boundaryFill4(x + 1, y, fillColor, borderColor); boundaryFill4(x - 1, y, fillColor, borderColor); boundaryFill4(x, y + 1, fillColor, borderColor); boundaryFill4(x, y - 1, fillColor, borderColor); } } void floodFill(int x, int y, float* fillColor, float* borderColor) { float interiorColor[3]; glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, interiorColor); if (interiorColor[0] != borderColor[0] || interiorColor[1] != borderColor[1] || interiorColor[2] != borderColor[2]) { drawPixel(x, y); glFlush(); floodFill(x + 1, y, fillColor, borderColor); floodFill(x - 1, y, fillColor, borderColor); floodFill(x, y + 1, fillColor, borderColor); floodFill(x, y - 1, fillColor, borderColor); } } void display() { float fillColor[] = { 1.0, 1.0, 0.0 }; float borderColor[] = { 0.0, 0.0, 0.0 }; glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.0, 0.0); glBegin(GL_LINE_LOOP); glVertex2i(100, 100); glVertex2i(200, 100); glVertex2i(200, 200); glVertex2i(100, 200); glEnd(); boundaryFill4(150, 150, fillColor, borderColor); //floodFill(150, 150, fillColor, borderColor); glFlush(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400, 400); glutCreateWindow("Boundary Fill and Flood Fill Algorithms"); gluOrtho2D(0, 400, 0, 400); glutDisplayFunc(display); glutMainLoop(); return 0; } ``` 该示例包含了两个函数:`boundaryFill4` 和 `floodFill`,它们分别实现了四连通边界填算法和泛滥填充算法。在 `display` 函数,我们先画了一个矩形,并在其心点处调用了这两个函数。 注意,该示例只实现了单色填充。如果你需要用不同的颜色填充不同区域,可以在 `drawPixel` 函数添加更多参数,并在 `boundaryFill4` 和 `floodFill` 函数进行判断和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KUOKUO众享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值