第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PyQt5 和 OpenGL 使用 VBO 繪制多個對象?

使用 PyQt5 和 OpenGL 使用 VBO 繪制多個對象?

狐的傳說 2023-07-18 17:43:40
我正在嘗試使用 pyqt5 在 pyopengl 中渲染多個對象。在學習完教程后,我創(chuàng)建了一個 3D 網(wǎng)格,它上傳波前 obj 文件并用紋理渲染它。這對我有用:class Model:    def __init__(self, file_name, texture_name):        self.object = ObjectLoader()        self.object.load_model(file_name)        //creating and compiling shaders        glUseProgram(shader)        vertex_buffer_object = GLuint(0)        glGenBuffers(1, vertex_buffer_object)        glBindBuffer(GL_ARRAY_BUFFER,  vertex_buffer_object)        glBufferData(GL_ARRAY_BUFFER, len(self.object.model) * 4, self.object.c_model, GL_DYNAMIC_DRAW)         glDrawArrays(GL_TRIANGLES, 0, len(self.object.vertex_index))        # vertices        vertex_offset = 0        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.object.model.itemsize * 3, ctypes.c_void_p(vertex_offset))        glEnableVertexAttribArray(0)        # textures        texture_offset = len(self.object.vertex_index) * 12        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, self.object.model.itemsize * 2, ctypes.c_void_p(texture_offset))        glEnableVertexAttribArray(1)        # normals        normal_offset = texture_offset + len(self.object.texture_index) * 8        glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.object.model.itemsize * 3, ctypes.c_void_p(normal_offset))        glEnableVertexAttribArray(2)        texture = GLuint(0)        glGenTextures(1, texture)        glBindTexture(GL_TEXTURE_2D, texture)        # texture wrapping        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)        # texture filtering        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)        glEnable(GL_BLEND)        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)我正在努力解決的是在Obj課堂上創(chuàng)造另一個Model。我嘗試創(chuàng)建它并通過另一個調(diào)用initializeGL進行渲染,但沒有發(fā)生任何事情,或者沒有預期的結(jié)果......知道我做錯了什么嗎?paintGLglDrawArrays
查看完整描述

1 回答

?
白衣染霜花

TA貢獻1796條經(jīng)驗 獲得超10個贊

您必須使用頂點數(shù)組對象。頂點數(shù)組對象存儲提供頂點數(shù)據(jù)所需的所有狀態(tài)。

模型的構(gòu)造函數(shù)僅創(chuàng)建所有必需的 OpenGL 對象。著色器程序不是模型的一部分。對所有型號使用相同的程序:

class Model:

? ? def __init__(self, file_name, texture_name):

? ? ? ? self.object = ObjectLoader()

? ? ? ? self.object.load_model(file_name)


? ? ? ? # create vertex array object

? ? ? ? self.vao = glGenVertexArrays(1)

? ? ? ? glBindVertexArray(self.vao)


? ? ? ? vertex_buffer_object = GLuint(0)

? ? ? ? glGenBuffers(1, vertex_buffer_object)

? ? ? ? glBindBuffer(GL_ARRAY_BUFFER,? vertex_buffer_object)

? ? ? ? glBufferData(GL_ARRAY_BUFFER, len(self.object.model) * 4, self.object.c_model, GL_DYNAMIC_DRAW)?

? ? ? ? glDrawArrays(GL_TRIANGLES, 0, len(self.object.vertex_index))


? ? ? ? # vertices

? ? ? ? vertex_offset = 0

? ? ? ? glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.object.model.itemsize * 3, ctypes.c_void_p(vertex_offset))

? ? ? ? glEnableVertexAttribArray(0)


? ? ? ? # textures

? ? ? ? texture_offset = len(self.object.vertex_index) * 12

? ? ? ? glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, self.object.model.itemsize * 2, ctypes.c_void_p(texture_offset))

? ? ? ? glEnableVertexAttribArray(1)


? ? ? ? # normals

? ? ? ? normal_offset = texture_offset + len(self.object.texture_index) * 8

? ? ? ? glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.object.model.itemsize * 3, ctypes.c_void_p(normal_offset))

? ? ? ? glEnableVertexAttribArray(2)


? ? ? ? # create texture

? ? ? ? self.texture = GLuint(0)

? ? ? ? glGenTextures(1, self.texture)

? ? ? ? glBindTexture(GL_TEXTURE_2D, self.texture)

? ? ? ? # texture wrapping

? ? ? ? glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)

? ? ? ? glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

? ? ? ? # texture filtering

? ? ? ? glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

? ? ? ? glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)


? ? ? ? image = Image.open(texture_name)

? ? ? ? flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)

? ? ? ? image_data = image.convert("RGB").tobytes()

? ? ? ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data)

在類中添加一個方法來綁定 OpenGL 對象(VAO 和紋理)并繪制網(wǎng)格:


class Model:

? ? # [...]


? ? def draw(self):


? ? ? ? // bind texture

? ? ? ? glBindTexture(GL_TEXTURE_2D, self.texture)


? ? ? ? // create vertex array object

? ? ? ? glBindVertexArray(self.vao)


? ? ? ? // draw object

? ? ? ? glDrawArrays(GL_TRIANGLES, 0, len(self.object.vertex_index))

通過此設置,您可以繪制多個模型:


class Obj(QOpenGLWidget):

? ? def __init__(self, parent):

? ? ? ? QOpenGLWidget.__init__(self, parent)


? ? def initializeGL(self):

? ? ? ? glClearColor(82/255, 95/255, 107/255, 1.0)

? ? ? ? glEnable(GL_DEPTH_TEST)

? ? ? ? glEnable(GL_BLEND)

? ? ? ? glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)


? ? ? ? self.model1 = Model("....obj", "texture.png")?

? ? ? ? self.model2 = Model("....obj", "texture2.png")??

? ? ? ? # [...]?


? ? def paintGL(self):

? ? ? ??

? ? ? ? glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

? ? ? ??

? ? ? ? # install program

? ? ? ? glUseProgram(self.shader)


? ? ? ? # setting the view, projection etc matrix

? ? ? ? # [...]

? ? ? ??

? ? ? ? self.model1.draw()

? ? ? ? self.model2.draw()

? ? ? ? # [...]?

? ? ? ??

? ? ? ? self.update()

? ? ? ??

? ? def resizeGL(self, width, height):

? ? ? ? glViewport(0, 0, width, height)

模型也可以組織在列表中:


class Obj(QOpenGLWidget):

? ?# [...]


? ? def initializeGL(self):

? ? ? ? # [...]


? ? ? ? self.models = [

? ? ? ? ? ? Model("....obj", "texture.png"),

? ? ? ? ? ? Model("....obj", "texture2.png")??

? ? ? ? ? ? # [...]

? ? ? ? ]?


? ? def paintGL(self):

? ? ? ? # [...]


? ? ? ? for model in self.models:

? ? ? ? ? ? model.draw()


? ? ? ? # [...]?


查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號