3

我似乎无法加载图像并将其用作我的程序的纹理:图像大小为 512*512,我不知道我做错了什么,有人可以帮助我吗?

主功能:

int main(int argc, char** argv)
{
  glutInit (&argc, argv);
  glutInitWindowSize (800,600);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  glutCreateWindow ("CS248 GLUT example");
  glutDisplayFunc (display);
  glutReshapeFunc (reshape);
  glutMainLoop ();
  return 0;
}

显示功能:

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glColor3f(1.0f,1.0f,1.0f);
    texture = LoadTexture("space.bmp");
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0); glVertex3f(-20.0,-20.0,0);
        glTexCoord2f(0.0, 1.0); glVertex3f(-20.0, 20.0, 0.0);
        glTexCoord2f(1.0, 1.0); glVertex3f(20.0, 20.0, 0.0);
        glTexCoord2f(1.0, 0.0); glVertex3f(20.0, -20.0, 0.0);   
    glEnd();
    glFlush();
}

所以,我在这里将 loadtexture 函数调用为 GLuint 纹理,这样:

GLuint LoadTexture( const char* texture )
{
    GLuint textureID = SOIL_load_OGL_texture( texture, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS ); 
    glGenTextures(1,&textureID);
    glBindTexture( GL_TEXTURE_2D, textureID );
    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_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

    return textureID;
}

重塑功能:

void reshape(GLsizei w, GLsizei h) {
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20.0f, 20.0f, -20.0f, 20.0f, -20.0f, 20.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

包括等:

#include <glut.h>
#include <SOIL.h>

GLuint texture;
4

1 回答 1

2
GLuint textureID = SOIL_load_OGL_texture( texture, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS ); 
glGenTextures(1,&textureID);

SOIL_load_OGL_texture 创建一个 OpenGL 纹理对象。在已经创建一个之后生成一个的(就是这样glGenTextures做的)会适得其反。

放弃那条线,你可能会没事的。

但是,您应该在每个显示器上都重新加载纹理。您应该在初始化期间创建一次该纹理,然后只需通过glBindTexture调用使用该纹理。

于 2013-05-04T14:01:14.403 回答