opengl 如何添加随机颜色?

0pizxfdo  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(257)

现在我已经有了立方体,接下来要做的是为每个立方体添加一个随机颜色,但是我在每个立方体上都做这个颜色时遇到了麻烦。正如您在我的代码输出中看到的,立方体的每个面都有相同的颜色,而边是不同的。我在代码中尝试了不同的方法,但是有时立方体的颜色都是相同的。
我想让这些立方体有不同的颜色。
我所需的输出:

下面是代码:

  1. import pygame
  2. import random
  3. from pygame.locals import *
  4. from OpenGL.GL import *
  5. from OpenGL.GLU import *
  6. verticies = ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
  7. (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5))
  8. edges = ((0,1), (0,3), (0,4), (2,1),(2,3), (2,7), (6,3), (6,4),(6,7), (5,1), (5,4), (5,7))
  9. surfaces = (
  10. (0,1,2,3),
  11. (3,2,7,6),
  12. (6,7,5,4),
  13. (4,5,1,0),
  14. (1,5,7,2),
  15. (4,0,3,6)
  16. )
  17. colors = (
  18. (1,0,0),
  19. (0,1,0),
  20. (0,0,1),
  21. (0,1,0),
  22. (1,1,1),
  23. (0,1,1),
  24. (1,0,0),
  25. (0,1,0),
  26. (0,0,1),
  27. (1,0,0),
  28. (1,1,1),
  29. (0,1,1),
  30. )
  31. def Cube():
  32. glBegin(GL_QUADS)
  33. x = 0
  34. for surface in surfaces:
  35. x+=1
  36. for vertex in surface:
  37. glColor3fv(colors[x])
  38. glVertex3fv(verticies[vertex])
  39. glEnd()
  40. glBegin(GL_LINES)
  41. for edge in edges:
  42. for vertex in edge:
  43. glVertex3fv(verticies[vertex])
  44. glEnd()
  45. def main():
  46. pygame.init()
  47. display = (800, 600)
  48. pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
  49. glMatrixMode(GL_PROJECTION)
  50. gluPerspective(60, (display[0]/display[1]), 0.1, 500)
  51. button_down = False
  52. glEnable(GL_DEPTH_TEST)
  53. glMatrixMode(GL_MODELVIEW)
  54. modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
  55. glTranslatef(0.0,0.0, -5)
  56. while True:
  57. glPushMatrix()
  58. glLoadIdentity()
  59. for event in pygame.event.get():
  60. if event.type == pygame.QUIT:
  61. pygame.quit()
  62. if event.type == pygame.KEYDOWN:
  63. if event.key == pygame.K_LEFT:
  64. glTranslatef(-1, 0, 0)
  65. if event.key == pygame.K_RIGHT:
  66. glTranslatef(1, 0, 0)
  67. if event.key == pygame.K_UP:
  68. glTranslatef(0, 1, 0)
  69. if event.key == pygame.K_DOWN:
  70. glTranslatef(0, -1, 0)
  71. if event.type == pygame.MOUSEMOTION:
  72. if button_down == True:
  73. glRotatef(event.rel[1], 1, 0, 0)
  74. glRotatef(event.rel[0], 0, 1, 0)
  75. for event in pygame.mouse.get_pressed():
  76. if pygame.mouse.get_pressed()[0] == 1:
  77. button_down = True
  78. elif pygame.mouse.get_pressed()[0] == 0:
  79. button_down = False
  80. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  81. glMultMatrixf(modelMatrix)
  82. modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
  83. glLoadIdentity()
  84. glTranslatef(0, 0, -5)
  85. glMultMatrixf(modelMatrix)
  86. for i in range(5):
  87. glPushMatrix()
  88. glTranslate(-2+i, 0, 0)
  89. Cube()
  90. glPopMatrix()
  91. glPopMatrix()
  92. pygame.display.flip()
  93. pygame.time.wait(10)
  94. main()
bbmckpt7

bbmckpt71#

定义6种颜色的数组:

  1. colors = [(1,0,0), (0,1,0), (0,0,1), (1,1,0), (1,0,1), (0,1,1)]

不要为每个顶点设置颜色:

  1. def Cube():
  2. glBegin(GL_QUADS)
  3. x = 0
  4. for surface in surfaces:
  5. x+=1
  6. for vertex in surface:
  7. # glColor3fv(colors[x]) <--- DELTETE
  8. glVertex3fv(verticies[vertex])
  9. glEnd()

但要为每个立方体设置颜色:

  1. for i in range(5):
  2. glPushMatrix()
  3. glTranslate(-2+i, 0, 0)
  4. glColor3fv(colors[i]) # <--- INSERT
  5. Cube()
  6. glPopMatrix()

展开查看全部

相关问题