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

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

Why can I see through my objects in PyOpenGL?

问题

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

glEnable(GL_DEPTH_TEST)

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

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

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

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

英文:

I have some PyOpenGL code:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from larry import Larry
from warpedcube import WarpedCube
import renderengine
from cube import Cube
from spear import Spear

# Define camera variables
camera_x = 0.0
camera_y = 0.0
camera_z = -5.0
camera_rotation = 0.0

def resize_viewport(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, (width / height), 0.1, 50.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

def handle_camera_movement():
    global camera_x, camera_y, camera_z, camera_rotation

    # Get the state of the keyboard
    keys = pygame.key.get_pressed()

    # Camera movement speed
    camera_speed = 0.1

    # Move the camera based on the arrow key inputs
    if keys[K_w]:
        camera_z += camera_speed
    if keys[K_d]:
        camera_z -= camera_speed
    if keys[K_a]:
        camera_x -= camera_speed
    if keys[K_d]:
        camera_x += camera_speed

    # Get the state of the mouse
    mouse_rel = pygame.mouse.get_rel()
    mouse_rel_x = mouse_rel[0]

    # Rotate the camera based on the mouse movement
    camera_rotation += mouse_rel_x * 0.1



def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL | RESIZABLE)
    resize_viewport(display[0], display[1])

    meshes = []
    render_engine = renderengine.RenderEngine(meshes)
    render_engine.addMesh(Larry, (0, 0, 0), (0, 0, 0, 0))
    render_engine.addMesh(Spear, (2, 0, 0), (0, 0, 0, 0))

    clock = pygame.time.Clock()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.VIDEORESIZE:
                display = event.size
                resize_viewport(display[0], display[1])

        handle_camera_movement()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Apply camera transformations
        glLoadIdentity()
        gluLookAt(camera_x, camera_y, camera_z, 0, 0, 0, 0, 1, 0)
        glRotatef(camera_rotation, 0, 1, 0)

        render_engine.renderMeshes()

        pygame.display.flip()
        clock.tick(60)

# Run the game!
main()
from OpenGL.GL import *
from OpenGL.GLU import *

class Mesh:
    def __init__(self, pos, rotation):
        self.vertices = ()
        self.edges = ()
        self.pos = pos
        self.rotation = rotation
        self.colors = ()
        self.surfaces = ()
    
    def render(self):
        glTranslatef(self.pos[0], self.pos[1], self.pos[2])
        glRotatef(self.rotation[0], self.rotation[1], self.rotation[2], self.rotation[3])

        glBegin(GL_QUADS)
        for surface in self.surfaces:
            x = 0
            for vertex in surface:
                x = x +1
                glColor3fv(self.colors[x])
                glVertex3fv(self.vertices[vertex])
        glEnd()

        glBegin(GL_LINES)
        for edge in self.edges:
            for vertex in edge:
                glVertex3fv(self.vertices[vertex])
        glEnd()
#This code takes an OBJ file and turns it into a format I can use for my game
def convert_obj_to_points_surfaces_and_edges(obj_file):
    points = []
    surfaces = []
    edges = set()

    with open(obj_file, 'r') as file:
        for line in file:
            if line.startswith('v '):
                vertex = line.strip().split()[1:]
                vertex = tuple(map(float, vertex))
                points.append(vertex)
            elif line.startswith('f '):
                surface = line.strip().split()[1:]
                surface = [int(vertex.split('/')[0]) - 1 for vertex in surface]

                # Check the winding direction of the surface
                if not is_surface_clockwise(surface, points):
                    surface = list(reversed(surface))

                surfaces.append(tuple(surface))

                # Generate edges from the surface
                for i in range(len(surface)):
                    edge = (surface[i], surface[(i + 1) % len(surface)])
                    edges.add(tuple(sorted(edge)))

    return points, surfaces, list(edges)


def is_surface_clockwise(surface, points):
    # Calculate the signed area of the surface
    area = 0.0
    for i in range(len(surface)):
        v1 = points[surface[i]]
        v2 = points[surface[(i + 1) % len(surface)]]
        area += (v2[0] - v1[0]) * (v2[1] + v1[1])

    # Check the sign of the area to determine the winding direction
    return area >= 0.0

# Usage example
obj_file = input("OBJ File: ")
points, surfaces, edges = convert_obj_to_points_surfaces_and_edges(obj_file)
print("Points:")
print(points)
print("Surfaces:")
print(surfaces)
print("Edges:")
print(edges)
from mesh import Mesh

class Larry(Mesh):
    def __init__(self, pos, rotation):
        super().__init__(pos, rotation)
        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))

        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))

        self.colors = (
            (0, 0.1, 0),
            (0, 0.2, 0),
            (0, 0.3, 0),
            (0, 0.4, 0),
            (0, 0.5, 0),
            (0, 0.6, 0),
            (0, 0.7, 0),
            (0, 0.8, 0),
            (0, 0.9, 0),
            (0, 0.99, 0),
            (0, 0.999, 0),
            (0, 1, 0)
        )
        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))
from mesh import Mesh
class Spear (Mesh):
    def __init__(self, pos, rotation):
        super().__init__(pos, rotation)
        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))
        
        '''self.colors = (
            (0.1, 0.1, 0.1),
            (0.2, 0.2, 0.2),
            (0.3, 0.3, 0.3),
            (0.4, 0.4, 0.4),
            (0.5, 0.5, 0.5),
            (0.6, 0.6, 0.6),
            (0.7, 0.7, 0.7),
            (0.8, 0.8, 0.8),
            (0.9, 0.9, 0.9),
            (0.99, 0.99, 0.99),
            (0.999, 0.999, 0.999),
            (1, 1, 1)
        )'''
        self.colors = (
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1),
            (1, 1, 1)
        )
        
        
        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))
        
        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))
class RenderEngine:
    def __init__(self, meshes):
        self.meshes = meshes
    
    def addMesh(self, mesh, pos, rotation):
        self.meshes.append(mesh(pos, rotation))

    def removeMesh(self, meshIndex):
        self.meshes.remove(meshIndex)
    
    def renderMeshes(self):
        for mesh in self.meshes:
            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

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

glEnable(GL_DEPTH_TEST)
英文:

You have to enable the Depth Test with

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:

确定