def paintGL(self): retinaScale = self.devicePixelRatio() gl.glViewport(0, 0, int(self.width() * retinaScale), int(self.height() * retinaScale)) gl.glClearColor(0.2, 0.3, 0.3, 1.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.cube.program.bind() projection = QMatrix4x4() projection.setToIdentity() projection.perspective(45., float(self.width()) / self.height(), 0.1, 100.) self.cube.program.setUniformValue('projection', projection) # Initialize view matrix. view = QMatrix4x4() view.setToIdentity() view.translate(0., 0., -10.) self.cube.program.setUniformValue('view', view) for i, t in enumerate(self.cubePositions): transform = QMatrix4x4() transform.setToIdentity() transform.translate(t) transform.rotate(1.2 ** ((i + 1) * 5) * self.timer.elapsed() / 10, QVector3D(0.5, 1, 0).normalized()) self.cube.render(transform) self.cube.program.release()
def sphericalToCartesian(theta, phi): thetaRot = QMatrix4x4() thetaRot.rotate(theta, QVector3D(1.0, 0.0, 0.0)) phiRot = QMatrix4x4() phiRot.rotate(phi, QVector3D(0.0, 1.0, 0.0)) v = QVector3D(0.0, 1.0, 0.0) return (v * thetaRot) * phiRot
def __init__(self, parent=None): QOpenGLWidget.__init__(self, parent) QOpenGLFunctions.__init__(self) self.core = "--coreprofile" in QCoreApplication.arguments() self.xRot = 0 self.yRot = 0 self.zRot = 0 self.lastPos = 0 self.logo = Logo() self.vao = QOpenGLVertexArrayObject() self.logoVbo = QOpenGLBuffer() self.program = QOpenGLShaderProgram() self.projMatrixLoc = 0 self.mvMatrixLoc = 0 self.normalMatrixLoc = 0 self.lightPosLoc = 0 self.proj = QMatrix4x4() self.camera = QMatrix4x4() self.world = QMatrix4x4() self.transparent = "--transparent" in QCoreApplication.arguments() if self.transparent: fmt = self.format() fmt.setAlphaBufferSize(8) self.setFormat(fmt)
def passUniforms(self, projection_matrix: QMatrix4x4, view_matrix: QMatrix4x4, model_matrix: QMatrix4x4): self.__shader.bind() self.__shader.setUniformMatrix4fv("u_projectionMatrix", projection_matrix.data()) self.__shader.setUniformMatrix4fv("u_viewMatrix", view_matrix.data()) self.__shader.setUniformMatrix4fv("u_modelMatrix", model_matrix.data())
def getViewMatrix(self): view = QMatrix4x4() center = self.scene.getFocusObject().getTranslation() up = QVector3D(0.0, 1.0, 0.0) right = QVector3D(1.0, 0.0, 0.0) r = QMatrix4x4() r.rotate(self.vertRot, QVector3D(1.0, 0.0, 0.0)) r.rotate(self.horRot, QVector3D(0.0, 1.0, 0.0)) view.lookAt(self.eyeLoc * r + center, center, up) return view
def calculateModel(self): translation = QMatrix4x4() translation.translate(self.translationVec) scale = QMatrix4x4() scale.scale(self.scaleVec) rotation = QMatrix4x4() rotation.rotate(self.rotationVec.y(), QVector3D(0, 1, 0)) rotation.rotate(self.rotationVec.x(), QVector3D(1, 0, 0)) rotation.rotate(self.rotationVec.z(), QVector3D(0, 0, 1)) return translation * rotation * scale
def create_axis_label_matrices(vectors): """ Creates the matrices used to transform the labels that form the gnomon. :param vectors: The vectors that describe the location of the text. :return: The transformation matrices. """ x_axis_matrix = QMatrix4x4() y_axis_matrix = QMatrix4x4() z_axis_matrix = QMatrix4x4() x_axis_matrix.translate(vectors[0]) y_axis_matrix.translate(vectors[1]) z_axis_matrix.translate(vectors[2]) return x_axis_matrix, y_axis_matrix, z_axis_matrix
def paintGL(self): "drawing loop" funcs = self.context.functions() # clean up what was drawn funcs.glClear(pygl.GL_COLOR_BUFFER_BIT | pygl.GL_DEPTH_BUFFER_BIT) self.vao.bind() self.vbo.bind() # actual drawing self.program.bind() rotvec = QVector3D(0.7, 0.2, 0.5) # bind textures for i, pos in enumerate(self.cubeCoords): # cubeModel = QMatrix4x4() cubeModel.translate(pos) angle = 30 * i cubeModel.rotate(angle, rotvec) self.program.setUniformValue("model", cubeModel) self.texture1.bind(self.texUnit1) self.texture2.bind(self.texUnit2) funcs.glDrawArrays(pygl.GL_TRIANGLES, 0, self.cubeVertices.size) self.vbo.release() self.program.release() self.texture1.release() self.texture2.release()
def testOperator(self): m = QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) v = 1 for x in range(4): for y in range(4): self.assertEqual(m[x, y], v) v += 1
def __init__(self, x_offset, y_offset, parent): super(NeutronAnimationController, self).__init__(parent) self._target = None self._matrix = QMatrix4x4() self._x_offset = x_offset self._y_offset = y_offset self._distance = 0
def set_position(self, new_position: QVector3D): self.position = new_position new_translation_matrix = QMatrix4x4() new_translation_matrix.translate(self.position) self.translation_matrix = new_translation_matrix self.__update_bbox__() self.__update_model_matrix__()
def set_z_pos(self, value): self.position.setZ(value) new_translation_matrix = QMatrix4x4() new_translation_matrix.translate(self.position) self.translation_matrix = new_translation_matrix self.__update_bbox__() self.__update_model_matrix__()
def quaternionToRotationMatrix(self, q): """ Return a rotation matrix from a quaternion. """ rotMat3x3 = q.toRotationMatrix() return QMatrix4x4(rotMat3x3(0, 0), rotMat3x3(0, 1), rotMat3x3(0, 2), 0, rotMat3x3(1, 0), rotMat3x3(1, 1), rotMat3x3(1, 2), 0, rotMat3x3(2, 0), rotMat3x3(2, 1), rotMat3x3(2, 2), 0, 0, 0, 0, 1)
def render(self): if not self.context.makeCurrent(self): raise Exception("makeCurrent() failed") functions = self.context.functions() if self.program is None: functions.glEnable(GL.GL_DEPTH_TEST) functions.glClearColor(0, 0, 0, 1) self.initGl() retinaScale = self.devicePixelRatio() functions.glViewport(0, 0, self.width() * retinaScale, self.height() * retinaScale) functions.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) self.program.bind() matrix = QMatrix4x4() matrix.perspective(60, 4 / 3, 0.1, 100) matrix.translate(0, 0, -2) matrix.rotate(self.angle, 0, 1, 0) self.program.setUniformValue(self.matrixUniform, matrix) if self.vao.isCreated(): self.vao.bind() else: # no VAO support, set the vertex attribute arrays now self.setupVertexAttribs() functions.glDrawArrays(GL.GL_TRIANGLES, 0, 3) self.vao.release() self.program.release() # swapInterval is 1 by default which means that swapBuffers() will (hopefully) block # and wait for vsync. self.context.swapBuffers(self) self.context.doneCurrent()
def resizeViewport( self, size = None ): # the athena_viewport QParameter exists because the Qt3D ViewportMatrix shader # uniform is unreliable: it doesn't seem to be consistently updated before a draw # operation occurs under a new viewport matrix. This messes up shader calculations, # especially in screenshots (where only a single frame is captured under new # rendering dimensions), which in turn wrecks the wireframe renderer. # # This function manually recreates the viewport matrix that Qt3D uses and keeps # it updated in the athena_viewport parameter. # Note that it's important to use physical pixel sizes in this calculation, so # always multiply on-screen sizes by self.screen().devicePixelRatio() viewport_matrix = QMatrix4x4() # The only renderer to use the athena_viewport parameter is the wireframe renderer, # which is always under framegraph.viewport2 viewport = self.framegraph.viewport2.normalizedRect() if ( size == None ): size = self._physicalPixelSize() # c.f. Qt3DRender::SubmissionContext::setViewport() viewport_matrix.viewport( viewport.x() * size.width(), (1.0 - viewport.y() - viewport.height()) * size.height(), viewport.width() * size.width(), viewport.height() * size.height() ); self.setAthenaViewport( viewport_matrix )
def matrix_test(): qmatrix = QMatrix4x4() qmatrix.translate(0, -0.2, 0) # qmatrix.rotate(90, 1, 0, 0) qinv_matrix, is_invertible = qmatrix.inverted() qnormal_matrix = qinv_matrix.normalMatrix() normal_matrix = np.array(qnormal_matrix.data()).reshape(3,3).transpose() inv_matrix = np.array(qinv_matrix.data()).reshape(4,4).transpose() matrix = np.array(qmatrix.data()).reshape(4,4).transpose() p0 = np.array([5, 0, 0]) n = np.array([0, 1, 0]) plane = st.Plane(p0, n) plane_info = st.PlaneIntersectionInfo() number_of_primitives = 5 primitives = [] qprimitives = [] transformed_primitives = [] for i in range(number_of_primitives): t_baricenter = (np.random.rand(3) - 0.5) * 100 t_baricenter = np.zeros(3) v0 = t_baricenter + (np.random.rand(3) - 0.5) * 2 v1 = t_baricenter + (np.random.rand(3) - 0.5) * 2 v2 = t_baricenter + (np.random.rand(3) - 0.5) * 2 primitives.append(st.Triangle(v0, v1, v2)) qv0 = qmatrix.map(QVector3D(v0[0], v0[1], v0[2])) qv1 = qmatrix.map(QVector3D(v1[0], v1[1], v1[2])) qv2 = qmatrix.map(QVector3D(v2[0], v2[1], v2[2])) qprimitives.append(st.Triangle(np.array(qv0.toTuple()), np.array(qv1.toTuple()), np.array(qv2.toTuple()))) qbvh = pyBVH.BVH(qprimitives, 'SAH') qbvh.plane_all_intersections(plane, plane_info) print(plane_info.intersections) bvh = pyBVH.BVH(primitives, 'EqualCounts') p0 = inv_matrix.dot(np.append(p0, 1))[0:3] n = normal_matrix.dot(np.array([0, 1, 0])) plane = st.Plane(p0, n) plane_info = st.PlaneIntersectionInfo() bvh.plane_all_intersections(plane, plane_info) # # method 1: # start_time = time.time() # result = [matrix[0:3, 0:3].dot(x) + matrix[0:3, 3] for x in plane_info.intersections] # print(time.time() - start_time) # print(result) # # method 2: # start_time = time.time() # intersections = np.array(plane_info.intersections).transpose() # result = list(matrix[0:3, 0:3].dot(intersections) + np.array(matrix[0:3, 3])[:,np.newaxis]) # print(time.time() - start_time) # print(result) # # method 3: start_time = time.time() intersections = np.array(plane_info.intersections) result = list(intersections.dot(matrix[:3,:3]) + np.array(matrix[0:3, 3])) # print(time.time() - start_time) print(result)
def set_channels(self, r, g, b, a): r, g, b, a = float(r), float(g), float(b), float(a) s = r + g + b if s > 1.1: self._u_channels = QMatrix4x4(r, 0, 0, 0, 0, g, 0, 0, 0, 0, b, 0, 0, 0, 0, a) elif s < 0.1: self._u_channels = QMatrix4x4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, a, a, 0) elif a: self._u_channels = QMatrix4x4(r, 0, 0, 0, 0, g, 0, 0, 0, 0, b, 0, 0, 0, 0, a) else: self._u_channels = QMatrix4x4(r, r, r, 0, g, g, g, 0, b, b, b, 0, 0, 0, 0, a) # redraw self.update()
def get_model_matrix(self): modelmat = QMatrix4x4() modelmat.setToIdentity() modelmat.translate(self.position) modelmat.rotate(self.roll, QVector3D(1.0, 0.0, 0.0)) modelmat.rotate(self.pitch, QVector3D(0.0, 1.0, 0.0)) modelmat.rotate(self.yaw, QVector3D(0.0, 0.0, 1.0)) return modelmat
def set_scale(self, new_scale: QVector3D): self.scale = new_scale new_scale_matrix = QMatrix4x4() new_scale_matrix.scale(self.scale) new_scale_matrix.scale(self.unit_of_measurement) self.scale_matrix = new_scale_matrix self.__update_bbox__() self.__update_model_matrix__()
def set_z_scale(self, value): self.scale.setZ(value) new_scale_matrix = QMatrix4x4() new_scale_matrix.scale(self.scale) new_scale_matrix.scale(self.unit_of_measurement) self.scale_matrix = new_scale_matrix self.__update_bbox__() self.__update_model_matrix__()
def computeLookAtMatrixQt(position: np.ndarray, target: np.ndarray, up: np.ndarray): "look at matrice" eye = QVector3D(position[0], position[1], position[2]) target = QVector3D(target[0], target[1], target[2]) upvec = QVector3D(up[0], up[1], up[2]) mat4 = QMatrix4x4() return mat4.lookAt(eye, target, upvec)
def set_unit_of_measurement(self, value): self.unit_of_measurement = value new_scale_matrix = QMatrix4x4() new_scale_matrix.scale(self.scale) new_scale_matrix.scale(self.unit_of_measurement) self.scale_matrix = new_scale_matrix self.is_bbox_refined = False self.__update_bbox__() self.__update_model_matrix__()
def rotate(self, dx, dy): #Variant1: default rotation order - rotate around X then around Y #newYaw = self.cubeTransform.rotationY() + dx/4 #newPitch = self.cubeTransform.rotationX() + dy/4 #self.cubeTransform.setRotationX(newPitch) #self.cubeTransform.setRotationY(newYaw) #Variant2: custom rotation order - rotate around Y then around X self.yaw += dx self.pitch += dy a = self.yaw * 3.14 / 180 yawMat = QMatrix4x4(math.cos(a), 0, math.sin(a), 0, 0, 1, 0, 0, -math.sin(a), 0, math.cos(a), 0, 0, 0, 0, 1) a = self.pitch * 3.14 / 180 pitchMat = QMatrix4x4(1, 0, 0, 0, 0, math.cos(a), -math.sin(a), 0, 0, math.sin(a), math.cos(a), 0, 0, 0, 0, 1) rotMat = pitchMat * yawMat self.cubeTransform.setMatrix(rotMat)
def __init__(self): self.__modelMatrix = QMatrix4x4() self.__vertices = vertices self.__vertexAmount = 0 self.__shader = None self.__vertexArray = None self.__vertexBuffer = None self.initObject()
def __init__(self): super().__init__() self.position = QVector3D(0.0, 0.0, 0.0) self.front = None self.worldUp = QVector3D(0.0, 1.0, 0.0) self.up = None self.right = None self.idmat = QMatrix4x4() self.idmat.setToIdentity()
def test_GIVEN_vectors_WHEN_calling_create_axis_label_matrices_THEN_correct_matrices_returned( ): vectors = [QVector3D(1, 0, 0), QVector3D(0, 1, 0), QVector3D(0, 0, 1)] expected_x = QMatrix4x4() expected_y = QMatrix4x4() expected_z = QMatrix4x4() expected_x.translate(vectors[0]) expected_y.translate(vectors[1]) expected_z.translate(vectors[2]) actual_x, actual_y, actual_z = Gnomon.create_axis_label_matrices(vectors) assert expected_x == actual_x assert expected_y == actual_y assert expected_z == actual_z
def __init__(self, parent=None): QOpenGLWidget.__init__(self, parent) QOpenGLFunctions.__init__(self) self.setMinimumSize(32, 32) self.info = "" self._supported_images = [ "TGA", "PNG", "JPG", "JPEG", "TIF", "TIFF", "BMP", "DDS" ] # indices indices = [0, 1, 3, 1, 2, 3] self._indices = array('I', indices) # vertices # 3 position | 2 texture coord vertex = [ 1.0, 1.0, 0.0, 1.0, 1.0, # top right 1.0, -1.0, 0.0, 1.0, 0.0, # bottom right -1.0, -1.0, 0.0, 0.0, 0.0, # bottom left -1.0, 1.0, 0.0, 0.0, 1.0 # top left ] self._vertex = array('f', vertex) # opengl data related self._program = QOpenGLShaderProgram() self._program_bg = QOpenGLShaderProgram() self._vao = QOpenGLVertexArrayObject() self._vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer) self._texture = None self._texture_size = (1, 1) self._location = () self._colors_default = (QColor.fromRgbF(0.65, 0.65, 0.65, 1.0), QColor.fromRgbF(0.90, 0.90, 0.90, 1.0)) self._u_colors = self._colors_default self._height = QVector4D(0, self.height(), 0, 0) self._u_channels = QMatrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
def pose(self): """ Get the camera pose of 'viewpoint' as a 4x4 matrix. """ if self._R is None or self._T is None: return None # convert transform matrix for Qt return QMatrix4x4(self._R[0], -self._R[1], -self._R[2], self._T[0], self._R[3], -self._R[4], -self._R[5], self._T[1], self._R[6], -self._R[7], -self._R[8], self._T[2], 0, 0, 0, 1)
def arr2qmat(arr: np.ndarray): "array to matrix 4x4" assert arr.shape == (4, 4) mat4 = QMatrix4x4() for rowNb in range(arr.shape[0]): rowarr = arr[rowNb, :] rowvec = arr2vec(rowarr) mat4.setRow(rowNb, rowvec) # return mat4
def paintGL(self): retinaScale = self.devicePixelRatio() gl.glViewport(0, 0, int(self.width() * retinaScale), int(self.height() * retinaScale)) gl.glClearColor(0.2, 0.3, 0.3, 1.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) transform = QMatrix4x4() transform.setToIdentity() transform.rotate(self.timer.elapsed() / 10, QVector3D(0, 0, 1)) transform.translate(QVector3D(0.25, 0.25, -2.)) projection = QMatrix4x4() projection.setToIdentity() projection.perspective(45., float(self.width()) / self.height(), 0.1, 100.) self.square.render(projection * transform)