关闭

opengl 贴图颜色失真(慎入)

标签: alignmentgenerationfilterbufferobjectdelete
3741人阅读 评论(3) 收藏 举报
分类:

2012.04.19更新:貌似是渲染的时候图片格式出错了,与glPixelStorei(GL_UNPACK_ALIGNMENT, 4)没关系。在glTextImage2D的时候应该用GL_BGR_EXT,而不是GL_RGB.



修改了Nehe的教程(因为aux库的auxDIBImageLoad()不能用了),把auxDIBImageLoad替换成下面的代码(网上找的)

bool LoadTextureEx(LPTSTR szFileName, GLuint &texid)     // Creates Texture From A Bitmap File
{
	HBITMAP hBMP;             // Handle Of The Bitmap
	BITMAP BMP;             // Bitmap Structure

	hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );

	if (!hBMP)              // Does The Bitmap Exist?
		return FALSE;            // If Not Return False

	GetObject(hBMP, sizeof(BMP), &BMP);        // Get The Object
	// hBMP:        Handle To Graphics Object
	// sizeof(BMP): Size Of Buffer For Object Information
	// &BMP:        Buffer For Object Information

	// Typical Texture Generation Using Data From The Bitmap
	glBindTexture(GL_TEXTURE_2D, texid);        // Bind To The Texture ID
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Min Filter
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Mag Filter
	glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);

	DeleteObject(hBMP);            // Delete The Object

	return TRUE;             // Loading Was Successful
}

结果颜色失真很严重。

后来对比网上的代码,发现少了一句 

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

bool LoadTextureEx(LPTSTR szFileName, GLuint &texid)     // Creates Texture From A Bitmap File
{
	HBITMAP hBMP;             // Handle Of The Bitmap
	BITMAP BMP;             // Bitmap Structure

	hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );

	if (!hBMP)              // Does The Bitmap Exist?
		return FALSE;            // If Not Return False

	GetObject(hBMP, sizeof(BMP), &BMP);        // Get The Object
	// hBMP:        Handle To Graphics Object
	// sizeof(BMP): Size Of Buffer For Object Information
	// &BMP:        Buffer For Object Information

	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);       // Pixel Storage Mode (Word Alignment / 4 Bytes), 注意这句

	// Typical Texture Generation Using Data From The Bitmap
	glBindTexture(GL_TEXTURE_2D, texid);        // Bind To The Texture ID
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Min Filter
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Mag Filter
	glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);

	DeleteObject(hBMP);            // Delete The Object

	return TRUE;             // Loading Was Successful
}


问题解决。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:143436次
    • 积分:2033
    • 等级:
    • 排名:第18912名
    • 原创:66篇
    • 转载:3篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论