OpenGL 纹理未加载 SOIL

OpenGL textures not loading with SOIL

嘿,我无法显示我的纹理,我不知道出了什么问题。教程没有帮助。这是我的代码:

Player p();


//The glutDisplayFunc();
void display() {

glPushMatrix();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);

//Load png with SOIL
playerTex = loadTex("Data/Sprites/player.png");
p.draw();

glFlush();
glPopMatrix(); }


//Load texture
GLuint loadTex(const char* c) {
GLuint temp = SOIL_load_OGL_texture(
    c,
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );

if (0 == temp)
{
    printf("SOIL loading error: '%s'\n", SOIL_last_result());
    //return 0;
}

glBindTexture(GL_TEXTURE_2D, temp);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

return temp; }


//Player object's draw function
void player::draw() {

glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
glColor3f(0, 0, 0);
    glTexCoord2f(0.0, 0.0);
    glVertex2f(x, y);
    glTexCoord2f(0.0, 1.0);
    glVertex2f(x + spriteW, y);
    glTexCoord2f(1.0, 1.0);
    glVertex2f(x + spriteW, y + spriteL);
glEnd();}

spriteX = 0, spriteY = 0, spriteW = 400, spriteL = 400, x = 0, y = 0

这是我得到的输出: Output Window

如您所见,黑色三角形显示正常,但没有纹理

纹理坐标应在0..1范围内,它们不是纹素坐标。除以纹理宽度和高度以从纹素坐标转换为纹理坐标。

您还使用以下行将颜色设置为黑色:

glColor3f(0, 0, 0);

不要那样做。使用白色(或更改 TEXTURE_ENV_MODE,但这似乎没有必要)。

glColor3f(1, 1, 1);

颜色乘以纹理颜色。您不妨将它移到 glBegin() glEnd() 块之外:

glColor(1, 1, 1);
glBegin(...);
    ...
glEnd();