如何建设网站简介,网页制作教程 赵丰年,做软装搭配的网站,wordpress 主机 优点glReadPixels函数用于从帧缓冲区中读取像素数据。它可以用来获取屏幕上特定位置的像素颜色值或者获取一块区域内的像素数据。下面是该函数的基本语法#xff1a; void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *da…glReadPixels函数用于从帧缓冲区中读取像素数据。它可以用来获取屏幕上特定位置的像素颜色值或者获取一块区域内的像素数据。下面是该函数的基本语法 void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *data);x 和 y读取区域左下角的像素坐标。 width 和 height读取区域的宽度和高度。 format像素数据的格式可以是 GL_RGBA、GL_RGB、GL_DEPTH_COMPONENT 等等。 type数据的数据类型如 GL_UNSIGNED_BYTE、GL_FLOAT 等。 data存储像素数据的缓冲区。 举个例子如果你想获取屏幕上位置 (100, 100) 处的像素颜色值你可以这样使用 glReadPixels
GLubyte pixel[3];
glReadPixels(100, 100, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);这将会把位置 (100, 100) 处的 RGB 像素颜色值存储在 pixel 数组中。
如果你想读取一个区域的像素数据可以调整 width 和 height 参数的值。
请注意glReadPixels 函数是一个相对慢的操作因为它涉及到从显存中读取数据这可能会在性能方面产生一些影响。在实际应用中如果需要频繁地读取像素数据最好考虑使用一些其他的技术来减少性能开销。
标题示例一实现读取屏幕中间的颜色
//实现读取屏幕中间的颜色
#define WindowWidth 400
#define WindowHeight 400#pragma warning(disable:4996)#include GL/glut.h
#include iostream
#include math.hvoid display() {glClearColor(0.0, 0.0, 0.0, 1.0);glClear(GL_COLOR_BUFFER_BIT);// Draw something on the screenglColor3f(1.0, 0.0, 0.0); // Red colorglBegin(GL_TRIANGLES);glVertex2f(-0.5, -0.5);glVertex2f(0.5, -0.5);glVertex2f(0.0, 0.5);glEnd();glFlush(); // Ensure all drawing commands are executed
}void readPixelData() {GLint viewport[4]; // Viewport dimensions [x, y, width, height]glGetIntegerv(GL_VIEWPORT, viewport); // Get viewport dimensionsGLubyte pixel[3];/*在例子中GLubyte pixel[3]; 定义了一个长度为3的数组用来存储从glReadPixels函数中读取到的像素颜色值。由于我们在这个例子中读取的是RGB颜色所以数组的长度是3分别用于存储红、绿和蓝三个颜色分量的值。*/GLint x viewport[2] / 2; // X coordinate at the center/*viewport[2] 表示视口viewport的宽度。视口是屏幕上用于显示OpenGL图形的区域。数组索引为 2 的元素存储的就是视口的宽度。viewport[2] / 2 就是将视口的宽度除以 2从而得到了视口的中心位置的 x 坐标。*/GLint y viewport[3] / 2; // Y coordinate at the center// Invert y-coordinate to match OpenGLs coordinate systemy viewport[3] - y - 1;glReadBuffer(GL_FRONT); // Set the buffer to read from the front bufferglReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);printf(Pixel color at center: R%u, G%u, B%u\n, pixel[0], pixel[1], pixel[2]);
}int main(int argc, char** argv) {glutInit(argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(800, 600);glutCreateWindow(glReadPixels Example);glutDisplayFunc(display);glutIdleFunc(readPixelData); // Read pixel data after drawing and during idle timeglutMainLoop();return 0;
} 标题示例二获取屏幕上鼠标点击位置的颜色
#include GL/glut.h
#include iostream// 存储窗口大小
int windowWidth 800;
int windowHeight 600;// 存储鼠标点击位置的颜色
GLubyte clickedColor[3] { 0, 0, 0 };
void display() {glClearColor(0.0f, 0.0f, 0.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);// 绘制渐变色矩形glBegin(GL_QUADS);// 左上角顶点红色glColor3ub(255, 0, 0);glVertex2f(-0.5f, 0.5f);// 右上角顶点绿色glColor3ub(0, 255, 0);glVertex2f(0.5f, 0.5f);// 右下角顶点蓝色glColor3ub(0, 0, 255);glVertex2f(0.5f, -0.5f);// 左下角顶点黄色glColor3ub(255, 255, 0);glVertex2f(-0.5f, -0.5f);glEnd();glFlush();
}
//鼠标左键按下并释放时获取鼠标点击位置的像素颜色并将该颜色信息显示在控制台中
void mouse(int button, int state, int x, int y) {if (button GLUT_LEFT_BUTTON state GLUT_DOWN) {GLint viewport[4];//这一行代码获取当前窗口的视口信息并将信息存储在 viewport 数组中。视口包括窗口的位置和尺寸。glGetIntegerv(GL_VIEWPORT, viewport);// 使用glReadPixels获取点击位置的像素颜色glReadPixels(x, viewport[3] - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, clickedColor);//viewport[3]表示视口的高度std::cout Clicked pixel color: R static_castint(clickedColor[0]) , G static_castint(clickedColor[1]) , B static_castint(clickedColor[2]) std::endl;}
}int main(int argc, char** argv) {glutInit(argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(windowWidth, windowHeight);glutCreateWindow(Mouse Click Color Picker);glutDisplayFunc(display);glutMouseFunc(mouse);glutMainLoop();return 0;
} 文章转载自: http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.dbddm.cn.gov.cn.dbddm.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.snnb.cn.gov.cn.snnb.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.lgkbn.cn.gov.cn.lgkbn.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.crfyr.cn.gov.cn.crfyr.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.thwhn.cn.gov.cn.thwhn.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.msmtf.cn.gov.cn.msmtf.cn