It is currently Mon Feb 24, 2020 5:37 pm


All times are UTC




Post new topic Reply to topic  [ 28 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Picking
PostPosted: Thu Nov 18, 2010 9:31 pm 

Joined: Wed Nov 10, 2010 7:15 pm
Posts: 43
Ok, so I've run into a fairly significant problem. That is that I'm currently just using the outClipPosition to get my distance from the camera. This fails because those x,y, and z are not the same as world - camera, which are the points I need to get exact centers for each cube.

So right now I have the right (I think) equations for the coloring of the cubes, but I get some funny color bands that change based on the angle of the camera (which means that it is because all the coordinates are shifting and so my colors change) what should happen is that every cube surface should have 1 and only 1 color (banding will totally wreck things!) here is where I'm at now:
Attachment:
distanceissues.jpg
distanceissues.jpg [ 73.78 KiB | Viewed 3068 times ]


shaders:
Code:
void PickerVP(
    float4 inPosition    : POSITION,
   
    out float4 outClipPosition        : POSITION,
    out float4 outWorldPosition    : TEXCOORD0,
   out float4 outLocation        : TEXCOORD1,
   
    uniform float4x4 world,
    uniform float4x4 viewProj
    )
{   
    //Compute the world space position
    outWorldPosition = mul(world, inPosition);   
   
   //Compute the clip space position
    outClipPosition = mul(viewProj, outWorldPosition);
   
   //Pass through the material
    outLocation = outClipPosition;
}

void PickerFP(
    float4 inPosition        : POSITION,
    float4 inWorldPosition    : TEXCOORD0,
    float4 inLocation : TEXCOORD1,
   
    out float4 result   : COLOR)
{   
   
   float3 worldNormal = cross(ddy(inWorldPosition.xyz), ddx(inWorldPosition.xyz));
   worldNormal = normalize(worldNormal);
   
   inLocation = inLocation / 16;
   
   result = float4(0,0,0,1);
   //result = float4(inLocation.x/32, inLocation.y/32, inLocation.z/32,1);
   if (distance(inLocation.xyz, float3(0,0,0)) < 32){
      if(worldNormal.x > 0.5){
         result = float4(0, (round(inLocation.y + .5) - .5)/32, (round(inLocation.z + .5 ) - .5)/32,1);
      }
      
      if(worldNormal.x < -0.5){
         result = float4(.2, (round(inLocation.y + .5) - .5)/32, (round(inLocation.z + .5) - .5)/32,1);
      }
      
      if(worldNormal.y > 0.5){
         result = float4((round(inLocation.x + .5) - .5)/32, 0, (round(inLocation.z + .5) - .5)/32,1);
      }
      
      if(worldNormal.y < -0.5){
         result = float4((round(inLocation.x + .5) - .5)/32, .2, (round(inLocation.z + .5) - .5)/32,1);
      }
      
      if(worldNormal.z > 0.5){
         result = float4((round(inLocation.x + .5) - .5)/32, (round(inLocation.y + .5) - .5)/32, 0,1);
      }
      
      if(worldNormal.z < -0.5){
         result = float4((round(inLocation.x + .5) - .5)/32, (round(inLocation.y + .5) - .5)/32, .2,1);
      }   
   }
}


weird note about the shaders, if I try to mess with the POSITION in the FP it always fails to compile, no idea why, but if I send it from the VP as a TEXCOORD it works just fine. totally drives me nuts when I have to create extra variables because using the same type (a float4 in both cases) does not behave the same. To top it off I couldn't even create a tmp of the POSITION in the FP without it failing - I HAD to send it from the VP in another TEXCOORD... what a waste of vid memory!/endrant.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Thu Nov 18, 2010 10:08 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
paycheck wrote:
Ok, so I've run into a fairly significant problem. That is that I'm currently just using the outClipPosition to get my distance from the camera. This fails because those x,y, and z are not the same as world - camera, which are the points I need to get exact centers for each cube.


You already have the world space position in 'inWorldPosition.xyz'. You're using it when you compute the normals for example. Or am I misunderstanding your question?

paycheck wrote:
weird note about the shaders, if I try to mess with the POSITION in the FP it always fails to compile, no idea why, but if I send it from the VP as a TEXCOORD it works just fine. totally drives me nuts when I have to create extra variables because using the same type (a float4 in both cases) does not behave the same. To top it off I couldn't even create a tmp of the POSITION in the FP without it failing - I HAD to send it from the VP in another TEXCOORD... what a waste of vid memory!/endrant.


I can't say I'm an expert here... I sometimes run into this kind of issue myself and work around it. Unfortunatly GPU programming has a number of catches for people used to programming for the CPU!


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Thu Nov 18, 2010 10:10 pm 

Joined: Wed Nov 10, 2010 7:15 pm
Posts: 43
UPDATE: Partial success!
So, while I had tried using outWorldPosition - cameraPos before, I always failed to compile then, but just by tweaking my existing code to use it, it worked.... BUT cameraPos is actually just always 0,0,0 and therefore 100% useless. if I could get the real cameraPos, I think I would be done with coloring : /
Attachment:
cameraTroubles.jpg
cameraTroubles.jpg [ 85.41 KiB | Viewed 3066 times ]

(notice how it just stops... the colors also don't change with distance from the camera now, they are just static, because cameraPosition is returning the null vector)

Ogre Program:
Code:
vertex_program PickerVP cg
{
    source Picker.cg
    entry_point PickerVP
    profiles vs_3_0 vs_2_x vs_2_0 vs_1_1 vp40 vp30 vp20 arbvp1

    default_params
    {
        param_named_auto world world_matrix
        param_named_auto viewProj viewproj_matrix
      param_named auto cameraPos camera_position
    }
}


fragment_program PickerFP cg
{
    source Picker.cg
    entry_point PickerFP
   
    profiles ps_3_x ps_3_0 ps_2_x ps_2_0 ps_1_4 ps_1_3 ps_1_2 ps_1_1 fp40 fp30 fp20 arbfp1

    default_params
    {
    }
}


CG programs:
Code:
void PickerVP(
    float4 inPosition    : POSITION,
   
    out float4 outClipPosition        : POSITION,
    out float4 outWorldPosition    : TEXCOORD0,
   out float4 crfp : TEXCOORD1, //camera relative fragment position
   
    uniform float4x4 world,
    uniform float4x4 viewProj,
   uniform float4 cameraPos
    )
{   
    //Compute the world space position
    outWorldPosition = mul(world, inPosition);   
   
   //Compute the clip space position
    outClipPosition = mul(viewProj, outWorldPosition);
   
   //Pass through the material
    crfp = outWorldPosition - cameraPos;
}

void PickerFP(
    float4 inPosition        : POSITION,
    float4 inWorldPosition    : TEXCOORD0,
    float4 crfp : TEXCOORD1,
   
    out float4 result   : COLOR)
{   
   
   float3 worldNormal = cross(ddy(inWorldPosition.xyz), ddx(inWorldPosition.xyz));
   worldNormal = normalize(worldNormal);
   
   crfp = crfp / 16;
   
   result = float4(0,0,0,1);
   //result = float4(crfp.x/32, crfp.y/32, crfp.z/32,1);
   if ((crfp.x < 32)&&(crfp.y < 32)&&(crfp.z < 32)){
      if(worldNormal.x > 0.5){
         result = float4((crfp.x - .5)/32, round(crfp.y)/32, round(crfp.z)/32,1);
      }
      
      if(worldNormal.x < -0.5){
         result = float4((crfp.x + .5)/32, round(crfp.y)/32, round(crfp.z)/32,1);
      }
      
      if(worldNormal.y > 0.5){
         result = float4(round(crfp.x)/32, (crfp.y - .5)/32, round(crfp.z)/32,1);
      }
      
      if(worldNormal.y < -0.5){
         result = float4(round(crfp.x)/32, (crfp.y + .5)/32, round(crfp.z)/32,1);
      }
      
      if(worldNormal.z > 0.5){
         result = float4(round(crfp.x)/32, round(crfp.y)/32, (crfp.z - .5)/32,1);
      }
      
      if(worldNormal.z < -0.5){
         result = float4(round(crfp.x)/32, round(crfp.y)/32, (crfp.z + .5)/32,1);
      }   
   }
}



EDIT: @David
Quote:
You already have the world space position in 'inWorldPosition.xyz'. You're using it when you compute the normals for example. Or am I misunderstanding your question?
the problem is that I need to be able to recolor voxels based on their distance in x,y,z from the camera, because I'm going to need to extract that color information to add to the index of the voxel the camera is at. Right now I have a few things I need to work out -

1) how do I get the world - camera difference. I now have world working correctly, but not camera, so it is taunting me (so close and yet so far)

2) my next thing I need to figure out is how to do face detection. I was thinking about adding a constant to the normal face, but upon thinking about it more, I don't think that will work at all. (no way to reverse that process) my second (and better I think) idea was to try and use the alpha channel to save which face is clicked on. the problem here is that I need to have the alpha channel have no effect on which color goes in the buffer (I cant have colors getting added from behind), but at the same time stay in the buffer so I can retrieve them. This may be something that people on the ogre forums can help me with, although they have been noticeably crappy at responding to my cg help questions (I don't think many people know anything about them - I certainly didn't till this project). Or it may just result in hours and hours of trial and error met with either success or defeat : /

Problem 1 is more pressing because I can't really do anything until I can get the color generation working for basic picking (if the voxel is all 1 color, then I will know which one it is (good enough for deletion) but I won't know which face was selected until I can figure out problem 2


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Thu Nov 18, 2010 10:23 pm 

Joined: Wed Nov 10, 2010 7:15 pm
Posts: 43
Just for clarity the most pressing question is how to get the camera relative yet un-perspective modified position of the fragments. (how is that for super confusing, maybe I should draw a picture in paint...)


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Thu Nov 18, 2010 10:31 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Are there any errors in your Ogre.log? I notice this mistake:
Code:
param_named auto cameraPos camera_position

Should be:
Code:
param_named_auto cameraPos camera_position

With an '_' instead of the space ;-)


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Thu Nov 18, 2010 10:37 pm 

Joined: Wed Nov 10, 2010 7:15 pm
Posts: 43
David Williams wrote:
Are there any errors in your Ogre.log? I notice this mistake:
Code:
param_named auto cameraPos camera_position

Should be:
Code:
param_named_auto cameraPos camera_position

With an '_' instead of the space ;-)


*Sigh* if I was a chick I'd have your babies. I hope you'll settle for letting me buy you a drink someday, or a spot on my credits. I can't believe I missed that, and that I've spent ~35 minutes trying to figure out what I did wrong. I didn't think it would compile and run with mistakes like that (I at least expect a white material...) lol. I guess I just need to test with the logs up all the time, cause there it is...
Code:
15:33:02: OGRE EXCEPTION(2:InvalidParametersException): Parameter called camObjPos does not exist.  in GpuProgramParameters::_findNamedConstantDefinition at ..\..\..\..\OgreMain\src\OgreGpuProgramParams.cpp (line 1433)
15:33:02: Compiler error: invalid parameters in Examples.material(837): setting of constant failed


EDIT: Done with shader.
Pic:
Attachment:
done.jpg
done.jpg [ 74.27 KiB | Viewed 3061 times ]


Shader:
Code:
void PickerVP(
    float4 inPosition    : POSITION,
   
    out float4 outClipPosition        : POSITION,
    out float4 outWorldPosition    : TEXCOORD0,
   out float4 crfp : TEXCOORD1, //camera relative fragment position
   
    uniform float4x4 world,
    uniform float4x4 viewProj,
   uniform float3 cameraPos
    )
{   
    //Compute the world space position
    outWorldPosition = mul(world, inPosition);   
   
   //Compute the clip space position
    outClipPosition = mul(viewProj, outWorldPosition);
   
   //Pass through the material
    crfp = (outWorldPosition/16) - round(float4(cameraPos,0)/16);
}

void PickerFP(
    float4 inPosition        : POSITION,
    float4 inWorldPosition    : TEXCOORD0,
    float4 crfp : TEXCOORD1,
   
    out float4 result   : COLOR)
{   
   
   float3 worldNormal = cross(ddy(inWorldPosition.xyz), ddx(inWorldPosition.xyz));
   worldNormal = normalize(worldNormal);
   
   result = float4(0,0,0,1);
   //result = float4(crfp.x/32, crfp.y/32, crfp.z/32,1);
   if ((crfp.x < 32)&&(crfp.y < 32)&&(crfp.z < 32)&&(crfp.x > -32)&&(crfp.y > -32)&&(crfp.z > -32)){
      if(worldNormal.x > 0.5){   //East
         result = float4(((crfp.x - .5) + 32)/64, (round(crfp.y) + 32)/64, (round(crfp.z) + 32)/64, .1666666);
      }
      
      if(worldNormal.x < -0.5){
         result = float4(((crfp.x + .5) + 32)/64, (round(crfp.y) + 32)/64, (round(crfp.z) + 32)/64, .3333333);
      }
      
      if(worldNormal.y > 0.5){
         result = float4((round(crfp.x) + 32)/64, ((crfp.y - .5) + 32)/64, (round(crfp.z) + 32)/64, .5);
      }
      
      if(worldNormal.y < -0.5){
         result = float4((round(crfp.x) + 32)/64, ((crfp.y + .5) + 32)/64, (round(crfp.z) + 32)/64, .6666666);
      }
      
      if(worldNormal.z > 0.5){
         result = float4((round(crfp.x) + 32)/64, (round(crfp.y) + 32)/64, ((crfp.z - .5) + 32)/64, .8333333);
      }
      
      if(worldNormal.z < -0.5){
         result = float4((round(crfp.x) + 32)/64, (round(crfp.y) + 32)/64, ((crfp.z + .5) + 32)/64, 1);
      }   
   }
}


now for the rest of the project...


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Fri Nov 19, 2010 12:10 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Great, looking good :-) But I should just let you know that I'm going away for a few days tomorrow. I should have internet but it won't be very frequent, so appologies in advance if I'm not very quick to reply...


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Picking
PostPosted: Tue Nov 23, 2010 6:47 am 

Joined: Wed Nov 10, 2010 7:15 pm
Posts: 43
I just wanted to post an update letting people know I'm still working on this, I think I'll have it done tomorrow morning, but then I'm going home for thanksgiving so unless I can convince the folks to let me program my side projects instead of hanging out with the fam, I probably won't get much done till next week. For any of you who celebrate it, have a great thanksgiving!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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