无法对对象施加纹理

时间:2018-08-19 13:43:00

标签: c++ opengl

我试图将草纹理放到一个简单的矩形中,但我不能强迫它工作。将纹理绑定到id的功能正常工作(已测试),因此它必须在此处,但我无法识别错误。矩形仍然是白色,没有任何纹理,也没有任何试图进行纹理化的迹象

void Draw()
    {
        glBegin(GL_QUADS);
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, grass);
        glPushMatrix();

        float m_amb[] = { 1.0f, 1.0f, 1.0f, 1.0f };
        float m_dif[] = { 1.0f, 1.0f, 1.0f, 1.0f };
        float m_spe[] = { 0.0f, 0.0f, 0.0f, 1.0f };
        glMaterialfv(GL_FRONT, GL_AMBIENT, m_amb);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, m_dif);
        glMaterialfv(GL_FRONT, GL_SPECULAR, m_spe);

        glTexCoord2f(0.0f, 0.0f);
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(place.x - 15.0f, place.y - 15.0f, place.z - 5.0f);

        glTexCoord2f(1.0f, 0.0f);
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(place.x +15.0f, place.y - 15.0f, place.z - 5.0f);

        glTexCoord2f(1.0f, 1.0f);
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(place.x + 15.0f, place.y + 15.0f, place.z - 5.0f);

        glTexCoord2f(0.0f, 1.0f);
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3f(place.x - 15.0f, place.y + 15.0f, place.z - 5.0f);
        glDisable(GL_TEXTURE_2D);
        glEnd();
        glPopMatrix();
    }

1 个答案:

答案 0 :(得分:1)

glBegin/glEnd序列之间不允许有其他命令,除了用于指定顶点属性(例如=ActiveCell.Offset(2,20)=True glTexCoord2fglNormal3f)的命令。

无视此操作将产生glVertex3f错误,如glBegin的OpenGL参考页所述。

要解决此问题,您必须将GL_INVALID_OPERATION直接移到第一条glBegin(GL_QUADS);指令之前,并将glTexCoord2f直接移到最后一条glEnd();指令之后:

glVertex3f