Sign in to follow this  
Jossos

Multitexturing not working

Recommended Posts

Ok, so I'm trying to get this shader to load 4 textures and blend them together (for tiling reasons).

#version 330

in vec2 TexCoord0;

uniform sampler2D gTex0;
uniform sampler2D gTex1;
uniform sampler2D gTex2;
uniform sampler2D gTex3;

out vec4 FragColor;

void main()
{
    vec4 color1 = texture2D(gTex0, TexCoord0);
    vec4 color2 = texture2D(gTex1, TexCoord0);
    vec4 color3 = texture2D(gTex2, TexCoord0);
    vec4 color4 = texture2D(gTex3, TexCoord0);

    color1 = color1 * (1 - TexCoord0.x) * TexCoord0.y;
    color2 = color2 * (1 - TexCoord0.x) * (1 - TexCoord0.y);
    color3 = color3 * TexCoord0.x * (1 - TexCoord0.y);
    color4 = color4 * TexCoord0.x * TexCoord0.y;

    FragColor = color1 + color2 + color3 + color4;
}

If i do 'FragColor = color1' (or any of the colors, like FragColor = color3 etc) it draws the texture, fading from its full color to black as it should do. Each of these textures work on their own, but when trying to use 2 or more (such as "FragColor = color1 + color2;"), it just renders black for some reason.

I can switch the color1/color2/color3/color4 in the shader without touching any other code and it renders that texture, so  im guessing that it is loading in all of the textures correctly.

 

If you need to see more code let me know, but it feels like the error is within the fragment shader itself.

 

Thanks

Share this post


Link to post
Share on other sites

Is there, by any chance, texture coords in vertices bigger than 1.0?

 

Thats what I was thinking. If you have texture coordinates outside the range [0,1] then the black areas for that single image would be negative, cancelling out the positive color value of the texture. Try putting clamp around the coefficient calculation for each color.

color1 = color1 * clamp((1 - TexCoord0.x) * TexCoord0.y, 0, 1);

Share this post


Link to post
Share on other sites

First of all, I have no idea why you're doing the "multiply texture colour by texcoords" thing - no offense intended, but that just seems ... insane.

 

Secondly, we'll also need to see your glActiveTexture, glBindTexture and glUniform1i calls for setting up the textures and for associating uniform locations with each texture unit.  Your shader looks fine (aside from that weird colour * texcoord thing) so it's more probable that the problem you're having is in these calls.

Share this post


Link to post
Share on other sites

Thanks for your responses.

 

 

Is there, by any chance, texture coords in vertices bigger than 1.0?

 

 

No I manually input the data:

m_tileMesh = new MESH[m_numOfTiles];
	VERTEX Vertices[4];

	for (int x = 0; x < m_width; x++)
	for (int z = 0; z < m_height; z++)
	{
// VECTOR2f is the texture coordinate
		Vertices[0] = VERTEX(VECTOR3f(x + 0.5f, z + 0.5f, 10.0f), VECTOR2f(0.0f, 0.0f), VECTOR3f());
		Vertices[1] = VERTEX(VECTOR3f(x + 0.5f, z + -0.5f, 10.0f), VECTOR2f(1.0f, 0.0f), VECTOR3f());
		Vertices[2] = VERTEX(VECTOR3f(x + -0.5f, z + -0.5f, 10.0f), VECTOR2f(1.0f, 1.0f), VECTOR3f());
		Vertices[3] = VERTEX(VECTOR3f(x + -0.5f, z + 0.5f, 10.0f), VECTOR2f(0.0f, 1.0f), VECTOR3f());

		uint Indices[] = { 0, 1, 2, 0, 2, 3 };

		m_tileMesh[z + x * m_width].LoadNewMesh(Vertices, 4, Indices, 6);
	}

I must say again that using just ONE of the textures works just fine, that being any of them, but using 2 or more at once makes it bug out.

 

 

First of all, I have no idea why you're doing the "multiply texture colour by texcoords" thing - no offense intended, but that just seems ... insane.

 

 

I'm trying to make an rts game, and as such the tiles need to blend. Each corner of every tile is a seperate texture, so I'm needing to blend 4 textures together (or 3 really since it's 2trangles for each quad, but im not optimizing yet, just trying to get it working).

 

 

Secondly, we'll also need to see your glActiveTexture, glBindTexture and glUniform1i calls for setting up the textures and for associating uniform locations with each texture unit.  Your shader looks fine (aside from that weird colour * texcoord thing) so it's more probable that the problem you're having is in these calls.

 

 

Ok here we go:

void MAP::DrawMap()
{
	// Makes this shader the current shader
	m_shader->Bind();
	
	// m_shader->GetProgramID() get's the ID of the shader.
	glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex0"), GL_TEXTURE0);
	glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex1"), GL_TEXTURE1);
	glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex2"), GL_TEXTURE2);
	glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex3"), GL_TEXTURE3);

	for (int x = 0; x < m_width; x++)
	for (int z = 0; z < m_height; z++)
	{
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, m_tilePoint[m_tile[x * m_width + z].m_tilePoint[0]].m_texID);

		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, m_tilePoint[m_tile[x * m_width + z].m_tilePoint[1]].m_texID);

		glActiveTexture(GL_TEXTURE2);
		glBindTexture(GL_TEXTURE_2D, m_tilePoint[m_tile[x * m_width + z].m_tilePoint[2]].m_texID);

		glActiveTexture(GL_TEXTURE3);
		glBindTexture(GL_TEXTURE_2D, m_tilePoint[m_tile[x * m_width + z].m_tilePoint[3]].m_texID);

		m_tileMesh[z + x * m_width].Render();	// Draws the tile
	}
}

I try to keep my posts as short as I can.

 

Also just for reference, here it is rendering ONE of the colors in the shader for the tiles (behind the cubes):

 

testcubes.jpg

 

So changing which single texture to make the fragcolor would make it pick the texture in one of the other corners, and they fade out just like this.

 

Any help is greatly appreciated, or if you know an easier way to blend 4 textures together for my purposes, I would also appreciate your input.

 

Thanks

 

Edit: changed the background color to blue, doing "FragColor = color1 + color2;" instead of "FragColor = colorx;" //x being any color, and when adding two colors, it doesn't render anything at all. as in I just see blue background, no black squares.

Edited by Jossos

Share this post


Link to post
Share on other sites
Pretty sure this:
glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex0"), GL_TEXTURE0);
has to be
glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex0"), 0);

Share this post


Link to post
Share on other sites

Pretty sure this:
glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex0"), GL_TEXTURE0);
has to be
glUniform1i(glGetUniformLocation(m_shader->GetProgramID(), "gTex0"), 0);

 

Wow man, wow. Ok that worked, I'm so stupid.

 

Maybe I should read up on these opengl functions a little more.

 

Thanks so much man, you rock! it looks beautiful

 

EDIT: Results

 

love_field.jpg

Edited by Jossos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this