搞OpenGL好几天了,现在开始写点东西。有问题请参考nehe的opengl教程。
1.载入纹理图片
FILE *File;
File=fopen(Filename,"r"); //打开纹理图片,Filename为文件路径
2.设置纹理
AUX_RGBImageRec *TextureImage[1]; //定义缓存
memset(TextureImage,0,sizeof(void *)*1); //分配空间
if(TextureImage[0] = &auxDIBImageLoad(Filename)) //载入纹理成功
{
glGenTextures(1, &texture[0]); //创建纹理
glBindTexture(GL_TEXTURE_2D, texture[0]); //绑定纹理到创建的空间
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); //载入纹理
glTexParameter(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //设置纹理过滤模式
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
if (TextureImage[0]) //释放纹理缓存空间
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
}
3.使用纹理
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]); //设置纹理,此步必须在glbegin之前
glBegin(GL_QUADS); //画一个矩形
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); //纹理的范围从0.0~1.0,分别于矩形的四角对应
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
最后别忘了在winmain中加入glEnable(GL_TEXTURE_2D);作用是开启纹理。当然还有头文件#include"stdio.h"