Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lensflare

Started by Winegums Apr 2, 2007 at 8:42 PM 2 replies 750+ views
Original Post
Winegums
Winegums
Hi, so I'm trying to put some lensflare into my application, and stumbled apon a nice tutorial on it here: http://www.gamedev.net/reference/articles/article813.asp however I've stumbled at the first hurdle (and the example program on the page is a broken link :/ ). How do I get the position of the light in screen coordinates? My first thought was to create a vector from the camera position and where it's looking, then do a sort of backwards picking job on the light source.
Zakwayda
Zakwayda
Quote:
Original post by Winegums
How do I get the position of the light in screen coordinates?
How about gluProject()?
Winegums
Winegums
thanks a lot jyk, did some jiggery pokery and i think i have that done. i've done all the vector maths and now i'm at the last stage...actually drawing the sprites.

how do i do that? at first i thought you may just go to ortho and do it like that, but that doesn't seem right, since the equations for screen coordinates in the tutorial have a 64 in them, which is a big number for ortho view...
Winegums
Winegums
I found another method to do this, and it seems to suit me better. I'm sorta at my wits end with it though, since for some reason i can't get the 'flares' to translate along the vector between the camera and the light source. I lay my code before the forum in hopes someone can see what i've done wrong.

void DrawLensFlare(){	//Get vector between light source and camera	Vector3D cameraLight;	cameraLight.x = aLight.position[0] - aCamera->mPos.x;	cameraLight.y = aLight.position[1] - aCamera->mPos.y;	cameraLight.z = aLight.position[2] - aCamera->mPos.z;	float cameraLightLength = sqrt(pow(cameraLight.x,2) + pow(cameraLight.y,2) + pow(cameraLight.z,2));	//Normalise the vector	cameraLight.x /= cameraLightLength;	cameraLight.y /= cameraLightLength;	cameraLight.z /= cameraLightLength;    // Start of with a simple quad...    Vector3D vPoint0(-1.0, -1.0, 0.0f);    Vector3D vPoint1( 1.0, -1.0, 0.0f);    Vector3D vPoint2( 1.0,  1.0, 0.0f);    Vector3D vPoint3(-1.0,  1.0, 0.0f);		float lensScale = 5.5f;		//value by which we scale size of bitmap		float distance = 0.8f;		//the scale from the camera to the light source (ie 1.0 is at the camera, 0.0 is at the light)        // If billboarding is toggled on, recompute the quad points        // with respect to the view matrix and the quad's center point.        float mat[16];        glGetFloatv( GL_MODELVIEW_MATRIX, mat );		Vector3D vRight;   vRight.x = mat[0];     vRight.y = mat[4];     vRight.z = mat[8];		Vector3D vUp;      vUp.x = mat[1];        vUp.y = mat[5];        vUp.z = mat[9];		Vector3D vCentre;		//Enable shaders		// Bind and enable Our Vertex Program To The Current State and grab the modelview matrix		cgGLSetStateMatrixParameter(cgModelViewMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);		cgGLEnableProfile(cgLensFlareVertexProfile);		cgGLBindProgram(cgLensFlareVertexProgram);	   //Same for fragment shader	 cgGLBindProgram(cgLensFlareFragmentProgram);	  cgGLEnableProfile(cgLensFlareFragmentProfile);	  cgGLEnableTextureParameter(lensFlareTexture);for(int i = 0; i < 4; i++){	//Attatch the relevant lensflare texture	 cgGLSetTextureParameter(lensFlareTexture, lensFlare->texture);	distance /= 2;	vCentre.x = aLight.position[0] + (distance * cameraLight.x);	vCentre.y = aLight.position[1] + (distance * cameraLight.y);	vCentre.z = aLight.position[2] + (distance * cameraLight.z);	// Now, build a quad around the center point based on the vRight 	// and vUp vectors. This will guarantee that the quad will be 	// orthogonal to the view.	vPoint0 = vCentre + ((-vRight - vUp) * lensScale);	vPoint1 = vCentre + (( vRight - vUp) * lensScale);	vPoint2 = vCentre + (( vRight + vUp) * lensScale);	vPoint3 = vCentre + ((-vRight + vUp) * lensScale);		glEnable(GL_TEXTURE_2D);		glEnable(GL_BLEND);		glDisable(GL_DEPTH_TEST);	// Turn Depth Testing Off		glColor4f(1.0f,0.0f,1.0f,0.5f);					glBlendFunc(GL_SRC_ALPHA,GL_ONE);		// Blending Function For Translucency Based On Source Alpha Value ( NEW )		glBegin( GL_QUADS );		{			glTexCoord2f(0.0f, 0.0f);			glVertex3f(vPoint0.x,vPoint0.y,vPoint0.z);			glTexCoord2f(1.0f, 0.0f);			glVertex3f(vPoint1.x, vPoint1.y,vPoint1.z);			glTexCoord2f(1.0f, 1.0f);			glVertex3f(vPoint2.x,vPoint2.y,vPoint2.z);			glTexCoord2f(0.0f, 1.0f);			glVertex3f(vPoint3.x,vPoint3.y,vPoint3.z);		}		glEnd();		glDisable(GL_BLEND);			  }	//Turn off shaders	  cgGLDisableProfile(cgLensFlareVertexProfile);	//Disable vertex shader	  cgGLDisableProfile(cgLensFlareFragmentProfile);			//Disable fragment shade	  cgGLDisableTextureParameter(lensFlareTexture);			//Disable textures}


from what i can see all the flares are being drawn on top of each other, the only thing i could think thats happening is that they're all being drawn along the vector between the camera and light, therefore they overlap from the view of the camera?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.