获取并设置像素的颜色
get and set color of pixel
我需要为像素设置颜色。
当我尝试设置某个像素的颜色时(通过单击鼠标左键)。我的鼠标功能。
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
pixel *p = new pixel();
p->x = x;
p->y = HEIGHT - y;
stack.push(p);
float arr[3];
readPixel(p->x, p->y, arr);
std::cout<<"pixel color: ";
std::cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<std::endl;
drawPixel(p->x, p->y);
}
}
这里readPixel
方法
void readPixel(int x, int y, float (&a)[3]) {
GLubyte arr[3];
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, arr);
a[0] = (float)arr[0] / 255.0f;
a[1] = (float)arr[1] / 255.0f;
a[2] = (float)arr[2] / 255.0f;
};
问题在于设置像素的颜色。我使用字段x
和y
创建结构pixel
。当我单击左按钮时,对象pixel
被添加到堆栈中。当我尝试为像素设置颜色(绘制它)时 - 像素在方法 drawPixel
中不会改变其颜色
void draw() {
glBegin(GL_POINTS);
if (!stack.empty()) {
drawPixel(stack.top()->x, stack.top()->y);
stack.pop();
}
glEnd();
glFlush();
};
void drawPixel(int x, int y) {
glRasterPos2i(x, y);
glDrawPixels(1, 1, GL_RGB, GL_UNSIGNED_BYTE, &val);
};
&val
在哪里float val[3] = { 1.0, 1.0, 0.0 };
所以问题是如何为坐标为 x 和 y 的像素设置颜色?
解决方案是将GL_UNSIGNED_BYTE
更改为GL_FLOAT
,而不是从堆栈中pop
项目
相关文章:
- 如何在24位SDL_Surface上设置像素的颜色
- 如何从SDL_Surface获取特定像素的颜色
- 如何返回定义良好的内存部分?例如来自图像数据的像素的颜色值
- 访问和更改基于另一个图像的像素颜色-opencv c++
- 可视化 如何获取图像中像素的颜色(以C++ (sfml) 为单位)
- OpenCV 访问像素的颜色值
- 无法获取窗口 C++ 的像素颜色
- 如何使用 openCV 更改图像每个像素的颜色
- 用Xcb而不是Xlib抓取像素的颜色
- 无法在QImage上绘制或更改像素的颜色
- 为什么像素不改变颜色
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- 检测特定像素上的某种颜色并在检测到后发送点击的最快方法是什么?
- 无法将图像的所有像素更改为所选颜色
- 如何使用 glDrawPixel 时更改像素的颜色,我的像素始终为红色
- 如何在 QLabel 中以像素为单位放置颜色数据
- 获取 OpenGL 纹理中特定像素的颜色
- 根据OPENCV中一个通道中的一个值,查找最大颜色像素
- glsl-获取像素颜色[像素着色器]
- magick++修剪像素不是背景颜色(像素的顶部和底部的图像相同的颜色)