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

📄 crosssimulation1view.cpp

📁 一个完整的交叉口仿真模型 实现生成交叉口 和车辆生成 运行 跟驰等
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CrossSimulation1View.cpp : implementation of the CCrossSimulation1View class
//

#include "stdafx.h"
#include "CrossSimulation1.h"

#include "CrossSimulation1Doc.h"
#include "CrossSimulation1View.h"
#include "CrossSetDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCrossSimulation1View

IMPLEMENT_DYNCREATE(CCrossSimulation1View, CView)

BEGIN_MESSAGE_MAP(CCrossSimulation1View, CView)
	//{{AFX_MSG_MAP(CCrossSimulation1View)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_DESTROY()
	ON_COMMAND(ID_CROSS_NOR, OnCrossNor)
	ON_WM_TIMER()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CCrossSimulation1View construction/destruction

CCrossSimulation1View::CCrossSimulation1View()
{
	// TODO: add construction code here
   sim_start=1;
   b_DrawCross=false;

   CROSS=NULL;
 

  
}

CCrossSimulation1View::~CCrossSimulation1View()
{
}

BOOL CCrossSimulation1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW); //add
		
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN|WS_MAXIMIZE;  //add


	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCrossSimulation1View drawing

void CCrossSimulation1View::OnDraw(CDC* pDC)
{
	CCrossSimulation1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	if(m_hglrc)
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hglrc);
	else
		return;

	if(sim_start==0) simEndTime=10;

	if(sim_start!=1)  //消除屏幕闪烁
	{

		glDrawBuffer(GL_BACK); //	指定在后台缓存中绘制		
		glLoadIdentity(); //	初始化变换矩阵		
		glClearColor(1.0,1.0,1.0,1.0f); //	清除背景颜色
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glFinish(); //	结束整个绘制	
		SwapBuffers(wglGetCurrentDC()); //	交换前后缓存		
		glDrawBuffer(GL_FRONT);  //	绘制前景
	}





	DrawPanorama(pDC);  //绘制当前时间的车辆位置
}

/////////////////////////////////////////////////////////////////////////////
// CCrossSimulation1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCrossSimulation1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCrossSimulation1View message handlers

int CCrossSimulation1View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
		int  n;

    m_pDC = new CClientDC(this);
    ASSERT(m_pDC != NULL);

	//	初始化像素格式
    static PIXELFORMATDESCRIPTOR pfd = 
	{
        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
        1,                              // version number
        PFD_DRAW_TO_WINDOW   |          // support window
          PFD_SUPPORT_OPENGL |          // support OpenGL
          PFD_DOUBLEBUFFER,             // double buffered
        PFD_TYPE_RGBA,		            // RGBA type
        24,                             // 24-bit color depth
        0, 0, 0, 0, 0, 0,               // color bits ignored
        0,                              // no alpha buffer
        0,                              // shift bit ignored
        0,                              // no accumulation buffer
        0, 0, 0, 0,                     // accum bits ignored
        32,                             // 32-bit z-buffer
        0,                              // no stencil buffer
        0,                              // no auxiliary buffer
        PFD_MAIN_PLANE,                 // main layer
        0,                              // reserved
        0, 0, 0                         // layer masks ignored
    };

	//	选择像素格式
    int pixelformat;
    if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
    {
        MessageBox("选择像素格式失败!");
        return -1;
    }

	//	设置像素格式
    if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
    {
        MessageBox("设置像素格式失败!");
        return -1;
    }
	
    n = ::GetPixelFormat(m_pDC->GetSafeHdc());
    ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);

	//	创建绘制情景对象
    m_hglrc = wglCreateContext(m_pDC->GetSafeHdc());
	//	选择绘制情景对象
    wglMakeCurrent(m_pDC->GetSafeHdc(), m_hglrc);

   SetTimer(1,1, NULL); //设置时间间隔

	
	return 0;
}

void CCrossSimulation1View::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
		if(m_hglrc)
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hglrc);
	else
		return;

	glViewport(0,0,cx,cy);
	//	设置变换模式为投影变换
	glMatrixMode(GL_PROJECTION);
	//	初始化投影变换矩阵
	glLoadIdentity();

	float sizeRatio;  //窗口大小比例(与隐含值的比。隐含值为1)
	sizeRatio=2.0;

	//	根据窗口大小设置调整正射投影矩阵  
	if(cx<=cy)
		glOrtho(-20.0*sizeRatio,20.0*sizeRatio,-20.0*(GLfloat)cy/(GLfloat)cx*sizeRatio,
			20.0*(GLfloat)cy/(GLfloat)cx*sizeRatio,-50.0*sizeRatio,50.0*sizeRatio);
	else
		glOrtho(-20.0*(GLfloat)cx/(GLfloat)cy*sizeRatio,
			20.0*(GLfloat)cx/(GLfloat)cy*sizeRatio,-20.0*sizeRatio,20.0*sizeRatio,-50.0*sizeRatio,50.0*sizeRatio);
	
	glMatrixMode(GL_MODELVIEW); //	设置变换模式为模型变换
	
	glLoadIdentity(); //初始化模型变换矩阵	

}


void CCrossSimulation1View::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	::wglMakeCurrent(NULL,  NULL); //	将当前绘图情景对象赋空

	//	删除当前绘图情景对象并释放所占内存
    if (m_hglrc)
        ::wglDeleteContext(m_hglrc);

    if (m_pDC)
        delete m_pDC;	
	
}

void CCrossSimulation1View::DrawPanorama(CDC *pDC)
{
	glDrawBuffer(GL_BACK); //	指定在后台缓存中绘制		
	glLoadIdentity(); //	初始化变换矩阵		
	glClearColor(1.0,1.0,1.0,1.0f); //	清除背景颜色
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


	///////////////////////////// 画十字交叉口//////////////////////

	///////////画东西方向的道路/////////////////////////////



     if(b_DrawCross)
	 {
		 
		 float      m_SouthLaneWidth=CROSS->m_SouthLaneWidth;
		 float      m_EastLaneWidth=CROSS->m_EastLaneWidth;
		 float      m_NorthLaneWidth=CROSS->m_NorthLaneWidth;
		 float      m_WestLaneWidth=CROSS->m_WestLaneWidth;
		 int        m_SouthLaneNumber=CROSS->m_SouthLaneNumber;
		 int        m_NorthLaneNumber=CROSS->m_NorthLaneNumber;
		 int        m_WestLaneNumber=CROSS->m_WestLaneNumber;
		 int        m_EastLaneNumber=CROSS->m_EastLaneNumber;



		 float a=m_NorthLaneNumber/2*m_NorthLaneWidth;  //左右侧离中心距离;
		 float b=m_EastLaneNumber/2*m_EastLaneWidth;    //南北侧离中心距离;
   
         turnwide=6.0;         //供转向所需宽度;

		 for(int i=0;i<=m_WestLaneNumber;i++)        //东西向
		 {

			 if(i==0||i==m_WestLaneNumber/2||i==m_WestLaneNumber)
			 {
				 glBegin(GL_LINES);   
				 glColor3d(0, 0, 1);           //设置颜色RGB;
				 glVertex2f(-60, b-m_EastLaneWidth*i);        //设置起点坐标;
				 glVertex2f(-a-turnwide, b-m_EastLaneWidth*i);          //设置终点坐标;
			     glEnd();


				 glBegin(GL_LINES);   
				 glColor3d(0, 0, 1);           //设置颜色RGB;
				 glVertex2f(a+turnwide, b-m_EastLaneWidth*i);        //设置起点坐标;
				 glVertex2f(60, b-m_EastLaneWidth*i);          //设置终点坐标;
				 glEnd();

			 }
			 else
			 {
				 glEnable(GL_LINE_STIPPLE);  //启动定义线型函数
				 glLineStipple(2, 0xAAAA);  //启动定义线型:虚线
				 
				 glBegin(GL_LINES);   
				 glColor3d(1, 0, 0);           //设置颜色RGB;
				 glVertex2f(-60, b-m_EastLaneWidth*i);        //设置起点坐标;
				 glVertex2f(-a-turnwide, b-m_EastLaneWidth*i);          //设置终点坐标;
				 glEnd();

				 glBegin(GL_LINES);   
				 glColor3d(1, 0, 0);                          //设置颜色RGB;
				 glVertex2f(a+turnwide, b-m_EastLaneWidth*i);        //设置起点坐标;
				 glVertex2f(60, b-m_EastLaneWidth*i);          //设置终点坐标;
				 glEnd();

				 glDisable(GL_LINE_STIPPLE);    //取消线型定义功能
			 }
		 }
		 
		for(int j=0;j<=m_NorthLaneNumber;j++)        //南北向
		 {
			 
			 if(j==0||j==m_NorthLaneNumber/2||j==m_NorthLaneNumber)
			 {
				 
				 glBegin(GL_LINES);   
				 glColor3d(0, 0, 1);           //设置颜色RGB;
				 glVertex2f( a-m_NorthLaneWidth*j,-60);        //设置起点坐标;
				 glVertex2f(a-m_NorthLaneWidth*j,-b-turnwide);          //设置终点坐标;
				 glEnd();
				 
				 
				 glBegin(GL_LINES);   
				 glColor3d(0, 0, 1);           //设置颜色RGB;
				 glVertex2f( a-m_NorthLaneWidth*j,60);        //设置起点坐标;
				 glVertex2f(a-m_NorthLaneWidth*j,b+turnwide);          //设置终点坐标;
				 glEnd();	 
				 
			 }
			 else
			 {
				 glEnable(GL_LINE_STIPPLE);  //启动定义线型函数
				 glLineStipple(2, 0xAAAA);  //启动定义线型:虚线
				 
				 glBegin(GL_LINES);   
				 glColor3d(1, 0, 0);           //设置颜色RGB;
				 glVertex2f( a-m_NorthLaneWidth*j,-60);        //设置起点坐标;
				 glVertex2f(a-m_NorthLaneWidth*j,-b-turnwide);          //设置终点坐标;;
				 glEnd();
				 	 
				 glBegin(GL_LINES);   
				 glColor3d(1, 0, 0);                          //设置颜色RGB;
		         glVertex2f( a-m_NorthLaneWidth*j,60);        //设置起点坐标;
				 glVertex2f(a-m_NorthLaneWidth*j,b+turnwide);          //设置终点坐标
				 glEnd();
				 
				 glDisable(GL_LINE_STIPPLE);    //取消线型定义功能
			 }
		 }


		///////////////画出四条连接线///////////////////
		glBegin(GL_LINES);   
		glColor3d(0, 0, 1);           //设置颜色RGB;
		glVertex2f( -a-turnwide,b);        //设置起点坐标;
		glVertex2f(-a,b+turnwide);          //设置终点坐标;
		glEnd();

		glBegin(GL_LINES);   
		glColor3d(0, 0, 1);           //设置颜色RGB;
		glVertex2f( -a-turnwide,b-m_EastLaneWidth*m_EastLaneNumber);        //设置起点坐标;
		glVertex2f(-a,-b-turnwide);          //设置终点坐标;
		glEnd();

		glBegin(GL_LINES);   
		glColor3d(0, 0, 1);           //设置颜色RGB;
		glVertex2f( a+turnwide,b);        //设置起点坐标;
		glVertex2f(a,b+turnwide);          //设置终点坐标;
		glEnd();

		glBegin(GL_LINES);   
		glColor3d(0, 0, 1);           //设置颜色RGB;
		glVertex2f( a+turnwide,b-m_EastLaneWidth*m_EastLaneNumber);        //设置起点坐标;
		glVertex2f(a,-b-turnwide);          //设置终点坐标;
		glEnd();

		/////////////画四条停车线//////
		glBegin(GL_LINES);   
		glColor3d(0.8, 0.8,0.8);           //设置颜色RGB;
		glVertex2f( -a-turnwide,b-m_EastLaneWidth*m_EastLaneNumber/2);        //设置起点坐标;
		glVertex2f(-a-turnwide,b-m_EastLaneWidth*m_EastLaneNumber);          //设置终点坐标;
		glEnd();

		glBegin(GL_LINES);   
		glColor3d(0.8, 0.8,0.8);           //设置颜色RGB;
		glVertex2f( a+turnwide,b);        //设置起点坐标;
		glVertex2f(a+turnwide,b-m_EastLaneWidth*m_EastLaneNumber/2);          //设置终点坐标;
		glEnd();

		glBegin(GL_LINES);   
		glColor3d(0.8, 0.8,0.8);           //设置颜色RGB;
		glVertex2f( -a,b+turnwide);        //设置起点坐标;
		glVertex2f(-a+m_NorthLaneWidth*m_NorthLaneNumber/2,b+turnwide);          //设置终点坐标;
		glEnd();

		glBegin(GL_LINES);   
		glColor3d(0.8, 0.8,0.8);           //设置颜色RGB;
		glVertex2f( -a+m_NorthLaneWidth*m_NorthLaneNumber/2,-b-turnwide);        //设置起点坐标;
		glVertex2f(a,-b-turnwide);          //设置终点坐标;
		glEnd();		 
				 


	 }

⌨️ 快捷键说明

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