代码:
global screens screenNumber win wsize flipIntv cx cy time_stamp
try
HideCursor; %隐藏鼠标
InitializeMatlabOpenGL; %图像初始化
Screen('Preference','SkipSyncTests',1); %跳过PTB刷新检测
screens = Screen('Screens'); %查找现有屏幕 一般为0
screenNumber = max(screens); %显示器编号 0开始
[win,wsize] = Screen('OpenWindow',screenNumber);%获取窗口信息保存在windowPtr类型指针win中 进行窗口操作
%wsize保存[0,0,xx,xx]四个值的变量后两位表示屏幕横竖轴分辨率
cx = wsize(3)/2; %取屏幕横轴中点
cy = wsize(4)/2; %取屏幕竖轴中点
flipIntv = Screen('GetFlipInterval',win); %获取屏幕刷新率 单位为秒 控制时间
Screen('FillRect',win,128); %绘制矩形灰色(中性色)方块 填满屏幕
time_stamp = Screen('Flip',win); %Flip用于时间控制 画面跳转
pixs = deg2pix(5,15.5,wsize(3),50); %5为图片边缘距离中点范围在人眼中的角度 获取距离人眼50cm处图片像素的大小 函数在下方 15.5为现使用电脑屏幕英寸
color_feature = [140,30,30]; %设置RGB颜色
Screen('FillRect',win,128); %绘制矩形灰色(中性色)方块 填满屏幕
Screen('DrawLine',win,color_feature,cx-pixs,cy,cx+pixs,cy,1); %画线
time_stamp = Screen('Flip',win,time_stamp+2); %time_stamp+2后跳转到画线屏幕
%Screen('DrawLine',windowPtr [,color],formH,fromV,toH,toV [,penWidth])
%一个变粗的线条
for width_now =1:7
Screen('FillRect' ,win ,128);
Screen('Drawline' ,win ,color_feature,cx-pixs,cy,cx+pixs,cy,width_now);
time_stamp = Screen('Flip',win ,time_stamp+1);
end
%画一个小方块
Screen('FillRect' ,win ,128);
Screen('FillRect' ,win ,color_feature,[cx-pixs,cy-pixs,cx+pixs,cy+pixs])
%Screen('FillRect',windowPtr [,color][,rect]);
time_stamp = Screen('Flip',win,time_stamp+2);
%画一个空心方块
Screen('FillRect' ,win ,128);
Screen('FrameRect',win ,color_feature,[cx-pixs,cy-pixs,cx+pixs,cy+pixs])
time_stamp = Screen('Flip',win,time_stamp+2);
%Screen(%FrameRect,windowPtr [,color] [,rect] [,penWidth]);
%空心与实心的语法差别不大,基本就剩Fill和Frame的差异
% 画圆形
Screen('FillRect' ,win ,128);
Screen('FillOval' ,win ,color_feature,[cx-pixs,cy-pixs,cx+pixs,cy+pixs]);
%Screen('FillOval',windowPtr [,color] [,rect]
%[.perfectUpToMaxDiameter]);
time_stamp = Screen('Flip' ,win ,time_stamp+2);
% 打字
txt = 'Welcome to MATLAB Tutorial by WangZeyi...';
bRect = Screen('TextBounds' ,win ,txt);%返回写这么多字需要多大位置
%[normBoundsRect,offsetBoundsRect,textHeight,xAdvance] = Screen('TextBounds',windowPtr,text[,x][,y][,yPositionlsBaseline][,swapTextDirection]);
Screen('DrawText' ,win ,txt,cx-bRect(3)/2,cy-bRect(4)/2,255);
%[newX,newY,textHeight] = Screen('DrawText',widowPtr,text,[,x][,y][,color][,backgroundColor][,yPositionlsBaseline][,swapTextDirection]);
time_stamp = Screen('Flip', win ,time_stamp+2);
% 画图
%加载图片
cd Screen_tutorial/images/ %本脚本与Screen_tutroial文件夹在同一目录下
files = dir('*tif*'); %读取后缀为tiff的文件 file为18*1的struct images文件夹内含18张图
image_loaded = imread(files(1).name);
imshow(image_loaded); %展示图片
Image_Index = Screen('MakeTexture' ,win ,image_loaded);
%textureIndex = Screen('MakeTexture',WindowIndex,imageMatrix,[,optimizeForDrawAngle=0][,special=0][,floatprecision][,textureOrientation=0][,textureShader=0]);
Screen('DrawTexture',win,Image_Index,[],[cx-pixs,cy-pixs,cx+pixs,cy+pixs]);
%Screen(‘DrawTexture’, windowPointer, texturePointer [,sourceRect] [,destinationRect] [,rotationAngle] [, filterMode] [, globalAlpha] [, modulateColor] [, textureShader] [, specialFlags] [, auxParameters]);
time_stamp = Screen('Flip',win,time_stamp+2);
time_stamp = Screen('Flip',win,time_stamp+3);
Screen('CloseAll');
catch
sca;
end
function pixs = deg2pix(degree,inch,pwidth,vdist)
screenWidth = inch*2.54/sqrt(1+9/16);
pix = screenWidth/pwidth;
pixs = round(2*tan((degree/2)*pi/180)*vdist/pix);%计算屏幕像素值
end