0

我想这是一个两部分的问题,这就是为什么我找不到更合适的帖子标题的原因。

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

2 回答 2

3

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

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

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

两种解决方案都不是很好的风格,所以如果你只是将 a 添加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 的愚蠢绑定机制。

于 2011-04-19T17:09:05.217 回答
0

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

&Main::texture is what we call a member field pointer, not a pointer. You'd need to point to an actual instance of a texture, so

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

Extra / bonus

In the unlikely event that you actully wanted to use a member pointer, here is an example (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;
}
于 2011-04-19T18:56:52.703 回答