Jump to content

bump mapping 2d


Recommended Posts

	float scale=0.1;
	poziom=(left-right)*scale;
	pion=(up-down)*scale;
	poziom=-poziom;
	pion=-pion;

	void RendererLightBumpMap(int x,int y,float r)
	{
		for(int py=y-r;py<y+r;py+=1)
		{
			for(int px=x-r;px<x+r;px+=1)
			{  
				float atten=1-sqrt( (px-x)*(px-x)+(py-y)*(py-y))/float(r);
				if(atten<0)
					atten=0;
				if(atten>1)
					atten=1;
				if(atten>0)
				{

				unsigned char red;
				unsigned char green;
				unsigned char blue;

				unsigned char leftred,leftgreen,leftblue;
				unsigned char rightred,rightgreen,rightblue;
				unsigned char upred,upgreen,upblue;
				unsigned char downred,downgreen,downblue;
				float left,right,up,down;
				float poziom,pion;
				GetPixel(px-1,py,leftred,leftgreen,leftblue);
				GetPixel(px+1,py,rightred,rightgreen,rightblue);
				GetPixel(px,py-1,upred,upgreen,upblue);
				GetPixel(px,py+1,downred,downgreen,downblue);

				left=float(float(leftred)/255+float(leftgreen)/255+float(leftblue)/255)/3;
				right=float(float(rightred)/255+float(rightgreen)/255+float(rightblue)/255)/3;
				up=float(float(upred)/255+float(upgreen)/255+float(upblue)/255)/3;
				down=float(float(downred)/255+float(downgreen)/255+float(downblue)/255)/3;
				float scale=0.1;
				poziom=(left-right)*scale;
				pion=(up-down)*scale;
				poziom=-poziom;
				pion=-pion;
				float lenght=sqrt(poziom*poziom+pion*pion);
				poziom=poziom/lenght;
				pion=pion/lenght;
				float lightDirX,lightDirY;
				lightDirX=x+r;
				lightDirY=y+r;
				lightDirX=lightDirX-px;
				lightDirY=lightDirY-py;
				lenght=sqrt(lightDirX*lightDirX+lightDirY*lightDirY);
				lightDirX=lightDirX/lenght;
				lightDirY=lightDirY/lenght;
				float diffuse=lightDirX*poziom+lightDirY*pion;
				
				
				if(diffuse<0)
					diffuse=0;
				if(diffuse>1)
					diffuse=1;
				GetPixel(px,py,red,green,blue);
				float r1,g1,b1;

				r1=float(red)/255;
				g1=float(green)/255;
				b1=float(blue)/255;
		
				float r,g,b;
				r=r1;
				g=g1;
				b=b1;
				
				
				//r1=r1*diffuse*atten+r1+(1-r1)*0.5*diffuse*atten;
				//g1=g1*diffuse*atten+g1+(1-g1)*0.5*diffuse*atten;
				//b1=b1*diffuse*atten+b1+(1-b1)*0.5*diffuse*atten;
				r1=diffuse;
				g1=diffuse;
				b1=diffuse;

				if(r1<r)
					r1=r;
				if(g1<g)
					g1=g;
				if(b1<b)
					b1=b;

				if(r1>1)
					r1=1;
				if(g1>1)
					g1=1;
				if(b1>1)
					b1=1;
				unsigned char rW=r1*255;
				unsigned char gW=g1*255;
				unsigned char bW=b1*255;
				
			
				Color3iT(rW,gW,bW);
				PutPixelT(px,py);
				}

			}
		}
	}

tutaj cały projekt:

https://megawrzuta.pl/2svbuhyv

chodzi o ten kawałek kodu co jest źle:

    float scale=0.1;
                poziom=(left-right)*scale;
                pion=(up-down)*scale;
                poziom=-poziom;
                pion=-pion;

bo jak jest tak:

    float scale=0.1;
                poziom=(left-right)*scale;
                pion=(up+down)*scale;
            to działa ale jakoś dziwnie

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...