我想这是一个两部分的问题,这就是为什么我找不到更合适的帖子标题的原因。
我正在使用 DevIL 加载图像,然后将其转换为熟悉的格式。这是我遇到问题的代码部分:
//Copy to OpenGL texture
if(!ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE))
{
throw runtime_error(std::string("Unable to convert image") +filename +std::string(" to display friendly format"));
}
glGenTextures(1, &Main::texture);
glBindTexture(GL_TEXTURE_2D, Main::texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Use nice (linear) scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Use nice (linear) scaling
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), width, height, 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
现在,对于glGenTextures()
方法,我得到
error C2664: 'glGenTextures' : cannot convert parameter 2 from 'GLuint Main::* ' to 'GLuint *'
并且对于glBindTexure()
error C2597: illegal reference to non-static member 'Main::texture'
我尝试过texture
以许多不同的方式声明,
static Gluint Main::texture;
static Gluint texture;
Gluint texture;
Gluint Main::texture;
在我的 .cpp 文件和我的 .h 文件中,但都不起作用。如果我尝试在 .h 文件中将其声明为static Gluint Main::texture
,我会收到LNK2019: unresolved external symbol__imp_
每个 DevIL 函数的错误(如果您希望我也发布它们,请告诉我)。
我很漂亮,这是代码中的一些东西,而不是我的依赖项或我的 .lib 或 .dll 文件不在正确的位置。那么我做错了什么?
编辑:这是我到目前为止的代码
头文件
class Main
{
private:
static GLuint texture;
public:
static void Init(int argc, char ** argv);
static void DisplayScene();
static void Idle();
static void Resize(int w, int h);
};
void main(int argc, char ** argv)
{
Main::Init(argc, argv);
}
.cpp 文件
#include <IL\il.h>
#include <IL\ilu.h>
#include <IL\ilut.h>
GLuint Main::texture = 0;
double xRes, yRes;
void Main::Init(int argc, char ** argv) //Initialisation method
{
////Window Management
glutInit( &argc, argv); //Initialises GLUT and processes any command line arguements.
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); //Specifies whether to use an RGBA or colour-index colour model. Can also specify whether to use a single or a double buffered window.
glutInitWindowSize(1024, 768);
glutCreateWindow("Adventure"); //Creates a window with an OpenGL context. It returns a unique identifier for the new window. Until glutMainLoop() is called, the window is not yet displayed.
/// Set up OpenGL for 2d rendering
gluOrtho2D(0.0, xRes, yRes, 0.0); //1.0 0.0
/// Enable textures
glEnable(GL_TEXTURE_2D);
/// Enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
ilInit(); //Initialise DevIL.
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); //Specifies the coordinate system OpenGL assumes as it draws the final image and how the image is mapped to the screen.
///The Display Callback
glutDisplayFunc(Main::DisplayScene); //Defines all the routines needed to draw the scene.
glutIdleFunc(Main::Idle);
///Running The Program
glutMainLoop(); //All windows that have been created are now shown, and rendering to those windows is now effective.
}
void Main::Idle()
{
glutPostRedisplay();
}
void Main::DisplayScene()
{
///Declarations
int x = 0;
int y = 0;
//float alpha;
const char* filename = "back.bmp";
ILboolean ilLoadImage(const char *filename);
//GLuint texture;
///Generate DevIL image and make it current context
ILuint image;
ilGenImages(1, &image);
ilBindImage(image);
///Load the image
if (!ilLoadImage(filename))
{
throw runtime_error(std::string("Unable to load image") +filename);
}
int width = ilGetInteger(IL_IMAGE_WIDTH);
int height = ilGetInteger(IL_IMAGE_HEIGHT);
///Copy to OpenGL texture
if(!ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE))
{
throw runtime_error(std::string("Unable to convert image") +filename +std::string(" to display friendly format"));
}
glGenTextures(1, &Main::texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Use nice (linear) scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Use nice (linear) scaling
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), width, height, 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
/// Free DevIL image since we have it in a texture now
ilDeleteImages(1, &image);
}