使用 glReadPixels 从 OpenGL 获取 ColorData
Get ColorData from OpenGL use glReadPixels
我尝试在pyopengl中使用边界填充算法。
但是当我想要获取三角形的颜色时,它并不适用。
它只是获取背景颜色,而不是三角形的颜色
您可以在 myDisplay() 中看到它 func.i`用蓝色绘制三角形
glBegin(GL_LINE_LOOP)
glVertex2i(100, 100)
glVertex2i(150, 100)
glVertex2i(150, 150)
但是我看不到它的颜色
同时,我画了一条线来测试,但是我也画不出它的颜色。
for i in range(120, 130):
setPixel(i, 110)
print(i, 110, getColor(i, 110))
print(i, 110, getColor(i, 111))
print(i, 110, getColor(i, 109))
这里是getcolor的函数
def getColor(x, y):
color = (GLuint * 1)(0)
#读取点(x,y)的颜色color
glReadPixels(122, 101, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
color=int(color[0])
r = color & 255
g = (color >> 8) & 255
b = (color >> 16) & 255
color = (r, g, b)
return color
这是代码
# -*- coding:utf-8 -*-
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
#判断c1,c2的颜色是否相同
def rgbColorEqual(c1, c2):
# if (abs(c1[0]-c2[0]) < 0.001 and abs(c1[1], c2[1]) < 0.001 and abs(c1[2]-c2[2]) < 0.001):
# return 1
# else:
# return 0
return 1
#绘制像素点(x,y)
def setPixel(x, y):
glBegin(GL_POINTS)
glVertex2i(x, y)
glEnd()
glFlush()
return
#获得点(x,y)的颜色
def getColor(x, y):
color = (GLuint * 1)(0)
#读取点(x,y)的颜色color
glReadPixels(122, 101, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
color=int(color[0])
r = color & 255
g = (color >> 8) & 255
b = (color >> 16) & 255
color = (r, g, b)
return color
#4-连通边界填充算法
def boundaryFill4(x, y, fillColor, borderColor):
interiorColor=getColor(x,y)
print(x,y,interiorColor)
# if(not rgbColorEqual(interiorColor, borderColor) and not rgbColorEqual(interiorColor, fillColor)):
# setPixel(x, y)
# boundaryFill4(x+1,y,fillColor,borderColor)
# boundaryFill4(x-1,y,fillColor,borderColor)
# boundaryFill4(x,y+1,fillColor,borderColor)
# boundaryFill4(x,y-1,fillColor,borderColor)
#显示函数
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT)
#设置填充色为a红色
#设置边界色为b蓝色
a = (1, 0, 0)
b = (0, 0, 1)
glColor3fv(b)
#绘制三角形的边
glBegin(GL_LINE_LOOP)
glVertex2i(100, 100)
glVertex2i(150, 100)
glVertex2i(150, 150)
glEnd()
glFlush()
#取图形中的一点,进行填充
glColor3fv(a)
for i in range(120, 130):
setPixel(i, 110)
print(i, 110, getColor(i, 110))
print(i, 110, getColor(i, 111))
print(i, 110, getColor(i, 109))
for i in range(100,150):
print(i,100,getColor(i,100))
boundaryFill4(120, 106, a, b)
if __name__ == "__main__":
glutInit()
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(100, 100)
glutInitWindowSize(500, 500)
glutCreateWindow('midpointcircle')
glClearColor(0.5, 0.5, 0.5, 0)
glMatrixMode(GL_PROJECTION)
gluOrtho2D(0.0, 500.0, 0.0, 500.0)
glutDisplayFunc(myDisplay)
glutMainLoop()
确保点和线图元绘制在像素的中心。由于顶点由投影矩阵(和模型视图矩阵)变换,如果您通过整数坐标指定顶点,它们可能会出现不准确:
glBegin(GL_LINE_LOOP)
glVertex2f(100+0.5, 100+0.5)
glVertex2f(150+0.5, 100+0.5)
glVertex2f(150+0.5, 150+0.5)
glEnd()
当然要在getColor
中使用x
和y
坐标;
glReadPixels(122, 101, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
元组是可比较的,因此rgbColorEqual
的实现很简单:
def rgbColorEqual(c1, c2):
return c1[0] == c2[0]
你必须 setPixel
并继续递归算法,如果 interiorColor
不等于 borderColor
且不等于 fillColor
:
def boundaryFill4(x, y, fillColor, borderColor):
interiorColor=getColor(x,y)
if not rgbColorEqual(interiorColor, borderColor) and not rgbColorEqual(interiorColor, fillColor):
setPixel(x, y)
boundaryFill4(x+1,y,fillColor,borderColor)
boundaryFill4(x-1,y,fillColor,borderColor)
boundaryFill4(x,y+1,fillColor,borderColor)
boundaryFill4(x,y-1,fillColor,borderColor)
getColor
返回的颜色通道在 [0, 255] 范围内。我建议通过相同范围内的值设置颜色属性 (glColor3ubv
):
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT)
#设置填充色为a红色
#设置边界色为b蓝色
a = (255, 0, 0)
b = (0, 0, 255)
glColor3ubv(b)
#绘制三角形的边
glBegin(GL_LINE_LOOP)
glVertex2f(100+0.5, 100+0.5)
glVertex2f(150+0.5, 100+0.5)
glVertex2f(150+0.5, 150+0.5)
glEnd()
glFlush()
#取图形中的一点,进行填充
glColor3ubv(a)
boundaryFill4(120, 106, a, b)
glFlush()
看例子:
# -*- coding:utf-8 -*-
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
#判断c1,c2的颜色是否相同
def rgbColorEqual(c1, c2):
return c1[0] == c2[0]
#绘制像素点(x,y)
def setPixel(x, y):
glBegin(GL_POINTS)
glVertex2f(x+0.5, y+0.5)
glEnd()
glFlush()
return
#获得点(x,y)的颜色
def getColor(x, y):
color = (GLuint * 1)(0)
#读取点(x,y)的颜色color
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
color=int(color[0])
r = color & 255
g = (color >> 8) & 255
b = (color >> 16) & 255
color = (r, g, b)
return color
#4-连通边界填充算法
def boundaryFill4(x, y, fillColor, borderColor):
interiorColor=getColor(x,y)
if not rgbColorEqual(interiorColor, borderColor) and not rgbColorEqual(interiorColor, fillColor):
setPixel(x, y)
boundaryFill4(x+1,y,fillColor,borderColor)
boundaryFill4(x-1,y,fillColor,borderColor)
boundaryFill4(x,y+1,fillColor,borderColor)
boundaryFill4(x,y-1,fillColor,borderColor)
glFlush()
#显示函数
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT)
#设置填充色为a红色
#设置边界色为b蓝色
a = (255, 0, 0)
b = (0, 0, 255)
glColor3ubv(b)
#绘制三角形的边
glBegin(GL_LINE_LOOP)
glVertex2f(100+0.5, 100+0.5)
glVertex2f(150+0.5, 100+0.5)
glVertex2f(150+0.5, 150+0.5)
glEnd()
glFlush()
#取图形中的一点,进行填充
glColor3ubv(a)
boundaryFill4(120, 106, a, b)
glFlush()
if __name__ == "__main__":
glutInit()
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(100, 100)
glutInitWindowSize(500, 500)
glutCreateWindow('midpointcircle')
glClearColor(0.5, 0.5, 0.5, 0)
glMatrixMode(GL_PROJECTION)
gluOrtho2D(0.0, 500.0, 0.0, 500.0)
glutDisplayFunc(myDisplay)
glutMainLoop()
我尝试在pyopengl中使用边界填充算法。
但是当我想要获取三角形的颜色时,它并不适用。
它只是获取背景颜色,而不是三角形的颜色
您可以在 myDisplay() 中看到它 func.i`用蓝色绘制三角形
glBegin(GL_LINE_LOOP)
glVertex2i(100, 100)
glVertex2i(150, 100)
glVertex2i(150, 150)
但是我看不到它的颜色
同时,我画了一条线来测试,但是我也画不出它的颜色。
for i in range(120, 130):
setPixel(i, 110)
print(i, 110, getColor(i, 110))
print(i, 110, getColor(i, 111))
print(i, 110, getColor(i, 109))
这里是getcolor的函数
def getColor(x, y):
color = (GLuint * 1)(0)
#读取点(x,y)的颜色color
glReadPixels(122, 101, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
color=int(color[0])
r = color & 255
g = (color >> 8) & 255
b = (color >> 16) & 255
color = (r, g, b)
return color
这是代码
# -*- coding:utf-8 -*-
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
#判断c1,c2的颜色是否相同
def rgbColorEqual(c1, c2):
# if (abs(c1[0]-c2[0]) < 0.001 and abs(c1[1], c2[1]) < 0.001 and abs(c1[2]-c2[2]) < 0.001):
# return 1
# else:
# return 0
return 1
#绘制像素点(x,y)
def setPixel(x, y):
glBegin(GL_POINTS)
glVertex2i(x, y)
glEnd()
glFlush()
return
#获得点(x,y)的颜色
def getColor(x, y):
color = (GLuint * 1)(0)
#读取点(x,y)的颜色color
glReadPixels(122, 101, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
color=int(color[0])
r = color & 255
g = (color >> 8) & 255
b = (color >> 16) & 255
color = (r, g, b)
return color
#4-连通边界填充算法
def boundaryFill4(x, y, fillColor, borderColor):
interiorColor=getColor(x,y)
print(x,y,interiorColor)
# if(not rgbColorEqual(interiorColor, borderColor) and not rgbColorEqual(interiorColor, fillColor)):
# setPixel(x, y)
# boundaryFill4(x+1,y,fillColor,borderColor)
# boundaryFill4(x-1,y,fillColor,borderColor)
# boundaryFill4(x,y+1,fillColor,borderColor)
# boundaryFill4(x,y-1,fillColor,borderColor)
#显示函数
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT)
#设置填充色为a红色
#设置边界色为b蓝色
a = (1, 0, 0)
b = (0, 0, 1)
glColor3fv(b)
#绘制三角形的边
glBegin(GL_LINE_LOOP)
glVertex2i(100, 100)
glVertex2i(150, 100)
glVertex2i(150, 150)
glEnd()
glFlush()
#取图形中的一点,进行填充
glColor3fv(a)
for i in range(120, 130):
setPixel(i, 110)
print(i, 110, getColor(i, 110))
print(i, 110, getColor(i, 111))
print(i, 110, getColor(i, 109))
for i in range(100,150):
print(i,100,getColor(i,100))
boundaryFill4(120, 106, a, b)
if __name__ == "__main__":
glutInit()
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(100, 100)
glutInitWindowSize(500, 500)
glutCreateWindow('midpointcircle')
glClearColor(0.5, 0.5, 0.5, 0)
glMatrixMode(GL_PROJECTION)
gluOrtho2D(0.0, 500.0, 0.0, 500.0)
glutDisplayFunc(myDisplay)
glutMainLoop()
确保点和线图元绘制在像素的中心。由于顶点由投影矩阵(和模型视图矩阵)变换,如果您通过整数坐标指定顶点,它们可能会出现不准确:
glBegin(GL_LINE_LOOP)
glVertex2f(100+0.5, 100+0.5)
glVertex2f(150+0.5, 100+0.5)
glVertex2f(150+0.5, 150+0.5)
glEnd()
当然要在getColor
中使用x
和y
坐标;
glReadPixels(122, 101, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
元组是可比较的,因此rgbColorEqual
的实现很简单:
def rgbColorEqual(c1, c2):
return c1[0] == c2[0]
你必须 setPixel
并继续递归算法,如果 interiorColor
不等于 borderColor
且不等于 fillColor
:
def boundaryFill4(x, y, fillColor, borderColor):
interiorColor=getColor(x,y)
if not rgbColorEqual(interiorColor, borderColor) and not rgbColorEqual(interiorColor, fillColor):
setPixel(x, y)
boundaryFill4(x+1,y,fillColor,borderColor)
boundaryFill4(x-1,y,fillColor,borderColor)
boundaryFill4(x,y+1,fillColor,borderColor)
boundaryFill4(x,y-1,fillColor,borderColor)
getColor
返回的颜色通道在 [0, 255] 范围内。我建议通过相同范围内的值设置颜色属性 (glColor3ubv
):
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT)
#设置填充色为a红色
#设置边界色为b蓝色
a = (255, 0, 0)
b = (0, 0, 255)
glColor3ubv(b)
#绘制三角形的边
glBegin(GL_LINE_LOOP)
glVertex2f(100+0.5, 100+0.5)
glVertex2f(150+0.5, 100+0.5)
glVertex2f(150+0.5, 150+0.5)
glEnd()
glFlush()
#取图形中的一点,进行填充
glColor3ubv(a)
boundaryFill4(120, 106, a, b)
glFlush()
看例子:
# -*- coding:utf-8 -*-
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
#判断c1,c2的颜色是否相同
def rgbColorEqual(c1, c2):
return c1[0] == c2[0]
#绘制像素点(x,y)
def setPixel(x, y):
glBegin(GL_POINTS)
glVertex2f(x+0.5, y+0.5)
glEnd()
glFlush()
return
#获得点(x,y)的颜色
def getColor(x, y):
color = (GLuint * 1)(0)
#读取点(x,y)的颜色color
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, color)
color=int(color[0])
r = color & 255
g = (color >> 8) & 255
b = (color >> 16) & 255
color = (r, g, b)
return color
#4-连通边界填充算法
def boundaryFill4(x, y, fillColor, borderColor):
interiorColor=getColor(x,y)
if not rgbColorEqual(interiorColor, borderColor) and not rgbColorEqual(interiorColor, fillColor):
setPixel(x, y)
boundaryFill4(x+1,y,fillColor,borderColor)
boundaryFill4(x-1,y,fillColor,borderColor)
boundaryFill4(x,y+1,fillColor,borderColor)
boundaryFill4(x,y-1,fillColor,borderColor)
glFlush()
#显示函数
def myDisplay():
glClear(GL_COLOR_BUFFER_BIT)
#设置填充色为a红色
#设置边界色为b蓝色
a = (255, 0, 0)
b = (0, 0, 255)
glColor3ubv(b)
#绘制三角形的边
glBegin(GL_LINE_LOOP)
glVertex2f(100+0.5, 100+0.5)
glVertex2f(150+0.5, 100+0.5)
glVertex2f(150+0.5, 150+0.5)
glEnd()
glFlush()
#取图形中的一点,进行填充
glColor3ubv(a)
boundaryFill4(120, 106, a, b)
glFlush()
if __name__ == "__main__":
glutInit()
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(100, 100)
glutInitWindowSize(500, 500)
glutCreateWindow('midpointcircle')
glClearColor(0.5, 0.5, 0.5, 0)
glMatrixMode(GL_PROJECTION)
gluOrtho2D(0.0, 500.0, 0.0, 500.0)
glutDisplayFunc(myDisplay)
glutMainLoop()