OpenGL PyQt 对象不缩放
Posted
技术标签:
【中文标题】OpenGL PyQt 对象不缩放【英文标题】:OpenGL PyQt object not scaling 【发布时间】:2020-08-31 11:15:02 【问题描述】:我刚刚使用 VBO 和 VAO 将旧的 glBegin 和 glEnd 代码翻译成一个。作为框架,我使用的是 Pyqt。我确实设法加载了一个 3D 对象并将其显示在屏幕上,但我无法缩放它。我在paintGL方法中尝试了glLoadIdentity,但没有任何改变。我怎么能缩放它? 这是我的代码:
vertex_code = '''
attribute vec2 position;
void main()
gl_Position = vec4(position, 0.0, 1.0);
gl_Position.y -= 0.5;
'''
fragment_code = '''
void main()
gl_FragColor = vec4(0.0, 1.0, 1.0, 0.3);
'''
class ObjRender(QOpenGLWidget):
def __init__(self, parent):
QGLWidget.__init__(self, parent)
self.object = ObjectLoader("myobj.obj")
self.triangleVertices = []
self.angle = 0
def initializeGL(self):
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_FLAT)
glDisable(GL_CULL_FACE)
program = glCreateProgram()
vertex = glCreateShader(GL_VERTEX_SHADER)
fragment = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(vertex, vertex_code)
glShaderSource(fragment, fragment_code)
glCompileShader(vertex)
## handle errors
glCompileShader(fragment)
## handle errors
glAttachShader(program, vertex)
glAttachShader(program, fragment)
glLinkProgram(program)
## handle errors
glDetachShader(program, vertex)
glDetachShader(program, fragment)
glUseProgram(program)
## build data
glClearColor(0.2, 0.2, 0.2, 0.7)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
self.initializeVertices()
self.triangleVertices = array(self.triangleVertices, 'f')
self.vertexBufferObject = VBO(self.triangleVertices)
self.vertexBufferObject.bind()
self.vertexArrayObject = glGenVertexArrays(1)
glBindVertexArray(self.vertexArrayObject)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 3*self.triangleVertices.itemsize, None)
glBindVertexArray(0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0.0, 0.0, -10.0)
glScalef(0.2, 0.2, 0.2)
glBindVertexArray(self.vertexArrayObject)
glDrawArrays(GL_TRIANGLES, 0, len(self.triangleVertices))
glBindVertexArray(0)
glFlush()
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
gluPerspective(45, 2.0, 7.0, 1000.0)
【问题讨论】:
【参考方案1】:如果你还想要我们Legacy OpenGL固定函数矩阵栈,那么你必须在顶点着色器中通过gl_ModelViewProjectionMatrix
来变换顶点位置:
attribute vec2 position;
void main()
vec4 pos = vec4(position.x, position.y-0.5, 0.0, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * pos;
注意,只要立即模式(glBegin
/glEnd
序列),矩阵堆栈就会被弃用。如今,顶点变换由用户定义的矩阵Uniform 变量完成。另见GLSL Programming/Vector and Matrix Operations 和Replacement for gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
【讨论】:
以上是关于OpenGL PyQt 对象不缩放的主要内容,如果未能解决你的问题,请参考以下文章