OpenGL-纹理读取BMP图片-代码

使用两种方法读取bmp图片,来作为OpenGL纹理

1.通过对bmp数据格式的分析,http://blog.csdn.net/o_sun_o/article/details/8351037(详细解析bmp格式的博客),读取其中有用的信息(图像宽度、图像高度、图像数据等)。

intnum_texture=-1;
int LoadBitmap(char *filename)
{
	int i, j=0; //Index variables
	FILE *l_file;//File pointer
	unsigned char *l_texture; //The pointer to the memory zone in which we will load thetexture
	// windows.hgives us these types to work with the Bitmap files
	BITMAPFILEHEADER fileheader;
	BITMAPINFOHEADER infoheader;
	RGBTRIPLE rgb;
	num_texture++;// The counter of the current texture is increased
	if( (l_file =fopen(filename,"rb"))==NULL)return (-1);// Open the file for reading   
	fread(&fileheader,sizeof(fileheader), 1,l_file);// Read the fileheader
	fseek(l_file,sizeof(fileheader),SEEK_SET);// Jump the fileheader
	fread(&infoheader,sizeof(infoheader), 1,l_file);// and read the infoheader
	// Now we need toallocate the memory for our image (width * height * color deep)
	l_texture= (byte *)malloc(infoheader.biWidth*infoheader.biHeight* 4);
	// And fill itwith zeros
	memset(l_texture, 0,infoheader.biWidth *infoheader.biHeight * 4);
	// At this pointwe can read every pixel of the image
	for (i=0;i <infoheader.biWidth*infoheader.biHeight;i++)
	{           
		// Weload an RGB value from the file
		fread(&rgb,sizeof(rgb), 1,l_file);
		// Andstore it
		l_texture[j+0] =rgb.rgbtRed;// Redcomponent
		l_texture[j+1] =rgb.rgbtGreen;// Greencomponent
		l_texture[j+2] =rgb.rgbtBlue;// Bluecomponent
		l_texture[j+3] = 255;// Alphavalue
		j+= 4; // Go to the next position
	}
	fclose(l_file);// Closes thefile stream

	glBindTexture(GL_TEXTURE_2D,num_texture);// Bind the ID texture specified by the 2nd parameter
	// The nextcommands sets the texture parameters
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);// Ifthe u,v coordinates overflow the range 0,1 the image is repeated
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);// Themagnification function ("linear" produces better results)
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);//The minifying function
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);// Wedon't combine the color with the original surface color, use only the texturemap.
	// Finally wedefine the 2d texture
	glTexImage2D(GL_TEXTURE_2D, 0, 4,infoheader.biWidth,infoheader.biHeight, 0,GL_RGBA,GL_UNSIGNED_BYTE,l_texture);
	// And create 2dmipmaps for the minifying function
	gluBuild2DMipmaps(GL_TEXTURE_2D, 4,infoheader.biWidth,infoheader.biHeight,GL_RGBA,GL_UNSIGNED_BYTE,l_texture);
	free(l_texture);// Free thememory we used to load the texture
	return (num_texture);// Returnsthe current texture OpenGL ID
}

2.利用已有的函数auxDIBImageLoad()读取bmp文件。

#include <stdio.h>
#include <tchar.h>
#include <glut.h>
#include <GLAux.h>//纹理
#include <glext.h>
void initTexture()
{
    AUX_RGBImageRec *m_bmpTexture; 
    //m_bmpTexture=auxDIBImageLoad(_T("texture1.bmp"));//skull.bmp
    m_bmpTexture=auxDIBImageLoad(_T("skull.bmp"));
    glGenTextures(1,texture);
    glBindTexture(GL_TEXTURE_2D,texture[0]);
    //纹理过滤函数
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    //指定纹理贴图和材质混合的方式
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);//默认为GL_REPLACE模式。
    //生成纹理//(目标纹理,执行细节级别,颜色组件,纹理宽度,纹理高度, 边框宽度必须为,颜色格式,数据类型,指向图像数据的指针)
    glTexImage2D(GL_TEXTURE_2D,0,3,m_bmpTexture->sizeX,m_bmpTexture->sizeY,
        0,GL_RGB,GL_UNSIGNED_BYTE,m_bmpTexture->data);
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值