游乐场中0x69F1454A(nvoglv32.dll)处引发异常。exe:0xC0000005:访问冲突读取位置0x0A08D000

游乐场中0x69F1454A(nvoglv32.dll)处引发异常。exe:0xC0000005:访问冲突读取位置0x0A08D000,c,opengl,C,Opengl,我想显示纹理,但编译器(Visual Studio 2017)给了我以下错误: 在playerdy.exe中的0x69F1454A(nvoglv32.dll)处引发异常:0xC0000005:访问冲突读取位置0x0A08D000。 loadBMP_自定义函数已从此网站获取: 任何帮助都将受到感激 这是我的代码: #include <GL\freeglut.h> #include <stdio.h> GLuint loadBMP_custom(const char

我想显示纹理,但编译器(Visual Studio 2017)给了我以下错误:

在playerdy.exe中的0x69F1454A(nvoglv32.dll)处引发异常:0xC0000005:访问冲突读取位置0x0A08D000。

loadBMP_自定义函数已从此网站获取:

任何帮助都将受到感激

这是我的代码:

#include <GL\freeglut.h>

#include <stdio.h>


GLuint loadBMP_custom(const char * imagepath) {

unsigned char header[54]; 

unsigned int width, height;
unsigned int imageSize; 

GLuint textureID;  

unsigned char * data;

FILE * file;

file = fopen(imagepath, "rb");

printf("%s\n", imagepath);


if (!file) { 
printf("Image could not be opened\n"); 
return 0; 
}


if (fread(header, 1, 54, file) != 54) { 
    printf("Not a correct BMP file\n");
}

if (header[0] != 'B' || header[1] != 'M') {
    printf("Not a correct BMP file\n");
}


imageSize = *(int*)&(header[0x22]);     //34
width = *(int*)&(header[0x12]);         //18
height = *(int*)&(header[0x16]);        //22


if (imageSize == 0)    imageSize = width*height * 3;




data = (unsigned char* )malloc(imageSize);


fread(data, 1, imageSize, file);

fclose(file);

for (int i = 0; i < width * height; ++i)
{
    int index = i * 3;
    unsigned char B, R;
    B = data[index];
    R = data[index + 2];

    data[index] = R;
    data[index + 2] = B;

}

glGenTextures(1, &textureID);


glBindTexture(GL_TEXTURE_2D, textureID);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, 
GL_UNSIGNED_BYTE, data);    

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

return textureID;
}


void displayMe(void) {

GLuint bmp;




glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);



glPushMatrix();

bmp = loadBMP_custom("C:\\Dev\\Playground\\Release\\template.bmp");

glBindTexture(GL_TEXTURE_2D, bmp);

glBegin(GL_POLYGON);

glTexCoord2f(0.0, 0.0);
glVertex2f(0.0, 0.0); 

glTexCoord2f(0.1, 0.0);
glVertex2f(0.1, 0.0);                    

glTexCoord2f(0.1, 0.1);
glVertex2f(0.1, 0.1);                    

glTexCoord2f(0.0, 0.1);
glVertex2f(0.0, 0.1);                    
glEnd();


glPopMatrix();

glutSwapBuffers();
}







void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);                   

glutCreateWindow("hi");   

glutDisplayFunc(displayMe);

glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}
#包括
#包括
GLuint loadBMP_自定义(常量字符*图像路径){
无符号字符头[54];
无符号整数宽度、高度;
无符号整数图像大小;
胶合蛋白;
无符号字符*数据;
文件*文件;
file=fopen(imagepath,“rb”);
printf(“%s\n”,imagepath);
如果(!file){
printf(“无法打开图像\n”);
返回0;
}
if(fread(头,1,54,文件)!=54){
printf(“不是正确的BMP文件\n”);
}
如果(标题[0]!='B'| |标题[1]!='M'){
printf(“不是正确的BMP文件\n”);
}
imageSize=*(int*)&(头[0x22]);//34
宽度=*(int*)&(头[0x12]);//18
高度=*(int*)&(页眉[0x16]);//22
如果(imageSize==0)imageSize=宽度*高度*3;
数据=(无符号字符*)malloc(图像大小);
fread(数据,1,图像大小,文件);
fclose(文件);
对于(int i=0;i
您告诉OpenGL它应该将
数据
指针解释为在每个像素处描述带有RGBA组件的图像,每个组件都是字节,但您只提供RGB数据,这意味着GL将在缓冲区结束后访问

glTexImage
的最后三个参数描述客户机内存中的图像数据:通道的编号和顺序、每个通道的数据格式以及指向第一个像素的指针。作为
internalFormat
使用的
GL\u RGB
仅描述了GL应在内部存储纹理的格式

正确的代码应该是

glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // use tightly-packed data buffer with no padding between image rows
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

请花时间识别您的代码并始终使用空行。请格式化您的示例代码。但在此之前:试着找到一个显示错误的最小示例。我们不是“修复我的应用程序”团队。这是程序执行时的运行时错误。因此,它与编译器提供的任何内容无关。显示更多代码,如分配
数据
。在每个帧中重新加载纹理是一个糟糕的想法。在主循环之前做一次。您应该找出异常发生在哪一行。使用调试器并逐步执行,直到崩溃。
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // use tightly-packed data buffer with no padding between image rows
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);