文件已加载但未显示纹理

The file is loaded but the texture is not displayed

我正在尝试制作 3D 射击游戏。我认为图像正在加载,因为它的尺寸已显示,但未显示为纹理。该程序编译和运行没有错误,但没有纹理。我不知道我哪里弄错了。

main.cpp:

#include "Functions.h"

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(wnd_width, wnd_height);
    glutInitWindowPosition(300, 100);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_NORMALIZE);
    glutCreateWindow("OpenGL my");
    glutDisplayFunc(display);
    glutIdleFunc(Idle);
    glutSpecialFunc(KeyPressed);
    TextureInit();
    glutMainLoop();
    return 0;
}

functions.h

#define STB_IMAGE_IMPLEMENTATION

#include <GL/glut.h>
#include <stb_image.h>
#include <iostream>
#include "point.h"
#include "camera.h"

int wnd_width=1300;
int wnd_height=900;
GLdouble aspect = wnd_width/wnd_height;
Camera cam;
unsigned int texture;
float text_coords[] = {0,0, 1,0, 1,1, 0,1};

void DrawFloor();
void DrawWall(float x, float width, float height);
void KeyPressed(int key, int x, int y);
void TextureInit();
void display();
void Idle();



void display(){
    glClearColor(0.6, 0.8, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90, aspect, 0.1, 3);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
              cam.view.x, cam.view.y, cam.view.z,
              0, 0.5, 0);

    glBegin(GL_QUADS);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, text_coords);
    //glColor3f(0, 0, 0.7);
    DrawFloor();
    //glColor3f(0, 0.8, 0.1);
    DrawWall(-0.5, 2, 0.7);
    DrawWall(0.5, 2, 0.7);
    glEnd();
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glutSwapBuffers();
}

void Idle(){

}

void DrawFloor(){
    glVertex3d(1, 0, 2.5);
    glVertex3d(1, 0, 0);
    glVertex3d(-1, 0, 0);
    glVertex3d(-1, 0, 2.5);
}

void DrawWall(float x, float width, float height){
    glVertex3f(x, height, 0);
    glVertex3f(x, height, width);
    glVertex3f(x, 0, width);
    glVertex3f(x, 0, 0);
}

void KeyPressed(int key, int x, int y){
    switch (key) {
    case GLUT_KEY_UP: { cam.MoveForward(); break; }
    case GLUT_KEY_DOWN: { cam.MoveBack(); break; }
    case GLUT_KEY_LEFT: {cam.TurnLeft(); break; }
    case GLUT_KEY_RIGHT: {cam.TurnRight(); break; }
    }
    cam.PrintPosition();
    glutPostRedisplay();
}

void TextureInit(){
    int width, height, cnt;
    unsigned char* data = stbi_load("whitemarble.jpg", &width, &height, &cnt, 0);
    if(data==nullptr) std::cout<< "NO\n";
    else std::cout<<width<<'\t'<<height<<'\n';
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    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_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
                 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    glBindTexture(GL_TEXTURE_2D, 0);
    stbi_image_free(data);
}

重做。现在可以了。谢谢。但是画面很拉长。

#define STB_IMAGE_IMPLEMENTATION
#include <GL/glut.h>
#include <stb_image.h>
#include <iostream>
#include "point.h"
#include "camera.h"

int wnd_width=1300;
int wnd_height=900;
GLdouble aspect = wnd_width/wnd_height;
Camera cam;
unsigned int texture;
float text_coords[] = {0,0, 1,0, 1,1, 0,1};

void DrawFloor();
void DrawWall(float x, float width, float height);
void KeyPressed(int key, int x, int y);
void TextureInit();
void display();
void Idle();

void TextureInit(){
    int width, height, cnt;
    unsigned char* data = stbi_load("whitemarble.jpg", &width, &height, &cnt, 0);
    if(data==nullptr) std::cout<< "NO\n";
    else std::cout<<width<<'\t'<<height<<'\n';
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    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_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
                 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glBindTexture(GL_TEXTURE_2D, 0);
    stbi_image_free(data);
}

void display(){
    glClearColor(0.6, 0.8, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90, aspect, 0.1, 3);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
              cam.view.x, cam.view.y, cam.view.z,
              0, 0.5, 0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glBegin(GL_QUADS);
    DrawFloor();
    DrawWall(-0.5, 2, 0.7);
    DrawWall(0.5, 2, 0.7);
    glEnd();
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glutSwapBuffers();
}

void DrawFloor(){
    glTexCoord2d(0, 0);
    glVertex3d(1, 0, 2.5);
    glTexCoord2d(1, 0);
    glVertex3d(1, 0, 0);
    glTexCoord2d(1, 1);
    glVertex3d(-1, 0, 0);
    glTexCoord2d(0, 1);
    glVertex3d(-1, 0, 2.5);
}

void DrawWall(float x, float width, float height){
    glVertex3f(x, height, 0);
    glVertex3f(x, height, width);
    glVertex3f(x, 0, width);
    glVertex3f(x, 0, 0);
}

void KeyPressed(int key, int x, int y){
    switch (key) {
    case GLUT_KEY_UP: { cam.MoveForward(); break; }
    case GLUT_KEY_DOWN: { cam.MoveBack(); break; }
    case GLUT_KEY_LEFT: {cam.TurnLeft(); break; }
    case GLUT_KEY_RIGHT: {cam.TurnRight(); break; }
    }
    cam.PrintPosition();
    glutPostRedisplay();
}



void Idle(){

}```

您的代码导致了 glBeginglEnd 之间只能使用一部分 GL 命令。您必须在指定顶点之前立即调用 glBegin

void display(){
    glClearColor(0.6, 0.8, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90, aspect, 0.1, 3);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
              cam.view.x, cam.view.y, cam.view.z,
              0, 0.5, 0);

    
    // glBegin(GL_QUADS);                             // <--- DELETE
    
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, text_coords);
    
    glBegin(GL_QUADS);                                // <--- INSERT
    //glColor3f(0, 0, 0.7);
    DrawFloor();
    //glColor3f(0, 0.8, 0.1);
    DrawWall(-0.5, 2, 0.7);
    DrawWall(0.5, 2, 0.7);
    glEnd();
    
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glutSwapBuffers();
}

不能混合固定函数属性和 glBegin/glEnd 序列。使用 glTexCoord 代替:

glTexCoord2d(0, 0);
glVertex3d(1, 0, 2.5);
glTexCoord2d(1, 0);
glVertex3d(1, 0, 0);
glTexCoord2d(1, 1);
glVertex3d(-1, 0, 0);
glTexCoord2d(0, 1);
glVertex3d(-1, 0, 2.5);