Question

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)

  • Windows XP Professional SP3
  • AMD Athlon 64 X2 3800+ Dual Core 2.0 GHz
  • NVIDIA GeForce 7800 GT
  • 2 GB RAM

Work computer: (this question applies to this computer)

  • Windows XP Professional SP3
  • Intel Pentium 4 2.8 Ghz (dual core, I think)
  • Intel 82945G Express Chipset Family
  • 1 GB RAM

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!

Was it helpful?

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!

OTHER TIPS

While I don't know much (read: any) Matlab, I do know that textures are very picky in openGL. Last I checked, openGL requires texture files to be square and of a power of two (i.e. 128 x 128, 256 x 256, 512 x 512).

If they aren't, openGL is supposed to pad the file with appropriate white pixels where they're needed to meet this condition, although it could be a crapshoot depending on which system you are running it on.

I suggest making sure that your checkerboard texture fits these requirements.

Also, I can't quite make sure from your code posted, but openGL expects you to map the corners of your texture to the corners of the object you are intending to texture.

Another bit of advice, maybe try a linear filter instead of nearest neighbor. It's heavier computationally, but results in a better image. This probably won't matter in the end.

While this help is not Matlab specific, hope it is useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top