1

我想将此图片加载到 2d 纹理中,然后将其绘制到屏幕上。主要问题是将图片加载到纹理变量中。以下代码输出正确的宽度和高度以及 rgba,但我如何将数据放入 3d 纹理中。

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
/* more includes... */
#include "stb_image.h"

using namespace std;

int main(int argc, char** argv) {
    int x,y,n;
    unsigned char *data = stbi_load("png.png", &x, &y, &n, 0);
    if (data == NULL) {
        // error
        cout << "Error, data was null";
    } else {
        // process
        cout << data << endl << endl;
    }
    stbi_image_free(data);

    cout << x << endl << y << endl << n;
    return 0;
}
4

1 回答 1

5

首先你需要

  • 可绘制的(窗口、PBuffer、帧缓冲区)
  • 与可绘制对象关联的 OpenGL 上下文

您可以使用 GLFW、SDL 或 GLUT 来获取这些(如果您只需要一个窗口,我个人推荐 GLFW)。

创建一个纹理名称

GLuint 纹理名称;

void somefunction(…)
{
glGenTextures(1, &texture_name);
glBindTexture(GL_TEXTURE_2D, texture_name);
glPixelStorei(…); /* multiple calls to glPixelStorei describing the layout of the data to come */
glTexImage2D(GL_TEXTURE_2D, miplevel, internal_format, width, height, border, format, type, data);
}

这是如何加载它的快速而肮脏的解释。绘图是另一项业务。我建议你阅读一些 OpenGL 教程。谷歌搜索“NeHe”或“Lighthouse3D”,或“Arcsynthesis OpenGL 教程”。

于 2011-09-29T17:14:09.603 回答