Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Multitexturing not working


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Jossos   Members   -  Reputation: 218

Like
0Likes
Like

Posted 01 July 2014 - 11:03 AM

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



Sponsor:

#2 vstrakh   Members   -  Reputation: 465

Like
1Likes
Like

Posted 01 July 2014 - 12:04 PM

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



#3 HappyCoder   Members   -  Reputation: 2843

Like
0Likes
Like

Posted 01 July 2014 - 02:50 PM

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);


#4 SeanMiddleditch   Members   -  Reputation: 6466

Like
0Likes
Like

Posted 01 July 2014 - 04:10 PM

Did you bind all four textures/samplers in separate slots? Did you activate all four texture units?



#5 mhagain   Crossbones+   -  Reputation: 8175

Like
0Likes
Like

Posted 01 July 2014 - 05:07 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 Jossos   Members   -  Reputation: 218

Like
0Likes
Like

Posted 01 July 2014 - 06:12 PM

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, 01 July 2014 - 06:31 PM.


#7 Ohforf sake   Members   -  Reputation: 1832

Like
1Likes
Like

Posted 02 July 2014 - 06:16 AM

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

#8 Jossos   Members   -  Reputation: 218

Like
0Likes
Like

Posted 02 July 2014 - 06:20 AM

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, 02 July 2014 - 07:11 AM.


#9 Jossos   Members   -  Reputation: 218

Like
0Likes
Like

Posted 07 July 2014 - 11:07 PM

nevermind - disregard this post ;)


Edited by Jossos, 08 July 2014 - 03:27 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS