纹理显示不正确 - 可能坐标错误OpenGL,C ++ [英] Texture not displaying properly - probably coordinates are wrong OpenGL, C++

查看:223
本文介绍了纹理显示不正确 - 可能坐标错误OpenGL,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会试着解释我的问题与图像。所以这是一个我用于OpenGL应用程序的测试纹理:



我使用9格图案,因此我绘制了9个具有特定纹理坐标的四边形。乍看之下,一切正常,但显示纹理时存在一个小问题:


在图片中,我标记了哪里是第一个四分之一,哪里是第二个。正如你所看到的,第一个显示正确,但第二个顺利地从第一个四色的颜色到它自己,但它应该从纯绿色和粉红色开始。所以我猜这是纹理坐标的问题。



这是它们的设置方式:

  //左下四边[1st quad] 
glBegin(GL_QUADS);
//左下角
glTexCoord2f(0.0f,1.0);
glVertex2i(pos.x,pos.y + height);

//左上角
glTexCoord2f(0.0f,(GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x,pos.y + height - m_borderWidth);

//右上角
glTexCoord2f(maxTexCoordBorderX,(GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x + m_borderWidth,pos.y + height - m_borderWidth);

//右下
glTexCoord2f(maxTexCoordBorderX,1.0);
glVertex2i(pos.x + m_borderWidth,pos.y + height);
glEnd();

//底部中间四边形[2nd quad]
glBegin(GL_QUADS);
//左下
glTexCoord2f(maxTexCoordBorderX,1.0);
glVertex2i(pos.x + m_borderWidth,pos.y + height);

//左上角
glTexCoord2f(maxTexCoordBorderX,(GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x + m_borderWidth,pos.y + height - m_borderWidth);

//右上
glTexCoord2f((GLfloat)1.0 - maxTexCoordBorderX,(GLfloat)1.0 - maxTexCoordBorderY);
glVertex2i(pos.x + width - m_borderWidth,pos.y + height - m_borderWidth);

//右下
glTexCoord2f((GLfloat)1.0 - maxTexCoordBorderX,1.0);
glVertex2i(pos.x + width - m_borderWidth,pos.y + height);
glEnd();

您可以看到我正在使用 maxTexCoordBorderX 根据边框和图像大小计算的变量。图片宽度为32,边框宽度为2.



maxTexCoordBorderX = 2/32 = 0.0625



任何人都可以帮忙找出问题出在哪里?

解决方案

罪魁祸首就是你没有在texel中心抽样。例如,如果您的像素纹理为32x32像素,则纹理中心会偏移1/64。



下面是4x4纹理的粗略图。正方形是图像的纹理像素(或像素)。

 
_________________ 1,1
| | | | |
| | | | |
| ___ | ___ | ___ | ___ | _0.75
| | | | |
| | | | |
| ___ | ___ | ___ | ___ | _0.5
| | | | |
| | | | |
| ___ | ___ | ___ | ___ | _0.25
| | | | |
| X | | | |
| ___ | ___ | ___ | ___ |
0,0 | 0.5 | 1
0.25 0.75

x =(0.125,0.125)

如果您抽样在其中一行上,您将得到一个恰好在两个纹素之间的值,这将会(如果您将纹理采样设置为线性混合)给您一个平均值。如果你想采样确切的texel值,你需要在texel的中心指定一个u,v。


I'll try to explain my problem with images. So this is a test texture I'm using for my OpenGL application:

As you can see, there is a 2-pixels wide border around the image with different colors for me to be able to see if coordinates are properly set in my application.

I'm using a 9-cell pattern so I'm drawing 9 quads with specific texture coordinates. At first sight everything works fine, but there is a small problem with displaying a texture:

In the picture I marked where is first quad, and where is the second one. As you can see, first one is displayed correctly, but second one smoothly goes from first quad's colors to it's own, but it should start with pure green and pink. So I'm guessing it's a problem with texture coordinates.

This is how they are set:

// Bottom left quad [1st quad]
glBegin(GL_QUADS);
    // Bottom left
    glTexCoord2f(0.0f, 1.0);                
    glVertex2i(pos.x, pos.y + height);

    // Top left
    glTexCoord2f(0.0f, (GLfloat)1.0 - maxTexCoordBorderY);  
    glVertex2i(pos.x, pos.y + height - m_borderWidth);

    // Top right
    glTexCoord2f(maxTexCoordBorderX, (GLfloat)1.0 - maxTexCoordBorderY);
    glVertex2i(pos.x + m_borderWidth, pos.y + height - m_borderWidth);

    // Bottom right
    glTexCoord2f(maxTexCoordBorderX, 1.0);
    glVertex2i(pos.x + m_borderWidth, pos.y + height);
glEnd();

// Bottom middle quad [2nd quad]
glBegin(GL_QUADS);
    // Bottom left
    glTexCoord2f(maxTexCoordBorderX, 1.0);          
    glVertex2i(pos.x + m_borderWidth, pos.y + height);

    // Top left
    glTexCoord2f(maxTexCoordBorderX, (GLfloat)1.0 - maxTexCoordBorderY);
    glVertex2i(pos.x + m_borderWidth, pos.y + height - m_borderWidth);

    // Top right
    glTexCoord2f((GLfloat)1.0 - maxTexCoordBorderX, (GLfloat)1.0 - maxTexCoordBorderY);
    glVertex2i(pos.x + width - m_borderWidth, pos.y + height - m_borderWidth);

    // Bottom right
    glTexCoord2f((GLfloat)1.0 - maxTexCoordBorderX, 1.0);   
    glVertex2i(pos.x + width - m_borderWidth, pos.y + height);
glEnd();

You can see that I'm using maxTexCoordBorderX variable which is calculated based on border and image size. Image width is 32 and border width is 2.

maxTexCoordBorderX = 2 / 32 = 0.0625

Could anyone please help with with finding out where the problem is?

解决方案

Most likely culprit is that you are not sampling on the texel centers. For example, if you have a 32x32 pixel texture, the texel-centers are offset by 1/64.

Here's a rough diagram of a 4x4 texture. The squares are the texels (or pixels) of the image.

_________________1,1
|   |   |   |   |
|   |   |   |   |
|___|___|___|___|_0.75
|   |   |   |   |
|   |   |   |   |
|___|___|___|___|_0.5
|   |   |   |   |
|   |   |   |   |
|___|___|___|___|_0.25
|   |   |   |   |
| X |   |   |   |
|___|___|___|___|
0,0 |  0.5  |   1
   0.25    0.75

x = (0.125, 0.125)

If you sample on one of the lines, you will get a value exactly between two texels, which will (if you have texture sampling set to linear blend) give you an average value. If you want to sample the exact texel value, you need to specify a u,v in the center of the texel.

这篇关于纹理显示不正确 - 可能坐标错误OpenGL,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆