我似乎无法加载图像并将其用作我的程序的纹理:图像大小为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();
}
所以,我在这里调用了对GLuint纹理的loadtexture函数,这样:
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;
答案 0 :(得分: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纹理对象。在创建一个 new 之后(这是glGenTextures
所做的)会产生适得其反的效果。
放弃那条线,你可能会好的。
但是,你应该不在每个显示器上重新加载纹理。您应该在初始化期间创建一次纹理,然后仅使用带有glBindTexture
调用的纹理。