⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glcamera.cpp

📁 关于OpenGL的实例教程源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	else
		return false;
}

void glCamera::RenderLensFlare()
{
	GLfloat Length = 0.0f;

	// Draw the flare only If the light source is in our line of sight
	if(SphereInFrustum(m_LightSourcePos, 1.0f) == TRUE)
	{
		vLightSourceToCamera = m_Position - m_LightSourcePos;		// Lets compute the vector that points to the camera from
																	// the light source.

		Length = vLightSourceToCamera.Magnitude();					// Save the length we will need it in a minute

		ptIntersect = m_DirectionVector * Length;					// Now lets find an point along the cameras direction
																	// vector that we can use as an intersection point. 
																	// Lets translate down this vector the same distance
																	// that the camera is away from the light source.
		ptIntersect += m_Position;

		vLightSourceToIntersect = ptIntersect - m_LightSourcePos;	// Lets compute the vector that points to the Intersect
																	// point from the light source
		Length = vLightSourceToIntersect.Magnitude();				// Save the length we will need it later.
		vLightSourceToIntersect.Normalize();						// Normalize the vector so its unit length
		
		glEnable(GL_BLEND);											// You should already know what this does
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);							// You should already know what this does
		glDisable(GL_DEPTH_TEST);									// You should already know what this does
		glEnable(GL_TEXTURE_2D);									// You should already know what this does
			
		/////////// Differenet Color Glows & Streaks /////////////////////
		//RenderBigGlow(1.0f, 1.0f, 1.0f, 1.0f, m_LightSourcePos, 1.0f);
		//RenderStreaks(1.0f, 1.0f, 0.8f, 1.0f, m_LightSourcePos, 0.7f);
		//
		//RenderBigGlow(1.0f, 0.9f, 1.0f, 1.0f, m_LightSourcePos, 1.0f);
		//RenderStreaks(1.0f, 0.9f, 1.0f, 1.0f, m_LightSourcePos, 0.7f);
		//////////////////////////////////////////////////////////////////


		//########################## NEW STUFF ##################################

		if (!IsOccluded(m_LightSourcePos))	//Check if the center of the flare is occluded
		{
			// Render the large hazy glow
			RenderBigGlow(0.60f, 0.60f, 0.8f, 1.0f, m_LightSourcePos, 16.0f);
			// Render the streaks
			RenderStreaks(0.60f, 0.60f, 0.8f, 1.0f, m_LightSourcePos, 16.0f);
			// Render the small Glow
			RenderGlow(0.8f, 0.8f, 1.0f, 0.5f, m_LightSourcePos, 3.5f);

			pt = vLightSourceToIntersect * (Length * 0.1f);				// Lets compute a point that is 20%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.9f, 0.6f, 0.4f, 0.5f, pt, 0.6f);				// Render the small Glow

			pt = vLightSourceToIntersect * (Length * 0.15f);			// Lets compute a point that is 30%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.		
		
			RenderHalo(0.8f, 0.5f, 0.6f, 0.5f, pt, 1.7f);				// Render the a Halo
		
			pt = vLightSourceToIntersect * (Length * 0.175f);			// Lets compute a point that is 35%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderHalo(0.9f, 0.2f, 0.1f, 0.5f, pt, 0.83f);				// Render the a Halo

			pt = vLightSourceToIntersect * (Length * 0.285f);			// Lets compute a point that is 57%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderHalo(0.7f, 0.7f, 0.4f, 0.5f, pt, 1.6f);				// Render the a Halo
		
			pt = vLightSourceToIntersect * (Length * 0.2755f);			// Lets compute a point that is 55.1%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.9f, 0.9f, 0.2f, 0.5f, pt, 0.8f);				// Render the small Glow

			pt = vLightSourceToIntersect * (Length * 0.4775f);			// Lets compute a point that is 95.5%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.93f, 0.82f, 0.73f, 0.5f, pt, 1.0f);			// Render the small Glow
		
			pt = vLightSourceToIntersect * (Length * 0.49f);			// Lets compute a point that is 98%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderHalo(0.7f, 0.6f, 0.5f, 0.5f, pt, 1.4f);				// Render the a Halo

			pt = vLightSourceToIntersect * (Length * 0.65f);			// Lets compute a point that is 130%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.7f, 0.8f, 0.3f, 0.5f, pt, 1.8f);				// Render the small Glow
		
			pt = vLightSourceToIntersect * (Length * 0.63f);			// Lets compute a point that is 126%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.4f, 0.3f, 0.2f, 0.5f, pt, 1.4f);				// Render the small Glow

			pt = vLightSourceToIntersect * (Length * 0.8f);				// Lets compute a point that is 160%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderHalo(0.7f, 0.5f, 0.5f, 0.5f, pt, 1.4f);				// Render the a Halo
		
			pt = vLightSourceToIntersect * (Length * 0.7825f);			// Lets compute a point that is 156.5%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.8f, 0.5f, 0.1f, 0.5f, pt, 0.6f);				// Render the small Glow

			pt = vLightSourceToIntersect * (Length * 1.0f);				// Lets compute a point that is 200%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderHalo(0.5f, 0.5f, 0.7f, 0.5f, pt, 1.7f);				// Render the a Halo
		
			pt = vLightSourceToIntersect * (Length * 0.975f);			// Lets compute a point that is 195%
			pt += m_LightSourcePos;										// away from the light source in the
																	// direction of the intersection point.
		
			RenderGlow(0.4f, 0.1f, 0.9f, 0.5f, pt, 2.0f);				// Render the small Glow

		}
		glDisable(GL_BLEND );										// You should already know what this does
		glEnable(GL_DEPTH_TEST);									// You should already know what this does
		glDisable(GL_TEXTURE_2D);									// You should already know what this does
	}
}

void glCamera::RenderHalo(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale)
{
	glPoint q[4];
	
	// Basically we are just going to make a 2D box
	// from four points we don't need a z coord because
	// we are rotating the camera by the inverse so the 
	// texture mapped quads will always face us.
	q[0].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[0].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[1].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[1].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	q[2].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[2].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[3].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[3].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	glPushMatrix();													// Save the model view matrix
	glTranslatef(p.x, p.y, p.z);									// Translate to our point
	glRotatef(-m_HeadingDegrees, 0.0f, 1.0f, 0.0f);
	glRotatef(-m_PitchDegrees, 1.0f, 0.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, m_HaloTexture);					// Bind to the Big Glow texture
	glColor4f(r, g, b, a);											// Set the color since the texture is a gray scale
	
	glBegin(GL_TRIANGLE_STRIP);										// Draw the Big Glow on a Triangle Strip
		glTexCoord2f(0.0f, 0.0f);					
		glVertex2f(q[0].x, q[0].y);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(q[1].x, q[1].y);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(q[2].x, q[2].y);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(q[3].x, q[3].y);
	glEnd();										
	glPopMatrix();													// Restore the model view matrix
}

void glCamera::RenderGlow(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale)
{
	glPoint q[4];
	
	// Basically we are just going to make a 2D box
	// from four points we don't need a z coord because
	// we are rotating the camera by the inverse so the 
	// texture mapped quads will always face us.
	q[0].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[0].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.

	q[1].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[1].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	q[2].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[2].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[3].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[3].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	glPushMatrix();													// Save the model view matrix
	glTranslatef(p.x, p.y, p.z);									// Translate to our point
	glRotatef(-m_HeadingDegrees, 0.0f, 1.0f, 0.0f);
	glRotatef(-m_PitchDegrees, 1.0f, 0.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, m_GlowTexture);					// Bind to the Big Glow texture
	glColor4f(r, g, b, a);											// Set the color since the texture is a gray scale
	
	glBegin(GL_TRIANGLE_STRIP);										// Draw the Big Glow on a Triangle Strip
		glTexCoord2f(0.0f, 0.0f);					
		glVertex2f(q[0].x, q[0].y);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(q[1].x, q[1].y);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(q[2].x, q[2].y);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(q[3].x, q[3].y);
	glEnd();										
	glPopMatrix();													// Restore the model view matrix
}

void glCamera::RenderBigGlow(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale)
{
	glPoint q[4];
	
	// Basically we are just going to make a 2D box
	// from four points we don't need a z coord because
	// we are rotating the camera by the inverse so the 
	// texture mapped quads will always face us.
	q[0].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[0].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[1].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[1].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	q[2].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[2].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[3].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[3].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	glPushMatrix();													// Save the model view matrix
	glTranslatef(p.x, p.y, p.z);									// Translate to our point
	glRotatef(-m_HeadingDegrees, 0.0f, 1.0f, 0.0f);
	glRotatef(-m_PitchDegrees, 1.0f, 0.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, m_BigGlowTexture);					// Bind to the Big Glow texture
	glColor4f(r, g, b, a);											// Set the color since the texture is a gray scale
	
	glBegin(GL_TRIANGLE_STRIP);										// Draw the Big Glow on a Triangle Strip
		glTexCoord2f(0.0f, 0.0f);					
		glVertex2f(q[0].x, q[0].y);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(q[1].x, q[1].y);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(q[2].x, q[2].y);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(q[3].x, q[3].y);
	glEnd();										
	glPopMatrix();													// Restore the model view matrix
}

void glCamera::RenderStreaks(GLfloat r, GLfloat g, GLfloat b, GLfloat a, glPoint p, GLfloat scale)
{
	glPoint q[4];
	
	// Basically we are just going to make a 2D box
	// from four points we don't need a z coord because
	// we are rotating the camera by the inverse so the 
	// texture mapped quads will always face us.
	q[0].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[0].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[1].x = (p.x - scale);											// Set the x coordinate -scale units from the center point.
	q[1].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	q[2].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[2].y = (p.y - scale);											// Set the y coordinate -scale units from the center point.
		
	q[3].x = (p.x + scale);											// Set the x coordinate scale units from the center point.
	q[3].y = (p.y + scale);											// Set the y coordinate scale units from the center point.
		
	glPushMatrix();													// Save the model view matrix
	glTranslatef(p.x, p.y, p.z);									// Translate to our point
	glRotatef(-m_HeadingDegrees, 0.0f, 1.0f, 0.0f);
	glRotatef(-m_PitchDegrees, 1.0f, 0.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D, m_StreakTexture);					// Bind to the Big Glow texture
	glColor4f(r, g, b, a);											// Set the color since the texture is a gray scale
	
	glBegin(GL_TRIANGLE_STRIP);										// Draw the Big Glow on a Triangle Strip
		glTexCoord2f(0.0f, 0.0f);					
		glVertex2f(q[0].x, q[0].y);
		glTexCoord2f(0.0f, 1.0f);
		glVertex2f(q[1].x, q[1].y);
		glTexCoord2f(1.0f, 0.0f);
		glVertex2f(q[2].x, q[2].y);
		glTexCoord2f(1.0f, 1.0f);
		glVertex2f(q[3].x, q[3].y);
	glEnd();										
	glPopMatrix();													// Restore the model view matrix
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -