Ejemplo n.º 1
0
 def __init__(
     self,
     intensity=QVector4D(1.0, 1.0, 1.0, 1.0),
     coefficients=QVector4D(1.0, 1.0, 1.0, 1.0),
     attenuation=QVector3D(1.0, 0.14, 0.07),
     cutOff=12.5,
     outerCutOff=15.0,
 ):
     ""
     self.color = QVector4D()
     self.intensity = intensity
     self.coeffs = coefficients
     self.set_color()
     self.cutOff = math.cos(math.radians(cutOff))
     self.outerCutOff = math.cos(math.radians(outerCutOff))
     self.attenuation = attenuation
     self.attenVals = [
         # data taken on 2019-08-30 from
         # https://learnopengl.com/Lighting/Light-casters
         # distance, attenConst, attenLin, attenQaud
         [7, 1.0, 0.14, 0.07],
         [13, 1.0, 0.35, 0.44],
         [20, 1.0, 0.22, 0.20],
         [32, 1.0, 0.14, 0.07],
         [50, 1.0, 0.09, 0.032],
         [65, 1.0, 0.07, 0.017],
         [100, 1.0, 0.045, 0.0075],
         [160, 1.0, 0.027, 0.0028],
         [200, 1.0, 0.022, 0.0019],
         [325, 1.0, 0.014, 0.0007],
         [600, 1.0, 0.007, 0.0002],
         [3250, 1.0, 0.0014, 0.000007],
     ]
Ejemplo n.º 2
0
 def __init__(self,
              name,
              scene,
              reflectionColor=QVector4D(1.0, 1.0, 1.0, 1.0),
              transmissionColor=QVector4D(1.0, 1.0, 1.0, 1.0),
              IOR=1.44):
     super().__init__(name, scene, reflectionColor)
     self.reflectionColor = reflectionColor
     self.transmissionColor = transmissionColor
     self.IOR = IOR
Ejemplo n.º 3
0
 def create_billboard_transformation(view_matrix, text_vector):
     """
     Uses the view matrix of the gnomon camera and the current position of the axis label text in order to create a
     matrix that makes the text plane orthogonal to the camera vector.
     :param view_matrix: The view matrix of the gnomon camera. This is the inverse of the translation matrix that
                         describes the position and rotation of the camera.
     :param text_vector: The vector of the axis label text.
     :return: A transformation matrix for making the text face the camera.
     """
     billboard_transformation = view_matrix.transposed()
     billboard_transformation.setRow(3, QVector4D())
     billboard_transformation.setColumn(3, QVector4D(text_vector, 1))
     return billboard_transformation
Ejemplo n.º 4
0
def test_GIVEN_view_matrix_and_vector_WHEN_calling_create_billboard_transformation_THEN_corect_matrix_returned(
):

    view_matrix = QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
                             16)
    text_vector = QVector3D(10, 10, 10)

    expected_matrix = view_matrix.transposed()
    expected_matrix.setRow(3, QVector4D())
    expected_matrix.setColumn(3, QVector4D(text_vector, 1))

    actual_matrix = Gnomon.create_billboard_transformation(
        view_matrix, text_vector)

    assert expected_matrix == actual_matrix
Ejemplo n.º 5
0
    def __init__(self, parent=None):
        QOpenGLWidget.__init__(self, parent)

        # shaders etc
        triangleTutoDir = os.path.dirname(__file__)
        trianglePardir = os.path.join(triangleTutoDir, os.pardir)
        mediaDir = os.path.join(trianglePardir, "media")
        shaderDir = os.path.join(mediaDir, "shaders")
        availableShaders = ["triangle", "triangle2"]
        self.shaders = {
            name: {
                "fragment": os.path.join(shaderDir, name + ".frag"),
                "vertex": os.path.join(shaderDir, name + ".vert")
            } for name in availableShaders
        }
        self.core = "--coreprofile" in QCoreApplication.arguments()

        # opengl data related
        self.context = QOpenGLContext()
        self.vao1 = QOpenGLVertexArrayObject()
        self.vbo1 = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)
        self.vao2 = QOpenGLVertexArrayObject()
        self.vbo2 = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)

        self.program1 = QOpenGLShaderProgram()
        self.program2 = QOpenGLShaderProgram()

        # some vertex data for corners of triangle
        self.vertexData1 = np.array(
            [0.9, 0.9, 0.0,  # x, y, z
             0.9, 0.7, 0.0,  # x, y, z
             0.7, 0.9, 0.0],  # x, y, z
            dtype=ctypes.c_float
        )
        self.vertexData2 = np.array(
            [-0.9, -0.9, 0.0,  # x, y, z
             -0.9, -0.7, 0.0,  # x, y, z
             -0.7, -0.9, 0.0],  # x, y, z
            dtype=ctypes.c_float
        )
        # triangle color
        self.triangleColor1 = QVector4D(1.0, 0.0, 0.0, 0.0)  # yellow triangle
        self.triangleColor2 = QVector4D(
            0.0, 0.0, 0.5, 0.0)  # not yellow triangle
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    def colorSlot1(self, value):
        if self.scene.getNumSelectedMaterials() != 1:
            return

        for mat in self.scene.getSelectedMaterialIterator():
            colorDialog = QColorDialog(parent=self)
            color = colorDialog.getColor(options=QColorDialog.ShowAlphaChannel)
            if color.isValid():
                if type(mat) == DiffuseMaterial:
                    mat.setColor(
                        QVector4D(color.redF(), color.greenF(), color.blueF(),
                                  color.alphaF()))
                    self.setupDiffuseMaterial(mat)
                elif type(mat) == SpecularMaterial:
                    mat.setReflectionColor(
                        QVector4D(color.redF(), color.greenF(), color.blueF(),
                                  color.alphaF()))
                    self.setupSpecularMaterial(mat)
            return
Ejemplo n.º 8
0
 def colorSlot(self, value):
     if self.scene.getNumSelectedObjects() != 1:
         return
     
     for light in self.scene.getSelectedObjectIterator():
         colorDialog = QColorDialog(parent = self)
         color = colorDialog.getColor(options = QColorDialog.ShowAlphaChannel)
         if color.isValid():
             light.setColor(QVector4D(color.redF(), color.greenF(), color.blueF(), color.alphaF()))
         return
Ejemplo n.º 9
0
    def __init__(self, parent=None):
        "Constructor"
        QOpenGLWidget.__init__(self, parent)
        tutoTutoDir = os.path.dirname(__file__)
        tutoPardir = os.path.join(tutoTutoDir, os.pardir)
        tutoPardir = os.path.realpath(tutoPardir)
        mediaDir = os.path.join(tutoPardir, "media")
        shaderDir = os.path.join(mediaDir, "shaders")
        #
        availableShaders = ["rectangle", "triangle"]
        self.shaders = {
            name: {
                "fragment": os.path.join(shaderDir, name + ".frag"),
                "vertex": os.path.join(shaderDir, name + ".vert")
            }
            for name in availableShaders
        }
        self.core = "--coreprofile" in QCoreApplication.arguments()

        # opengl data related
        self.context = QOpenGLContext()
        self.program = QOpenGLShaderProgram()
        self.vao = QOpenGLVertexArrayObject()
        self.vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)
        self.indices = np.array(
            [
                0,
                1,
                3,  # first triangle
                1,
                2,
                3  # second triangle
            ],
            dtype=ctypes.c_uint)

        # vertex data of the panel that would hold the image
        self.vertexData = np.array(
            [
                # viewport position || colors           ||   texture coords
                0.5,
                0.5,
                0.0,  # top right
                0.5,
                -0.5,
                0.0,  # bottom right
                -0.5,
                -0.5,
                0.0,  # bottom left
                -0.5,
                0.5,
                0.0,  # top left
            ],
            dtype=ctypes.c_float)

        self.rectColor = QVector4D(0.0, 1.0, 1.0, 0.0)
Ejemplo n.º 10
0
    def computeScaleUnitFromModelMatrix(self, axis, modelMat, camera,
                                        windowSize):
        """ Compute the length of the screen projected vector axis unit transformed by the model matrix.
            Args:
                axis (QVector3D): chosen axis ((1,0,0) or (0,1,0) or (0,0,1))
                modelMat (QMatrix4x4): model matrix used for the transformation
                camera (QCamera): camera viewing the scene
                windowSize (QSize): size of the window in pixels
            Returns:
                float: length (in pixels)
        """
        decomposition = self.decomposeModelMatrix(modelMat)

        posMat = QMatrix4x4()
        posMat.translate(decomposition.get("translation"))

        rotMat = self.quaternionToRotationMatrix(
            decomposition.get("quaternion"))

        unitScaleModelMat = posMat * rotMat * QMatrix4x4()

        worldCenterPoint = unitScaleModelMat.map(QVector4D(0, 0, 0, 1))
        worldAxisUnitPoint = unitScaleModelMat.map(
            QVector4D(axis.x(), axis.y(), axis.z(), 1))
        screenCenter2D = self.pointFromWorldToScreen(worldCenterPoint, camera,
                                                     windowSize)
        screenAxisUnitPoint2D = self.pointFromWorldToScreen(
            worldAxisUnitPoint, camera, windowSize)

        screenVector = QVector2D(
            screenAxisUnitPoint2D.x() - screenCenter2D.x(),
            -(screenAxisUnitPoint2D.y() - screenCenter2D.y()))

        value = screenVector.length()
        return value if (
            value and
            value > 10) else 10  # Threshold to avoid problems in extreme case
Ejemplo n.º 11
0
 def set_color(self):
     "Set light source color using coeffs and intensities"
     if isinstance(self.intensity, QVector3D):
         self.color = QVector3D(
             self.intensity.x() * self.coeffs.x(),
             self.intensity.y() * self.coeffs.y(),
             self.intensity.z() * self.coeffs.z(),
         )
     else:
         self.color = QVector4D(
             self.intensity.x() * self.coeffs.x(),
             self.intensity.y() * self.coeffs.y(),
             self.intensity.z() * self.coeffs.z(),
             self.intensity.w() * self.coeffs.w(),
         )
         self.color = self.color.toVector3DAffine()
Ejemplo n.º 12
0
 def createDiffuseMaterial(self):
     self.scene.addMaterial(
         DiffuseMaterial('Diffuse Material', self.scene,
                         QVector4D(0.7, 0.7, 0.7, 1.0)))
Ejemplo n.º 13
0
 def __init__(self, name, scene, color=QVector4D(0.5, 0.5, 0.5, 1.0)):
     super().__init__(name, scene, color)
     self.color = color
Ejemplo n.º 14
0
def arr2vec(arr: np.ndarray):
    "convert array 2 vector"
    sqarr = np.squeeze(arr)
    assert sqarr.size == 4
    return QVector4D(sqarr[0], sqarr[1], sqarr[2], sqarr[3])
Ejemplo n.º 15
0
 def setAttenuationByTableVals(self, index: int):
     "Set attenuation values by table"
     row = self.attenVals[index]
     self.attenuation = QVector4D(row[1], row[2], row[3], 1.0)
Ejemplo n.º 16
0
 def resizeGL(self, width, height):
     self.__update_scale(width, height)
     self._height = QVector4D(0, self.height(), 0, 0)
     self.glViewport(0, 0, width, height)
Ejemplo n.º 17
0
 def testQVector4DToTuple(self):
     vec = QVector4D(1, 2, 3, 4)
     self.assertEqual((1, 2, 3, 4), vec.toTuple())
Ejemplo n.º 18
0
 def setVector4(self, name, v):
     if self.enabled <= 0:
         raise Exception("shader must be enabled")
     self.program.setUniformValue(name, QVector4D(*v.flat))
Ejemplo n.º 19
0
 def setUp(self):
     self.original = QVector4D(1, 2, 3, 4)
Ejemplo n.º 20
0
 def __init__(self, name, scene = None, material = None, translation = QVector3D(), rotation = QVector3D(), scale = QVector3D(1.0, 1.0, 1.0), emitterShape = SceneObjectType.SPHERE, intensity = 1.0, color = QVector4D(1.0, 1.0, 1.0, 1.0), isVisible = True):
     super().__init__(name, scene, material, translation, rotation, scale)
     self.emitterShape = emitterShape
     self.intensity = intensity
     self.color = color
     self.isVisible = isVisible