Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
texture_opengl33_pyqt5
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
import sys import numpy as np from OpenGL import GL as gl from PyQt5.QtCore import Qt from PyQt5.QtGui import (QImage, QMatrix4x4, QOpenGLBuffer, QOpenGLShader, QOpenGLShaderProgram, QOpenGLTexture) from PyQt5.QtWidgets import QApplication, QOpenGLWidget class OpenGLWidget(QOpenGLWidget): def __init__(self): super().__init__() self.setWindowTitle("Texture, PyQt5, OpenGL 3.3") self.resize(400, 400) def initializeGL(self): gl.glClearColor(0.2, 0.2, 0.2, 1.0) vert_shader_src = """ #version 330 core in vec2 aPosition; in vec2 aTexCoord; out vec2 vTexCoord; void main() { gl_Position = vec4(aPosition, 0.0, 1.0); vTexCoord = aTexCoord; } """ frag_shader_src = """ #version 330 core in vec2 vTexCoord; uniform sampler2D uSampler; out vec4 fragColor; void main() { fragColor = texture2D(uSampler, vTexCoord); } """ self.program = QOpenGLShaderProgram() self.program.addShaderFromSourceCode(QOpenGLShader.Vertex, vert_shader_src) self.program.addShaderFromSourceCode(QOpenGLShader.Fragment, frag_shader_src) self.program.link() self.program.bind() vert_positions = np.array([ -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5], dtype=np.float32) self.vert_pos_buffer = QOpenGLBuffer() self.vert_pos_buffer.create() self.vert_pos_buffer.bind() self.vert_pos_buffer.allocate(vert_positions, len(vert_positions) * 4) self.program.bindAttributeLocation("aPosition", 0) self.program.setAttributeBuffer(0, gl.GL_FLOAT, 0, 2) self.program.enableAttributeArray(0) tex_coords = np.array([ 0, 1, 1, 1, 0, 0, 1, 0], dtype=np.float32) self.tex_coord_buffer = QOpenGLBuffer() self.tex_coord_buffer.create() self.tex_coord_buffer.bind() self.tex_coord_buffer.allocate(tex_coords, len(tex_coords) * 4) self.program.bindAttributeLocation("aTexCoord", 1) self.program.setAttributeBuffer(1, gl.GL_FLOAT, 0, 2) self.program.enableAttributeArray(1) self.texture = QOpenGLTexture(QOpenGLTexture.Target2D) self.texture.create() self.texture.setData(QImage("brown_brick_256x256.png")) self.texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self.texture.setWrapMode(QOpenGLTexture.ClampToEdge) self.proj_view_matrix = QMatrix4x4() def paintGL(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT) self.texture.bind() gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4) def main(): QApplication.setAttribute(Qt.AA_UseDesktopOpenGL) a = QApplication(sys.argv) w = OpenGLWidget() w.show() sys.exit(a.exec_()) if __name__ == "__main__": main()
[
+
]
Show input
edit mode
|
history