matlab练习程序(渲染三原色)

这里我用的空间是x向右为正,y向下为正,z向屏幕里面为正。相当于标准右手系绕x轴旋转了180度。

将三个点光源放在

r = [0.3,0,0.5];
g = [0.3,-0.5*cos(pi/6),-0.5*sin(pi/6)];
b = [0.3,0.5*cos(pi/6),-0.5*sin(pi/6)];

这三个位置上,向四周发射光线,取光线到y-z平面的模的倒数作为光的强度。

图像如下:

matlab练习程序(渲染三原色)_第1张图片

程序如下:

clear all;
close all;
clc;

r = [0.3,0,0.5];
g = [0.3,-0.5*cos(pi/6),-0.5*sin(pi/6)];
b = [0.3,0.5*cos(pi/6),-0.5*sin(pi/6)];
imgr1=[];imgr2=[];
imgg1=[];imgg2=[];
imgb1=[];imgb2=[];
for y=-1:0.004:1
   imgr1=[];imgg1=[];imgb1=[];
   for z=-1:0.004:1       
       pm=[0,y,z];         
       lightr=pm-r;
       lightg=pm-g;
       lightb=pm-b;
       imgr=1/norm(lightr);
       imgg=1/norm(lightg);
       imgb=1/norm(lightb);
       
       imgr1=[imgr1 imgr];
       imgg1=[imgg1 imgg];      
       imgb1=[imgb1 imgb];           
   end
   imgr2=[imgr2;imgr1];
   imgg2=[imgg2;imgg1];  
   imgb2=[imgb2;imgb1]; 
end

[h w]=size(imgr2);

img=zeros(h,w,3);
img(:,:,1) = mat2gray(imgr2);
img(:,:,2) = mat2gray(imgg2);
img(:,:,3) = mat2gray(imgb2);
imshow(img)
imwrite(img,'img.png')

 

转载于:https://www.cnblogs.com/tiandsp/p/8947429.html

你可能感兴趣的:(matlab练习程序(渲染三原色))