Jump to content
  • Advertisement
Sign in to follow this  
rajesh_nest

Changing the color of the selected region

This topic is 3738 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 have a texture. I am selecting a region of interest in the texture using the mouse. Now my task is to change the change the color of the selected region. How should I pass the selected region to pixel shader so that I can change the color there. Once changed how can I render. Ramachandra Innanchaya.

Share this post


Link to post
Share on other sites
Advertisement
You should, at first, compute, with a proportion, the value of texture size
If you are using a resolution of 800x600, and knowing that in DirectX all it's drawn in -1;1 range, if your point it's 300 and 300, you will make

x : 300 = 1 : 800 = 0,375
y : 300 = 1 : 600 = 0,5

You will create a particular shader (on a render target) that, in vertex shader, if Pos.x = 0,375 && Pos.y = 0,5 then will set a const global flag to true.

In pixel shader, if flag == true, return yourcolor.

Share this post


Link to post
Share on other sites
Quote:
Original post by XVincentX

x : 300 = 1 : 800 = 0,375
y : 300 = 1 : 600 = 0,5

You will create a particular shader (on a render target) that, in vertex shader, if Pos.x = 0,375 && Pos.y = 0,5 then will set a const global flag to true.

In pixel shader, if flag == true, return yourcolor.



Thanks XVincentX. My region of selection is rectangle. Then there are four points in the texture.Then we need to set the flag true for all the four values.
Am I right. Please correct me if am wrong.


Ramachandra Innanchaya.



Share this post


Link to post
Share on other sites
Yes, it woul be correct. Anyway, a rectangle can be described by coordinates of only 2 point, with this



typedef struct tagRECT {
LONG left; // This is the upper-left corner x-coordinate.
LONG top; // The upper-left corner y-coordinate.
LONG right; // The lower-right corner x-coordinate.
LONG bottom; // The lower-right corner y-coordinate.
} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;




Then you will go on with my previous advices.

If you do not want to use pixel shader, you can also use a viewport in that screen portion, change color and reset viewport to initial state.

Share this post


Link to post
Share on other sites

Tried as per your suggestion.But my shader code seems to be breaking. The following is what I tried



// For getting the region for which color needs to be changed.

LRESULT CALLBACK WindowProc( HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_LBUTTONDOWN:
{
pRECT->left = LOWORD (lParam);
pRECT->bottom = HIWORD (lParam);
bMousing = true;
}
break;
case WM_LBUTTONUP:
{
pRECT->right = LOWORD(lParam);
pRECT->top = HIWORD(lParam);
bMousing = false;
}
break;
}
// For storing the extremes of the region.
typedef struct BoundingBox
{
long left; // This is the upper-left corner x-coordinate.
long top; // The upper-left corner y-coordinate.
long right; // The lower-right corner x-coordinate.
long bottom; // The lower-right corner y-coordinate.
}rectangle* pRECT;;

void setTechniqueVariables( void )
{
....
g_pEffect->SetTexture( "testTexture", g_pTexture );
g_pEffect->SetValue("pRect", pRECT,4);

}


And in the fx file



float4x4 worldViewProj : WorldViewProjection; float4 pRect : POSITION;
bool flag = false;

//Setting the flag in the vertex shader.
VS_OUTPUT myvs( VS_INPUT IN )
{
VS_OUTPUT OUT;

OUT.hposition = mul( worldViewProj, float4(IN.position, 1) );
if((OUT.hposition.x > pRect.x) && (OUT.hposition.y < pRect.z))
{
flag = true;
}
}
//Checking the flag and changing the color in the pixel shader.
PS_OUTPUT myps( VS_OUTPUT IN )
{
PS_OUTPUT OUT;

if(flag == true)
{
OUT.color = float4(1.0,0.0,0.0,0.0);
}
OUT.color = tex2D( Sampler, IN.texture0 ) * IN.color;
}


Kindly suggest what resulted in the breaking of the pixel shader.


Thanks in advance

Ramachandra Innanchaya







Share this post


Link to post
Share on other sites
There are some issues in your code.


case WM_LBUTTONDOWN:

{

pRECT->left = LOWORD (lParam);

pRECT->bottom = HIWORD (lParam);

bMousing = true;

}

break;

case WM_LBUTTONUP:

{

pRECT->right = LOWORD(lParam);

pRECT->top = HIWORD(lParam);

bMousing = false;



1) Mmm, i'm not really sure about this.
After your rect structure it's full, you should make a coparision in order to set your RECT in a ok-state, so


#define SWAP(x,y) int p; x = p; x = y; y = p

if (pRect->right < pRect->left)
SWAP(pRect->right,pRect->left);

if (pRect->top < pRect->bottom)
SWAP(pRect->top,pRect->bottom);




In this way, even if you will select from bottom to up, the rect will be in a consistent state.

2) I think that lParam will give you the point position in pixel, and not in Direct3D coordinate system. So, as i wrote up for you,

x : 300 = 1 : 800 = 0,375
y : 300 = 1 : 600 = 0,5
you have to do:

if (pRect->right > YourWindowHeight/2)
pRect->right/= (YourWindowHeight/2);
else
pRect->right= -(pRect->right / (YourWindowHeight/2));



Same thing for the others.

At the end, you are using directly Direct3D Coordinates, so your shader must not multiply position with matrices.

This technique should be performed as a PostProcessing

Share this post


Link to post
Share on other sites
Quote:
Original post by XVincentX

if (pRect->right > YourWindowHeight/2)
pRect->right/= (YourWindowHeight/2);
else
pRect->right= -(pRect->right / (YourWindowHeight/2));



Sir, What is the logic behind the conversion of mouse coordinates[YourWindowWidth,YourWindowHeight] to world coordinates [ -1,1]

Ramachandra Innanchaya.

Share this post


Link to post
Share on other sites
Let's suppose we have a window 800x600.
Direct3D sees all in a range of -1,1
So we already know that

Width: 0 = -1; 800 = 1
Height: 0 = -1; 600 = 1

Now, how can we understand a point in pixel coordinates in D3D system?

Let's take a certain point, 235 - 147

If point.x < 0, then the x point it's between -1 and 0 in D3D System; so between 0 and Height/2 in Pixel System

Same thing for y value, so...


void ConvertFromPixelToD3DSystem(int &x, int &y)
{
int WInterval = 2 / Width;
int HInterval = 2 / Height;

x = ( x * Width ) - 1;
y = (y * Height) - 1;
}




In our point case
P(235,147) = D(-0,4125,-0,51)

EDIT: Oh now i read...my previous code was wrong, i apologize, i wrote directly without thinking.


[Edited by - XVincentX on July 2, 2008 8:32:20 AM]

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!