#version 330 core
out vec4 FragColor;
in vec2 tex;
uniform sampler2D tex0; // for the first object
uniform sampler2D tex1; // for the second object
uniform sampler2D tex2; // for the third object
uniform sampler2D tex3; // etc..
uniform sampler2D tex4;
uniform sampler2D tex5;
uniform int selectedTexture;
void main() {
if (selectedTexture == 0)
FragColor = texture(tex0, tex);
else if (selectedTexture == 1)
FragColor = texture(tex1, tex);
else if (selectedTexture == 2)
FragColor = texture(tex2, tex);
else if (selectedTexture == 3)
FragColor = texture(tex3, tex);
else if (selectedTexture == 4)
FragColor = texture(tex4, tex);
else if (selectedTexture == 5)
FragColor = texture(tex5, tex);
}
Above is my Fragment Shader
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
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_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load("Images/UImenu/Campaign.png", &width, &height, &nrChannels, STBI_rgb_alpha);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
//ALTERNATIVE TEX WHEN HOVERING ON CAMPAIGN BAR
glGenTextures(1, &texture1hover);
glBindTexture(GL_TEXTURE_2D, texture1hover);
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_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
data = stbi_load("Images/UImenu/CampaignHover.png", &width, &height, &nrChannels, STBI_rgb_alpha);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
Above are my textures loaded properly and without any errors.
void bindTexturesToSamplers(GLuint shaderProgram) {
glUniform1i(glGetUniformLocation(shaderProgram, "tex0"), 0);
glUniform1i(glGetUniformLocation(shaderProgram, "tex1"), 1);
glUniform1i(glGetUniformLocation(shaderProgram, "tex2"), 2);
glUniform1i(glGetUniformLocation(shaderProgram, "tex3"), 3);
glUniform1i(glGetUniformLocation(shaderProgram, "tex4"), 4);
glUniform1i(glGetUniformLocation(shaderProgram, "tex5"), 5);
}
Then I’m calling this above before I use my shader program.
if (xpos > campaignSquare.position.x - campaignSquare.width && xpos < campaignSquare.position.x + campaignSquare.width && ypos > campaignSquare.position.y - campaignSquare.height
&& ypos < campaignSquare.position.y + campaignSquare.height) {
glActiveTexture(GL_TEXTURE0 + 5);
glBindTexture(GL_TEXTURE_2D, tex1hover);
glUniform1i(glGetUniformLocation(shaderProgram, "selectedTexture"), 5);
glBindVertexArray(vao1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
else {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
glUniform1i(glGetUniformLocation(shaderProgram, "selectedTexture"), 0);
glBindVertexArray(vao1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
And above you have the code that checks for collisions, and when one happens, my image is swapped. But in fact, it never swaps, it turns completely black. I checked the Fragment Shader and had it swap the image for color red, and it works! However it doesn’t work with the .png. I tried using the same .png to see if there was something wrong it mine, and it doesn’t work. What’s the issue?