OpenGL PyQt 对象不缩放
OpenGL PyQt object not scaling
我刚刚使用 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)
如果你还想使用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 variables. See also GLSL Programming/Vector and Matrix Operations and Replacement for gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
我刚刚使用 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)
如果你还想使用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 variables. See also GLSL Programming/Vector and Matrix Operations and Replacement for gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;