openglmatlabpsychtoolbox

How to magnify/stretch a texture with Matlab Psychtoolbox (OpenGL)?


Update: This only seems to be a problem at some computers. The normal, intuitive code seems to work fine one my home computer, but the computer at work has trouble.

Home computer: (no problems)

Work computer: (this question applies to this computer)

Original post:

I'm trying to apply a very simple texture to a part of the screen using Psychtoolbox in Matlab with the following code:

win = Screen('OpenWindow', 0, 127); % open window and obtain window pointer
tex = Screen('MakeTexture', win, [255 0;0 255]); % get texture pointer
% draw texture. Args: command, window pointer, texture pointer, source
% (i.e. the entire 2x2 matrix), destination (a 100x100 square), rotation
% (none) and filtering (nearest neighbour)
Screen('DrawTexture', win, tex, [0 0 2 2], [100 100 200 200], 0, 0);
Screen('Flip', win); % flip the buffer so the texture is drawn
KbWait; % wait for keystroke
Screen('Close', win); % close screen

Now I would expect to see this (four equally sized squares):

Expected result

But instead I get this (right and bottom sides are cut off and top left square is too large):

Actual result

Obviously the destination rectangle is a lot bigger than the source rectangle, so the texture needs to be magnified. I would expect this to happen symmetrically like in the first picture and this is also what I need. Why is this not happening and what can I do about it?

I have also tried using [128 0 1152 1024] as a destination rectangle (as it's the square in the center of my screen). In this case, all sides are 1024, which makes each involved rectangle a power of 2. This does not help. Increasing the size of the checkerboard results in a similar situation where the right- and bottommost sides are not showed correctly.

Like I said, I use Psychtoolbox, but I know that it uses OpenGL under the hood. I don't know much about OpenGL either, but maybe someone who does can help without knowing Matlab. I don't know.

Thanks for your time!


Solution

  • Without knowing a lot about the Psychtoolbox, but having dealt with graphics and user interfaces a lot in MATLAB, the first thing I would try would be to fiddle with the fourth input to Screen (the "source" input). Try shifting each corner by half-pixel and whole-pixel values. For example, the first thing I would try would be:

    Screen('DrawTexture', win, tex, [0 0 2.5 2.5], [100 100 200 200], 0, 0);
    

    And if that didn't seem to do anything, I would next try:

    Screen('DrawTexture', win, tex, [0 0 3 3], [100 100 200 200], 0, 0);
    

    My reasoning for this advice: I've noticed sometimes that images or GUI controls in my figures can appear to be off by a pixel, which I can only speculate is some kind of round-off error when scaling or positioning them.

    That's the best advice I can give. Hope it helps!