OpenGL - 纹理不显示在整个定义的区域上

时间:2016-10-25 04:28:41

标签: c++ opengl

我正在尝试OpenGL的纹理贴图功能,并且纹理显示在屏幕上,但不在我设置的区域上。 该区域是长度为100.0的四边形,纹理仅显示在底部 在glTexImage2D中使用GL_RGB时,只填充四分之三的四边形,当我将其更改为GL_RGBA时,它变为四边形的四分之一。

主要参数声明:

BYTE* m_pBMPBuffer;
int m_iWidth;
int m_iHeight;
GLuint m_uiTexture;

设置纹理贴图的代码:

void CTextureMappingView::InitializeTexture()
{
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &m_uiTexture);
    glBindTexture(GL_TEXTURE_2D, m_uiTexture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_iWidth, m_iHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, m_pBMPBuffer);
}

缓冲区初始化:

m_iWidth = 64;
m_iHeight = 64;
m_pBMPBuffer = new BYTE[m_iWidth * m_iHeight * 3];

for (int i = 0 ; i < m_iWidth * m_iHeight ; i += 3)
{
    m_pBMPBuffer[i] = 255;
    m_pBMPBuffer[i + 1] = 0;
    m_pBMPBuffer[i + 2] = 0;
}

渲染:

void CTextureMappingView::RenderScene()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(XAngle, 1.0f, 0.0f, 0.0f);
    glRotatef(YAngle, 0.0f, 1.0f, 0.0f);

    glPushMatrix();
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        glBindTexture(GL_TEXTURE_2D, m_uiTexture);

        glBegin(GL_POLYGON);
            glTexCoord2d(0.0, 0.0);
            glVertex3d(0.0, 0.0, 0.0);

            glTexCoord2d(0.0, 1.0);
            glVertex3d(0.0, 100.0, 0.0);

            glTexCoord2d(1.0, 1.0);
            glVertex3d(100.0, 100.0, 0.0);

            glTexCoord2d(1.0, 0.0);
            glVertex3d(100.0, 0.0, 0.0);
        glEnd();

        glDisable(GL_TEXTURE_2D);
    glPopMatrix();
}

目前的结果:

OpenGL - Simple Texture Mapping Result / Only one third of area filled

1 个答案:

答案 0 :(得分:1)

您只初始化纹理的三分之一:

for (int i = 0 ; i < m_iWidth * m_iHeight ; i += 3)

你应该上升到m_iWidth * m_iHeight * 3,因为那是你分配的内容。