OpenGL映射到Psychtoolbox(像素)坐标

时间:2017-04-28 09:35:34

标签: matlab opengl coordinates psychtoolbox

我前段时间问过similar question,但我想我现在可以更好地制定它了。

下面的代码(我正在调整,而不是从头开始编写)在正确的位置绘制一个白色条,并且具有正确的角度,如下图所示。它使用Psychtoolbox中的OpenGL命令(在Matlab上运行)。 Psychtoolbox坐标非常简单且基于像素:(0,0)位于左上角。 Y向下增加,x向右增加。

我无法理解OpenGL和Psychtoolbox坐标之间的映射。特别是,行glTranslatef( SkitSet.xLever, SkitSet.yLever, 0 );将白条向下翻转1.5个单位。这些单位是什么?我在哪里告诉matlab OpenGL中的1.5个单位大约是垂直屏幕的一半?

%% Psychtoolbox settings
whichScreen  = 0;
Screen('Preference', 'SkipSyncTests', 1); %1->without testing
inScreenRect = [0 0 500 500];


% Open window and specify some OpenGL settings
InitializeMatlabOpenGL( [], [], [], 0 );
[win , winRect] = PsychImaging( 'OpenWindow', whichScreen, 0, inScreenRect, [], [], 0, 0 );
Screen( 'BeginOpenGL', win );
aspectRatio = winRect( 4 ) / winRect( 3 );                       %% Get the aspect ratio of the screen
[winCenter.x, winCenter.y] = RectCenter(winRect);        %find the center of the screen
glMatrixMode( GL.PROJECTION );
glLoadIdentity;
gluPerspective( 25, 1 / aspectRatio, 0.1, 100 );
glMatrixMode( GL.MODELVIEW );
glLoadIdentity;
gluLookAt( 0, 0, 8, 0, 0, 0, 0, 1, 0);          %Set view point, center and direction
glClearColor( 0, 0, 0, 0 );
Screen( 'EndOpenGL', win );

SkitSet.xLever          = 0;        
SkitSet.yLever          = -1.5000;
SkitSet.LeverLength     = 0.4000;
SkitSet.LeverWidth      = 0.0400;


Screen( 'BeginOpenGL', win );
glClear;
glPushMatrix;
glTranslatef( SkitSet.xLever, SkitSet.yLever, 0 );
glRotatef( -45, 0.0, 0.0, 1.0 );                                         
glRectf( -SkitSet.LeverLength, -SkitSet.LeverWidth / 2, 0, SkitSet.LeverWidth / 2 );
glPopMatrix;
Screen( 'EndOpenGL', win );
Screen( 'Flip', win, [], [], 0 );                                   

Result of minimal code

1 个答案:

答案 0 :(得分:0)

这是一个OpenGL问题,而不是任何Psychtoolbox特定的问题。你正在设置投影平截头体。因此,您绘制的任何刺激都将使用您设置的投影和模型视图进行投影。这些单位是任意的,但有助于将它们考虑在内,例如米。如果gluPerspective中的zNear和zFar以米为单位,则glTranslatefglRectf等所有其他调用也会在同一单位中获取输入参数。如果你想在屏幕的一半处移动东西,你必须计算在Y和Z中的位置,给定你在gluPerspective中指定的FOV。如果您只是绘制一个杠杆,您确定要设置自己的投影和模型视图矩阵吗? Psychtoolbox默认为您提供技巧,然后您也可以在OpenGL模式下处理单个像素。如果PTB默认不起作用,则使用窗口rect作为四个顶点设置gluortho2d