Trouble displaying texture

Hello everyone, I am trying to load texture using the SOIL library and display it on screen. I’m trying to make a simple 2D game but for some reason I’m unable to load the background. Could anyone please point out where I’m going wrong?

Here’s my load texture function:


void LoadTexture()
{
	int iWidth, iHeight;
	unsigned char* image = SOIL_load_image("background0.png", &iWidth, &iHeight, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glGenerateMipmap(GL_TEXTURE_2D);
	SOIL_free_image_data(image);
	glBindTexture(GL_TEXTURE_2D, 0);
}

Here’s where I’m doing several startups.

void Init()
{
//... (init glew etc...)
GLfloat Texture[] =
	{
		0.0f, 0.0f,  // Top Left
		1.0f, 0.0f,  // Top Right
		1.0f, 1.0f,  // Bottom Right
		0.0f, 1.0f   // Bottom Left
	};

        // Create vertex and fragment shader
	program = myShader.CreateProgram("VertexShader.vs", "FragShader.fs");

	// .... (generate buffers for vao, vbo etc...)

	glGenBuffers(1, &g_texture);
	glBindTexture(GL_TEXTURE_2D, g_texture);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

	LoadTexture();

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(2);
}

Here’s my render function:


void Render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glUseProgram(program);
	LoadTexture();
	glBindVertexArray(vao);
	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0); 
	glBindVertexArray(0);

	glutSwapBuffers();
}

And lastly, here are my shaders:

Fragment Shader:

#version 430 core

in vec4 outputColors;
in vec2 TexCoord;

out vec4 VertexColors;

uniform sampler2D aTexture;

void main()
{
	VertexColors = texture(aTexture, TexCoord) * outputColors; // <-- Displays black screen :( 
	//VertexColors = outputColors; <-- works (displays a colored quad)
}

Vertex Shader:

#version 430 core

in layout (location = 0) vec3 VertexPos;
in layout (location = 1) vec4 VertexColors;
in layout (location = 2) vec2 TextureCoord;

out vec4 outputColors;
out vec2 TexCoord;

void main()
{
	gl_Position = vec4(VertexPos, 1.0f);
	outputColors = VertexColors;
	TexCoord = TextureCoord;
}

The output of this is a black window.

All help is appreciated, thank you so much for reading my query.

What have you tried? Have you verified that the image loaded contains reasonable data? Have you checked for GL errors? Have you tried using your texture coordinates as the red/green color output values? Do they look reasonable? etc.

glGenBuffers(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_texture);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);

This, basically.

You’re generating a buffer, then you’re binding the buffer name as a texture, then you’re setting up your vertex attrib pointer.

I suspect that your intention is that this glBindTexture call really should be a glBindBuffer call, and then you also need to use glGenTextures and glBindTexture before your glTexImage call.