Hello I've recently worked with OpenGL and SDL and I was adding textures to my GL_QUADS and when I ran my program I came across with a runtime error. I've searched all over the internet for a fix but I couldn't find anything so I guess I had one more option. Asking here.
So here is some of my code.
int loadTexture(std::string fileName){
SDL_Surface *image=IMG_Load(fileName.c_str());
SDL_DisplayFormatAlpha(image);
unsigned int id;
glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,&id);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image->w,image >h,0,GL_RGBA,GL_UNSIGNED_BYTE,image->pixels);
SDL_FreeSurface(image);
return id;
}
That's my loadTexture function.
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 800.0 / 600.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
tex=loadTexture("test.png");
}
That's my init function for OpenGL. Btw I have declared my tex variable.
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(rotation, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0);
glVertex3f(-2.0, 2.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(2.0, 2.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(2.0, -2.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(-2.0, -2.0, 0.0);
glEnd();
}
That's my render function for all my OpenGL render stuff...
The render function is called in the main function which contains a game loop.
Here is the runtime error when I run it with Visual C++
Unhandled exception at 0x004ffee9 in OpenGL Crap.exe: 0xC0000005: Access violation reading location 0x05c90000.
So I have only had this error when I added textures...
...
So I found where the error occured it was at this line
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image->w,image->h,0,GL_RGBA,GL_UNSIGNED_BYTE,image->pixels);
but I have totally no Idea what could it be.
Update
Only thanks to zero298