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

📄 crosshair.cpp

📁 c++程序
💻 CPP
字号:
// Crosshair.cpp: Implementierung der Klasse CCrosshair.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Crosshair.h"

#define CROSSHAIR_SCALING 30.0f

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

CCrosshair::CCrosshair()
{
	// Init member variables
	m_CrosshairSize = 1.0f;
	m_CrosshairTransparency = 1.0f;
}

CCrosshair::~CCrosshair()
{

}

void CCrosshair::DrawCrosshair(unsigned int iFOV, float fAspect)
{
	// Draw the crosshair

	// Save depth buffer bit, color buffer bit, lighting bit and fog bit
	glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | 
		         GL_LIGHTING_BIT     | GL_FOG_BIT);

		// Use crosshair texture
		m_CrosshairTexture.Use();

		// Enable blending
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glColor4f(1.0f, 1.0f, 1.0f, m_CrosshairTransparency);

		// Disable checking the depth buffer
		glDisable(GL_DEPTH_TEST);

		// Disable lighting and fog
		glDisable(GL_LIGHTING);
		glDisable(GL_FOG);
		
		// Distance of the crosshair to the camera
		const float fDistance = -1.0f;

		// Project the quad into a 2D plane that is centered in the screen. Size depends on
		// m_CrosshairSize. fAspect is used to scale the to Project2D passed coordinates so that
		// the crosshair is always rectangular, regardless of the screen's aspect.

		float fLowerLeft[3];
		float fLowerRight[3];
		float fUpperLeft[3];
		float fUpperRight[3];

		Project2D(-m_CrosshairSize / CROSSHAIR_SCALING, -m_CrosshairSize * fAspect / 
			CROSSHAIR_SCALING, fDistance, iFOV, fAspect, fLowerLeft);
		Project2D(m_CrosshairSize / CROSSHAIR_SCALING, -m_CrosshairSize * fAspect / 
			CROSSHAIR_SCALING, fDistance, iFOV, fAspect, fLowerRight);
		Project2D(-m_CrosshairSize / CROSSHAIR_SCALING, m_CrosshairSize * fAspect / 
			CROSSHAIR_SCALING, fDistance, iFOV, fAspect, fUpperLeft);
		Project2D(m_CrosshairSize / CROSSHAIR_SCALING, m_CrosshairSize * fAspect / 
			CROSSHAIR_SCALING, fDistance, iFOV, fAspect, fUpperRight);
	
		// Draw crosshair quad
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f);
			glVertex3fv(fLowerLeft);
			glTexCoord2f(1.0f, 0.0f);
			glVertex3fv(fLowerRight);
			glTexCoord2f(1.0f, 1.0f);
			glVertex3fv(fUpperRight);
			glTexCoord2f(0.0f, 1.0f);
			glVertex3fv(fUpperLeft);
		glEnd();

	glPopAttrib();
}

void CCrosshair::Project2D(const float fU, const float fV, const float fPlane,
						const unsigned int iFOV, float fAspect, float fVertex3DOut[3])
{
	// Project a point in 2D space. The point in 2D space is normalized. 
	// Lower-left corner is -1,-1 and upper-right is +1,+1

	fVertex3DOut[2] = fPlane;
	fVertex3DOut[1] =- tanf(iFOV * 0.5f * 6.283185f / 360.0f ) * fVertex3DOut[2];
	fVertex3DOut[0] = fVertex3DOut[1] * fAspect * fU;
	fVertex3DOut[1] *= fV;
}

void CCrosshair::LoadBMPTexture(char szFilename[])
{
	// Loads a crosshair in form of a BMP texture with
	// transparent background into the class

	m_CrosshairTexture.LoadTransparentBMP(szFilename);
}

void CCrosshair::SetCrosshairSize(float fCrosshairSize)
{
	if (fCrosshairSize > 0.0f)
		m_CrosshairSize = fCrosshairSize;
}

float CCrosshair::GetCrosshairSize()
{
	return m_CrosshairSize;
}

void CCrosshair::SetCrosshairTransparency(float fTransparency)
{
	if (fTransparency > 0.0f && fTransparency < 1.0f)
		m_CrosshairTransparency = fTransparency;
}

float CCrosshair::GetCrosshairTransparency()
{
	return m_CrosshairTransparency;
}

⌨️ 快捷键说明

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