0

これは2つの部分からなる問題だと思います。そのため、投稿に適したタイトルを見つけることができませんでした。

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ファイルでそれをとして宣言しようとすると、すべてのDevIL関数でエラーstatic Gluint Main::textureLNK2019: 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);
}
4

2 に答える 2

3

type のインスタンス(オブジェクト)がないため、エラーが表示されますMainが、もちろん、Mainそれから何かを使用する前に a が必要です。

これを修正するためのいくつかのオプションがあります - そして、あなたはすでにいくつか試したようです. staticクラス スコープで作成するか、ネームスペース スコープで「通常の」グローバルを使用することにより、テクスチャ変数をグローバルにすることができます。

最初のケースでは、.h ファイルのクラスで変数を宣言し、.cpp ファイルで定義ます。これを試したときに変数を定義していないようです。2 番目のケースでは、クラスの外側で .hとして宣言し、関数の外側で定義します。static GLuint texture;GLuint Main::texture = 0;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 のばかげたバインド メカニズムをより多く処理する必要があります。

于 2011-04-19T17:09:05.217 に答える
0

エラー C2664: 'glGenTextures': パラメーター 2 を 'GLuint Main::* ' から 'GLuint *' に変換できません

&Main::textureは、ポインターではなく、メンバー フィールド ポインターと呼ばれるものです。テクスチャの実際のインスタンスを指す必要があるため、

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

おまけ・おまけ

めったにないことですが、実際にメンバー ポインターを使用したい場合は、次の例を参照してください ( codepad リンク)。

#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 に答える