﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-无尽的夜空，唯有月光撒满窗前-最新评论</title><link>http://www.cppblog.com/lyricc/CommentsRSS.aspx</link><description /><language>zh-cn</language><pubDate>Mon, 05 Mar 2012 05:31:42 GMT</pubDate><lastBuildDate>Mon, 05 Mar 2012 05:31:42 GMT</lastBuildDate><generator>cnblogs</generator><item><title>re: 用freeimage 生成opengl texture</title><link>http://www.cppblog.com/lyricc/archive/2013/12/23/163986.html#204959</link><dc:creator>shift0ogg</dc:creator><author>shift0ogg</author><pubDate>Mon, 23 Dec 2013 05:55:00 GMT</pubDate><guid>http://www.cppblog.com/lyricc/archive/2013/12/23/163986.html#204959</guid><description><![CDATA[建议这样效率更高些<br>int glmLoadTexture(const char* filename, <br>				const unsigned int texID)<br>{<br>	//image format<br>	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;<br>	FIBITMAP *dib = NULL , *dib2 = NULL ;<br>	BYTE ss ; <br>	int pix = 0 ; <br><br>	BYTE* bits = NULL;<br>	//image width and height<br>	unsigned int w = 0 , h = 0 ;<br>	//OpenGL's image ID to map to<br>	GLuint _texID = texID ;<br>	<br>	//check the file signature and deduce its format<br>	fif = FreeImage_GetFileType(filename, 0);<br>	//if still unknown, try to guess the file format from the file extension<br>	if(fif == FIF_UNKNOWN) <br>		fif = FreeImage_GetFIFFromFilename(filename);<br>	//if still unkown, return failure<br>	if(fif == FIF_UNKNOWN)<br>		return 0;<br>	<br>	//check that the plugin has reading capabilities and load the file<br>	if(FreeImage_FIFSupportsReading(fif))<br>		dib = FreeImage_Load(fif, filename , 0  );<br>	//if the image failed to load, return failure<br>	if(!dib)<br>		return 0;<br><br>	dib2 = FreeImage_ConvertTo32Bits(dib);<br>	FreeImage_Unload(dib);<br><br><br><br><br>	<br>	//retrieve the image data<br>	bits = FreeImage_GetBits(dib2);<br>	//get the image width and height<br>	w = FreeImage_GetWidth(dib2);<br>	h = FreeImage_GetHeight(dib2);<br>	//if this somehow one of these failed (they shouldn't), return failure<br>	if((bits == 0) || (w == 0) || (h == 0))<br>		return 0;<br>	<br>	//if this texture ID is in use, unload the current texture<br>	<br>	for( pix=0; pix&lt; w*h ; pix++)		<br>    {	<br>		ss=bits[pix*4+0];<br>		bits[pix*4+0] = bits[pix*4+2] ; <br>		bits[pix*4+2] = ss ; <br>    }<br>	<br><br><br>	if(glIsTexture(_texID))<br>		glDeleteTextures(1, &amp;_texID);<br>	<br>	//generate an OpenGL texture ID for this texture<br>	//glGenTextures(1, &amp;_texID);<br><br>	//bind to the new texture ID<br>	glBindTexture(GL_TEXTURE_2D, _texID);<br><br>	<br>	if (((w &amp; (w-1)) == 0) &amp;&amp; ((h &amp; (h-1)) == 0))<br>		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);<br>	else<br>		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bits);<br>	<br>	//Free FreeImage's copy of the data<br>	FreeImage_Unload(dib2);<br><br>	return 1 ; <br>}<br><img src ="http://www.cppblog.com/lyricc/aggbug/204959.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lyricc/" target="_blank">shift0ogg</a> 2013-12-23 13:55 <a href="http://www.cppblog.com/lyricc/archive/2013/12/23/163986.html#204959#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>