如何在python opengl中进行块冲突

问题描述

我正在尝试在Python PyOpenGL中进行块冲突,但我不知道如何。我试图做

for blocks in blockPositions:
    #make camera go back up again

在我的循环中,但这会使我的游戏变慢。我在PyOpenGL中也是新手,正在关注一些教程,但对碰撞和东西一无所知。 我在Google上搜索了很多有关在PyOpenGL中发生碰撞的信息,但没有发现任何问题。我只发现不相关的结果,例如使用其他语言和其他模块的内容

我的代码

import glfw
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram,compileShader
import pyrr
from TextureLoader import load_texture
import numpy as np

from camera import Camera

cam = Camera()
WIDTH,HEIGHT = 1280,720
lastX,lastY = WIDTH / 2,HEIGHT / 2
first_mouse = True
left,right,forward,backward = False,False,False

def key_input_clb(window,key,scancode,action,mode):
    global left,backward
    if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
        glfw.set_window_should_close(window,True)

    if key == glfw.KEY_W and action == glfw.PRESS:
        forward = True
    elif key == glfw.KEY_W and action == glfw.RELEASE:
        forward = False
    if key == glfw.KEY_S and action == glfw.PRESS:
        backward = True
    elif key == glfw.KEY_S and action == glfw.RELEASE:
        backward = False
    if key == glfw.KEY_A and action == glfw.PRESS:
        left = True
    elif key == glfw.KEY_A and action == glfw.RELEASE:
        left = False
    if key == glfw.KEY_D and action == glfw.PRESS:
        right = True
    elif key == glfw.KEY_D and action == glfw.RELEASE:
        right = False

def do_movement():
    if left:
        cam.process_keyboard("LEFT",0.05)
    if right:
        cam.process_keyboard("RIGHT",0.05)
    if forward:
        cam.process_keyboard("FORWARD",0.05)
    if backward:
        cam.process_keyboard("BACKWARD",0.05)


def mouse_look_clb(window,xpos,ypos):
    global first_mouse,lastX,lastY

    if first_mouse:
        lastX = xpos
        lastY = ypos
        first_mouse = False

    xoffset = xpos - lastX
    yoffset = lastY - ypos

    lastX = xpos
    lastY = ypos

    cam.process_mouse_movement(xoffset,yoffset)


vertex_src = """
# version 330
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec2 a_texture;
layout(location = 2) in vec3 a_offset;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 move;
out vec2 v_texture;
void main()
{
    vec3 final_pos = a_position + a_offset;
    gl_Position =  projection * view * move * model * vec4(final_pos,1.0f);
    v_texture = a_texture;
}
"""

fragment_src = """
# version 330
in vec2 v_texture;
out vec4 out_color;
uniform sampler2D s_texture;
void main()
{
    out_color = texture(s_texture,v_texture);
}
"""


def window_resize_clb(window,width,height):
    glViewport(0,height)
    projection = pyrr.matrix44.create_perspective_projection_matrix(45,width / height,0.1,100)
    gluniformMatrix4fv(proj_loc,1,GL_FALSE,projection)


if not glfw.init():
    raise Exception("glfw can not be initialized!")

window = glfw.create_window(WIDTH,HEIGHT,"My OpenGL window",None,None)

if not window:
    glfw.terminate()
    raise Exception("glfw window can not be created!")

glfw.set_window_pos(window,400,200)

glfw.set_window_size_callback(window,window_resize_clb)
glfw.set_cursor_pos_callback(window,mouse_look_clb)
glfw.set_key_callback(window,key_input_clb)
glfw.set_input_mode(window,glfw.CURSOR,glfw.CURSOR_disABLED)

glfw.make_context_current(window)


cube_buffer = [-0.5,-0.5,0.5,0.0,1.0,1.0]

cube_buffer = np.array(cube_buffer,dtype=np.float32)

cube_indices = [ 0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,20]

cube_indices = np.array(cube_indices,dtype=np.uint32)

shader = compileProgram(compileShader(vertex_src,GL_VERTEX_SHADER),compileShader(fragment_src,GL_FRAGMENT_SHADER))

VAO = glGenVertexArrays(1)
VBO = glGenBuffers(1)
EBO = glGenBuffers(1)

glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER,VBO)
glBufferData(GL_ARRAY_BUFFER,cube_buffer.nbytes,cube_buffer,GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,cube_indices.nbytes,cube_indices,GL_STATIC_DRAW)

glEnabLevertexAttribArray(0)
glVertexAttribPointer(0,GL_FLOAT,cube_buffer.itemsize * 5,ctypes.c_void_p(0))
glEnabLevertexAttribArray(1)
glVertexAttribPointer(1,ctypes.c_void_p(12))

textures = glGenTextures(1)
load_texture("src/grass.png",textures)

instance_array = []
offset = 1

for z in range(0,100,2):
    for x in range(0,2):
        translation = pyrr.Vector3([0.0,0.0])
        translation.x = x + offset
        translation.y = y + offset
        translation.z = z + offset
        instance_array.append(translation)

len_of_instance_array = len(instance_array) 
instance_array = np.array(instance_array,np.float32).flatten()

instanceVBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,instanceVBO)
glBufferData(GL_ARRAY_BUFFER,instance_array.nbytes,instance_array,GL_STATIC_DRAW)

glEnabLevertexAttribArray(2)
glVertexAttribPointer(2,ctypes.c_void_p(0))
glVertexAttribDivisor(2,1) 

gluseProgram(shader)
glClearColor(0,1)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)

projection = pyrr.matrix44.create_perspective_projection_matrix(45,WIDTH / HEIGHT,100)
cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([-50.0,-50.0,-200.0]))

model_loc = glGetUniformlocation(shader,"model")
proj_loc = glGetUniformlocation(shader,"projection")
view_loc = glGetUniformlocation(shader,"view")
move_loc = glGetUniformlocation(shader,"move")

gluniformMatrix4fv(proj_loc,projection)
gluniformMatrix4fv(model_loc,cube_pos)


while not glfw.window_should_close(window):
    glfw.poll_events()
    do_movement()

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    move = pyrr.matrix44.create_from_translation(pyrr.Vector3([0,glfw.get_time()*8]))
    gluniformMatrix4fv(move_loc,move)

    view = cam.get_view_matrix()
    gluniformMatrix4fv(view_loc,view)

    glDrawElementsInstanced(GL_TRIANGLES,len(cube_indices),GL_UNSIGNED_INT,len_of_instance_array)

    glfw.swap_buffers(window)


glfw.terminate()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)