Beispiel #1
0
    def _plate_nor(self) -> Vector3:
        x_rot = matrix44.create_from_axis_rotation(axis=X_AXIS,
                                                   theta=self.plate_theta_x)
        y_rot = matrix44.create_from_axis_rotation(axis=Y_AXIS,
                                                   theta=self.plate_theta_y)

        # pitch then roll
        nor = matrix44.apply_to_vector(mat=x_rot, vec=Z_AXIS)
        nor = matrix44.apply_to_vector(mat=y_rot, vec=nor)
        nor = vector.normalize(nor)

        return Vector3(nor)
Beispiel #2
0
 def rotateCam(self, axis, dtheta):
     rotation_mat = matrix44.create_from_axis_rotation(axis, dtheta)
     self.camLookAt -= self.camPos
     newvec = Vector4.from_vector3(self.camLookAt, 1.0)
     newvec = rotation_mat.dot(newvec)
     self.camLookAt = Vector3.from_vector4(newvec)[0]
     self.camLookAt += self.camPos
Beispiel #3
0
    def world_to_plate(self, x: float, y: float, z: float) -> Vector3:
        move = matrix44.create_from_translation([
            -self.plate.x,
            -self.plate.y,
            -(self.plate.z + PLATE_ORIGIN_TO_SURFACE_OFFSET),
        ])
        vec = matrix44.apply_to_vector(mat=move, vec=[x, y, z])

        # rotate
        x_rot = matrix44.create_from_axis_rotation([1.0, 0.0, 0.0],
                                                   -self.plate_theta_x)
        y_rot = matrix44.create_from_axis_rotation([0.0, 1.0, 0.0],
                                                   -self.plate_theta_y)
        vec = matrix44.apply_to_vector(mat=x_rot, vec=vec)
        vec = matrix44.apply_to_vector(mat=y_rot, vec=vec)

        return Vector3(vec)
Beispiel #4
0
 def test_create_from_axis_rotation_non_normalized(self):
     result = matrix44.create_from_axis_rotation([1., 1., 1.], np.pi)
     np.testing.assert_almost_equal(result,
                                    matrix44.create_from_quaternion([
                                        5.77350000e-01, 5.77350000e-01,
                                        5.77350000e-01, 6.12323400e-17
                                    ]),
                                    decimal=3)
     self.assertTrue(result.dtype == np.float)
Beispiel #5
0
 def test_create_from_axis_rotation(self):
     # wolfram alpha can be awesome sometimes
     result = matrix44.create_from_axis_rotation(
         [0.57735, 0.57735, 0.57735], np.pi)
     np.testing.assert_almost_equal(result,
                                    matrix44.create_from_quaternion([
                                        5.77350000e-01, 5.77350000e-01,
                                        5.77350000e-01, 6.12323400e-17
                                    ]),
                                    decimal=3)
     self.assertTrue(result.dtype == np.float)
Beispiel #6
0
 def __updateModelViewMatrix(self, position, rotation, scale, viewMatrix,
                             vboData):
     translation = Matrix44.from_translation(position)
     translation.m11 = viewMatrix.m11
     translation.m12 = viewMatrix.m21
     translation.m13 = viewMatrix.m31
     translation.m21 = viewMatrix.m12
     translation.m22 = viewMatrix.m22
     translation.m23 = viewMatrix.m32
     translation.m31 = viewMatrix.m13
     translation.m32 = viewMatrix.m23
     translation.m33 = viewMatrix.m33
     rotation = matrix44.create_from_axis_rotation((0.0, 0.0, 1.0),
                                                   radians(rotation))
     scale = Matrix44.from_scale([scale, scale, scale])
     modelMatrix = translation * rotation * scale
     modelViewMatrix = viewMatrix * modelMatrix
     self.__storeMatrixData(modelViewMatrix, vboData)
Beispiel #7
0
def on_draw():
    global time
    time += 0.01
    game_window.clear()
    ctx.screen.use()

    trans = matrix44.create_from_translation(
        (math.cos(time), math.sin(time / 5) * 5 - 6, 0))
    rot = matrix44.create_from_y_rotation(math.pi / 2)
    mat = matrix44.multiply(trans, rot)

    ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
    scene.draw(
        projection_matrix=projection,
        camera_matrix=mat,
    )

    fbo.use()
    fbo.clear()

    gl.glUseProgram(0)
    gl.glBindVertexArray(0)

    main_batch.draw()
    counter.draw()

    trans = matrix44.create_from_translation([0, 0, -1])
    rot = matrix44.create_from_axis_rotation(
        [math.sin(time) / 4,
         math.cos(time) / 4,
         math.sin(time) / 20],
        math.sin(time) / 5)
    mat = matrix44.multiply(trans, rot)

    ctx.screen.use()
    fbo.color_attachments[0].use(location=0)
    texture_program['scale'].value = (800 / window_x, 600 / window_y)
    texture_program['m_proj'].write(projection)
    texture_program['m_view'].write(mat.astype('f4').tobytes())
    quad.render(texture_program)
Beispiel #8
0
def main():
    global delta_time, last_frame

    glfw.init()
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    window = glfw.create_window(SRC_WIDTH, SRC_HEIGHT, "learnOpenGL", None,
                                None)
    if not window:
        glfw.terminate()
        raise ValueError("Failed to create window")

    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)
    glfw.set_scroll_callback(window, scroll_callback)

    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    gl.glEnable(gl.GL_DEPTH_TEST)

    lamp_shader = Shader(CURDIR / "shaders/1.lamp.vs",
                         CURDIR / "shaders/6.1.lamp.fs")
    lighting_shader = Shader(CURDIR / "shaders/6.multiple_lights.vs",
                             CURDIR / "shaders/6.multiple_lights.fs")

    vertices = [
        # positions        normals           texture coords
        -0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        0.0,
        0.0,
        0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        1.0,
        0.0,
        0.5,
        0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        1.0,
        1.0,
        0.5,
        0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        1.0,
        1.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        0.0,
        0.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        1.0,
        0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        1.0,
        -0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        -1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        0.5,
        -0.5,
        -1.0,
        0.0,
        0.0,
        1.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        -1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        -1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        0.5,
        -1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        -1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.5,
        -0.5,
        1.0,
        0.0,
        0.0,
        1.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.5,
        -0.5,
        0.5,
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        -1.0,
        0.0,
        0.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        0.0,
        -1.0,
        0.0,
        1.0,
        1.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        -1.0,
        0.0,
        1.0,
        0.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        -1.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        -1.0,
        0.0,
        0.0,
        0.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        -1.0,
        0.0,
        0.0,
        1.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.0,
        1.0,
        1.0,
        0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        0.0,
        0.0,
        0.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0
    ]
    vertices = (c_float * len(vertices))(*vertices)

    cube_positions = [(0.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -2.2, -2.5),
                      (-3.8, -2.0, -12.3), (2.4, -0.4, -3.5),
                      (-1.7, 3.0, -7.5), (1.3, -2.0, -2.5), (1.5, 2.0, -2.5),
                      (1.5, 0.2, -1.5), (-1.3, 1.0, -1.5)]

    point_light_positions = [
        (0.7, 0.2, 2.0),
        (2.3, -3.3, -4.0),
        (-4.0, 2.0, -12.0),
        (0.0, 0.0, -3.0),
    ]

    light_settings = {
        "desert": {
            "clear_color": (.75, .52, .30, 1.0),
            "direction_color": (.4, .4, .4),
            "spot_color": (.2, .2, .2),
            "point_colors": [(1.0, 0.0, 0.0), (1.0, 0.6, 0.0), (1.0, 0.0, 0.0),
                             (1.0, 0.6, 0.0)]
        },
        "factory": {
            "clear_color": (0.01, 0.01, 0.01, 1.0),
            "direction_color": (.4, .4, .4),
            "spot_color": (.2, .2, .2),
            "point_colors": [(.17, .17, .49), (.17, .17, .49), (.17, .17, .49),
                             (.17, .17, .49)]
        },
        "horror": {
            "clear_color": (0.0, 0.0, 0.0, 1.0),
            "direction_color": (.01, .01, .01),
            "spot_color": (.2, .2, .2),
            "point_colors": [(.3, .1, .1), (.3, .1, .1), (.3, .1, .1),
                             (.3, .1, .1)]
        },
        "biochemical_lab": {
            "clear_color": (0.9, 0.9, 0.9, 1.0),
            "direction_color": (.4, .4, .4),
            "spot_color": (.2, .2, .2),
            "point_colors": [(0.4, 0.7, 0.1), (0.4, 0.7, 0.1), (0.4, 0.7, 0.1),
                             (0.4, 0.7, 0.1)]
        },
    }

    cube_vao = gl.glGenVertexArrays(1)
    vbo = gl.glGenBuffers(1)

    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
    gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(vertices), vertices,
                    gl.GL_STATIC_DRAW)

    gl.glBindVertexArray(cube_vao)

    # -- position attribute
    gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE,
                             8 * sizeof(c_float), c_void_p(0))
    gl.glEnableVertexAttribArray(0)
    # -- normal attribute
    gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT,
                             gl.GL_FALSE, 8 * sizeof(c_float),
                             c_void_p(3 * sizeof(c_float)))
    gl.glEnableVertexAttribArray(1)
    # -- texture coordinate
    gl.glVertexAttribPointer(2, 2, gl.GL_FLOAT,
                             gl.GL_FALSE, 8 * sizeof(c_float),
                             c_void_p(6 * sizeof(c_float)))
    gl.glEnableVertexAttribArray(2)

    # -- second configure light vao (vbo is the same)
    light_vao = gl.glGenVertexArrays(1)
    gl.glBindVertexArray(light_vao)

    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
    gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE,
                             8 * sizeof(c_float), c_void_p(0))
    gl.glEnableVertexAttribArray(0)

    # -- load texture
    diffuse_map = load_texture("container2.png")
    specular_map = load_texture("container2_specular.png")

    # -- shader configuration
    lighting_shader.use()
    lighting_shader.set_int("material.diffuse", 0)
    lighting_shader.set_int("material.specular", 1)

    LIGHTS = light_settings.get("horror")
    while not glfw.window_should_close(window):
        # -- time logic
        current_frame = glfw.get_time()
        delta_time = current_frame - last_frame
        last_frame = current_frame

        # -- input
        process_input(window)

        # -- render
        gl.glClearColor(*LIGHTS.get("clear_color"))
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        lighting_shader.use()
        lighting_shader.set_vec3("viewPos", camera.position)
        lighting_shader.set_float("material.shininess", 32.0)

        # -- directional light
        lighting_shader.set_vec3("dirLight.direction", [-0.2, -1.0, -0.3])
        lighting_shader.set_vec3("dirLight.ambient", [0.05, 0.05, 0.05])
        lighting_shader.set_vec3("dirLight.diffuse",
                                 LIGHTS.get("direction_color"))
        lighting_shader.set_vec3("dirLight.specular", [0.5, 0.5, 0.5])
        # -- point light 1
        lighting_shader.set_vec3("pointLights[0].position",
                                 point_light_positions[0])
        lighting_shader.set_vec3("pointLights[0].ambient", [0.05, 0.05, 0.05])
        lighting_shader.set_vec3("pointLights[0].diffuse",
                                 LIGHTS.get("point_colors")[0])
        lighting_shader.set_vec3("pointLights[0].specular", [1.0, 1.0, 1.0])
        lighting_shader.set_float("pointLights[0].constant", 1.0)
        lighting_shader.set_float("pointLights[0].linear", 0.09)
        lighting_shader.set_float("pointLights[0].quadratic", 0.032)
        # -- point light 2
        lighting_shader.set_vec3("pointLights[1].position",
                                 point_light_positions[1])
        lighting_shader.set_vec3("pointLights[1].ambient", [0.05, 0.05, 0.05])
        lighting_shader.set_vec3("pointLights[1].diffuse",
                                 LIGHTS.get("point_colors")[1])
        lighting_shader.set_vec3("pointLights[1].specular", [1.0, 1.0, 1.0])
        lighting_shader.set_float("pointLights[1].constant", 1.0)
        lighting_shader.set_float("pointLights[1].linear", 0.09)
        lighting_shader.set_float("pointLights[1].quadratic", 0.032)
        # -- point light 3
        lighting_shader.set_vec3("pointLights[2].position",
                                 point_light_positions[2])
        lighting_shader.set_vec3("pointLights[2].ambient", [0.05, 0.05, 0.05])
        lighting_shader.set_vec3("pointLights[2].diffuse",
                                 LIGHTS.get("point_colors")[2])
        lighting_shader.set_vec3("pointLights[2].specular", [1.0, 1.0, 1.0])
        lighting_shader.set_float("pointLights[2].constant", 1.0)
        lighting_shader.set_float("pointLights[2].linear", 0.09)
        lighting_shader.set_float("pointLights[2].quadratic", 0.032)
        # -- point light 4
        lighting_shader.set_vec3("pointLights[3].position",
                                 point_light_positions[3])
        lighting_shader.set_vec3("pointLights[3].ambient", [0.05, 0.05, 0.05])
        lighting_shader.set_vec3("pointLights[3].diffuse",
                                 LIGHTS.get("point_colors")[3])
        lighting_shader.set_vec3("pointLights[3].specular", [1.0, 1.0, 1.0])
        lighting_shader.set_float("pointLights[3].constant", 1.0)
        lighting_shader.set_float("pointLights[3].linear", 0.09)
        lighting_shader.set_float("pointLights[3].quadratic", 0.032)
        # -- spotLight
        lighting_shader.set_vec3("spotLight.position", camera.position)
        lighting_shader.set_vec3("spotLight.direction", camera.front)
        lighting_shader.set_vec3("spotLight.ambient", [0.0, 0.0, 0.0])
        lighting_shader.set_vec3("spotLight.diffuse", LIGHTS.get("spot_color"))
        lighting_shader.set_vec3("spotLight.specular", [1.0, 1.0, 1.0])
        lighting_shader.set_float("spotLight.constant", 1.0)
        lighting_shader.set_float("spotLight.linear", 0.09)
        lighting_shader.set_float("spotLight.quadratic", 0.032)
        lighting_shader.set_float("spotLight.cutOff",
                                  math.cos(math.radians(12.5)))
        lighting_shader.set_float("spotLight.outerCutOff",
                                  math.cos(math.radians(15.0)))

        # -- view.projection transformations
        projection = Matrix44.perspective_projection(camera.zoom,
                                                     SRC_WIDTH / SRC_HEIGHT,
                                                     0.1, 100.0)
        view = camera.get_view_matrix()
        lighting_shader.set_mat4("projection", projection)
        lighting_shader.set_mat4("view", view)

        # -- world transformation
        model = Matrix44.identity()
        lighting_shader.set_mat4("model", model)

        # -- bind diffuse map
        gl.glActiveTexture(gl.GL_TEXTURE0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, diffuse_map)

        # -- bind specular map
        gl.glActiveTexture(gl.GL_TEXTURE1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, specular_map)

        # -- render continers
        gl.glBindVertexArray(cube_vao)
        for idx, position in enumerate(cube_positions):
            angle = 20.0 * idx
            rotation = matrix44.create_from_axis_rotation([1.0, 0.3, 0.5],
                                                          math.radians(angle))
            translation = Matrix44.from_translation(position)
            model = translation * rotation
            lighting_shader.set_mat4('model', model)
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36)

        # -- draw lamp object(s)
        lamp_shader.use()
        lamp_shader.set_mat4("projection", projection)
        lamp_shader.set_mat4("view", view)

        gl.glBindVertexArray(light_vao)
        for idx, pos in enumerate(point_light_positions):
            model = Matrix44.identity()
            model *= Matrix44.from_translation(pos)
            model *= Matrix44.from_scale(Vector3([.2, .2, .2]))
            lamp_shader.set_mat4("model", model)
            lamp_shader.set_vec3("color", LIGHTS.get("point_colors")[idx])
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36)

        glfw.swap_buffers(window)
        glfw.poll_events()

    gl.glDeleteVertexArrays(1, id(cube_vao))
    gl.glDeleteVertexArrays(1, id(light_vao))
    gl.glDeleteBuffers(1, id(vbo))
    glfw.terminate()
Beispiel #9
0
 def test_create_from_axis_rotation_non_normalised(self):
     result = matrix44.create_from_axis_rotation([1.,1.,1.], np.pi)
     np.testing.assert_almost_equal(result, matrix44.create_from_quaternion([5.77350000e-01, 5.77350000e-01, 5.77350000e-01, 6.12323400e-17]), decimal=3)
     self.assertTrue(result.dtype == np.float)
Beispiel #10
0
 def test_create_from_axis_rotation(self):
     # wolfram alpha can be awesome sometimes
     result = matrix44.create_from_axis_rotation([0.57735, 0.57735, 0.57735],np.pi)
     np.testing.assert_almost_equal(result, matrix44.create_from_quaternion([5.77350000e-01, 5.77350000e-01, 5.77350000e-01, 6.12323400e-17]), decimal=3)
     self.assertTrue(result.dtype == np.float)
def m4MatrixRotation(axis, radian):
    return m4.create_from_axis_rotation(np.array(axis)*1.0, radian).transpose()


# """
#     Singleton instance logger
#      - Functions
#       - Enable automatic spacing depending on the hierarchy of function calls 
#        - Requires decorating functions with Logger.hierarchy() (i.e. @Logger.hierarchy)
#      - Purpose:
#       - Prevent unanticipated logs created by other modules, such as matplotlib
#      - Usage:
#       - Call the following two lines in the main file:
#        - Logger.setLevel(logging.DEBUG) # Set level
#          logging.log(logging.NOTSET, "") # To activate logging (Required for some reason)
# """
# class Logger():
#     _spaceLevel = 0
#     _instance = None
    
#     @staticmethod
#     def hierarchy(func):
#         """
#             Decorator
#         """
#         def __c(*args, **kwargs):
#             Logger.addSpace()
#             res = func(*args, **kwargs)
#             Logger.reduceSpace()
#             return res
#         return __c
    
#     @classmethod
#     def initLogger(cls):
#         if cls._instance is None:
#                 cls._instance = logging.getLogger()
    
#     @classmethod
#     def setLevel(cls, level):
#         cls._instance.setLevel(level)
    
#     @classmethod
#     def D(cls, s, extraSpace=0):
#         cls._instance.log(logging.DEBUG, cls.spaceStr(s, extraSpace))
        
#     @classmethod
#     def I(cls, s, extraSpace=0):
#         cls._instance.log(logging.INFO, cls.spaceStr(s, extraSpace))
        
#     @classmethod
#     def spaceStr(cls, s, extraSpace=0):
#         return f"{' '*(cls.spaceLevel+extraSpace)}{s}"
    
#     @classmethod    
#     def addSpace(cls):
#         cls.spaceLevel += 1
    
#     @classmethod    
#     def reduceSpace(cls):
#         cls.spaceLevel -= 1
        
#     @classmethod
#     def switchLogger(cls, name):
#         cls._instance = logging.getLogger(name)
    
#     @classmethod
#     def get(cls, name=None):
#         return logging.getLogger(name) if name else cls._instance
Beispiel #12
0
def main():
    global delta_time, last_frame, camera

    glfw.init()
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    window = glfw.create_window(width, height, "LearnOpenGL", None, None)
    if not window:
        print("Window Creation failed!")
        glfw.terminate()

    glfw.make_context_current(window)
    glfw.set_window_size_callback(window, on_resize)
    glfw.set_cursor_pos_callback(window, on_mouse_event)
    glfw.set_scroll_callback(window, on_mouse_scroll)

    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    gl.glEnable(gl.GL_DEPTH_TEST)
    camera = Camera(position=Vector3([0.0, 0.0, 3.0]))
    shader = Shader(CURDIR / 'shaders/7.1.camera.vs',
                    CURDIR / 'shaders/7.1.camera.fs')

    vertices = [
        # positions      tex_coords
        -0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        0.5,
        -0.5,
        -0.5,
        1.0,
        0.0,
        0.5,
        0.5,
        -0.5,
        1.0,
        1.0,
        0.5,
        0.5,
        -0.5,
        1.0,
        1.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        0.5,
        -0.5,
        0.5,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        1.0,
        0.5,
        0.5,
        0.5,
        1.0,
        1.0,
        -0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        -0.5,
        0.5,
        -0.5,
        1.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        1.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.5,
        0.5,
        -0.5,
        1.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        0.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        0.0,
        1.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        1.0,
        1.0,
        0.5,
        -0.5,
        0.5,
        1.0,
        0.0,
        0.5,
        -0.5,
        0.5,
        1.0,
        0.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        1.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.5,
        0.5,
        -0.5,
        1.0,
        1.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0
    ]
    vertices = (c_float * len(vertices))(*vertices)

    cube_positions = [(0.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -2.2, -2.5),
                      (-3.8, -2.0, -12.3), (2.4, -0.4, -3.5),
                      (-1.7, 3.0, -7.5), (1.3, -2.0, -2.5), (1.5, 2.0, -2.5),
                      (1.5, 0.2, -1.5), (-1.3, 1.0, -1.5)]

    vao = gl.glGenVertexArrays(1)
    gl.glBindVertexArray(vao)

    vbo = gl.glGenBuffers(1)
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
    gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(vertices), vertices,
                    gl.GL_STATIC_DRAW)

    gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE,
                             5 * sizeof(c_float), c_void_p(0))
    gl.glEnableVertexAttribArray(0)

    gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT,
                             gl.GL_FALSE, 5 * sizeof(c_float),
                             c_void_p(3 * sizeof(c_float)))
    gl.glEnableVertexAttribArray(1)

    # -- load texture 1
    texture1 = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture1)
    # -- texture wrapping
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_REPEAT)
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_REPEAT)
    # -- texture filterting
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)

    img = Image.open(Tex('container.jpg')).transpose(Image.FLIP_TOP_BOTTOM)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, img.width, img.height, 0,
                    gl.GL_RGB, gl.GL_UNSIGNED_BYTE, img.tobytes())
    gl.glGenerateMipmap(gl.GL_TEXTURE_2D)

    # -- load texture 2
    texture2 = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture2)
    # -- texture wrapping
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_REPEAT)
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_REPEAT)
    # -- texture filterting
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
    gl.glTexParameter(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)

    img = Image.open(Tex('awesomeface.png'))
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, img.width, img.height, 0,
                    gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, img.tobytes())
    gl.glGenerateMipmap(gl.GL_TEXTURE_2D)

    shader.use()
    shader.set_int("texture1", 0)
    shader.set_int("texture2", 1)

    projection = Matrix44.perspective_projection(45, width / height, 0.1,
                                                 100.0)
    shader.set_mat4('projection', projection)

    while not glfw.window_should_close(window):
        current_frame = glfw.get_time()
        delta_time = current_frame - last_frame
        last_frame = current_frame

        process_input(window)

        gl.glClearColor(.2, .3, .3, 1.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        gl.glActiveTexture(gl.GL_TEXTURE0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, texture1)
        gl.glActiveTexture(gl.GL_TEXTURE1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, texture2)

        shader.use()
        projection = Matrix44.perspective_projection(camera.zoom,
                                                     width / height, 0.1,
                                                     100.0)
        shader.set_mat4('projection', projection)

        view = camera.get_view_matrix()
        shader.set_mat4('view', view)

        gl.glBindVertexArray(vao)
        for idx, position in enumerate(cube_positions):
            angle = 20.0 * idx
            rotation = matrix44.create_from_axis_rotation([1.0, 0.3, 0.5],
                                                          math.radians(angle))
            translation = Matrix44.from_translation(position)
            model = translation * rotation
            shader.set_mat4('model', model)
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36)

        glfw.poll_events()
        glfw.swap_buffers(window)

    gl.glDeleteVertexArrays(1, id(vao))
    gl.glDeleteBuffers(1, id(vbo))
    glfw.terminate()
Beispiel #13
0
def main():
    global delta_time, last_frame

    glfw.init()
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    window = glfw.create_window(SRC_WIDTH, SRC_HEIGHT, "learnOpenGL", None,
                                None)
    if not window:
        glfw.terminate()
        raise ValueError("Failed to create window")

    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
    glfw.set_cursor_pos_callback(window, mouse_callback)
    glfw.set_scroll_callback(window, scroll_callback)

    glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    gl.glEnable(gl.GL_DEPTH_TEST)

    lamp_shader = Shader(CURDIR / "shaders/1.lamp.vs",
                         CURDIR / "shaders/1.lamp.fs")
    lighting_shader = Shader(CURDIR / "shaders/5.3.light_casters.vs",
                             CURDIR / "shaders/5.3.light_casters.fs")

    vertices = [
        # positions        normals           texture coords
        -0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        0.0,
        0.0,
        0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        1.0,
        0.0,
        0.5,
        0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        1.0,
        1.0,
        0.5,
        0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        1.0,
        1.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        0.0,
        -1.0,
        0.0,
        0.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        1.0,
        0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        1.0,
        -0.5,
        0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        -1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        0.5,
        -0.5,
        -1.0,
        0.0,
        0.0,
        1.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        -1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        -0.5,
        -1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        -0.5,
        -0.5,
        0.5,
        -1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        -1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.5,
        -0.5,
        1.0,
        0.0,
        0.0,
        1.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        1.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.5,
        -0.5,
        0.5,
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.5,
        0.5,
        0.5,
        1.0,
        0.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        -1.0,
        0.0,
        0.0,
        1.0,
        0.5,
        -0.5,
        -0.5,
        0.0,
        -1.0,
        0.0,
        1.0,
        1.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        -1.0,
        0.0,
        1.0,
        0.0,
        0.5,
        -0.5,
        0.5,
        0.0,
        -1.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        -0.5,
        0.5,
        0.0,
        -1.0,
        0.0,
        0.0,
        0.0,
        -0.5,
        -0.5,
        -0.5,
        0.0,
        -1.0,
        0.0,
        0.0,
        1.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0,
        0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.0,
        1.0,
        1.0,
        0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        0.5,
        0.5,
        0.0,
        1.0,
        0.0,
        0.0,
        0.0,
        -0.5,
        0.5,
        -0.5,
        0.0,
        1.0,
        0.0,
        0.0,
        1.0
    ]
    vertices = (c_float * len(vertices))(*vertices)

    cube_positions = [(0.0, 0.0, 0.0), (2.0, 5.0, -15.0), (-1.5, -2.2, -2.5),
                      (-3.8, -2.0, -12.3), (2.4, -0.4, -3.5),
                      (-1.7, 3.0, -7.5), (1.3, -2.0, -2.5), (1.5, 2.0, -2.5),
                      (1.5, 0.2, -1.5), (-1.3, 1.0, -1.5)]

    cube_vao = gl.glGenVertexArrays(1)
    vbo = gl.glGenBuffers(1)

    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
    gl.glBufferData(gl.GL_ARRAY_BUFFER, sizeof(vertices), vertices,
                    gl.GL_STATIC_DRAW)

    gl.glBindVertexArray(cube_vao)

    # -- position attribute
    gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE,
                             8 * sizeof(c_float), c_void_p(0))
    gl.glEnableVertexAttribArray(0)
    # -- normal attribute
    gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT,
                             gl.GL_FALSE, 8 * sizeof(c_float),
                             c_void_p(3 * sizeof(c_float)))
    gl.glEnableVertexAttribArray(1)
    # -- texture coordinate
    gl.glVertexAttribPointer(2, 2, gl.GL_FLOAT,
                             gl.GL_FALSE, 8 * sizeof(c_float),
                             c_void_p(6 * sizeof(c_float)))
    gl.glEnableVertexAttribArray(2)

    # -- second configure light vao (vbo is the same)
    light_vao = gl.glGenVertexArrays(1)
    gl.glBindVertexArray(light_vao)

    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
    gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE,
                             8 * sizeof(c_float), c_void_p(0))
    gl.glEnableVertexAttribArray(0)

    # -- load texture
    diffuse_map = load_texture("container2.png")
    specular_map = load_texture("container2_specular.png")

    # -- shader configuration
    lighting_shader.use()
    lighting_shader.set_int("material.diffuse", 0)
    lighting_shader.set_int("material.specular", 1)

    while not glfw.window_should_close(window):
        # -- time logic
        current_frame = glfw.get_time()
        delta_time = current_frame - last_frame
        last_frame = current_frame

        # -- input
        process_input(window)

        # -- render
        gl.glClearColor(0.1, 0.1, 0.1, 1.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

        lighting_shader.use()
        lighting_shader.set_vec3("light.position", camera.position)
        lighting_shader.set_vec3("light.direction", camera.front)
        lighting_shader.set_float("light.cutOff", math.cos(math.radians(12.5)))
        lighting_shader.set_vec3("viewPos", camera.position)

        # -- light properties
        lighting_shader.set_vec3("light.ambient", Vector3([0.1, 0.1, 0.1]))
        # we configure the diffuse intensity slightly higher; the right lighting conditions differ with each lighting method and environment.
        # each environment and lighting type requires some tweaking to get the best out of your environment.
        lighting_shader.set_vec3("light.diffuse", Vector3([0.8, 0.8, 0.8]))
        lighting_shader.set_vec3("light.specular", Vector3([1.0, 1.0, 1.0]))
        lighting_shader.set_float("light.constant", 1.0)
        lighting_shader.set_float("light.linear", 0.09)
        lighting_shader.set_float("light.quadratic", 0.032)

        # -- material properties
        lighting_shader.set_float("material.shininess", 32.0)

        # -- view.projection transformations
        projection = Matrix44.perspective_projection(camera.zoom,
                                                     SRC_WIDTH / SRC_HEIGHT,
                                                     0.1, 100.0)
        view = camera.get_view_matrix()
        lighting_shader.set_mat4("projection", projection)
        lighting_shader.set_mat4("view", view)

        # -- world transformation
        model = Matrix44.identity()
        lighting_shader.set_mat4("model", model)

        # -- bind diffuse map
        gl.glActiveTexture(gl.GL_TEXTURE0)
        gl.glBindTexture(gl.GL_TEXTURE_2D, diffuse_map)

        # -- bind specular map
        gl.glActiveTexture(gl.GL_TEXTURE1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, specular_map)

        # -- render continers
        gl.glBindVertexArray(cube_vao)
        for idx, position in enumerate(cube_positions):
            angle = 20.0 * idx
            rotation = matrix44.create_from_axis_rotation([1.0, 0.3, 0.5],
                                                          math.radians(angle))
            translation = Matrix44.from_translation(position)
            model = translation * rotation
            lighting_shader.set_mat4('model', model)
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36)

        # # -- draw lamp object
        # lamp_shader.use()
        # lamp_shader.set_mat4("projection", projection)
        # lamp_shader.set_mat4("view", view)

        # model = Matrix44.identity()
        # model *= Matrix44.from_translation(light_pos)
        # model *= Matrix44.from_scale(Vector3([.2, .2, .2]))
        # lamp_shader.set_mat4("model", model)

        # gl.glBindVertexArray(light_vao)
        # gl.glDrawArrays(gl.GL_TRIANGLES, 0, 36)

        glfw.swap_buffers(window)
        glfw.poll_events()

    gl.glDeleteVertexArrays(1, id(cube_vao))
    gl.glDeleteVertexArrays(1, id(light_vao))
    gl.glDeleteBuffers(1, id(vbo))
    glfw.terminate()
def m4MatrixRotation(axis, radian):
    return m4.create_from_axis_rotation(np.array(axis) * 1.0,
                                        radian).transpose()
Beispiel #15
0
 def rotate_axis(self, axis, angle):
     """Helper function that multiplies the `model_matrix` with a rotation
     matrix around the passed in axis."""
     m = matrix44.create_from_axis_rotation(axis, angle)
     self.model_matrix = m.dot(self.model_matrix)
Beispiel #16
0
 def rotate(self, ang, vect):
     rotate = mat4.create_from_axis_rotation(vect, ang)
     self.model = mat4.multiply(self.model, rotate)