在 HP Pavilion g6 上使用带有 Visual Studio 2010 Professional 的 Windows 7 Home Premium 64 位。代码是用 C/C++ 编写的。
链接:
Additional Include Directories: N/A - Everything needed is in the VS2010 Include Directory
Additional Library Directories: N/A - Everything needed is in the VS2010 Lib Directory
Additional Dependencies (Debug Mode): glew32d.lib;glew32sd.lib;glu32.lib;opengl32.lib;gdi32.lib;winmm.lib;user32.lib;
代码:
#include "stdafx.h"
#include <stdio.h>
#include <GL\glew.h>
#include <GL\wglew.h>
#include <GL\glut1.h>
#include <GL\glext.h>
using namespace std;
int main(int argc, char** argv)
{
glutInit(&argc, argv);
const unsigned char * version ;
version = (const unsigned char *)glGetString(GL_VERSION);
printf ("My OpenGL version is %s\n", version); //Comment this out for error in Glut, leave for the other 2 errors
// Obtain a buffer identifier from OpenGL
GLuint bufferID = 0;
glGenBuffers( 1, &bufferID ); //Comment out for no reported errors
return 0;
}
报告的错误:
glut.h:
First-chance exception at 0x00000000 in gl_crap3.exe: 0xC0000005: Access violation.
Unhandled exception at 0x76fe15de in gl_crap3.exe: 0xC0000005: Access violation.
Error occurs on line 486 - static void APIENTRY glutInit_ATEXIT_HACK(int *argcp, char **argv) { __glutInitWithExit(argcp, argv, exit); }
osfinfo.c:
First-chance exception at 0x00000000 in gl_crap3.exe: 0xC0000005: Access violation.
Unhandled exception at 0x76fe15de in gl_crap3.exe: 0xC0000005: Access violation.
Error occurs on line 467 - return retval; // in function, int __cdecl __lock_fhandle (int fh)
dbgheap.c:
First-chance exception at 0x00000000 in gl_crap3.exe: 0xC0000005: Access violation.
Unhandled exception at 0x76fe15de in gl_crap3.exe: 0xC0000005: Access violation.
Error occurs on line 504 - __finally {_munlock(_HEAP_LOCK);}
mlock.c:
First-chance exception at 0x00000000 in gl_crap3.exe: 0xC0000005: Access violation.
Unhandled exception at 0x76fe15de in gl_crap3.exe: 0xC0000005: Access violation.
Error occurs on line 375 - } // end of function, void __cdecl _unlock (int locknum)
其他明显的症状:
- 当 printf 命令未注释掉时,崩溃在osfinfo.c、dbgheap.c 和 mlock.c 之间交替出现
- 当 printf IS注释掉时,只会在 glut.h 上崩溃
- 当glGenBuffers被注释掉时,没有未处理的异常错误——程序不会崩溃
- printf 语句的输出是:My OpenGL version is (null) - 无论程序是否崩溃,都会发生
我完全被这个难住了,互联网上似乎没有任何东西可以帮助我。我什至首先专注于尝试解决“OpenGL version = null”问题,但我空手而归,因为我要么错误地遵循了这个网站上的建议(对 OpenGL 渲染上下文没有足够好的理解),要么这个问题是0xC0000005 错误的症状。
编辑:由于这个问题似乎是由多个问题引起的,我决定放置我的代码的更新版本以及我从中得到的相应错误。到目前为止,感谢您对我的帮助,但 glGenBuffers 似乎只是对我怀恨在心之类的。
新代码:
#include "stdafx.h"
#include <cmath>
#include <math.h>
#include <iostream>
#include <GL\glew.h>
#include <GL\glut.h>
#include <GL\glext.h>
#include <GL\wglew.h>
#include <GL\wglext.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
using namespace std;
void drawScene();
void handleKeypress(unsigned char key, int x, int y);
void handleMouse(int button, int state, int x, int y);
void handleResize(int w, int h);
void update(int value);
void printw (float x, float y, float z, char* format, ...);
GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(960, 460);
glutCreateWindow("Piece of Shit");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutMouseFunc(handleMouse);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
void drawScene()
{
double x, z;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
GLuint bufferID[1];
GLenum err = glewInit();
if (GLEW_OK != err)
printw(-6, 1.5, -10, "Fail: %s\n", glewGetErrorString(err));
printw(-3, 0.3, -10, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glGenBuffers( 1, &bufferID[0] ); // comment out and you have no problems********************************
glutSwapBuffers();
}
void handleKeypress(unsigned char key, int x, int y)
{
}
void handleMouse(int button, int state, int x, int y)
{
}
void handleResize(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
void update(int value)
{
glutPostRedisplay();
glutTimerFunc(20, update, 0);
}
void printw (float x, float y, float z, char* format, ...)
{
va_list args; // Variable argument list
int len; // String length
int i; // Iterator
char* text; // Text
// Initialize a variable argument list
va_start(args, format);
// Return the number of characters in the string referenced the list of arguments.
// _vscprintf doesn't count terminating '\0' (that's why +1)
len = _vscprintf(format, args) + 1;
// Allocate memory for a string of the specified size
text = (char*) malloc (len * sizeof(char));
// Write formatted output using a pointer to the list of arguments
vsprintf_s(text, len, format, args);
// End using variable argument list
va_end(args);
// Specify the raster position for pixel operations.
glRasterPos3f (x, y, z);
// Draw the characters one by one
for (i = 0; text[i] != '\0'; i++)
glutBitmapCharacter(font_style, text[i]);
// Free the allocated memory for the string
free(text);
}
错误:gl_crap3.obj:错误 LNK2001:未解析的外部符号 ___glewGenBuffers