function rjmat2pov(filename,varargin)
%Currently the colormap is stored for every surface
%If only one surface object has many colors this can produce rather large
%files. It is not hard in principle to alter rjfindlist to accomodate this.


default={
    'noshadowlist',[];     %Vector containing handles to objects casting no shadow
    'noreflectionlist',[]   %Vector containing handles to objects casting no reflection  
	};

eval(setvalues)


campos=get(gca,'CameraPosition');
camtarget=get(gca,'CameraTarget');
angle=1.8*get(gca,'CameraViewAngle');
camup=get(gca,'CameraUpVector');

forward=(camtarget-campos);
temp=cross(forward,camup);     %Changed this after thesis from temp=cross(camtarget,camup);
rightvec=4/3*temp/norm(temp);  %Flipping sign changes nothing for some
                               %reason - but works like I want it to anyway
surfhandles=findobj(gca,'Type','surface','Visible','on');
lighthandles=findobj(gca,'Type','light');

rjfid=fopen([filename '.pov'],'w');  %Changed this after thesis to add '.pov'

%%%%%%%%%%%%%%%%%%%%%%%a=num2str('0.0');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%          The header                 %%%%%%%%%%%%%%%%%%                  

fprintf(rjfid,'#include "colors.inc" \n\n');
fprintf(rjfid,'background { color White }\n\n');
fprintf(rjfid,'camera {\n');
fprintf(rjfid,'location <%.8f,%.8f,%.8f> \n',campos');
fprintf(rjfid,'right <%.8f,%.8f,%.8f> \n',rightvec');
fprintf(rjfid,'up <%.8f,%.8f,%.8f> \n',camup');
fprintf(rjfid,'sky <%.8f,%.8f,%.8f> \n',camup');
fprintf(rjfid,'angle %.8f\n',angle);
fprintf(rjfid,'look_at  <%.8f,%.8f,%.8f> \n',camtarget');
fprintf(rjfid,'}\n\n');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%        The surfaces                 %%%%%%%%%%%%%%%%%%

for i=1:length(surfhandles)
    h=surfhandles(i)
    
    %Below ifcase inserted after thesis was printed
    if isequal(get(h,'Cdatamapping'),'scaled')
	cdat=get(h,'Cdata');
	cdat=cdat-min(get(gca,'Clim'));
	cdat=cdat/(max(get(gca,'Clim'))-min(get(gca,'Clim')));
	cny=ones(size(cdat));
	disp('Converting from Cdatamapping scaled to Cdatamapping direct')
	disp('This may affect the matlab image')
	disp('There is a more clever way of doing this :)')
	for ci=1:size(cdat,1)
	    for cj=1:size(cdat,2)
		cny(ci,cj)=floor(cdat(ci,cj)*(size(colormap,1)-1))+1;
	    end
	end
	set(h,'Cdata',cny);
    end

    a=num2str(0.3*get(h,'AmbientStrength'));
    b=num2str(0.6*get(h,'DiffuseStrength'));
    c=num2str(0.4*get(h,'SpecularStrength'));
    d=num2str(get(h,'SpecularExponent'));
    e=num2str(get(h,'SpecularColorReflectance'));
    p='0.7';
    ps=num2str('7');    %I wonder if phong size is related to specular exponent
    
    %a='0.00';
    %b='0.00';
    %c='0.7';
    %d='10';
    %e='1';
    
    fin_string=[' finish{reflection {' c ' exponent ' d  ' metallic ' e  '}'...
		 ' phong ' p  ' phong_size ' ps  ' ambient '  a ' diffuse ' b '}'];
 
    %I don't know how to set the reflection properties for the whole
    %object. So I specify them in the colorlist below.
    
    incell=rjfindlist(get(h,'X'),get(h,'Y'),get(h,'Z'),get(h,'Cdata'));
    vertex=incell{1};
    index=incell{2};
    normal=incell{3};
    n_vertex=size(vertex,1);

    fprintf(rjfid,'\n\nmesh2 { \n');
    
    fprintf(rjfid,'vertex_vectors { \n %g',n_vertex);
    fprintf(rjfid,',\n <%.8f,%.8f,%.8f>',vertex');
    fprintf(rjfid,'\n}\n\n');
    
    fprintf(rjfid,'normal_vectors { \n %g',n_vertex);
    fprintf(rjfid,',\n <%.8f,%.8f,%.8f>',normal');
    fprintf(rjfid,'\n}\n\n');
    
    fprintf(rjfid,'texture_list { \n %g,',size(colormap,1));
    fprintf(rjfid,['\n texture{pigment{rgb<%.7f,%.7f,%.7f >}' fin_string '}'],colormap');
    fprintf(rjfid,'\n}\n\n');
    
    fprintf(rjfid,'face_indices { \n %g',size(index,1));
    fprintf(rjfid,',\n <%g,%g,%g>,%g',index');
    fprintf(rjfid,'\n}\n');
    
    for sii=1:length(noshadowlist)
	if isequal(h,noshadowlist(sii))
	    disp('Setting no_shadow')
	    fprintf(rjfid,'no_shadow \n\n');
	end
    end
    
    for sjj=1:length(noreflectionlist)
	if isequal(h,noshadowlist(sjj))
	    disp('Setting no_reflection')
	    fprintf(rjfid,'no_reflection \n\n');
	end
    end
    
    fprintf(rjfid,'\n}\n\n');    
end

fprintf(rjfid,'global_settings { ambient_light rgb<1, 1, 1> }\n\n')

for i=1:length(lighthandles)
    h=lighthandles(i);
    if isequal(get(h,'Style'),'infinite')
	fprintf(rjfid,'light_source {<%.8f,%.8f,%.8f> color White parallel point_at <0,0,0>}\n\n', ...
		get(h,'Position'))
    else
	fprintf(rjfid,'light_source {<%.8f,%.8f,%.8f> color White}\n\n', ...
		get(h,'Position'))
    end
end

fclose(rjfid);
    
    




















