glutBitmapString 访问冲突

发布于 2024-10-28 01:03:13 字数 896 浏览 5 评论 0原文

(操作系统:Windows 7,编译器:Visual Studio 2010 C++ 编译器)

我有一个正确工作的 OpenGL 程序,可以绘制一些球体和模型,应用一些着色器等..等等。

现在我认为添加一些文本会很好,所以我在我的绘制方法中添加了以下三行:

glColor3f(0.5f, 0.5f, 0.5f);
glRasterPos2f(0, 0);
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (unsigned char*)"some text");

现在不知何故,这一切都使我的程序陷入无限的“访问冲突”循环,我似乎无法修复。我什至评论了所有其他绘制代码,只输出文本,但它仍然给出访问冲突错误,我在这里不知所措,因为似乎没有什么会影响这一点。那么有人对如何解决这个问题有一些指示吗?))?

我可以发布我所有的绘制代码,但我什至尝试了一个空项目,所以我很确定它不是其余的代码。

编辑:我尝试进一步缩小错误范围,似乎 glRasterPos2f 引发了访问冲突(很奇怪)。它不在任何 glBegin 和 glEnd 调用之间,并且不存在 OpenGL 错误。

编辑2:经过一些建议后,我尝试了以下代码,消除了访问冲突,但仍然没有显示任何文本

glColor3f(0.5f, 0.5f, 0.5f);
glRasterPos2f(0.0f, 0.0f);
std::string str("Hello World");
char* p = new char[strlen(str.c_str() + 1)];
strcpy(p, str.c_str());
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)p);

(OS: Windows 7, Compiler: Visual Studio 2010 C++ compiler)

I've got a correctly working OpenGL program that draws some spheres and models, applies some shaders etc.. etc..

Now I thought it would be nice to add some text, so I added the following three lines to my draw method:

glColor3f(0.5f, 0.5f, 0.5f);
glRasterPos2f(0, 0);
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (unsigned char*)"some text");

Now somehow this all makes my program get stuck in an infinite "access violation" loop, which I can't seem to fix. I even commented all the other draw code out, to just output the text, and it still gives the access violation error, I'm at a loss here because there is nothing that seems to affect this. So does anybody have some pointers ;)) on how to fix this issue?

I could post all my draw code, but I even tried an empty project, so I'm pretty sure it's not the rest of the code.

Edit: I tried narrowing down the error even more, and it seems that glRasterPos2f is throwing the acces violation (weirdly enough). It's not between any glBegin and glEnd calls, and there is no OpenGL error.

Edit2: After some advice I tried the following code, I got rid of the access violation, but still no text is displayed

glColor3f(0.5f, 0.5f, 0.5f);
glRasterPos2f(0.0f, 0.0f);
std::string str("Hello World");
char* p = new char[strlen(str.c_str() + 1)];
strcpy(p, str.c_str());
glutBitmapString(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)p);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街女流氓 2024-11-04 01:03:13

glutBitmapString 的类型为 const unsigned char*< /code> 而不是 unsigned char*。那会有帮助吗?我在字符串转换方面也遇到了问题。

我发现我没有关闭字符串,因为 unsigned char* 应该比 string 长 1。下面是一个片段,它解决了具有相同参数类型的另一种方法的问题:

string fullPath = TextureManager::s_sTEXTUREFOLDER + filename;
char *filePath = new char[strlen(fullPath.c_str())+1];
strcpy(filePath,fullPath.c_str());

然后您就得到了 char*

glutBitmapString is of type const unsigned char* and not of unsigned char*. Would that help? I had problems with string casting as well.

And I found out, that I wasn't closing my string, because the unsigned char* should be 1 longer than a string. Here was a snippet that solved it for another method with the same parameter type:

string fullPath = TextureManager::s_sTEXTUREFOLDER + filename;
char *filePath = new char[strlen(fullPath.c_str())+1];
strcpy(filePath,fullPath.c_str());

And then you have your char*.

垂暮老矣 2024-11-04 01:03:13

我最近在尝试使用 glBitmapString() 时遇到了同样的错误。我用的是vs2008。我在函数调用处设置了一个断点并单步执行(使用 freeglut_font.c)。在里面我注意到异常是在被描述为未初始化的情况下抛出的。因此,在我的初始化函数中,我添加了...

char* argv[] = {"some","stuff"};    int argc=2;
glutInit(&argc,argv);   
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);

当然,您可以使用您喜欢的任何 argc/argv 。这以及 Marnix 的建议为我解决了异常错误。

请注意,我实际上并没有创建一个带有 glut 的窗口。

I recently got the same error while attempting to use glBitmapString(). I was using vs2008. I set a breakpoint at the function call and stepped into it (using freeglut_font.c). Inside I noticed that the exception was being thrown upon what was described to be glut not being initialized. So inside my initialization function I added...

char* argv[] = {"some","stuff"};    int argc=2;
glutInit(&argc,argv);   
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);

Where of course you can use whatever argc/argv you please. This, as well as what was suggested by Marnix solved the exception errors for me.

Note that I don't actually create a window with glut.

雨落星ぅ辰 2024-11-04 01:03:13

尝试将字符串放入临时变量中。你必须施放的事实应该引起警惕。

glColor3f(0.5f, 0.5f, 0.5f);
glRasterPos2f(0, 0);
unsigned char* s = "some text";
glutBitmapString(GLUT_BITMAP_HELVETICA_12, s);

如果 const 不起作用,则 glutBitmapString() 可能正在修改字符串。

Try putting the string in a temporary variable. The fact that you have to cast should raise a red flag.

glColor3f(0.5f, 0.5f, 0.5f);
glRasterPos2f(0, 0);
unsigned char* s = "some text";
glutBitmapString(GLUT_BITMAP_HELVETICA_12, s);

if const doesn't work, then glutBitmapString() may be modifying the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文