It is currently Sat Aug 22, 2020 3:44 pm


All times are UTC




Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Mapping Position to texture coordinates
PostPosted: Sat Jul 23, 2011 10:09 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
I am having a terrible time trying to use an ogre material to place a simple grass texture onto my landscape.

I've seen a post about how to do this for CubicVoxels but I am using the marching cubes algorithm and the smooth extractor.

Apparently all I need to do is use the same position coordinates for the texture coordinates as seen on this post.

http://89.151.96.106/forums/viewtopic.php?f=2&t=28452

Quote:
Just use a shader and assign the texture coords output from the vertex position input - I've done this before in a volumetric application. It's dead easy.


I haven't found it dead easy... This seems to be easy in c++ code, but apparently it is much more efficient to put this conversion in a shader so that the GPU handles it.

Does anyone have any experience with Ogre Materials and shaders that can give me some pointers on applying textures to polyvox meshes?

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Sun Jul 24, 2011 11:46 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
You don't actually need to do anything in the vertex shader - the key point is that in the fragment shader you shoud already have the world space position and you can use this directly as a texture coordinate. E.g to project a texture along the XY plane you can do something like (psuedocode):

Code:
float4 WorldPos = ...; //I assume you have this?
float4 colour = tex2D(MyTexture, WorldPos.xy); //Uses x and y components as texture coordinates.


So you don't actually pass texture coordinates to the GPU, you simply use the world space posiition as if it actually was texture coordinates. In practice, you probably want to scale, e.g:

Code:
float4 colour = tex2D(MyTexture, WorldPos.xy / 100.0f); //Uses x and y comonents as texture coordinates.


Will make the texture repeat every 100 voxels.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Sun Jul 24, 2011 3:01 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
This is going to sound a little noObish... but I don't entirely see how to use this with Ogre. I know Ogre has fragment shaders to go along with the material, but isn't the texture applied in the '.material' file?

Also you mentioned worldspace coordinates. I'm so new at shaders I have no idea how the shader even gets these coordinates. I assume its the positional coordinates I retrieve from polyvox, but I am not sure.

Your code makes sense, I just seem to be missing a couple key pieces of understanding in order to apply it.

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Mon Jul 25, 2011 8:14 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
It sounds like you might need to brush up on shaders then, before you try anything too fancy with PolyVox. You can even use something really simple like Ogre's sphere prefab. Even using just a sphere you can learn how the texturing and lighting pipeline works, and even try techniques such as triplanar texturing which you will probably want for the terrain.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Mon Jul 25, 2011 10:46 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
Very true. I have alot to learn in order to use shaders appropriately. For now I am just trying to get an ugly texture on so i can see the 3d details of my terrain. It shouldn't be anything fancy. I just don't know how to get the polyvox coordinates used as texture coordinates. How do I pass them in?

I'll keep trying to find a semi beginners friendly guide to shaders. Maybe I can figure it out. Anyone know any good guides to shaders?

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Tue Jul 26, 2011 8:18 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
GM_Riscvul wrote:
Very true. I have alot to learn in order to use shaders appropriately. For now I am just trying to get an ugly texture on so i can see the 3d details of my terrain. It shouldn't be anything fancy. I just don't know how to get the polyvox coordinates used as texture coordinates. How do I pass them in?


If you want to get by without shaders then you can probably make use of the ManualObject::textureCoord() function. Using this you can probably apply a texture projected along a single axis but you won't be able to do anything fancy like triplanar texturing.

If you do want to use shaders (and ultimatly you will) then it is beyond what I can explain here. You can't mix shaders with the fixed function pipeline, so when you start using shaders for texturing you also have to implement your own lighting and the projection of vertices from local to world to clip space.

GM_Riscvul wrote:
I'll keep trying to find a semi beginners friendly guide to shaders. Maybe I can figure it out. Anyone know any good guides to shaders?


The Cg Tutorial is a prety comprehensive book which should get you started if you want to use Cg. You'll also need to deal with Ogre specific stuff such as setting up your material scripts, but I don't know what to look at there beyond the Ogre manual.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Sun Aug 07, 2011 5:03 am 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
So I have been studying alot about shaders and I'm making some progress.
My texture shader is still not working, but I have made some very simple shaders that work.

I had, what I hope is, a simple question.

These worldspace coordinates. Are they just the Positional coordinates? or do I need to multiply the position by the ogre world_matrix?

I believe multiplying the position in by the world_matrix will give me the world space coordinates but I am not sure.

Thanks for the help and patience.

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Mon Aug 08, 2011 5:28 am 

Joined: Sat Sep 18, 2010 9:45 pm
Posts: 189
You may want to read up a little bit on the fundamentals of how 2D images of 3D objects are displayed on the screen. A good place to do this can be found here:

http://glprogramming.com/red/chapter03.html

Once you have a grasp of that you can get the picture of how it's done in the modern GPU scheme of things.

http://developer.nvidia.com/node/76 (section 1.2.3)

So basically when you render an object, they started in model space (local space, etc). A world matrix (for example) would transform your objects into a common space, usually called world-space, a space where they co-exist. From there it's a series of further transformations until finally you reach screen space, which is a coordinate system that maps directly to your 2D screen. Read the first link, it's all there.

In a programmable shaders you have to do all the transformations. Before this in the fixed function pipeline the implication was it was done for you, you would just issue state change commands on the CPU (in OpenGL it's just enter some GL_MATRIX state, do the transforms by invoking the transform functions).

Refer to Ogre's shader (material section) section on how to do this in Ogre. Basically you just have to define materials scripts and bind auto parameters and also declare them in your shaders.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Wed Aug 10, 2011 3:29 am 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
I'm so close. All I am seeing is a flat square that is slightly off-white due to 80% ambient light, but no texture. As far as I know the code is so close. I just don't know what my mistake is. I'm guessing my parameters are wrong.

Here is the material file.

Code:
fragment_program MyFragmentShader1 cg
{
   source WorldCraftTerrainShaders.cg
   entry_point MyFragmentShader1
   profiles ps_1_1 arbfp1
   default_params
   {
      param_named_auto world world_matrix
   }

}

vertex_program MyVertexShader1 cg
{
   source WorldCraftTerrainShaders.cg
   entry_point MyVertexShader1
   profiles vs_1_1 arbvp1

   default_params
   {
      param_named_auto worldViewMatrix worldviewproj_matrix
      param_named_auto world world_matrix
   }
}

material Worldcraft/Greengrass
{
   technique
   {
      pass
      {
         fragment_program_ref MyFragmentShader1
         {
         }
         texture_unit
         {
            texture grass_1024.jpg
         }
      }
   }
}


Here is the cg file.

Code:
void MyFragmentShader1(float4 position : POSITION,
                       float4 worldPosition : TEXCOORD0,
                       out float4 color: COLOR
                       uniform float4x4 world)
{
   worldPosition = mul(world, position);
   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)
{
   

}


The vertex shader is not being used currently.

So am I messing up a parameter or miscalculating the world space?

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Mapping Position to texture coordinates
PostPosted: Thu Aug 11, 2011 9:07 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
I can't write much as i'm posting from my phone, but the key point here is that the transform must be done in the vertex shader. You can't do it in the fragment shader. I.e. the first line of your fragment shader needs to be moved to the vertex shader. There are probably more mistakes, i'll try to help more when i'm at my pc.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net