gpt4 book ai didi

c++ - 如何使用 stb_image.c 绘制纹理

转载 作者:行者123 更新时间:2023-11-30 01:28:22 29 4
gpt4 key购买 nike

我想将这张图片加载到二维纹理中,然后将其绘制到屏幕上。主要问题是将图片加载到纹理变量中。以下代码输出正确的宽度和高度以及 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;
}

最佳答案

首先你需要

  • 可绘制对象(窗口、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 教程。 Google 搜索“NeHe”或“Lighthouse3D”或“Arcsynthesis OpenGL 教程”。

关于c++ - 如何使用 stb_image.c 绘制纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7600602/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com