前段时间我问了一个类似的问题,但我想我现在可以更好地表述它。
下面的代码(我正在改编,而不是从头开始编写)在正确的位置和正确的角度绘制了一个白条,如下图所示。它使用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 );