1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
GLuint VAO;
GLuint vertexVBO;
GLuint colorVBO;
GLuint textureVBO;
GLuint EBO;
GLuint textureID;
glGenTextures(1, &textureID);
SDL_Surface *textureData = 0;
textureData = IMG_Load("Crate.jpg");
if (!textureData)
{
std::cout << "Error loading texture from file" << std::endl;
}
else
{
std::cout << "Texture loaded from file successfully" << std::endl;
}
glBindTexture(GL_TEXTURE_2D, textureID);
unsigned char* pixels = (unsigned char*)textureData->pixels; //The program keeps breaking here with "Exception Thrown"
unsigned int width = textureData->w;
unsigned int height = textureData->h;
unsigned int depth = textureData->format->BytesPerPixel;
unsigned int format = ((depth == 4) ? GL_RGBA : GL_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, pixels);
SDL_FreeSurface(textureData);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
GLfloat UVs[] = { 0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f };
GLuint textureAttributeID = glGetAttribLocation(TheShader::Instance()->GetShaderProgram(), "textureUV");
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &vertexVBO);
glGenBuffers(1, &colorVBO);
glGenBuffers(1, &textureVBO);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, textureVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(UVs), UVs, GL_DYNAMIC_DRAW);
glVertexAttribPointer(textureAttributeID, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(textureAttributeID);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, textureID);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); //Program breaks during runtime here if I am compiling on an Nvidia machine with an unhandled exception (nvoglv32.dll).
glBindVertexArray(0);
|