Python OpenGL将不加载纹理

问题描述

我正在遵循一个教程系列,我们在Python OpenGL中制作一个带有纹理的多维数据集。由于某种原因,当我运行代码时,它会产生一个最奇怪的黑屏。请帮助我,我很失落,还有最后期限。

在上一个版本中,我不得不添加一些窗口提示一个顶点数组对象,由于某些愚蠢的原因,本教程未包含该对象。我有什么需要更改/添加内容,因为我真的被卡住了。

@H_404_5@import glfw
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram,compileShader
import numpy as np
import pyrr
from PIL import Image


vertex_src = """

# version 330

layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_color;
layout(location = 2) in vec2 a_texture;

uniform mat4 rotation;

out vec3 v_color;
out vec2 v_texture;

void main()
{
    gl_Position = rotation * vec4(a_position,1.0);
    v_color = a_color;
    v_texture = a_texture;

    //v_texture = 1 - a_texture;                        //Flips the texture vertically and horizontally
    //v_texture = vec2(a_texture.s,1 - a_texture.t);   //Flips the texture vertically
}

"""


fragment_src = """

#version 330

in vec3 v_color;
in vec2 v_texture;

out vec4 out_color;

uniform sampler2D s_texture;

void main()
{
    out_color = texture(s_texture,v_texture);  // * vec4(v_color,1.0f);
}

"""



#glfw callback functions
def window_resize(window,width,height):
    glViewport(0,height)


#initialising glfw library
if not glfw.init():
    raise Exception("glfw cannot be initialised")



glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR,3)
glfw.window_hint(glfw.CONTEXT_VERSION_MInor,2)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT,GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE,glfw.OPENGL_CORE_PROFILE)


#creating the window
window = glfw.create_window(1280,720,"My OpenGL Window",None,None)


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


glfw.set_window_pos(window,400,200)
glfw.set_window_size_callback(window,window_resize)
glfw.make_context_current(window)


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


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


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

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


VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,VBO)
glBufferData(GL_ARRAY_BUFFER,vertices.nbytes,vertices,GL_STATIC_DRAW)

#Vertex array object
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)

glEnabLevertexAttribArray(0)
glVertexAttribPointer(0,GL_FLOAT,GL_FALSE,vertices.itemsize * 8,ctypes.c_void_p(0))

glEnabLevertexAttribArray(1)
glVertexAttribPointer(1,ctypes.c_void_p(12))

glEnabLevertexAttribArray(2)
glVertexAttribPointer(2,ctypes.c_void_p(24))

texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D,texture)



glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT)

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)


image = Image.open("my_pic.png")
image = image.transpose(Image.FLIP_TOP_BottOM)

img_data = image.convert("RGBA").tobytes()
glTexImage2D(GL_TEXTURE_2D,GL_RGBA,image.width,image.height,GL_UNSIGNED_BYTE,img_data)

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


rotation_loc = glGetUniformlocation(shader,"rotation")


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

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time())
    rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time())

    gluniformMatrix4fv(rotation_loc,pyrr.matrix44.multiply(rot_x,rot_y))

    glDrawElements(GL_TRIANGLES,len(indices),GL_UNSIGNED_INT,None)

    glfw.swap_buffers(window)


glfw.terminate()

解决方法

glDrawElements呈现由索引缓冲区中的索引指定的基元。您错过了Index buffersGL_ELEMENT_ARRAY_BUFFER)。由于索引缓冲区是在顶点数组对象中声明的,因此必须先创建并绑定VAO,然后才能创建和绑定:

AO = glGenVertexArrays(1)
glBindVertexArray(VAO)

IBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,IBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,indices.nbytes,indices,GL_STATIC_DRAW)