gpt4 book ai didi

c++ - 想要在 OpenGL 中的短语上放置纹理

转载 作者:太空狗 更新时间:2023-10-29 21:10:37 25 4
gpt4 key购买 nike

我想在我创建的短语上添加纹理。但是,目前我的代码没有纹理映射。它只会在运行时打开一个警告,指出该文件无法打开。为什么?请修复它。

void view() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, id);
glutSolidSphere(0.4, 20, 20);
}

void main(int argc, char** argv) {
unsigned int id;
AUX_RGBImageRec *texture;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 800);
glutCreateWindow("test");
GLfloat plane_coef_s[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat plane_coef_t[] = { 0.0, 1.0, 0.0, 1.0 };
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, plane_coef_s);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_T, GL_OBJECT_PLANE, plane_coef_t);
sphere = gluNewQuadric();
gluQuadricTexture(sphere, GL_TRUE);
tex = auxDIBImageLoad("test.bmp");
glGenTextures(2, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex->sizeX, tex->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, tex->data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

最佳答案

首先,确保 text.bmp 文件与 .cpp 文件位于同一位置。

glutSolidSphere(0.4, 20);好像没有贴图贴图。将 GLUquadric *sphere; 声明为全局变量并更改glugSphere (sphere, 0.4, 20, 20);.

而且你必须将这两个变量声明为全局变量。

所以,切换到下面的代码。

unsigned int id;
AUX_RGBImageRec *texture;
GLUquadric *sphere;

void view() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, id);
gluSphere(sphere, 0.4, 20, 20);
}

void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 800);
glutCreateWindow("test");
GLfloat plane_coef_s[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat plane_coef_t[] = { 0.0, 1.0, 0.0, 1.0 };
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, plane_coef_s);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_T, GL_OBJECT_PLANE, plane_coef_t);
sphere = gluNewQuadric();
gluQuadricTexture(sphere, GL_TRUE);
tex = auxDIBImageLoad("test.bmp");
glGenTextures(2, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex->sizeX, tex->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, tex->data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

关于c++ - 想要在 OpenGL 中的短语上放置纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430027/

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