如何使用 Matlab Psychtoolbox (OpenGL) 放大/拉伸纹理?

发布于 2024-07-18 08:02:07 字数 1747 浏览 8 评论 0原文

更新:这似乎只是某些计算机上的问题。 正常、直观的代码在我的家用计算机上似乎运行良好,但在工作计算机上却出现了问题。

家用计算机:(没问题)

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

工作计算机:(此问题适用于这台计算机)

  • Windows XP Professional SP3
  • Intel Pentium 4 2.8 Ghz(双核 )核心,我认为)
  • 英特尔 82945G Express 芯片组系列
  • 1 GB RAM

原始帖子:

我正在尝试使用 Matlab 中的 Psychtoolbox 并使用以下代码将非常简单的纹理应用到屏幕的一部分:

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

现在我希望看到这个(四个大小相等的方块):

预期结果

但我得到了这个(右侧和底部被切掉,左上角的方块太大):

实际结果

显然目标矩形比源矩形大很多,因此需要放大纹理。 我希望这会像第一张图片一样对称地发生,这也是我所需要的。 为什么这种情况没有发生?我能做什么?

我还尝试使用 [128 0 1152 1024] 作为目标矩形(因为它是屏幕中心的正方形)。 在本例中,所有边都是 1024,这使得每个涉及的矩形都是 2 的幂。这没有帮助。 增加棋盘的大小会导致类似的情况,即右侧和最底侧无法正确显示。

就像我说的,我使用 Psychtoolbox,但我知道它在幕后使用 OpenGL。 我对 OpenGL 也不太了解,但也许了解 OpenGL 的人可以在不了解 Matlab 的情况下提供帮助。 我不知道。

谢谢你的时间!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-07-25 08:02:07

虽然我对 Matlab 了解不多(阅读:任何),但我确实知道 openGL 中的纹理非常挑剔。 上次我检查过,openGL 要求纹理文件是正方形且是 2 的幂(即 128 x 128、256 x 256、512 x 512)。

如果不是,openGL 应该在需要满足此条件的地方用适当的白色像素填充文件,尽管这可能会很冒险,具体取决于您运行它的系统。

我建议确保您的棋盘纹理符合这些要求。

另外,我不能从你发布的代码中完全确定,但 openGL 希望你将纹理的角映射到你想要纹理的对象的角。

另一条建议是,也许尝试使用线性滤波器而不是最近邻滤波器。 它的计算量较大,但会产生更好的图像。 这最终可能并不重要。

虽然此帮助不是 Matlab 特定的,但希望它有用。

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.

黎歌 2024-07-25 08:02:07

我对 Psychtoolbox 不太了解,但在 MATLAB 中处理过很多图形和用户界面,我首先尝试的就是摆弄 Screen 的第四个输入(“源”输入) )。 尝试将每个角移动半像素和全像素值。 例如,我会尝试的第一件事是:

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

如果这似乎没有任何作用,我接下来会尝试:

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

我对这个建议的推理:我注意到有时我的图中的图像或 GUI 控件可能会出现偏离一个像素,我只能推测这是缩放或定位它们时的某种舍入误差。

这是我能给出的最好建议。 希望能帮助到你!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文