问题描述
你好,我昨晚捡到了免费图片,而且,它看起来像一个很棒的图书馆.但是,我一直在遇到问题.我正在使用GLWF,并且已将FreeImage正确安装到我的项目中,并将其链接到我的项目.我从下载示例中复制了TextureManager类,但是,我的图像无法正确显示.
如果有人可以看一下我的代码,并填补我对该库新用户的常见错误,我将非常感激.
TextureManager.cpp
Hello, I picked up freeimage last night, and, It seems like a really awesome library. I have been having issues getting it to work however. I am using GLWF, and, installed FreeImage correctly into my project, as well as linked it to my project. I copied the TextureManager class from the downloads examples, but, I am having issues getting my image to display properly.
If somebody could take a look at my code, as well as fill me in on common mistakes new users of this library face, I would be very appreciative.
TextureManager.cpp
#include "TextureManager.h"
TextureManager::TextureManager()
{
}
TextureManager::~TextureManager()
{
}
void TextureManager::LoadTexture(string name, char* imageURL)
{
//image format
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
//pointer to the image, once loaded
FIBITMAP *dib(0);
//pointer to the image data
BYTE* bits(0);
//image width and height
unsigned int width(0), height(0);
//OpenGL's image ID to map to...This is set to the next index in the map
cout << "Texture Number: " << textures.size() << endl;
GLuint gl_texID = textures.size();
//check the file signature and deduce its format
fif = FreeImage_GetFileType(imageURL, 0);
//if still unknown, try to guess the file format from the file extension
if(fif == FIF_UNKNOWN)
{
fif = FreeImage_GetFIFFromFilename(imageURL);
cout << "Getting Image Type" << endl;
}
//if still unkown, return failure
if(fif == FIF_UNKNOWN)
{
cout << "Failed to get image type" << endl;
}
//check that the plugin has reading capabilities and load the file
if(FreeImage_FIFSupportsReading(fif))
{
//dib = FreeImage_ConvertTo32Bits(dib);
dib = FreeImage_Load(fif, imageURL);
cout << "Trying to load image: " << endl;
if(fif == 13)
{
cout << "Image Format is PNG" << endl;
}
}
//if the image failed to load, return failure
if(!dib)
{
cout << "Failed to load image" << endl;
}
//retrieve the image data
bits = FreeImage_GetBits(dib);
//get the image width and height
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
cout << "Image Height: " << height << " Width: " << width << endl;
//if this somehow one of these failed (they shouldn't), return failure
if((bits == 0) || (width == 0) || (height == 0))
//if this texture ID is in use, unload the current texture
if(textures.find(name) != textures.end())
{
cout << "Deleting Texture: " << name << endl;
glDeleteTextures(1, &(textures[name]));
}
cout << "Texture Loaded, generating texture..." << endl;
//Insert GL Texture Index Into Image Map
textures.insert ( pair<string,int>(name, gl_texID) );
//generate an OpenGL texture ID for this texture
glGenTextures(1, &gl_texID);
//bind to the new texture ID
glBindTexture(GL_TEXTURE_2D, gl_texID);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
//store the texture data for OpenGL use
//glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height, border, image_format, GL_UNSIGNED_BYTE, bits);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // If the 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); // The magnification function ("linear" produces better results)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//Free FreeImage's copy of the data
FreeImage_Unload(dib);
//return success
//glBindTexture(GL_TEXTURE_2D, gl_texID);
}
void TextureManager::UnloadTexture(string name)
{
if(textures.find(name) != textures.end())
{
glDeleteTextures(1, &(textures[name]));
textures.erase(name);
}
}
void TextureManager::UnloadAllTextures()
{
//start at the begginning of the texture map
std::map<string, GLuint>::iterator i = textures.begin();
//Unload the textures untill the end of the texture map is found
while(i != textures.end())
{
UnloadTexture(i->first);
}
//clear the texture map
textures.clear();
}
void TextureManager::BindTexture(string name)
{
//bool result(true);
//if this texture ID mapped, bind it's texture as current
if(textures.find(name) != textures.end())
{
glBindTexture(GL_TEXTURE_2D, textures[name]);
}
//otherwise, binding failed
}
Graphics.cpp
Graphics.cpp
#include "Graphics.h"
//Graphics* Graphics::graphics = NULL;
Graphics::Graphics()
{
FreeImage_Initialise();
}
Graphics::~Graphics()
{
}
/*Graphics* Graphics::get()
{
if(graphics == NULL){ graphics = new Graphics(); }
return graphics;
}
*/
void Graphics::release()
{
//delete graphics;
}
void Graphics::initGraphics()
{
textureManager.LoadTexture("courior", "Resources\\font\\courior2.png");
}
void Graphics::draw()
{
// OpenGL rendering goes here...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Enable Blending
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
textureManager.BindTexture("courior");
glBegin(GL_QUADS);
glTexCoord2f(0, 1);
glVertex2f(-128, 128);
glTexCoord2f(1, 1);
glVertex2f(128, 128);
glTexCoord2f(1, 0);
glVertex2f(128, -128);
glTexCoord2f(0, 0);
glVertex2f(-128, -128);
glEnd();
//Disable Blending
//glDisable(GL_BLEND);
// Swap front and back rendering buffers
glfwSwapBuffers();
}
我的纹理已加载到initGraphics()函数中.再次,我非常感谢我能为此提供的任何帮助.我一定是在做些小错误.
同样,我的问题是绑定图像在四边形上显示为完全白色.通过测试,我确定courior2.png是没有透明部分的512 x512彩色图像.图像是否以任何方式,形状或形式显示.它会显示.
我知道我应该将其发布在FreeImage支持论坛上,但是到目前为止,我尝试获得答案的尝试均未成功.因此,我希望这个社区中的某人有一些在openGL中加载PNG的经验:).你们一直都在帮助我. :)
谢谢,
Shawn
My texture is loaded in the initGraphics() function. Again, I am super appreciative of any help I can get on this. I must be doing something small wrong.
Also, again, my issue is that the bound image displays as completely white on the quad. Through testing, I made sure courior2.png was a 512 x512 multicolored image with no transparent portions. If the image was displaying in any way, shape, or form. It would show.
I know that I should post this on the FreeImage support forums, but, my attempts to get answers there have been unsuccessful thus far. As such, I am hoping that somebody in this community has some experience with loading PNGs in openGL :). You guys have always helped me before. :)
Thanks,
Shawn
推荐答案
//Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//Define Viewport and lower left corner
glViewport(0, 0, wWidth, wHeight);
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, wWidth, wHeight, 0, -1, 1);
//Set the center of the screen as (0, 0)
glTranslatef(wWidth / 2, wHeight / 2, 0);
这篇关于使用FreeImage时:PNG图像加载和绑定,但是是白色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!