Lab_Rat
asked on
Moving a pixel along a vector!
Save Me!
The following function is -supposed- to move a pixel along a vector from the current point to the origin a little way.
Variables and array's and all that are fine, what the problem is, is the -equation- for moving the point.
What's supposed to happen is:
A point's X and Y distances from the origin are found, then the distance of the point directly from the origin is calculated using SQRT( DX*DX + DY*DY),
The X length and Y length are then reduced to single units by dividing DX and DY by the distance between the origin and the pixel.
Then the new distance is calulated by adding a small amount (can be negative) to the current distance.
Then the X and Y unit values are multiplied by the new distance value to find the co-ordinates of the translated pixel to be used instead of the pixel found at that point.
It should work, but the effect the following code generates produces is to move a pixel that should only move on the X direction (as the x pixel is in aline with the x origin) a little on the Y position too.
What on earth is going on? why does the pixel on the X origin's plane move on the Y too, and why does the same happen for Y?
It -must- be the equation, but I can't see what the problem is!
HELP!
void generateBumpAndBaseImage()
{
int currentPixel=0;
int dx=-((windowX/2)+centerX), dy=-((wind owY/2)+cen terY),rx=0 ,ry=0,main Pixel,dry, targetX=0, targetY=0, tempX,temp Y,distX,di stY;
float sx,sy,smy,scaler,distance, xVector,yV ector,newD istance;
consta+=3;
for(int y=dy;y<dy+windowY;targetY+ +,y++)
{
//**lighting**
sy=y/40f;
smy=(sy*sy)+consta;
//**texture**
distY=(targetY-centerY);
dry=distY*distY;
targetX=0;
for(int x=dx;x<dx+windowX;targetX+ +,x++)
{
//**lighting**
sx=x/40f;
logo.imageA[currentPixel]= (int)(((Ma th.sin(((s x*sx)+cons ta)+smy)*1 28)+128)/d epth);
//**texture**
distX=(targetX-centerX);
//Find the distance current pixel is away from origin
distance=(float)(Math.sqrt ((distX*di stX)+dry)) ;
//Find the unit x and y values of the pixel from the origin
xVector=distX/distance;
yVector=distY/distance;
//Create new distance using bumpmap information as the alteration
newDistance=distance+(floa t)((logo.i mageA[curr entPixel]- 128f)/10f) ;
//Move from the origin to the offset pixel
tempX=(int)(xVector*newDis tance)+cen terX;
tempY=(int)(yVector*newDis tance)+cen terY;
//Check the new pixel is in bounds
if(tempX<0) tempX=0;
if(tempX>windowX) tempX=windowX;
if(tempY<0) tempY=0;
if(tempY>windowY-2) tempY=windowY-2;
//Map the X,Y co-ordinates onto the array
mainPixel=tempX+(tempY*win dowX);
//Store translated pixel in the image array
logo.imageR[currentPixel]= baseTextur e.imageR[m ainPixel];
logo.imageG[currentPixel]= baseTextur e.imageG[m ainPixel];
logo.imageB[currentPixel]= baseTextur e.imageB[m ainPixel];
currentPixel++;
}
}
}
site: http://www.untamed.co.uk
The following function is -supposed- to move a pixel along a vector from the current point to the origin a little way.
Variables and array's and all that are fine, what the problem is, is the -equation- for moving the point.
What's supposed to happen is:
A point's X and Y distances from the origin are found, then the distance of the point directly from the origin is calculated using SQRT( DX*DX + DY*DY),
The X length and Y length are then reduced to single units by dividing DX and DY by the distance between the origin and the pixel.
Then the new distance is calulated by adding a small amount (can be negative) to the current distance.
Then the X and Y unit values are multiplied by the new distance value to find the co-ordinates of the translated pixel to be used instead of the pixel found at that point.
It should work, but the effect the following code generates produces is to move a pixel that should only move on the X direction (as the x pixel is in aline with the x origin) a little on the Y position too.
What on earth is going on? why does the pixel on the X origin's plane move on the Y too, and why does the same happen for Y?
It -must- be the equation, but I can't see what the problem is!
HELP!
void generateBumpAndBaseImage()
{
int currentPixel=0;
int dx=-((windowX/2)+centerX),
float sx,sy,smy,scaler,distance,
consta+=3;
for(int y=dy;y<dy+windowY;targetY+
{
//**lighting**
sy=y/40f;
smy=(sy*sy)+consta;
//**texture**
distY=(targetY-centerY);
dry=distY*distY;
targetX=0;
for(int x=dx;x<dx+windowX;targetX+
{
//**lighting**
sx=x/40f;
logo.imageA[currentPixel]=
//**texture**
distX=(targetX-centerX);
//Find the distance current pixel is away from origin
distance=(float)(Math.sqrt
//Find the unit x and y values of the pixel from the origin
xVector=distX/distance;
yVector=distY/distance;
//Create new distance using bumpmap information as the alteration
newDistance=distance+(floa
//Move from the origin to the offset pixel
tempX=(int)(xVector*newDis
tempY=(int)(yVector*newDis
//Check the new pixel is in bounds
if(tempX<0) tempX=0;
if(tempX>windowX) tempX=windowX;
if(tempY<0) tempY=0;
if(tempY>windowY-2) tempY=windowY-2;
//Map the X,Y co-ordinates onto the array
mainPixel=tempX+(tempY*win
//Store translated pixel in the image array
logo.imageR[currentPixel]=
logo.imageG[currentPixel]=
logo.imageB[currentPixel]=
currentPixel++;
}
}
}
site: http://www.untamed.co.uk
ASKER
: thinks :
Hmmmmmmmm...... interesting!
I'm in work right now, but as soon as I've got home and been the loo, I'll cut & paste.
If you're reading this in the mean time, can I draw your attention to the current way it looks at:
Http://surf.to/labratspage
It's not the live version, just somewhere I can upload applets too without using my domains... hence all the JS errors because fortuncities code conflicts with mine. :o)
But it shows the effect I'm getting (The pixels move topy-left to bottomy-right, rather than uppy-downy and lefty-righty along the origin planes), I'll upload the latest change's you have proposed, and if it doesn't work, I'll send you the full .Java files if you want to look at them, so you can compile them and see what's going on properly.
:o)
I hope you come back! I'll keep monitoring this thread and am ready with the 250 points.
Hmmmmmmmm...... interesting!
I'm in work right now, but as soon as I've got home and been the loo, I'll cut & paste.
If you're reading this in the mean time, can I draw your attention to the current way it looks at:
Http://surf.to/labratspage
It's not the live version, just somewhere I can upload applets too without using my domains... hence all the JS errors because fortuncities code conflicts with mine. :o)
But it shows the effect I'm getting (The pixels move topy-left to bottomy-right, rather than uppy-downy and lefty-righty along the origin planes), I'll upload the latest change's you have proposed, and if it doesn't work, I'll send you the full .Java files if you want to look at them, so you can compile them and see what's going on properly.
:o)
I hope you come back! I'll keep monitoring this thread and am ready with the 250 points.
ASKER
I found it by random alterations.
Erm, it was all to do with relative positions of things... I needed to actually change dx and xy's values, all the vector calculations were actually accurate!
Er.... I'm not sure how much work you did on your answer, would you mind taking just a few points as you didn't quite get it?
void generateBumpAndBaseImage()
{
int currentPixel=0;
int dx=-((windowX/2)+centerX-( windowX/2) ),dy=-((wi ndowY/2)+c enterY-(wi ndowY/2)), rx=0,ry=0, mainPixel, dry,target X=0,target Y=0,tempX, tempY,dist X,distY;
float sx,sy,smy,scaler,distance, xVector,yV ector,newD istance;
consta+=3;
for(int y=dy;y<dy+windowY;targetY+ +,y++)
{
//**lighting**
sy=y/40f;
smy=(sy*sy)+consta;
//**texture**
distY=(targetY-centerY);
dry=distY*distY;
targetX=0;
for(int x=dx;x<dx+windowX;targetX+ +,x++)
{
//**lighting**
sx=x/40f;
logo.imageA[currentPixel]= (int)(((Ma th.sin(((s x*sx)+cons ta)+smy)*1 28)+128)/d epth);
//**texture**
distX=(targetX-centerX);
//Find the distance current pixel is away from origin
distance=(float)(Math.sqrt ((distX*di stX)+dry)) ;
//Find the direction of the pixel from the origin
xVector=distX/distance;
yVector=distY/distance;
//Create new distance using bumpmap information as the alteration
newDistance=distance+(floa t)(Math.ab s(logo.ima geA[curren tPixel]-12 8f)/10f);
//Move from the origin to the offset pixel
tempX=(int)(xVector*newDis tance)+cen terX;
tempY=(int)(yVector*newDis tance)+cen terY;
//Check the new pixel is in bounds
if(tempX<0) tempX=0;
if(tempX>windowX) tempX=windowX;
if(tempY<0) tempY=0;
if(tempY>windowY-2) tempY=windowY-2;
//Map the X,Y co-ordinates onto the array
mainPixel=tempX+(tempY*win dowX);
//Store translated pixel in the image array
logo.imageR[currentPixel]= baseTextur e.imageR[m ainPixel];
logo.imageG[currentPixel]= baseTextur e.imageG[m ainPixel];
logo.imageB[currentPixel]= baseTextur e.imageB[m ainPixel];
currentPixel++;
}
}
}
Erm, it was all to do with relative positions of things... I needed to actually change dx and xy's values, all the vector calculations were actually accurate!
Er.... I'm not sure how much work you did on your answer, would you mind taking just a few points as you didn't quite get it?
void generateBumpAndBaseImage()
{
int currentPixel=0;
int dx=-((windowX/2)+centerX-(
float sx,sy,smy,scaler,distance,
consta+=3;
for(int y=dy;y<dy+windowY;targetY+
{
//**lighting**
sy=y/40f;
smy=(sy*sy)+consta;
//**texture**
distY=(targetY-centerY);
dry=distY*distY;
targetX=0;
for(int x=dx;x<dx+windowX;targetX+
{
//**lighting**
sx=x/40f;
logo.imageA[currentPixel]=
//**texture**
distX=(targetX-centerX);
//Find the distance current pixel is away from origin
distance=(float)(Math.sqrt
//Find the direction of the pixel from the origin
xVector=distX/distance;
yVector=distY/distance;
//Create new distance using bumpmap information as the alteration
newDistance=distance+(floa
//Move from the origin to the offset pixel
tempX=(int)(xVector*newDis
tempY=(int)(yVector*newDis
//Check the new pixel is in bounds
if(tempX<0) tempX=0;
if(tempX>windowX) tempX=windowX;
if(tempY<0) tempY=0;
if(tempY>windowY-2) tempY=windowY-2;
//Map the X,Y co-ordinates onto the array
mainPixel=tempX+(tempY*win
//Store translated pixel in the image array
logo.imageR[currentPixel]=
logo.imageG[currentPixel]=
logo.imageB[currentPixel]=
currentPixel++;
}
}
}
If you've fixed it yourself, please post a free (0 point) question in the CS topic area asking to have the question PAQ'd (moved into the previously asked questions) and your points refunded.
ahh i see where i went wrong, no matter.
and the applet crashed
and the applet crashed
ASKER
Ok, I sure will when I get a spare few minutes.
The finished applet is <a href=http://www.untamed.co.uk/Site/Diary/Downloads/bigThing/bumpMapping.htm>-HERE-</a>
I'll award 50 points, 'cos I want to thank you for helping me out.
Cheers. :o)
(would you mind explaining in english what happened? I fixed it by a drunken guess at 2 in the morning...)
The finished applet is <a href=http://www.untamed.co.uk/Site/Diary/Downloads/bigThing/bumpMapping.htm>-HERE-</a>
I'll award 50 points, 'cos I want to thank you for helping me out.
Cheers. :o)
(would you mind explaining in english what happened? I fixed it by a drunken guess at 2 in the morning...)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
void generateBumpAndBaseImage()
{
int currentPixel=0;
int dx=-((windowX/2)+centerX),
float sx,sy,smy,scaler,distance,
consta+=3;
for(int y=dy;y<dy+windowY;y++)
{
//**lighting**
sy=y/40f;
smy=(sy*sy)+consta;
dry=y*y;
for(int x=dx;x<dx+windowX;x++)
{
//**lighting**
sx=x/40f;
logo.imageA[currentPixel]=
//Find the distance current pixel is away from origin
distance=(float)(Math.sqrt
//Find the unit x and y values of the pixel from the origin
xVector=x/distance;
yVector=y/distance;
//Create new distance using bumpmap information as the alteration
newDistance=distance+(floa
//Move from the origin to the offset pixel
tempX=(int)(xVector*newDis
tempY=(int)(yVector*newDis
//Check the new pixel is in bounds
if(tempX<0) tempX=0;
if(tempX>windowX) tempX=windowX;
if(tempY<0) tempY=0;
if(tempY>windowY-2) tempY=windowY-2;
//Map the X,Y co-ordinates onto the array
mainPixel=tempX+(tempY*win
//Store translated pixel in the image array
logo.imageR[currentPixel]=
logo.imageG[currentPixel]=
logo.imageB[currentPixel]=
currentPixel++;
}
}
}