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

📄 simulateview.cpp

📁 这是关于飞机生存力计算软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SimulateView.cpp : implementation of the CSimulateView class
//

#include "stdafx.h"
#include "Simulate.h"

#include "SimulateDoc.h"
#include "SimulateView.h"

#include "TransformSetting.h"

#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSimulateView

IMPLEMENT_DYNCREATE(CSimulateView, CView)

BEGIN_MESSAGE_MAP(CSimulateView, CView)
	//{{AFX_MSG_MAP(CSimulateView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_CONTEXTMENU()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimulateView construction/destruction

CSimulateView::CSimulateView()
{
	// TODO: add construction code here
	m_pMesh = NULL;
	m_dFOV = 40;
	m_CurrentTransformDirection = TRANALONG_X;
	m_bTransforming = FALSE;
	m_nTransformMode = NOCHOICE;

	m_fShiftWithX = 0.0f;
	m_fShiftWithY = 0.0f;
	m_fShiftWithZ = 0.0f;
	m_fRotateAngleWithX = 0.0f;
	m_fRotateAngleWithY = 0.0f;
	m_fRotateAngleWithZ = 0.0f;
	m_fScale = 1.0f;

	m_fMovementStep = 1.0;
	m_fRotationStep = 2.5;
	m_fScaleFactor = 0.05;

	m_Eye.m_fX = 0.0;
	m_Eye.m_fY = 0.0;
	m_Eye.m_fZ = 1000.0;

	m_Center.m_fX = 0.0;
	m_Center.m_fY = 0.0;
	m_Center.m_fZ = 0.0;

	m_Up.m_fX = 0.0;
	m_Up.m_fY = 1.0;
	m_Up.m_fZ = 0.0;

//	stripeImage = new GLubyte[3*stripeImageWidth];
}

CSimulateView::~CSimulateView()
{
	gluDeleteQuadric(m_pMesh);
//	delete [] stripeImage;
}

BOOL CSimulateView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSimulateView drawing

void CSimulateView::OnDraw(CDC* pDC)
{
	CSimulateDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
	DrawScene();
}

/////////////////////////////////////////////////////////////////////////////
// CSimulateView printing

BOOL CSimulateView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSimulateView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSimulateView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CSimulateView diagnostics

#ifdef _DEBUG
void CSimulateView::AssertValid() const
{
	CView::AssertValid();
}

void CSimulateView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSimulateDoc* CSimulateView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSimulateDoc)));
	return (CSimulateDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSimulateView message handlers

int CSimulateView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	HWND hWnd = GetSafeHwnd();
	HDC hDC = ::GetDC(hWnd);
	
	if(SetWindowPixelFormat(hDC)==FALSE)
		return 0;
	
	if(CreateViewGLContext(hDC)==FALSE)
		return 0;
	
	// Default mode
	glPolygonMode(GL_FRONT,GL_LINE);
	glPolygonMode(GL_BACK,GL_LINE);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glEnable(GL_NORMALIZE); 
	glShadeModel(GL_SMOOTH);
	glClearDepth(1000000.0f);
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);

	// Lights, material properties
	GLfloat	vflLight0_ambient[]  = {0.7f, 0.7f, 0.7f, 1.0f};
	GLfloat	vflLight0_diffuse[]  = {0.8f, 0.8f, 0.8f, 1.0f};
	GLfloat	vflLight0_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
	GLfloat vflLight0_Position[] = {0.0f, 0.0f, 1000.0f, 1.0f};
	
	glClearDepth( 1.0 );
	
	glLightfv( GL_LIGHT0, GL_AMBIENT, vflLight0_ambient);
	glLightfv( GL_LIGHT0, GL_DIFFUSE, vflLight0_diffuse);
	glLightfv( GL_LIGHT0, GL_SPECULAR, vflLight0_specular);
	glLightfv( GL_LIGHT0, GL_POSITION, vflLight0_Position);
	glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

	// Default : lighting
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);

	//Debug code for texture
//----------------------------------------------------------------
/*    int i,j;
    FILE *bmp; 
    unsigned int bmp_x, bmp_y;
    unsigned char bmp_red, bmp_green, bmp_blue;

	if((bmp = fopen("e:\\Hill.bmp", "rb")) != NULL)
	{
		fseek(bmp, 0x12, SEEK_SET);
		fread(&bmp_x, 1, 4, bmp);

		fseek(bmp, 0x16, SEEK_SET);
		fread(&bmp_y, 1, 4, bmp);

		fseek(bmp, 0x36, SEEK_SET);
		for(j=0; j<bmp_y; j++)
		{
			for(i=0; i<bmp_x; i++)
			{
				fread(&bmp_blue, 1, 1, bmp);
				fread(&bmp_green, 1, 1, bmp);
				fread(&bmp_red, 1, 1, bmp);
				stripeImage[j][i][0] = (float)bmp_red/255.;
				stripeImage[j][i][1] = (float)bmp_green/255.;
				stripeImage[j][i][2] = (float)bmp_blue/255.;
				if((bmp_blue + bmp_green + bmp_red) == 0)
					stripeImage[j][i][3] = 0.;
				else
					stripeImage[j][i][3] = 1.;

			}
		}
	   fclose(bmp);
	}

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glAlphaFunc(GL_NOTEQUAL, 0);
	glDepthFunc(GL_LEQUAL);	
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);*/
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//	  glTexImage2D(GL_TEXTURE_2D, 0, 4, stripeImageWidth, stripeImageWidth, 0,
//		GL_RGBA, GL_FLOAT, &stripeImage[0][0][0]);

//    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
//    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

//    glEnable(GL_TEXTURE_GEN_S);
//    glEnable(GL_TEXTURE_GEN_T);
	
//    glEnable(GL_TEXTURE_2D);
//    glEnable(GL_CULL_FACE);
//    glEnable(GL_AUTO_NORMAL);
//----------------------------------------------------------------
	
	//Get new OpenGL objects from the library. These objects
	//must be explicitly released when the window is distroyed
	m_pMesh = gluNewQuadric();
	gluQuadricDrawStyle(m_pMesh, GLU_FILL);

	//Create the context menu
	UINT nMainMenuID = 500;
	m_MainMenu.CreatePopupMenu();
	m_MainMenu.AppendMenu(MF_STRING, nMainMenuID++, "Move");
	m_MainMenu.AppendMenu(MF_STRING, nMainMenuID++, "Rotate");
	m_MainMenu.AppendMenu(MF_STRING, nMainMenuID++, "Uniform Scale");
	m_MainMenu.AppendMenu(MF_STRING, nMainMenuID++, "Step Setting...");
	m_MainMenu.AppendMenu(MF_SEPARATOR);

	m_TranMenu.CreatePopupMenu();

	return 0;

}

void CSimulateView::OnDestroy() 
{
	if(wglGetCurrentContext() != NULL)
		wglMakeCurrent(NULL,NULL);
	
	if(m_hrc != NULL)
	{
		wglDeleteContext(m_hrc);
		m_hrc = NULL;
	}

	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CSimulateView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	CSimulateDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	m_nCXOld = cx;
	m_nCYOld = cy;

	CSize size(cx,cy); 

	glViewport(0,0,size.cx,size.cy);
/*	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if(size.cx <= size.cy) 
		glOrtho(-50.0,
				50.0,
				-50.0 * (GLfloat)(size.cy)/(GLfloat)(size.cx),
				50.0 * (GLfloat)(size.cy)/(GLfloat)(size.cx),
				-1000000000,
				100000000);
	else
		glOrtho(-50.0 * (GLfloat)(size.cx)/(GLfloat)(size.cy),
				50.0 * (GLfloat)(size.cx)/(GLfloat)(size.cy),
				-50.0,
				50.0,
				-1000000000,
				100000000);

	glMatrixMode(GL_MODELVIEW);*/

	DrawScene();

}

void CSimulateView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
//	CView::OnTimer(nIDEvent);
	Invalidate(FALSE);
}

void CSimulateView::DrawScene()
{
	CSimulateDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

//	CString sMsg;
//	sMsg.Format("Length: %f, Width: %f, Height: %f",
//				pDoc->m_fGlobalYMax - pDoc->m_fGlobalYMin,
//				pDoc->m_fGlobalXMax - pDoc->m_fGlobalXMin,
//				pDoc->m_fGlobalZMax - pDoc->m_fGlobalZMin);
//	AfxMessageBox(sMsg);

	//Float Vectors used to describe different colors
	GLfloat vflBlueAmbient[] = {0.3f, 0.3f, 0.3f, 1.0f};
	GLfloat vflBlueDiffuse[] = {0.0f, 0.8f, 0.0f, 1.0f};
	GLfloat vflBlueSpecular[] = {0.6f, 0.6f, 0.6f, 1.0f};

	glClearColor(0.0, 0.0, 0.0, 1.0);
	//clear the depth and background buffers
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	gluLookAt(m_Eye.m_fX,
			m_Eye.m_fY,
			m_Eye.m_fZ,
			m_Center.m_fX,
			m_Center.m_fY,
			m_Center.m_fZ,
			m_Up.m_fX,
			m_Up.m_fY,
			m_Up.m_fZ);

//	Calculate the projection matrix manually
/*	float fCenterX, fCenterY, fCenterZ;
	float fXEye, fYEye, fZEye;
	float fXUpper, fYUpper, fZUpper;
	CVector NewX, NewY, NewZ;

	fXEye = 10.0; fYEye = 0.0; fZEye = 1000.0;
	fCenterX = 0.0; fCenterY = 0.0; fCenterZ = 0.0;
	fXUpper = 0.0; fYUpper = 1.0; fZUpper = 0.0;
	
	NewZ.m_fX = fXEye - fCenterX;
	NewZ.m_fY = fYEye - fCenterY;
	NewZ.m_fZ = fZEye - fCenterZ;
	NewZ.Normalize();

	NewX.m_fX = fXUpper;
	NewX.m_fY = fYUpper;
	NewX.m_fZ = fZUpper;
	NewX.CrossProduct(NewZ);
	NewX.Normalize();

	NewY.m_fX = NewZ.m_fX;
	NewY.m_fY = NewZ.m_fY;
	NewY.m_fZ = NewZ.m_fZ;
	NewY.CrossProduct(NewX);
	NewY.Normalize();
	
	GLfloat Matrix[16];
	Matrix[0]=NewX.m_fX; Matrix[1]=NewY.m_fX; Matrix[2]=NewZ.m_fX; Matrix[3]=0.0;
	Matrix[4]=NewX.m_fY; Matrix[5]=NewY.m_fY; Matrix[6]=NewZ.m_fY; Matrix[7]=0.0;
	Matrix[8]=NewX.m_fZ; Matrix[9]=NewY.m_fZ; Matrix[10]=NewZ.m_fZ; Matrix[11]=0.0;
	Matrix[12]=0.0; Matrix[13]=0.0; Matrix[14]=0.0; Matrix[15]=1.0;
	glMultMatrixf(Matrix);

	Matrix[0]=1.0; Matrix[1]=0.0; Matrix[2]=0.0; Matrix[3]=0.0;
	Matrix[4]=0.0; Matrix[5]=1.0; Matrix[6]=0.0; Matrix[7]=0.0;
	Matrix[8]=0.0; Matrix[9]=0.0; Matrix[10]=1.0; Matrix[11]=0.0;
	Matrix[12]=-1.0 * fXEye; Matrix[13]=-1.0 * fYEye; Matrix[14]=-1.0 * fZEye; Matrix[15]=1.0;
	glMultMatrixf(Matrix);*/
		
	//Because the openGL is in space mode!
	//So we should use shift->rotate->scale order to thansform
	//the objects
	//Shift along X,Y,Z
	glTranslatef(m_fShiftWithX, m_fShiftWithY, m_fShiftWithZ);

/*	glDisable(GL_LIGHTING);
	glBegin(GL_LINES);

⌨️ 快捷键说明

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