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

     2023-02-16     305

关键词:

【中文标题】OpenGL 中的错误 C2664 和 C2597 以及 C++ 中的 DevIL【英文标题】:error C2664 and C2597 in OpenGL and DevIL in C++ 【发布时间】:2011-04-19 16:50:10 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

您的错误出现是因为您没有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 类的奖励积分,但这需要更多地处理 OpenGL 愚蠢的绑定机制。

【讨论】:

我想我会尝试你提出的第一个解决方案,只是为了简单起见,稍后当我有一个工作代码时,我会开始使用纹理类。但是,链接器错误仍然存​​在,我感觉它可能与纹理变量无关。你知道它可能是什么吗,或者你想让我发布我到目前为止的代码吗? 您是否在链接器导入中包含 DevIL .lib 文件? (这是未解决外部问题的典型原因) 是的,包括那个。我不知道它是什么【参考方案2】:

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

&amp;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;

【讨论】:

这让我很困惑......所以我必须创建一个 Main 类的实例?我以为我不必那样做……那有什么改变?我现在是否必须编辑我所有的方法,使它们不是 Main::[methodname] ?我将编辑我的原始帖子以显示我到目前为止的内容,如果您能指出我需要更改的内容,我将非常感激。

为啥我会收到错误错误 C2664:'reverseString'

】为啥我会收到错误错误C2664:\\\'reverseString\\\'【英文标题】:WhydoigeterrorerrorC2664:\'reverseString\'为什么我会收到错误错误C2664:\'reverseString\'【发布时间】:2014-07-1403:50:25【问题描述】:#include<iostream>#include<string>#include<... 查看详情

glEnableVertexAttribArray 中“索引”参数的含义和(可能)OS X OpenGL 实现中的错误

...glEnableVertexAttribArray中“索引”参数的含义和(可能)OSXOpenGL实现中的错误【英文标题】:Meaningof"index"parameteringlEnableVertexAttribArrayand(possibly)abugintheOSXOpenGLimplementation【发布时间】:2015-01-2618:39:30【问题描述】:1)我是否正... 查看详情

OSX 和 OpenGL:错误:函数 fgOpenWindow 中的内部错误 <FBConfig with required capabilities not found>

】OSX和OpenGL:错误:函数fgOpenWindow中的内部错误<FBConfigwithrequiredcapabilitiesnotfound>【英文标题】:OSXandOpenGL:ERROR:Internalerror<FBConfigwithnecessarycapabilitiesnotfound>infunctionfgOpenWindow【发布时间】:2013-05-1900:22:02【问 查看详情

错误 C2664:无法将参数 1 从“int”转换为“int []”

】错误C2664:无法将参数1从“int”转换为“int[]”【英文标题】:errorC2664:cannotconvertparameter1from\'int\'to\'int[]\'【发布时间】:2014-03-0315:13:49【问题描述】:#include<iostream>usingnamespacestd;classaminprivate:constintlength=10;intnewArray[lengt 查看详情

错误 C2664:无法将参数 1 从“int”转换为“int (__cdecl *)(int)”

】错误C2664:无法将参数1从“int”转换为“int(__cdecl*)(int)”【英文标题】:errorC2664:cannotconvertparameter1from\'int\'to\'int(__cdecl*)(int)\'【发布时间】:2011-06-1314:52:09【问题描述】:在将函数作为另一个函数的参数传递时遇到了一些问题... 查看详情

VS 错误 C2664(从函数返回字符串)C++

】VS错误C2664(从函数返回字符串)C++【英文标题】:VSerrorC2664(returnstringfromafunction)C++【发布时间】:2014-07-1317:58:10【问题描述】:我确定这在网站上的某个地方得到了回答,但找不到它......我正在用C++在VS10下编写。我正在写一... 查看详情

错误 C2664:“SQLGetData”:无法将参数 6 从“SDWORD *”转换为“SQLLEN *”

】错误C2664:“SQLGetData”:无法将参数6从“SDWORD*”转换为“SQLLEN*”【英文标题】:errorC2664:\'SQLGetData\':cannotconvertparameter6from\'SDWORD*\'to\'SQLLEN*\'【发布时间】:2012-12-1722:03:33【问题描述】:我有在VisualStudio2008上以32位模式编译的... 查看详情

OpenGL 中的可视化问题(glOrtho 和投影)

】OpenGL中的可视化问题(glOrtho和投影)【英文标题】:VisualizationIssueinOpenGL(glOrtho&projection)【发布时间】:2016-11-0908:51:58【问题描述】:我不知道我是否在我的函数中做错了什么(可能是glOrtho的参数错误),所以结果是错误... 查看详情

从 YouTube 复制的 OpenGL 着色器中的语法错误

】从YouTube复制的OpenGL着色器中的语法错误【英文标题】:SyntaxerrorinOpenGlshaderthatiscopiedfromYouTube【发布时间】:2020-05-0721:53:17【问题描述】:我尝试学习OpenGL,现在我想在黑色背景上用顶点和着色器制作一个简单的红色三角形。... 查看详情

错误 C2664:“MessageBoxA”:无法将参数 2 从“std::string”转换为“LPCSTR”[重复]

】错误C2664:“MessageBoxA”:无法将参数2从“std::string”转换为“LPCSTR”[重复]【英文标题】:errorC2664:\'MessageBoxA\':cannotconvertparameter2from\'std::string\'to\'LPCSTR\'[duplicate]【发布时间】:2013-09-1911:59:08【问题描述】:我在C++中试过这个... 查看详情

错误 C2664 'void IVerify::SetParams(void)':无法将参数 1 从 'std::wstring' 转换为 'wchar_t *'

】错误C2664\\\'voidIVerify::SetParams(void)\\\':无法将参数1从\\\'std::wstring\\\'转换为\\\'wchar_t*\\\'【英文标题】:Error C2664 \'voidIVerify::SetParams(void)\':cannotconvertargument1from\'std::wstring\'to\'wchar_t*\'错误C2664\'voidIVerify: 查看详情

在窗口标题和消息框中显示错误的语言:win32 应用程序,opengl

...在窗口标题和消息框中显示错误的语言:win32应用程序,opengl【英文标题】:ShowingWrongLanguageinwindowtitleandmessagebox:win32application,opengl【发布时间】:2014-11-0407:51:23【问题描述】:我试图在VisualStudio2013中使用opengl和win32应用程序创建... 查看详情

使用 OpenGL 的 C++ 中的原始重启索引错误

】使用OpenGL的C++中的原始重启索引错误【英文标题】:PrimitiveRestartIndexerrorinC++withOpenGL【发布时间】:2011-07-2321:03:10【问题描述】:我只是在学习OpenGL,并尝试实现一个简单的测试来测试PrimitiveRestartIndex的工作原理。无论我尝试... 查看详情

如何使用 jogl 2 中的索引和 OpenGL 3.3 显示 3D 对象

】如何使用jogl2中的索引和OpenGL3.3显示3D对象【英文标题】:Howdoyoudisplaya3Dobjectusingindicesinjogl2withOpenGL3.3【发布时间】:2014-10-0105:33:11【问题描述】:我尝试编写一个脚本来使用JOGL2和OpenGL3.3显示基本的3D对象/多边形三角形,但是... 查看详情

Mac 的 Visual Studio 代码中的 OpenGL 符号突出显示错误

】Mac的VisualStudio代码中的OpenGL符号突出显示错误【英文标题】:OpenGLsymbolshighlightingerrorinvisualstudiocodeforMac【发布时间】:2017-05-2214:16:42【问题描述】:当我在Mac的vscode中编写c++代码时,编辑器一直显示OpenGL符号的红色下划线警告... 查看详情

OpenGL 和 GLFW 中的主要和次要是啥意思?

】OpenGL和GLFW中的主要和次要是啥意思?【英文标题】:WhatdoesmajorandminormeaninOpenGLwithGLFW?OpenGL和GLFW中的主要和次要是什么意思?【发布时间】:2017-04-2723:17:52【问题描述】:我在一些使用集成显卡的戴尔PC上创建了一个带有GLFW的O... 查看详情

无法捕捉 OpenGL 和顶点数组对象的错误

】无法捕捉OpenGL和顶点数组对象的错误【英文标题】:TroublecatchingmistakewithOpenGLandVertexArrayObjects【发布时间】:2012-12-0712:18:00【问题描述】:我在发现这个OpenGL实现的错误时遇到了麻烦。当我运行程序时,我唯一得到的是黑屏。... 查看详情

“不支持请求的 OpenGL 实现。实现:1”pyqt5 中的错误

】“不支持请求的OpenGL实现。实现:1”pyqt5中的错误【英文标题】:"RequestedOpenGLimplementationisnotsupported.Implementation:1"errorinpyqt5【发布时间】:2020-07-1517:14:28【问题描述】:当我运行这段代码时fromPyQt5.QtCoreimport*fromPyQt5.QtWidg... 查看详情