gpt4 book ai didi

c++ - 纹理随着 gluSphere 和 glutPostRedisplay 消失

转载 作者:行者123 更新时间:2023-11-30 04:31:04 24 4
gpt4 key购买 nike

我刚开始使用 opengl。事实上,这是我的第一个项目。我想将一些纹理映射到背景中的四边形,并想在前面使用 gluSphere 绘制一个球体,我想为此设置动画。所以,我先映射纹理,然后在显示函数中绘制球体,然后调用 glutPostRedisplay。首次调用显示时,它确实正确显示纹理和球体。但是,一旦调用 glutPostRedisplay,纹理就会消失,只绘制球体。我在下面给出了我的代码。对于使用任何不良的 opengl 做法,我深表歉意。

void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawTex();
glPushMatrix();
glTranslatef(SIZE/2, SIZE/2, 0);
glutSolidSphere(15.0, 20, 20);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}

void LoadTextureRAW( const char * filename, int wrap ) {
GLuint texture;
int width, height;
unsigned char * data;

FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) {
return;
}

// allocate buffer
width = 1073;
height = 918;
data = (unsigned char *)malloc( width * height * 3 );

// read texture data
fclose( file );

// select our current texture
glBindTexture( GL_TEXTURE_2D, 1 );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );

// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );

// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );
}

void drawTex() {
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0);
glVertex3d(0.0,SIZE/2, -SIZE);
glTexCoord2d(1.0,0.0);
glVertex3d(SIZE, SIZE/2, -SIZE);
glTexCoord2d(1.0,1.0);
glVertex3d(SIZE, SIZE/2, SIZE);
glTexCoord2d(0.0,1.0);
glVertex3d(0.0, SIZE/2, SIZE);
glEnd();
glFlush();
}

void map() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable( GL_TEXTURE_2D );
LoadTextureRAW("background.bmp", true);
glBindTexture( GL_TEXTURE_2D, 1 );
drawTex();
}

void init() {
glClearColor(1, 1, 1, 1);
glClearDepth( SIZE );
glOrtho(0, SIZE, 0, SIZE, -SIZE, SIZE);
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { SIZE/2, 0, 1.0, 0.0 };
GLfloat model_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
glClearColor(1.0, 1.0, 1.0, 1.0);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
//glColor3f(0,0,0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glRotatef(-40, 1, 0, 0);
glRotatef(-40, 0, 1, 0);
map();
}

非常感谢任何帮助。

最佳答案

一些注意事项:在固定函数管道中,必须在将 View 变换(=相机)应用于模型 View 矩阵之后设置灯光位置。事实上,init() 中的整个代码实际上都属于显示函数。

glClearDepth 应该设置为 1,除非你知道自己在做什么(清晰深度适用于 NDC 空间,作为 OpenGL 初学者,这对于入门来说“太高级”了。只需将它设置为 1 就可以了.

map() 函数毫无意义。纹理被初始化一次,然后仅在渲染带纹理的几何体之前绑定(bind)。

最后但同样重要的是,glutSolidSphere 不会生成正确的纹理坐标。但是你需要给 OpenGL 纹理坐标。我的建议:抛弃 glutSolidSphere,自己做几何。像这样:https://stackoverflow.com/a/5989676/524368

关于c++ - 纹理随着 gluSphere 和 glutPostRedisplay 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375271/

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