遵循 C++/OpenGL 教程(导入图像)的问题

Problems following a C++/OpenGL Tutorial (importing images)

我需要使用 OpenGL 和 C++ 创建一个小游戏。我正在使用 this 教程开始(我的目标实际上不是创建游戏,而是使用我最终创建的代码)。

我完成了视频 8(第一个 linked)但是我 运行 遇到了问题。我的代码在

行崩溃了
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageData.Width, imageData.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData.Data);

当我将 loadAndBufferImage 方法参数更改为 运行dom(hgsjrkbgfdkj 工作正常)时,代码不会崩溃,但显然它不会加载任何图像。我不确定我做错了什么。我的 IDE 在 glfwReadImage 行上发出警告,因为它不喜欢变量 3 为 NULL(尽管它完美地运行该行)。

glfwReadImage(fileName, &imageData, NULL);

我不确定我错过了什么 out/am 做错了什么。可以是图像吗?我使用了一个通过视频描述中提供的 link 转换的。我在视频中唯一没有做的是 7.40 左右的小图像导入部分。我使用的是 NetBeans 而不是 XCode,我只是在我的资源文件夹中导入了 rocket.tga 文件(右键单击资源文件夹,添加现有项,添加图像)。

这是我的 GameWindow.cpp 代码的完整副本

#define GLEW_STATIC
#include "GameWindow.h"
#define GL_GLEXT_PROTOTYPES

#include <iostream>

typedef struct {
    GLfloat positionCoordinates[3];
    GLfloat textureCoordinates[2];
} VertexData;

#define Square_Size 100

VertexData vertices[] = {
    {{0.0f, 0.0f, 0.0f},{0.0f,0.0f}},
    {{Square_Size, 0.0f, 0.0f}, {1.0f,0.0f}},
    {{Square_Size, Square_Size, 0.0f}, {1.0f,1.0f}},
    {{0.0f, Square_Size, 0.0f}, {0.0f,1.0f}}
};

void GameWindow::setRunning(bool newRunning) {
    _running = newRunning;
}

bool GameWindow::getRunning() {
    return _running;
}

GLuint GameWindow::loadAndBufferImage(const char *fileName){
    GLFWimage imageData;
    glfwReadImage(fileName, &imageData, NULL);

    GLuint textureBufferID;

    glGenTextures(1, &textureBufferID);
    glBindTexture(GL_TEXTURE_2D, textureBufferID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageData.Width, imageData.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData.Data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glfwFreeImage(&imageData);

    return textureBufferID;
}

GameWindow::GameWindow(bool running):_running(running), _height(800), _width(800*16/9) {
    glClearColor(1.0f,1.0f,1.0f,1.0f);
    glViewport(0.0f, 0.0f, _width, _height);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0,_width,0,_height);
    glMatrixMode(GL_MODELVIEW);

    glGenBuffers(1, &_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(VertexData), (GLvoid *) offsetof(VertexData,positionCoordinates));
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData), (GLvoid *) offsetof(VertexData, textureCoordinates));

    _textureBufferID = loadAndBufferImage("rocket.tga");
}

void GameWindow::render() {
    glClear(GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_QUADS, 0, 4);

    glfwSwapBuffers();
}

void GameWindow::update() {

}

我认为我使用的是旧版本的 GLFW。大约 2.7 版,因为我无法让新的工作。再一次,虽然我认为不是很相关。

glTexImage2D() 中对 format 使用 imageData.Format:

glTexImage2D
    (
    GL_TEXTURE_2D,
    0,
    GL_RGBA,
    imageData.Width,
    imageData.Height,
    0,
    imageData.Format,  // instead of GL_RGBA
    GL_UNSIGNED_BYTE,
    imageData.Data
    );

否则,如果 imageData.Format == GL_RGB 而你撒谎说它是 GL_RGBA,那么 OpenGL 会很乐意 read right off the end of imageData.Data 寻找不存在的 RGBA 元组。