mcfletch / pyopengl

Repository for the PyOpenGL Project
Other
314 stars 97 forks source link

Getting Aborted (core dumped) #81

Closed NaNraptor closed 2 years ago

NaNraptor commented 2 years ago

Specs: intel i9-12900k (with igpu) running mesa igpu drivers Nvidia RTX 3080-ti with Nvidia 510 proprietary drivers Linux 21.10 with the 16.14 kernel Using GLFW for python for a window and context provider

Observed behaviour: While running my code with the intel igpu I get a red screen in the window and nothing else While running my code with the nvidia gpu I get a black screen in the window that has red lines and dots briefly flickering before getting aborted (core dumped)

Expected behaviour: A window that does not crash and has red background and a rectangle drawn in the middle of the screen

Note: to run the program using my nvidia gpu I use the following command: _NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia py main.py

Any idea what might be going wrong?

Here is code, minified as much as possible:

import glfw
from OpenGL.GL import *
import numpy as np

glfw.init()

glfw.window_hint(glfw.FOCUSED, glfw.FALSE)
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_COMPAT_PROFILE)
# glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, glfw.TRUE)

window = glfw.create_window(640, 400, "Window", None, None)
glfw.make_context_current(window)

vertices = [
    0.5,  0.5, 0.0,
    0.5, -0.5, 0.0,
    -0.5, -0.5, 0.0,
    -0.5,  0.5, 0.0
]

indices = [
    0, 1, 3,
    1, 2, 3
]

vertices = np.array(vertices, dtype=np.float32)
indices = np.array(indices, dtype=np.int32)

vertex_shader_source = """
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
"""

frag_shader_source = """
#version 330 core
out vec4 FragColor;
void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
"""

vertex_shader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertex_shader, [vertex_shader_source])
glCompileShader(vertex_shader)
print(glGetShaderiv(vertex_shader, GL_COMPILE_STATUS))

frag_shader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(frag_shader, [frag_shader_source])
glCompileShader(frag_shader)
print(glGetShaderiv(frag_shader, GL_COMPILE_STATUS))

shader_program = glCreateProgram()
glAttachShader(shader_program, vertex_shader)
glAttachShader(shader_program, frag_shader)
glLinkProgram(shader_program)
print(glGetProgramiv(shader_program, GL_LINK_STATUS))

glDeleteShader(vertex_shader)
glDeleteShader(frag_shader)

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

glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)

glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)

glClearColor(1, 0, 0, 1)

while not glfw.window_should_close(window):
    glClear(GL_COLOR_BUFFER_BIT)
    glUseProgram(shader_program)
    glBindVertexArray(VAO)
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
    glfw.swap_buffers(window)
    glfw.poll_events()

glfw.terminate()
NaNraptor commented 2 years ago

Apparently it is because you need glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None) instead of glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0). solved and explained here: https://github.com/FlorianRhiem/pyGLFW/issues/62