错误C2664和C2597在OpenGL和DevIL在C + + [英] error C2664 and C2597 in OpenGL and DevIL in C++

查看:428
本文介绍了错误C2664和C2597在OpenGL和DevIL在C + +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是一个两部分的问题,这就是为什么我找不到更合适的标题为这篇文章。

I guess this is a two part problem, which is why I couldn't find a more suitable title for the post.

我使用DevIL加载图像然后将其转换为熟悉的格式。这是我的代码我遇到麻烦的部分:

I am loading an image using DevIL and then converting it to a familiar format. Here is the part of my code I am having troubles with:

//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()方法,我得到

Now, for the glGenTextures() method, I get

error C2664: 'glGenTextures' : cannot convert parameter 2 from 'GLuint Main::* ' to 'GLuint *'

glBindTexure() p>

and for glBindTexure()

error C2597: illegal reference to non-static member 'Main::texture'

我尝试以许多不同的方式声明 texture

I have tried declaring texture in many different ways,

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函数的错误(让我知道,如果你想我张贴他们)。

both in my .cpp file and my .h file, but none of it works. If I try to declare it in the .h file as static Gluint Main::texture, I get the LNK2019: unresolved external symbol__imp_ error for every DevIL function (let me know if you want me to post them as well).

我很漂亮这是代码中的东西,而不是我的依赖或我的.lib或.dll文件不在正确的地方。

I am pretty it is something in the code as opposed to my dependencies or my .lib or .dll files not being in the right place. So what am I doing wrong?

头文件

p>

The header file

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);
}


推荐答案

没有类型 Main 的实例(对象),但是当你需要一个 Main 从它。

Your error appear because you don't have an instance (object) of type Main, but of course you need a Main before you can use anything from it.

现在你有几个选项来解决这个问题 - 它看起来像你已经尝试了几个。你可以使纹理变量为全局变量,或者通过在类范围中使用 static ,或者在命名空间范围使用普通全局变量。

Now you have several options to fix this - and it appears like you already tried a few. You can make the texture variable global, either by making it static at class-scope, or by using an "ordinary" global at namespace scope.

对于第一种情况,您可以在您的类中声明该变量为 static GLuint texture; 。 h文件,您可以<.c> 将其定义为 GLuint Main :: texture = 0; 看来你没有定义的变量,当你尝试这个。
对于第二种情况,你在你的类中声明为 extern GLuint texture; ,并将它定义为 GLuint texture

For the first case, you'd declare the variable as static GLuint texture; in your class in your .h file and you define it as GLuint Main::texture = 0; in your .cpp file. It seems you didn't define the variable when you tried this. For the second case, you declare it as extern GLuint texture; in your .h outside of your class and define it as GLuint texture outside of any function.

两个解决方案都不是很好的风格,所以如果你只是添加一个 GLuint texture 到你的Main类,然后创建一个Main实例并使用它的成员变量 - 最好从Main中的一个函数,所以你可以使用 this

Both solutions aren't very good style, so it would be better if you just added a GLuint texture to your Main class, then create a Main instance and used its member variable - preferably from a function within Main, so you can use this.

理想情况下,你应该有像 Texture 这样的类:

Ideally, you'd have something like a Texture class like this:

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类,但这需要处理更多的OpenGLs愚蠢的绑定机制。

bonus points for moving more functionality into the Texture class, but that requires dealing more with OpenGLs stupid bind mechanic.

这篇关于错误C2664和C2597在OpenGL和DevIL在C + +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆