你为什么能够透过PyOpenGL中的物体看到它们?

huangapple go评论119阅读模式
英文:

Why can I see through my objects in PyOpenGL?

问题

你的代码中的问题可能与深度测试相关。要解决透视问题,你可以尝试在初始化OpenGL时启用深度测试。在main() 函数的初始化部分添加以下代码:

  1. glEnable(GL_DEPTH_TEST)

这将启用深度测试,确保对象在渲染时按深度顺序绘制,从而避免透视问题。

另外,确保你的物体的顶点数据是按照正确的顺序绘制的,以确保深度测试有效。如果仍然存在问题,你可能需要检查物体的法线方向以及光照设置,以确保它们正确渲染。

最后,如果问题仍然存在,你可能需要检查OpenGL状态和渲染引擎的其他设置,以确保没有其他因素导致透视问题。

请注意,透视问题通常是与OpenGL的设置和渲染顺序有关,而不仅仅是Blender模型的问题。如果你按照正确的步骤配置OpenGL并检查了上述建议,应该能够解决透视问题。

英文:

I have some PyOpenGL code:

  1. import pygame
  2. from pygame.locals import *
  3. from OpenGL.GL import *
  4. from OpenGL.GLU import *
  5. from larry import Larry
  6. from warpedcube import WarpedCube
  7. import renderengine
  8. from cube import Cube
  9. from spear import Spear
  10. # Define camera variables
  11. camera_x = 0.0
  12. camera_y = 0.0
  13. camera_z = -5.0
  14. camera_rotation = 0.0
  15. def resize_viewport(width, height):
  16. glViewport(0, 0, width, height)
  17. glMatrixMode(GL_PROJECTION)
  18. glLoadIdentity()
  19. gluPerspective(45, (width / height), 0.1, 50.0)
  20. glMatrixMode(GL_MODELVIEW)
  21. glLoadIdentity()
  22. def handle_camera_movement():
  23. global camera_x, camera_y, camera_z, camera_rotation
  24. # Get the state of the keyboard
  25. keys = pygame.key.get_pressed()
  26. # Camera movement speed
  27. camera_speed = 0.1
  28. # Move the camera based on the arrow key inputs
  29. if keys[K_w]:
  30. camera_z += camera_speed
  31. if keys[K_d]:
  32. camera_z -= camera_speed
  33. if keys[K_a]:
  34. camera_x -= camera_speed
  35. if keys[K_d]:
  36. camera_x += camera_speed
  37. # Get the state of the mouse
  38. mouse_rel = pygame.mouse.get_rel()
  39. mouse_rel_x = mouse_rel[0]
  40. # Rotate the camera based on the mouse movement
  41. camera_rotation += mouse_rel_x * 0.1
  42. def main():
  43. pygame.init()
  44. display = (800, 600)
  45. pygame.display.set_mode(display, DOUBLEBUF | OPENGL | RESIZABLE)
  46. resize_viewport(display[0], display[1])
  47. meshes = []
  48. render_engine = renderengine.RenderEngine(meshes)
  49. render_engine.addMesh(Larry, (0, 0, 0), (0, 0, 0, 0))
  50. render_engine.addMesh(Spear, (2, 0, 0), (0, 0, 0, 0))
  51. clock = pygame.time.Clock()
  52. while True:
  53. for event in pygame.event.get():
  54. if event.type == pygame.QUIT:
  55. pygame.quit()
  56. quit()
  57. elif event.type == pygame.VIDEORESIZE:
  58. display = event.size
  59. resize_viewport(display[0], display[1])
  60. handle_camera_movement()
  61. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  62. # Apply camera transformations
  63. glLoadIdentity()
  64. gluLookAt(camera_x, camera_y, camera_z, 0, 0, 0, 0, 1, 0)
  65. glRotatef(camera_rotation, 0, 1, 0)
  66. render_engine.renderMeshes()
  67. pygame.display.flip()
  68. clock.tick(60)
  69. # Run the game!
  70. main()
  1. from OpenGL.GL import *
  2. from OpenGL.GLU import *
  3. class Mesh:
  4. def __init__(self, pos, rotation):
  5. self.vertices = ()
  6. self.edges = ()
  7. self.pos = pos
  8. self.rotation = rotation
  9. self.colors = ()
  10. self.surfaces = ()
  11. def render(self):
  12. glTranslatef(self.pos[0], self.pos[1], self.pos[2])
  13. glRotatef(self.rotation[0], self.rotation[1], self.rotation[2], self.rotation[3])
  14. glBegin(GL_QUADS)
  15. for surface in self.surfaces:
  16. x = 0
  17. for vertex in surface:
  18. x = x +1
  19. glColor3fv(self.colors[x])
  20. glVertex3fv(self.vertices[vertex])
  21. glEnd()
  22. glBegin(GL_LINES)
  23. for edge in self.edges:
  24. for vertex in edge:
  25. glVertex3fv(self.vertices[vertex])
  26. glEnd()
  1. #This code takes an OBJ file and turns it into a format I can use for my game
  2. def convert_obj_to_points_surfaces_and_edges(obj_file):
  3. points = []
  4. surfaces = []
  5. edges = set()
  6. with open(obj_file, 'r') as file:
  7. for line in file:
  8. if line.startswith('v '):
  9. vertex = line.strip().split()[1:]
  10. vertex = tuple(map(float, vertex))
  11. points.append(vertex)
  12. elif line.startswith('f '):
  13. surface = line.strip().split()[1:]
  14. surface = [int(vertex.split('/')[0]) - 1 for vertex in surface]
  15. # Check the winding direction of the surface
  16. if not is_surface_clockwise(surface, points):
  17. surface = list(reversed(surface))
  18. surfaces.append(tuple(surface))
  19. # Generate edges from the surface
  20. for i in range(len(surface)):
  21. edge = (surface[i], surface[(i + 1) % len(surface)])
  22. edges.add(tuple(sorted(edge)))
  23. return points, surfaces, list(edges)
  24. def is_surface_clockwise(surface, points):
  25. # Calculate the signed area of the surface
  26. area = 0.0
  27. for i in range(len(surface)):
  28. v1 = points[surface[i]]
  29. v2 = points[surface[(i + 1) % len(surface)]]
  30. area += (v2[0] - v1[0]) * (v2[1] + v1[1])
  31. # Check the sign of the area to determine the winding direction
  32. return area >= 0.0
  33. # Usage example
  34. obj_file = input("OBJ File: ")
  35. points, surfaces, edges = convert_obj_to_points_surfaces_and_edges(obj_file)
  36. print("Points:")
  37. print(points)
  38. print("Surfaces:")
  39. print(surfaces)
  40. print("Edges:")
  41. print(edges)
  1. from mesh import Mesh
  2. class Larry(Mesh):
  3. def __init__(self, pos, rotation):
  4. super().__init__(pos, rotation)
  5. self.vertices = ((-0.231012, 4.429499, -0.86453), (0.235075, -0.904579, -1.261621), (-0.341033, 4.744262, -0.056556), (0.304412, 3.946776, -1.238979), (0.774998, -1.199254, -0.893162), (1.203292, 3.85893, -0.539944), (1.28265, -0.996872, -0.16664), (1.088736, 4.024172, 0.589252), (0.55723, 4.660621, -0.288246), (0.380585, -1.749349, 0.002136), (0.844691, -0.914622, 1.019827), (0.387678, 3.790498, 1.242065), (-0.309286, -0.875632, 1.264355), (-0.176239, -1.54508, 0.788108), (-0.055953, 4.547951, 0.827892), (-0.603082, 3.932153, 1.126323), (-1.099868, -0.882433, 0.700272), (-1.242486, 4.004383, 0.195455), (-0.384412, -1.740398, 0.207738), (-1.231312, -1.005803, -0.255771), (-0.902459, 4.000164, -0.883229), (-0.728425, -0.728341, -1.126776), (-0.347402, -1.506015, -0.781873), (1.025894, 3.772904, -0.968067), (1.039972, 3.418759, -1.110369), (0.795936, 3.986678, -1.076115), (0.715457, 3.221713, -1.161545), (0.823535, 3.542591, -0.561156), (0.715258, 3.287505, -0.639371), (0.467648, 3.893744, -0.683583), (0.224674, 3.600287, -0.882727), (0.508783, 3.76569, -1.344666), (0.370808, 3.279565, -1.075454), (-0.691137, 3.376753, -1.341618), (-0.591995, 3.225636, -1.151239), (-0.614602, 4.007747, -1.000307), (-0.240785, 3.643576, -1.200423), (-0.206438, 3.793073, -0.913614), (-0.332865, 3.248648, -0.910799), (-0.45043, 3.464775, -0.580573), (-0.551495, 3.911132, -0.665671), (-0.8517, 3.29021, -0.779843), (-1.021075, 3.676092, -0.931031), (-0.712928, 3.818214, -1.301324), (-0.60438, 3.606557, -1.429486), (-0.445738, 3.669789, -1.270528), (-0.5085, 3.529455, -1.045165), (-0.555707, 3.828574, -1.158618), (-0.798977, 3.643018, -1.008046), (-0.856286, 3.681354, -1.283398), (-0.644632, 3.415343, -1.222406), (0.592447, 3.436328, -1.183199), (0.718815, 3.60458, -1.445182), (0.682996, 3.833877, -1.198205), (0.866836, 3.474671, -1.095717), (0.848225, 3.810788, -1.200828), (0.659705, 3.664495, -0.993135), (0.480128, 3.65747, -1.225691))
  6. self.edges = ((22, 1, 4), (5, 6, 4), (8, 5, 3), (4, 6, 9), (6, 10, 7), (13, 10, 9), (9, 18, 13), (10, 12, 11), (12, 16, 15), (17, 14, 15), (13, 18, 16), (16, 19, 17), (2, 20, 17), (20, 21, 19), (19, 21, 22), (18, 22, 9), (22, 21, 1), (3, 1, 21), (0, 8, 3), (0, 2, 8), (9, 22, 4), (5, 8, 7), (6, 7, 5), (10, 6, 9), (7, 14, 8), (11, 14, 7), (10, 11, 7), (14, 2, 8), (13, 12, 10), (11, 15, 14), (15, 11, 12), (13, 16, 12), (16, 17, 15), (17, 2, 14), (18, 19, 16), (17, 20, 19), (18, 19, 22), (2, 0, 20), (20, 0, 3), (20, 3, 21), (4, 1, 3, 5), (31, 25, 24), (28, 30, 29), (32, 28, 26), (31, 26, 32), (43, 35, 36), (36, 35, 37), (38, 39, 37), (41, 38, 34), (38, 41, 39), (41, 40, 39), (40, 42, 35), (35, 43, 42), (42, 43, 33), (41, 33, 34), (29, 25, 23, 27), (26, 31, 24), (25, 23, 24), (26, 28, 24), (27, 23, 24), (28, 27, 24), (28, 29, 27), (32, 30, 28), (30, 29, 31), (32, 30, 31), (29, 25, 31), (33, 43, 36), (33, 36, 38), (34, 33, 38), (37, 40, 35), (38, 37, 36), (40, 37, 39), (42, 40, 41), (42, 33, 41), (44, 47, 45), (44, 45, 46), (44, 46, 50), (48, 49, 47), (46, 47, 45), (48, 47, 46), (50, 48, 46), (50, 49, 48), (49, 44, 50), (49, 47, 44), (53, 55, 52), (51, 52, 54), (52, 55, 54), (56, 53, 55), (57, 53, 56), (56, 55, 54), (51, 56, 54), (51, 57, 56), (57, 53, 52), (51, 57, 52))
  7. self.colors = (
  8. (0, 0.1, 0),
  9. (0, 0.2, 0),
  10. (0, 0.3, 0),
  11. (0, 0.4, 0),
  12. (0, 0.5, 0),
  13. (0, 0.6, 0),
  14. (0, 0.7, 0),
  15. (0, 0.8, 0),
  16. (0, 0.9, 0),
  17. (0, 0.99, 0),
  18. (0, 0.999, 0),
  19. (0, 1, 0)
  20. )
  21. self.surfaces = ((22, 1, 4), (5, 6, 4), (8, 5, 3), (4, 6, 9), (6, 10, 7), (13, 10, 9), (9, 18, 13), (10, 12, 11), (12, 16, 15), (17, 14, 15), (13, 18, 16), (16, 19, 17), (2, 20, 17), (20, 21, 19), (19, 21, 22), (18, 22, 9), (22, 21, 1), (3, 1, 21), (0, 8, 3), (0, 2, 8), (9, 22, 4), (5, 8, 7), (6, 7, 5), (10, 6, 9), (7, 14, 8), (11, 14, 7), (10, 11, 7), (14, 2, 8), (13, 12, 10), (11, 15, 14), (15, 11, 12), (13, 16, 12), (16, 17, 15), (17, 2, 14), (18, 19, 16), (17, 20, 19), (18, 19, 22), (2, 0, 20), (20, 0, 3), (20, 3, 21), (4, 1, 3, 5), (31, 25, 24), (28, 30, 29), (32, 28, 26), (31, 26, 32), (43, 35, 36), (36, 35, 37), (38, 39, 37), (41, 38, 34), (38, 41, 39), (41, 40, 39), (40, 42, 35), (35, 43, 42), (42, 43, 33), (41, 33, 34), (29, 25, 23, 27), (26, 31, 24), (25, 23, 24), (26, 28, 24), (27, 23, 24), (28, 27, 24), (28, 29, 27), (32, 30, 28), (30, 29, 31), (32, 30, 31), (29, 25, 31), (33, 43, 36), (33, 36, 38), (34, 33, 38), (37, 40, 35), (38, 37, 36), (40, 37, 39), (42, 40, 41), (42, 33, 41), (44, 47, 45), (44, 45, 46), (44, 46, 50), (48, 49, 47), (46, 47, 45), (48, 47, 46), (50, 48, 46), (50, 49, 48), (49, 44, 50), (49, 47, 44), (53, 55, 52), (51, 52, 54), (52, 55, 54), (56, 53, 55), (57, 53, 56), (56, 55, 54), (51, 56, 54), (51, 57, 56), (57, 53, 52), (51, 57, 52))
  1. from mesh import Mesh
  2. class Spear (Mesh):
  3. def __init__(self, pos, rotation):
  4. super().__init__(pos, rotation)
  5. self.vertices = ((-0.077482, -0.077482, 0.650129), (-0.077482, 0.077482, 0.650129), (-0.077482, -0.077482, -0.69948), (-0.077482, 0.077482, -0.69948), (0.077482, -0.077482, 0.650129), (0.077482, 0.077482, 0.650129), (0.077482, -0.077482, -0.69948), (0.077482, 0.077482, -0.69948), (-0.077482, 0.0, -0.69948), (0.0, -0.077482, -0.69948), (0.0, 0.077482, -0.69948), (0.077482, 0.0, -0.69948), (0.0, 0.0, -0.893731))
  6. '''self.colors = (
  7. (0.1, 0.1, 0.1),
  8. (0.2, 0.2, 0.2),
  9. (0.3, 0.3, 0.3),
  10. (0.4, 0.4, 0.4),
  11. (0.5, 0.5, 0.5),
  12. (0.6, 0.6, 0.6),
  13. (0.7, 0.7, 0.7),
  14. (0.8, 0.8, 0.8),
  15. (0.9, 0.9, 0.9),
  16. (0.99, 0.99, 0.99),
  17. (0.999, 0.999, 0.999),
  18. (1, 1, 1)
  19. )'''
  20. self.colors = (
  21. (1, 1, 1),
  22. (1, 1, 1),
  23. (1, 1, 1),
  24. (1, 1, 1),
  25. (1, 1, 1),
  26. (1, 1, 1),
  27. (1, 1, 1),
  28. (1, 1, 1),
  29. (1, 1, 1),
  30. (1, 1, 1),
  31. (1, 1, 1),
  32. (1, 1, 1)
  33. )
  34. self.edges = ((4, 6), (3, 10), (5, 7), (0, 2), (8, 12), (10, 12), (1, 3), (2, 8), (6, 11), (7, 10), (4, 5), (0, 1), (0, 4), (1, 5), (3, 8), (9, 12), (11, 12), (2, 9), (7, 11), (6, 9))
  35. self.surfaces = ((0, 1, 3, 8, 2), (12, 10, 7, 11), (6, 11, 7, 5, 4), (0, 1, 5, 4), (2, 9, 6, 4, 0), (7, 10, 3, 1, 5), (9, 12, 11, 6), (2, 8, 12, 9), (8, 3, 10, 12))
  1. class RenderEngine:
  2. def __init__(self, meshes):
  3. self.meshes = meshes
  4. def addMesh(self, mesh, pos, rotation):
  5. self.meshes.append(mesh(pos, rotation))
  6. def removeMesh(self, meshIndex):
  7. self.meshes.remove(meshIndex)
  8. def renderMeshes(self):
  9. for mesh in self.meshes:
  10. mesh.render()

When I run my code, I can see through my objects, as if they are transparent. How do I prevent this from happening? I also have made all the objects in Blender, and followed this tutorial:

https://pythonprogramming.net/opengl-rotating-cube-example-pyopengl-tutorial/

Also, is this a problem with my code, or do I need to add more code?

答案1

得分: 0

你需要使用以下代码启用深度测试

  1. glEnable(GL_DEPTH_TEST)
英文:

You have to enable the Depth Test with

  1. glEnable(GL_DEPTH_TEST)

huangapple
  • 本文由 发表于 2023年6月30日 00:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定