matlab practice program (rendering three primary colors)

The space I use here is that x is positive to the right, y is positive downward, and z is positive to the inside of the screen. Equivalent to a standard right-handed system rotated 180 degrees around the x-axis.

Place three point lights on

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)];

At these three positions, light is emitted around, and the inverse of the modulus of the light to the yz plane is taken as the intensity of the light.

The image is as follows:

The procedure is as follows:

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')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324862367&siteId=291194637