OpenGL中的C2664和C2597错误,c++中的DevIL错误
error C2664 and C2597 in OpenGL and DevIL in C++
我想这是一个两部分的问题,这就是为什么我找不到一个更合适的标题。
我正在使用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
,我会得到每个DevIL函数的LNK2019: unresolved external symbol__imp_
错误(如果您希望我也发布它们,请告诉我)。
我很漂亮,这是代码中的东西,而不是我的依赖关系或我的。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 <ILil.h>
#include <ILilu.h>
#include <ILilut.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);
}
你的错误出现,因为你没有一个实例(对象)类型的Main
,但当然你需要一个Main
之前,你可以使用它的任何东西。
现在你有几个选项来解决这个问题-看起来你已经尝试了一些。你可以使纹理变量全局,要么在类作用域将其设置为static
,要么在命名空间作用域使用"普通"全局。
对于第一种情况,您将在您的.h文件中的类中声明变量为static GLuint texture;
,而您将在您的.cpp文件中定义为GLuint Main::texture = 0;
。当你尝试这个时,你似乎没有定义变量。对于第二种情况,在类外部的.h中将其声明为extern GLuint texture;
,并在任何函数外部将其定义为GLuint texture
。
这两个解决方案都不是很好的风格,所以它会更好,如果你只是添加一个GLuint texture
到你的Main类,然后创建一个Main实例,并使用其成员变量-最好从Main内的函数,所以你可以使用this
。
理想情况下,你会有像这样的Texture
类:
class Texture
{
Texture() {glGenTextures(1, &id);}
~Texture() {glDeleteTextures(1, &id);}
void Bind() {glBindTexture(GL_TEXTURE_2D, id);}
private:
GLuint id;
};
void someFunction()
{
Texture myTexture;
myTexture.Bind();
// do stuff with the texture - like glTexParameteri, glTexImage
}
将更多的功能移动到Texture类中是加分项,但这需要处理更多opengl愚蠢的绑定机制。
错误C2664:"glGenTextures":无法将参数2从"GLuint Main::*"转换为"GLuint *"
&Main::texture
是我们所说的成员字段指针,而不是指针。你需要指向一个纹理的实际实例,所以
Main* mymain; /* = new Main(...); somewehere in the code */
glGenTextures(1, &mymain->texture);
Extra/bonus
在不太可能的情况下,您确实想使用成员指针,这里有一个示例(codead链接):
#include <stdio.h>
struct A
{
int p,q,r;
int getp() { return p; }
};
int main()
{
int x;
A a[] = {
{ 1,2,3 },
{ 4,5,6 },
{ 7,8,9 }
};
// ---- pointer to member functions
int A::* member;
member = &A::p;
printf("member p: %in", a[2].*member);
member = &A::q;
printf("member q: %in", a[2].*member);
member = &A::r;
printf("member r: %in", a[2].*member);
// ---- pointer to member functions
int (A::*getter)();
getter = &A::getp;
x = (a[1].*getter)();
printf("getter: %in", x);
x = (a[2].*getter)();
printf("getter: %in", x);
return 0;
}
- 删除映射和分割错误中的一个过去结束元素
- C++类错误中的模板专用化
- 模板元编程,简历资格错误中的冲突
- Cpp 错误中的<<运算符
- 功能错误中的表达语法帮助!C/C
- Cython将扩展模块传递到to_py_call_code错误中的python reults
- 在测试网上部署智能合约时合约错误中的未知操作 hi
- C 错误中的pronic号码
- 循环和运行时错误中的C 变量声明
- C++ 分段错误中的命令行参数
- 映射引发错误中的 C++ 自定义对象
- MSVC11 错误中的 x64 位 asm
- C++错误中的复合 [没有匹配调用 'push_back'的成员函数]
- 包括Code::Blocks错误中的多个标头
- c++编译器错误中的简单类继承
- 获取Openssl证书错误中的错误字符串
- <Mat> 函数逻辑错误中的向量传递
- 分配错误中的非 Ivalue
- 链接器错误中的"Symbol from plugin"
- 赋值错误中的非左值