• Advertisement
Sign in to follow this  

Bug during realistic light algorithm

This topic is 3827 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

Hey, After reading alot about global illumination, I want to program my own global illumination algorithm for realistic lighting. That also records indirect light. Useing a camera facing from each pixel of a wall, the light of each pixel has been captured. After my pc generated a cool looking environment (eventually), the user can walk freely around it. Now there is a bug in it: Shadow appears on top of the wall, while there must be no shadow. When checking the incoming data: the data seems to be correct, the top of the texture has 255 255 255 (pure white values!). Why has my texture moved? shadow spots appearing at the wrong place Camera captures the environment:
void Photonenvironment::RenderToTexture()
{
	int viewport[4];
        glGetIntegerv(GL_VIEWPORT,(int*)viewport);
	glViewport(0,0,tempres,tempres);				

	renderBlockingwall();

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,cameratexture);	
	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, tempres, tempres, 0);
	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE,temp);
	
	glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);
	glDisable(GL_TEXTURE_2D);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
}




Storing light values of its environment:
void Photonenvironment::averagelight(int u, int v)
{
	float total = (float)tempres*(float)tempres*(float)3.0f;
	float totalonecolor = (float)tempres*(float)tempres;
	int lightamountrgb = 0;
	int lightamountr = 0;int lightamountg = 0;int lightamountb = 0;
	float averagergb = 0.0f;
	float averager = 0.0f;float averageg = 0.0f;float averageb = 0.0f;
	
	for (int vc = 0 ; vc < tempres; vc++)
	 {
		 for (int uc = 0; uc < tempres; uc++)
		{
			lightamountrgb+=(int)(temp[(vc*(tempres*3))+(uc*3)+0]+
				(int)temp[(vc*(tempres*3))+(uc*3)+1]+
				(int)temp[(vc*(tempres*3))+(uc*3)+2]);
			lightamountr+=(int)temp[(vc*(tempres*3))+(uc*3)+0];
			lightamountg+=(int)temp[(vc*(tempres*3))+(uc*3)+1];
			lightamountb+=(int)temp[(vc*(tempres*3))+(uc*3)+2];
		}
    }
	averagergb = (float)lightamountrgb/(float)total;
	averager = (float)lightamountr/(float)totalonecolor;
	averageg = (float)lightamountg/(float)totalonecolor;
	averageb = (float)lightamountb/(float)totalonecolor;
	
	float magnitude = 1.0f;


	int lightresultrgb = 0;
	if((averagergb*magnitude) > 255.0f)
	{
		lightresultrgb = 255;
	}
	else
	{
		if((averagergb*magnitude) < 0.0f)
		{
			lightresultrgb = 0;
		}
		else
		{
			lightresultrgb = (int)(averagergb * magnitude);
		}
	}

	// red
	int lightresultr = 0;
	if((averager*magnitude) > 255.0f)
	{
		lightresultr = 255;
	}
	else
	{
		if((averager*magnitude) < 0.0f)
		{
			lightresultr = 0;
		}
		else
		{
			lightresultr = (int)(averager * magnitude);
		}
	}
	// green
	int lightresultg = 0;
	if((averageg*magnitude) > 255.0f)
	{
		lightresultg = 255;
	}
	else
	{
		if((averageg*magnitude) < 0.0f)
		{
			lightresultg = 0;
		}
		else
		{
			lightresultg = (int)(averageg * magnitude);
		}
	}
	// blue
	int lightresultb = 0;
	if((averageb*magnitude) > 255.0f)
	{
		lightresultb = 255;
	}
	else
	{
		if((averageb*magnitude) < 0.0f)
		{
			lightresultb = 0;
		}
		else
		{
			lightresultb = (int)(averageb * magnitude);
		}
	}

	photontexturedata[(v*(photontextureres*3))+(u*3)+0] = (unsigned char)lightresultr;//0.0f;
	photontexturedata[(v*(photontextureres*3))+(u*3)+1] = (unsigned char)lightresultg;//0.0f;
	photontexturedata[(v*(photontextureres*3))+(u*3)+2] = (unsigned char)lightresultb;//0.0f;
	printf("%i %i %i %i %i %i\n",lightamountr,u,v,lightresultr,lightresultg,lightresultb);
	
	}




Creating the texture:
void Photonenvironment::updatephotontexture()
{
	glGenTextures(1,&photontexture);
	glBindTexture(GL_TEXTURE_2D,photontexture);
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
	glTexImage2D(GL_TEXTURE_2D,0,3,photontextureres,photontextureres,0,GL_RGB,GL_UNSIGNED_BYTE,photontexturedata);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	gluBuild2DMipmaps(GL_TEXTURE_2D,3,photontextureres,photontextureres,GL_RGB,GL_UNSIGNED_BYTE,photontexturedata);
	}




Rendering the texture:
glPushMatrix();
glDisable(GL_LIGHTING);
glColor3f(1.0f,1.0f,1.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,photontexture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,1.0f);
glVertex3f(wallc1x,wallc1y,wallc1z);
glTexCoord2f(0.0f,0.0f);
glVertex3f(wallc2x,wallc2y,wallc2z);
glTexCoord2f(1.0f,0.0f);
glVertex3f(wallc3x,wallc3y,wallc3z);
glTexCoord2f(1.0f,1.0f);
glVertex3f(wallc4x,wallc4y,wallc4z);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();




Share this post


Link to post
Share on other sites
Advertisement
It looks to me as though the texture is wrapping when you finaly come for render it on the quad, you need to set your texture wrap mode to clamp to fix it. They made the same mistake in Boom Boom rocket on XBLA when they are doing their glow rendering, this causes there to be glow creaping from the top of the screen when the thing that is glowing is at the bottom.

Share this post


Link to post
Share on other sites
Yeah, this looks like some wrapping is taking place. You might want to set your wrap modes for the s and t texture coordinate to GL_CLAMP_TO_EDGE.

Also, you specify GL_LINEAR for both the magnification and also for the minification filter but then you create mipmaps with gluBuild2DMipmaps. You either don't have to create the mipmaps because they will not ever be used with these filtering parameter or you would have to specify GL_LINEAR_MIPMAP_LINEAR or some other mipmapping filter in order for the mipmaps to actually be used.

Share this post


Link to post
Share on other sites
Agreed RE: wrapping/clamping.

Quote:
Original post by Trenki
Also, you specify GL_LINEAR for both the magnification and also for the minification filter but then you create mipmaps with gluBuild2DMipmaps. You either don't have to create the mipmaps because they will not ever be used with these filtering parameter or you would have to specify GL_LINEAR_MIPMAP_LINEAR or some other mipmapping filter in order for the mipmaps to actually be used.

Note that GL_LINEAR_MIPMAP_LINEAR is only for the minification filter. It isn't a valid parameter for magnification (you'd get a GL_INVALID_ENUM after the call), for which GL_LINEAR is probably what you want. More on all subjects.

Share this post


Link to post
Share on other sites
Thank you all! It works now.





glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement