I have code trying load/display an image and keep getting this Unhandled Exception Error. I think its because the image is not a multiple of 4 or something like that (no idea) here is my code:
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_opengl.h"
#include <math.h>
#include <stdio.h>
int loadImage();
void drawImage();
extern SDL_Event event;
GLuint texture = NULL;
GLenum texture_format=NULL;
GLint nofcolors;
int loadImage()
{
SDL_Surface* surface;
if((surface = IMG_Load("AvatarBasicCharacterModel.jpg"))){
//get number of channels in the SDL surface
nofcolors = surface -> format -> BytesPerPixel;
//contains an alpha channel
if(nofcolors == 4)
{
if ( surface -> format -> Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if ( nofcolors == 3) //no alpha channel
{
if(surface ->format->Rmask==0x000000ff)
texture_format=GL_RGB;
else
texture_format=GL_BGR;
}
//Have Opengl generate a texture object handle for us
glGenTextures(1, &texture);
//Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture);
//Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface -> w, surface ->h, 0, texture_format, GL_UNSIGNED_BYTE, surface -> pixels );
}
else //could not load image
SDL_Quit();
return 1;
if (surface){
SDL_FreeSurface(surface);
}
}
void drawImage()
{
//Clear the screen before drawing
glClear( GL_COLOR_BUFFER_BIT);
//Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(60,700,0.0f);
glBegin(GL_QUADS);
//Set color
glColor4f(0.0,0.0,1.0,0.0);
glTexCoord2i(1,1);
glVertex2f(150.0,0);//bottom right
glTexCoord2i(0,1);
glVertex2f(150.0,-50);//top right
glTexCoord2i(0,0);
glVertex2f(0.0,-50);//top left
glTexCoord2i(1,0);
glVertex2f(0.0,0);//bottom left
glEnd();
glPopMatrix();
}
I want to A) Understand why the image needs to be a certain width/hieght and B) How to make it work.
The alpha channels and stuff. Not following.