Python教程

Blinn-Phong 光照模型 Demo (Python)

本文主要是介绍Blinn-Phong 光照模型 Demo (Python),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

image

import numpy as np
from matplotlib import pyplot as plt

for _i in range(4):
    for _j in range(4):

        a = 100
        mid = a//2
        img_w = a
        img_h = a
        r = a * 0.4

        img = [[0 for j in range(img_w)] for i in range(img_h)]

        l = np.array([-1., 1., 1.])
        l /= np.linalg.norm(l, ord=2)

        e = np.array([0, 0, 1])

        diff = 0.3
        spec = _i * 0.2
        spow = _j * _j + 1
        ambi = 0.1

        for i in range(img_h):
            for j in range(img_w):
                x = (j-mid) / r
                y = -(i-mid) / r
                if x*x+y*y < 1:
                    z = (1-x**2-y**2)**0.5
                    n = np.array([x, y, z])
                    h = l+e
                    h /= np.linalg.norm(h, ord=2)
                    c = ambi + diff * max(0, np.dot(n, l)) + \
                        spec * (max(0, np.dot(n, h)) ** spow)
                    img[i][j] = c
        plt.subplot(4,4,_i*4+_j+1)
        plt.imshow(img, cmap='gray', vmin = 0, vmax = 1)

plt.show()

image

这篇关于Blinn-Phong 光照模型 Demo (Python)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!