OpenGL中的错误C2664和C2597以及C ++中的DevIL

时间:2011-04-19 16:50:11

标签: c++ opengl devil c2664

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

我正在使用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 <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);
}

2 个答案:

答案 0 :(得分:3)

出现错误是因为您没有Main类型的实例(对象),但当然您需要Main才能使用其中的任何内容。

现在你有几种方法可以解决这个问题 - 看起来你已经尝试了一些。您可以通过在类范围内使用static或在命名空间范围内使用“普通”全局来使纹理变量成为全局变量。

对于第一种情况,您在.h文件声明您的班级中的static GLuint texture; > 定义它在.cpp文件中为GLuint Main::texture = 0;。您尝试此操作时似乎没有定义变量。 对于第二种情况,您在班级外的.h中将其声明为extern GLuint texture;,并在任何函数之外将其定义为GLuint texture

两种解决方案的风格都不是很好,所以如果你只是在你的Main类中添加一个GLuint texture,然后创建一个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类中的奖励积分,但这需要更多地处理OpenGLs愚蠢的绑定机制。

答案 1 :(得分:0)

  

错误C2664:'glGenTextures':无法将参数2从'GLuint Main :: *'转换为'GLuint *'

&Main::texture是我们所说的成员字段指针,而不是指针。你需要指向一个实际的纹理实例,所以

 Main* mymain; /* = new Main(...); somewehere in the code */
 glGenTextures(1, &mymain->texture);

额外/奖金

万一你真的想要使用成员指针,这是一个例子(codepad link):

#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: %i\n", a[2].*member);
    member = &A::q;
    printf("member q: %i\n", a[2].*member);
    member = &A::r;
    printf("member r: %i\n", a[2].*member);

    // ---- pointer to member functions

    int (A::*getter)();

    getter = &A::getp;

    x = (a[1].*getter)();
    printf("getter: %i\n", x);

    x = (a[2].*getter)();
    printf("getter: %i\n", x);

    return 0;
}