Saturday, December 3, 2022
HomeGame Developmentpython - Why does this voxel engine crash after a couple of...

python – Why does this voxel engine crash after a couple of seconds?


I am attempting this voxel engine in Python. It runs okay, however it crashes after a couple of seconds. Does anybody know what is occurring?

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

from digital camera import Digital camera

cam = Digital camera()
WIDTH, HEIGHT = 1280, 720
lastX, lastY = WIDTH / 2, HEIGHT / 2
first_mouse = True
left, proper, ahead, backward = False, False, False, False


# the keyboard enter callback
def key_input_clb(window, key, scancode, motion, mode):
    world left, proper, ahead, backward
    if key == glfw.KEY_ESCAPE and motion == glfw.PRESS:
        glfw.set_window_should_close(window, True)

    if key == glfw.KEY_W and motion == glfw.PRESS:
        ahead = True
    elif key == glfw.KEY_W and motion == glfw.RELEASE:
        ahead = False
    if key == glfw.KEY_S and motion == glfw.PRESS:
        backward = True
    elif key == glfw.KEY_S and motion == glfw.RELEASE:
        backward = False
    if key == glfw.KEY_A and motion == glfw.PRESS:
        left = True
    elif key == glfw.KEY_A and motion == glfw.RELEASE:
        left = False
    if key == glfw.KEY_D and motion == glfw.PRESS:
        proper = True
    elif key == glfw.KEY_D and motion == glfw.RELEASE:
        proper = False


# do the motion, name this operate in the primary loop
def do_movement():
    if left:
        cam.process_keyboard("LEFT", 0.05)
    if proper:
        cam.process_keyboard("RIGHT", 0.05)
    if ahead:
        cam.process_keyboard("FORWARD", 0.05)
    if backward:
        cam.process_keyboard("BACKWARD", 0.05)


# the mouse place callback operate
def mouse_look_clb(window, xpos, ypos):
    world 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 = """
# model 330
format(location = 0) in vec3 a_position;
format(location = 1) in vec2 a_texture;
format(location = 2) in vec3 a_offset;
format(location = 3) in vec3 a_offset_texture;//To ensure that each dice to have it is personal texture
uniform mat4 projection;
uniform mat4 view;
uniform mat4 transfer;
out vec2 v_texture;
void predominant()
{
    vec3 final_pos=vec3(0.,0.,0.);//= a_position + a_offset;
    final_pos.x = a_position.x + a_offset.x;
    final_pos.y = a_position.y + a_offset.y;
    final_pos.z = a_position.z + a_offset.z;
    gl_Position =  projection * view * transfer * vec4(final_pos,1.);
    v_texture.xy = a_texture.xy + a_offset_texture.xy;
    //v_texture = a_texture + a_offset_texture; code right here
}
"""

fragment_src = """
# model 330
in vec2 v_texture;
out vec4 out_color;
uniform sampler2D s_texture;

float map(float worth, float min1, float max1, float min2, float max2) {
  return min2 + (worth - min1) * (max2 - min2) / (max1 - min1);
}

float roundValues(float fvalue,float ivalue) {
    return 0.f;
}

void predominant()
{
    vec3 sky_col = vec3(172./256.,32./256.,32./256.);
    
    vec4 in_col = texture(s_texture, v_texture);
    
    float coe = 1.-gl_FragCoord.z/(100.*gl_FragCoord.w);
    
    in_col.x = map(coe,0.,1.,sky_col.x,in_col.x);
    in_col.y = map(coe,0.,1.,sky_col.y,in_col.y);
    in_col.z = map(coe,0.,1.,sky_col.z,in_col.z);
    
    out_color = in_col;
}
"""


# the window resize callback operate
def window_resize_clb(window, width, top):
    glViewport(0, 0, width, top)
    projection = pyrr.matrix44.create_perspective_projection_matrix(45, width / top, 0.1, 100)
    glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)


# initializing glfw library
if not glfw.init():
    increase Exception("glfw cannot be initialized!")

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

# verify if window was created
if not window:
    glfw.terminate()
    increase Exception("glfw window cannot be created!")

# set window's place
glfw.set_window_pos(window, 400, 200)

# set the callback operate for window resize
glfw.set_window_size_callback(window, window_resize_clb)
# set the mouse place callback
glfw.set_cursor_pos_callback(window, mouse_look_clb)
# set the keyboard enter callback
glfw.set_key_callback(window, key_input_clb)
# seize the mouse cursor
glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

# make the context present
glfw.make_context_current(window)

multiplier=float(2.)
batch_coe=float(1./multiplier)

#              positions        texture_coords
cube_buffer = [-0.5, -0.5,  0.5, 0.0, 0.0,#where it used to be 1.as image uperbounds, now it hase been replaced with batch_coe
             0.5, -0.5,  0.5, batch_coe, 0.0,
             0.5,  0.5,  0.5, batch_coe, batch_coe,
            -0.5,  0.5,  0.5, 0.0, batch_coe,

            -0.5, -0.5, -0.5, 0.0, 0.0,
             0.5, -0.5, -0.5, batch_coe, 0.0,
             0.5,  0.5, -0.5, batch_coe, batch_coe,
            -0.5,  0.5, -0.5, 0.0, batch_coe,

             0.5, -0.5, -0.5, 0.0, 0.0,
             0.5,  0.5, -0.5, batch_coe, 0.0,
             0.5,  0.5,  0.5, batch_coe, batch_coe,
             0.5, -0.5,  0.5, 0.0, batch_coe,

            -0.5,  0.5, -0.5, 0.0, 0.0,
            -0.5, -0.5, -0.5, batch_coe, 0.0,
            -0.5, -0.5,  0.5, batch_coe, batch_coe,
            -0.5,  0.5,  0.5, 0.0, batch_coe,

            -0.5, -0.5, -0.5, 0.0, 0.0,
             0.5, -0.5, -0.5, batch_coe, 0.0,
             0.5, -0.5,  0.5, batch_coe, batch_coe,
            -0.5, -0.5,  0.5, 0.0, batch_coe,

             0.5,  0.5, -0.5, 0.0, 0.0,
            -0.5,  0.5, -0.5, batch_coe, 0.0,
            -0.5,  0.5,  0.5, batch_coe, batch_coe,
             0.5,  0.5,  0.5, 0.0, batch_coe]

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

cube_indices = [ 0,  1,  2,  2,  3,  0,
                 6,  5,  4,  4,  7,  6,#it needs fixing in indexing and invericies
                 8,  9, 10, 10, 11,  8,
                12, 13, 14, 14, 15, 12,
                16, 17, 18, 18, 19, 16,
                20, 21, 22, 22, 23, 20]
                #4,  5,  6,  6,  7,  4

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

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

# VAO, VBO and EBO
VAO = glGenVertexArrays(1)
VBO = glGenBuffers(1)
EBO = glGenBuffers(1)

# dice VAO
glBindVertexArray(VAO)
# dice Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, cube_buffer.nbytes, cube_buffer, GL_STATIC_DRAW)
# dice Component Buffer Object
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, cube_indices.nbytes, cube_indices, GL_STATIC_DRAW)

# dice vertices
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(0))
# dice textures
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, cube_buffer.itemsize * 5, ctypes.c_void_p(12))

textures = glGenTextures(1)
load_texture("basilikh_kakava.jpg", textures)

#### occasion VBO
instance_array = []
offset = 1

for z in vary(0, 50, 2):
    for y in vary(0, 50, 2):
        for x in vary(0, 50, 2):
            translation = pyrr.Vector3([0.0, 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) # do that earlier than you flatten the array
instance_array = np.array(instance_array, np.float32).flatten()
####
#### occasion VBO texture
instance_array_text = []

for z in vary(0, 50, 2):
    for y in vary(0, 50, 2):
        for x in vary(0, 50, 2):
            translation = pyrr.Vector3([0.0, 0.0,0.0])
            instance_array_text.append(translation)

len_of_instance_array_text = len(instance_array_text) # do that earlier than you flatten the array
instance_array_text = np.array(instance_array, np.float32).flatten()
####

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

glEnableVertexAttribArray(2)
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glVertexAttribDivisor(2, 1) # 1 means, each occasion could have it is personal translate

####new code for textures

#instanceVBO_text = glGenBuffers(1)
#glBindBuffer(GL_ARRAY_BUFFER, instanceVBO_text)
#glBufferData(GL_ARRAY_BUFFER, instance_array_text.nbytes, None, GL_DYNAMIC_DRAW)

#glEnableVertexAttribArray(3)
#glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
#glVertexAttribDivisor(3, 1) # 1 means, each occasion could have it is personal texture

####

glUseProgram(shader)
glClearColor(172./256.,32./256.,32./256.,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, 0.1, 100)
cube_pos = pyrr.matrix44.create_from_translation(pyrr.Vector3([0., 0., 0.]))

#model_loc = glGetUniformLocation(shader, "mannequin")
proj_loc = glGetUniformLocation(shader, "projection")
view_loc = glGetUniformLocation(shader, "view")
move_loc = glGetUniformLocation(shader, "transfer")

glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
#glUniformMatrix4fv(model_loc, 1, GL_FALSE, cube_pos)

glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glFrontFace(GL_CW);

# the primary software loop
whereas not glfw.window_should_close(window):
    
    #index=int(rnd()*(len_of_instance_array_text-1)*3)
    
    #instance_array[index]=rnd()*100
    
    #instance_array_text[index]=batch_coe*int(rnd()*multiplier)
    print("0")
    #glBindBuffer(GL_ARRAY_BUFFER, instanceVBO)
    #glBufferSubData(GL_ARRAY_BUFFER,0, instance_array.nbytes, instance_array)
    
    print("1")
    #glBindBuffer(GL_ARRAY_BUFFER, instanceVBO_text)
    #glBufferSubData(GL_ARRAY_BUFFER,0, instance_array_text.nbytes, instance_array_text)
    print("2")
    glfw.poll_events()
    do_movement()
    print("3")
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    print("4")
    transfer = pyrr.matrix44.create_from_translation(pyrr.Vector3([0., 0., 0.]))
    print("5")
    glUniformMatrix4fv(move_loc, 1, GL_FALSE, transfer)
    print("6")
    view = cam.get_view_matrix()
    glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
    print("7")
    glDrawElementsInstanced(GL_TRIANGLES, len(cube_indices), GL_UNSIGNED_INT, None, len_of_instance_array)
    print("8")
    glfw.swap_buffers(window)
    print("9")
# terminate glfw, liberate allotted sources
glfw.terminate()

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments