Runtime Error in basic OpenGL/SDL shader program

Hi, I'm using C++ to try and create a basic shader program with OpenGL and SDL. So far I've been able to create a window and render a multi-colored box on screen. Now I've decided to go one step further and add a texture onto that square. So from what I've followed and understood, I should have all the necessary code to get a texture to show up, although it keeps breaking at a certain point each time! (It also seems to not be able to run on Nvidia machines)

Below I've shown all the code from my Texture class with comments next to the lines that give me run time errors. the GetShaderProgram function gets GLuint m_shaderProgramID from my shader class.

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);
What is the error given?
I'm currently working on an AMD PC so I can't find the error I'm getting with the "glDrawElements" (line 66) although it was very similar to this:
Unhandled exception at 0x5D9F74E3 (nvoglv32.dll) in Application.exe: 0xC0000005: Access violation reading location 0x00000000.

Other than that it seems to compile although nothing is being drawn (just getting a black screen in the SDL window).
Does your program have an associated console window?

There is nothing to keep execution from reaching line 25 if the condition on line 14 is true, meaning that the call to IMG_Load was unsuccessful, and that you are dereferencing a null pointer value on line 25 (coincidentally, exactly what that access violation is complaining of.)
Topic archived. No new replies allowed.