Playground.exe中0x69F1454A(nvoglv32.dll)抛出异常:0xC0000005:访问冲突读取位置0x0A08D000

时间:2017-10-31 12:02:38

标签: c opengl

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

Exception thrown at 0x69F1454A (nvoglv32.dll) in Playground.exe: 0xC0000005: Access violation reading location 0x0A08D000.

loadBMP_custom功能取自本网站: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/

任何帮助都将受到赞赏。

这是我的代码:

#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;
}

1 个答案:

答案 0 :(得分:2)

if (imageSize == 0)    imageSize = width*height * 3;
[...]
data = (unsigned char* )malloc(imageSize);
[...]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

你告诉OpenGL它应该将data指针解释为描述每个像素上带有RGBA分量的图像,每个像素都是字节,但是你只提供RGB数据,这意味着GL将访问超出结束你的缓冲区。

glTexImage的最后三个参数描述客户端内存中的图像数据:通道的数字和顺序,每个通道的数据格式以及指向第一个像素的指针。您用作GL_RGB的{​​{1}}仅描述了GL在内部存储纹理的格式。

正确的代码应该是

internalFormat