Jump to content
  • Advertisement
Sign in to follow this  
Richy2k

GLSL Multitexturing problems...

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I decided to get off my backside and have a play about with GLSL today...started to enjoy playing about and making models wobble about using the vertex shader, and colours go funky with a fragment shader, but i'm having a hell of a lot of trouble trying to write a shader that will do texture splatting for me. If you have texturing enabled, BUT in a fragment shader you set gl_FragColor to any specific colour shouldn't the output be that colour? I decided to try this as no matter what my fragment shader said the output was the same, the vertex shader, however, affected what I was doing. Both shaders can be made to do things interesting until I introduce textures. I'll post some code and a screen shot to illustrate my problems, with the fragment shader I'm using shouldn't the output be ALL red? Also, the code i'm using to render has been hacked about, ill post it without sections i've commented out. Vertex Shader
void main(void)
{
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; 
}

Fragment Shader
void main (void)
{
   vec4 texval1 = texture2D(0, vec2(gl_TexCoord[0]));
   vec4 texval2 = texture2D(1, vec2(gl_TexCoord[1]));
   
   gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5); //0.5*(texval1+texval2);
}

Code which renders (removed commented out stuff)
void cTerrainRenderer::Render()

{

	glUseProgramObjectARB(m_ShaderProgram);


	

	// Setup the render state for 2D Texturing

	// and Blending

	glEnable(GL_BLEND);

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glEnable(GL_COLOR_MATERIAL);

	

	// Get the vertex, normal and texture arrays ready

	glEnableClientState(GL_VERTEX_ARRAY);

	glEnableClientState(GL_NORMAL_ARRAY);



	// Pass in pointers for GL to use

	glVertexPointer(3, GL_FLOAT, 0, m_pTerrainData->m_pVertexArray);

	glNormalPointer(GL_FLOAT, 0, m_pTerrainData->m_pNormalArray);



	// First loop through the layers

	int l_iLayer = 3;

	//for (int l_iLayer = 0; l_iLayer < m_pTerrainData->m_NumLayers; ++l_iLayer)

	{

		// Alpha Channel on Texture Unit 0

		glActiveTextureARB(GL_TEXTURE0_ARB);

		glClientActiveTextureARB(GL_TEXTURE0_ARB);


		// Bind the texture

		m_pRenderer->m_pTextureManager->BindTexture(m_pTerrainData->m_pTextureLayers[l_iLayer+1].m_DiffuseMap);



		glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);

		glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_REPLACE);



		// Give it some texture coordinates

		glEnableClientState(GL_TEXTURE_COORD_ARRAY);

		glTexCoordPointer(2, GL_FLOAT, 0, m_pTerrainData->m_pTextureArray);



		// Diffuse Map on Texture Unit 1

		glActiveTextureARB(GL_TEXTURE1_ARB);

		glClientActiveTextureARB(GL_TEXTURE1_ARB);

		

		// Bind Texture

		m_pRenderer->m_pTextureManager->BindTexture(m_pTerrainData->m_pTextureLayers[l_iLayer].m_DiffuseMap);



		glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);

		glTexEnvf (GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_INCR);



		// Give it some texture coordinates

		glEnableClientState(GL_TEXTURE_COORD_ARRAY);

		glTexCoordPointer(2, GL_FLOAT, 0, m_pTerrainData->m_pTextureArray);



		// Enter the Texture Matrix Mode :-D

		glMatrixMode(GL_TEXTURE);

		glPushMatrix();

		

		// Translate the texture

		glRotatef(m_pTerrainData->m_pTextureLayers[l_iLayer].m_Angle, 0.0f, 0.0f, 1.0f);

		glScalef(m_pTerrainData->m_pTextureLayers[l_iLayer].m_Scale.X, m_pTerrainData->m_pTextureLayers[l_iLayer].m_Scale.Y, 0.0f);



		// Loop through the fragment list

		for (int l_iFragment = 0; l_iFragment < m_pTerrainData->m_NumFragments; ++l_iFragment)

		{

			// Send the polygons down the pipeline (experimental, no checks)

			glDrawElements(GL_TRIANGLES, m_pTerrainData->m_pTerrainFragments[l_iFragment].m_NumIndices, GL_UNSIGNED_INT, m_pTerrainData->m_pTerrainFragments[l_iFragment].m_pIndices);

		}

		

		// Exit the Texture Matrix Mode :-O

		glPopMatrix();

		glMatrixMode(GL_MODELVIEW);



		// Now to unbind for the next run

		// Diffuse Map on Texture Unit 1

		glActiveTextureARB(GL_TEXTURE1_ARB);

		glClientActiveTextureARB(GL_TEXTURE1_ARB);

		glDisableClientState(GL_TEXTURE_COORD_ARRAY);

		m_pRenderer->m_pTextureManager->UnBindTexture(m_pTerrainData->m_pTextureLayers[l_iLayer].m_DiffuseMap);



		// Alpha Channel on Texture Unit 0

		glActiveTextureARB(GL_TEXTURE0_ARB);

		glClientActiveTextureARB(GL_TEXTURE0_ARB);

		glDisableClientState(GL_TEXTURE_COORD_ARRAY);

		m_pRenderer->m_pTextureManager->UnBindTexture(m_pTerrainData->m_pTextureLayers[l_iLayer+1].m_DiffuseMap);





	}	

	

	// Restore the render state

	glDisableClientState(GL_VERTEX_ARRAY);

	glDisableClientState(GL_NORMAL_ARRAY);



	glDisable(GL_BLEND);


	glUseProgramObjectARB(0);



 }


And now what i actually get with that code: I do a couple of translations on the rocky texture, and when I go up close there is actually a snowy texture...with my vertex shader these transformations shouldn't even take place should they? Up close on 'terrain': Any help appreciated

Share this post


Link to post
Share on other sites
Advertisement
Ok, I just looked through your code a bit. You need to understand that once you call the shader object all rendering is passed off to the shader. You can get rid of all the FFP code you have like blending ect... It will do no good. You are responsible for all rendering once you call a shader object for that mesh. From what I can tell you should have red on your fragments, but you have your textures showing up on your terrain and from the code you posted thats not possible due to you do nothing with the textures in the fragment shader.

Share this post


Link to post
Share on other sites
thats exactly what my problem is, but I don't have the slightest idea why, although my rendering code I posted is simply my FFP with sections commented out.

Got it working! Just done one of my crazy code rewrites and it decided to work...probably a tiny mistake I completely missed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!