我无法正常点亮模型。我正在制作一个由小的立方体/体素组成的对象,并且所有面都具有相同的光强度。我正在用Python编写代码,并将numpy用于数组。我的点数组是三角形角点的平面一维数组。我的法线阵列是每个三角形的平面3D向量的1D平面。
以下代码在初始化过程中:
# ===============================================
# First add points to buffer
# ===============================================
points = numpy.array(points,dtype=numpy.float32)
points=points * scale
self.voxelPtBufferLen = len(points) // 3
glEnableClientState(GL_VERTEX_ARRAY)
self.voxelPtBufferIdx = glGenBuffers(1)
dataSizePt = arrays.ArrayDatatype.arrayByteCount(points)
# print ("datasize",dataSize)
glBindBuffer(GL_ARRAY_BUFFER, self.voxelPtBufferIdx)
glBufferData(GL_ARRAY_BUFFER, dataSizePt, points, GL_STATIC_DRAW)
glDisableClientState(GL_VERTEX_ARRAY)
# ===============================================
# Second add normals for each triangle to buffer
# ===============================================
normals = numpy.array(normals, dtype=numpy.float32)
self.voxelNmBufferLen = len(normals)
glEnableClientState(GL_NORMAL_ARRAY)
self.voxelNmBufferIdx = glGenBuffers(1)
dataSizeNm = arrays.ArrayDatatype.arrayByteCount(normals)
glBindBuffer(GL_ARRAY_BUFFER, self.voxelNmBufferIdx)
glBufferData(GL_ARRAY_BUFFER, dataSizeNm, normals, GL_STATIC_DRAW)
glDisableClientState(GL_NORMAL_ARRAY)
然后绘制这个,我有以下代码
colorFront = (0.1, 0.3, 0.6)
colorBack = (1, 0.3, 0.3)
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (0.2, 0.2, 0.2))
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (0.8, 0.8, 1.0))
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, 50.0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_NORMALIZE)
glLight(GL_LIGHT1, GL_AMBIENT, (0.3, 0.0, 0.0, 1))
glLight(GL_LIGHT1, GL_DIFFUSE, (0.6, 0.0, 0.0, 1))
glLight(GL_LIGHT1, GL_SPECULAR, (0.9, 0.0, 0.0, 1))
glLight(GL_LIGHT1, GL_POSITION, (10,10,10))
glLight(GL_LIGHT1, GL_SPOT_DIRECTION, (-10,-10,-10))
glEnable(GL_LIGHT1)
glBegin(GL_QUADS)
glColor3f(1.0,1.0,0)
glVertex3f(10, 10, 10)
glVertex3f(12, 12, 12)
glVertex3f(10, 12, 12)
glVertex3f(12, 10, 12)
glEnd()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glColor3fv(colorFront)
# DRAW VOXELS
glEnableClientState(GL_VERTEX_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, self.voxelPtBufferIdx)
glVertexPointer(3,GL_FLOAT,0,None)
glEnableClientState(GL_NORMAL_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, self.voxelNmBufferIdx)
glNormalPointer(GL_FLOAT, 0, 0)
glDrawArrays(GL_TRIANGLES, 0, self.voxelPtBufferLen)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
答案 0 :(得分:1)
主要问题是必须启用照明。参见glEnable
:
glEnable(GL_LIGHTING)
启用照明后,网格的颜色由材质参数(glMaterial
)确定。另请参见Basic OpenGL Lighting。
您没有设置环境材质参数(GL_AMBIENT
),并且漫反射材质参数(GL_DIFFUSE
)似乎很弱。尝试类似的东西:
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (0.3, 0.3, 0.3))
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (1.0, 1.0, 1.0))