I’m back again finally after learning more about glsl 3.3/opengl 3.3 and I’ve been trying to get my multi texturing to work with little success so far.
This just draws a quad with a single texture, I loaded 2 textures and if I change the fragSampler uniform I can change textures but I want to make this change per vertex…
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(5*sizeof(float)));
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, grass.getTexture());
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, rock.getTexture());
glUniform1i(fragSamplerLoc, 2);
glDrawElements(GL_QUADS, (sizeof(index) / sizeof(unsigned int)), GL_UNSIGNED_INT, (void*)0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
edit: in my frag shader fragSampler is just a uniform sampler2D.
I figured I could just add the texture unit at the end of each of my vertices data… like this…
first 3 are vertex, next 2 are texture cords and finally the texture unit.
float data[] = {
-0.2, -0.2, 0.0, 0.0, 0.0, 1,
0.2, -0.2, 0.0, 1.0, 0.0, 2,
0.2, 0.2, 0.0, 1.0, 1.0, 2,
-0.2, 0.2, 0.0, 0.0, 1.0, 2
};
If I just use…
layout (location = 2) int textUnit;
and pass the unit to the frag shader per vertex I thought it would work but it was drawing random colors instead of the textures. I also tried making the int textUnit into a sampler2D textUnit since that’s how my current code works it just sets the samplers to the current texture unit… no luck with this either. I also tried…
layout (location = 2) int textUnit;
in vec2 textCoord;
sampler2D textSampler
out outpurColor;
void main()
{
outputColor = texture(textSampler + textUnit, textCoord);
}
This didn’t work at all. I’m sure I’m missing something simple here but it’s been bugging me for 2 days so here I am again.
PS: If you notice some small syntax error like a misspelled variable it’s cause I wrote this code out as I’ve been testing stuff all day and don’t have most of the old stuff in my program anymore. It’s likely not the cause of my issues.