Volumes Of Fun
http://www.volumesoffun.com/phpBB3/

Mapping Position to texture coordinates
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=249
Page 2 of 2

Author:  David Williams [ Thu Aug 11, 2011 8:24 pm ]
Post subject:  Re: Mapping Position to texture coordinates

Ok, the following code is untested and may not compile... but hopefully you get the idea :-)

Code:
void MyFragmentShader1(float4 position : POSITION,
                       float4 worldPosition : TEXCOORD0,
                       out float4 color: COLOR)
{
   color = tex2D(texture, WorldPosition.xy / 10.0f);
  //color = float4(1.0f, 0.0f, 0.0f, 1.0f); //Can try this line for testing
}

void MyVertexShader1(float4 position : POSITION,
                     out float4 oPosition : POSITION,
                     out float4 worldPosition : TEXCOORD0,
                     uniform float4x4 worldViewMatrix,
                     uniform float4x4 world)
{
  worldPosition = mul(world, position);
  oPosition = mul(worldViewMatrix, position);
}

Author:  GM_Riscvul [ Fri Aug 12, 2011 4:32 am ]
Post subject:  Re: Mapping Position to texture coordinates

Alright I misinterpreted a previous post to mean I didn't need a vertex shader. The code you provided was very similar to what I had before I removed my vertex shader.

Here is what the cg program looked like before that. Almost like the one you gave me. I assume I have to pass in the texture in order for the program to use it. That is the only difference I am aware of.

However Ogre keeps informing me that my fragment shader will not compile. Everything looks right. Unfortunatly I don't know the subtle syntax differences between CG and C.

Could it be that texture coordinates may not be of float4 type?

I think they are supposed to be float2.

Code:
void MyFragmentShader1(float4 position : POSITION,
                       float4 worldPosition : TEXCOORD0,
                       out float4 color : COLOR,
                       uniform sampler2D texture)
{
   color = tex2D(texture, WorldPosition.xy / 10.0f);
}

void MyVertexShader1(float4 position : POSITION,
                     out float4 oPosition : POSITION,
                     out float4 worldPosition : TEXCOORD0,
                     uniform float4x4 worldViewMatrix,
                     uniform float4x4 world)
{
  worldPosition = mul(world, position);
  oPosition = mul(worldViewMatrix, position);
}

Author:  GM_Riscvul [ Fri Aug 12, 2011 4:45 am ]
Post subject:  Re: Mapping Position to texture coordinates

Ok, that was a stupid mistake. one of the worldPosition variables had been accidently capitalized.

So now I see a texture, but it looks terribly stretched along one axis. instead of grass I see long green lines.

Its late and my adjustments are getting me nowhere. However I have never been so excited to see a badly skewed green texture.

Let me know if you know what mistake I could be making.

Thank you for the help.

Author:  beyzend [ Sat Aug 13, 2011 12:38 am ]
Post subject:  Re: Mapping Position to texture coordinates

One advice is to look at ogre.log when sometimes doesn't work. Especially if texture is showing up as black.

Author:  David Williams [ Sat Aug 13, 2011 8:50 am ]
Post subject:  Re: Mapping Position to texture coordinates

GM_Riscvul wrote:
So now I see a texture, but it looks terribly stretched along one axis. instead of grass I see long green lines.


The code is only correct for triangles facing along the z axis. Try flying around the mesh and looking at it from differnt angles and you should see what I mean.

If they are facing along a diffent axis then you can try:

Code:
color = tex2D(texture, WorldPosition.xz / 10.0f);


or

Code:
color = tex2D(texture, WorldPosition.yz / 10.0f);


Obviously you eventually want a solution which works for all directions, which is where triplanar texturing comes in.

Page 2 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/