📄 wanguanview.cpp
字号:
// WanGuanView.cpp : implementation of the CWanGuanView class
//
#include "stdafx.h"
#include "YBC.h"
#include "GuanDuan.h"
#include "WanGuan.h"
#include "GL/gl.h"
#include "GL/glut.h"
#include "WanGuanDoc.h"
#include "WanGuanView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
YBCArray gYBCs;
GuanDuanArray gGuanDuans;
/////////////////////////////////////////////////////////////////////////////
// CWanGuanView
IMPLEMENT_DYNCREATE(CWanGuanView, CView)
BEGIN_MESSAGE_MAP(CWanGuanView, CView)
//{{AFX_MSG_MAP(CWanGuanView)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_SIZE()
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()
/////////////////////////////////////////////////////////////////////////////
// CWanGuanView construction/destruction
CWanGuanView::CWanGuanView()
{
// TODO: add construction code here
}
CWanGuanView::~CWanGuanView()
{
}
BOOL CWanGuanView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CWanGuanView drawing
void CWanGuanView::OnDraw(CDC* pDC)
{
CWanGuanDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CWanGuanView printing
BOOL CWanGuanView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CWanGuanView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CWanGuanView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CWanGuanView diagnostics
#ifdef _DEBUG
void CWanGuanView::AssertValid() const
{
CView::AssertValid();
}
void CWanGuanView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CWanGuanDoc* CWanGuanView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWanGuanDoc)));
return (CWanGuanDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CWanGuanView message handlers
BOOL CWanGuanView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // pfd结构的大小
1, // 版本号
PFD_DRAW_TO_WINDOW | // 支持在窗口中绘图
PFD_SUPPORT_OPENGL | // 支持 OpenGL
PFD_DOUBLEBUFFER, // 双缓存模式
PFD_TYPE_RGBA, // RGBA 颜色模式
24, // 24 位颜色深度
0, 0, 0, 0, 0, 0, // 忽略颜色位
0, // 没有非透明度缓存
0, // 忽略移位位
0, // 无累加缓存
0, 0, 0, 0, // 忽略累加位
32, // 32 位深度缓存
0, // 无模板缓存
0, // 无辅助缓存
PFD_MAIN_PLANE, // 主层
0, // 保留
0, 0, 0 // 忽略层,可见性和损毁掩模
};
int pixelformat;
pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
if(pfd.dwFlags & PFD_NEED_PALETTE)
SetLogicalPalette(); //设置逻辑调色板
return TRUE;
}
BOOL CWanGuanView::InitializeOpenGL(CDC *pDC)
{
CYBC TmpYBC,TmpYBC1;
TmpYBC.fY=20;TmpYBC.fB=0;TmpYBC.fC=90;TmpYBC.fR=40;
gYBCs.Add(TmpYBC);
TmpYBC.fY=40;TmpYBC.fB=90;TmpYBC.fC=90;TmpYBC.fR=40;
gYBCs.Add(TmpYBC);
TmpYBC.fY=30;TmpYBC.fB=90;TmpYBC.fC=90;TmpYBC.fR=40;
gYBCs.Add(TmpYBC);
TmpYBC.fY=20;TmpYBC.fB=0;TmpYBC.fC=90;TmpYBC.fR=40;
gYBCs.Add(TmpYBC);
m_pDC = pDC;
SetupPixelFormat();
//生成绘制描述表
m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
//置当前绘制描述表
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
// /* set the projection transformation 设置投影变换关系*/
// glMatrixMode(GL_PROJECTION);
// glFrustum(-2.0F, 2.0F, -2.0F, 2.0F, 1.0F, 5.0F);
//
// /* set the viewing transformation */
// glMatrixMode(GL_MODELVIEW);
// glTranslatef(0.0F, 0.0F, -2.0F);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// glOrtho(-150,150,-150,150,-150,150);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0f, 1000.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//设置材质
GLfloat ambient[4] = { 0.05F, 0.05F, 0.05F, 1.0F };
//GLfloat diffuse[4] = { 0.6F, 0.0F, 0.2F, 1.0F };
GLfloat diffuse[4] = { 1.0F, 1.0F, 0.0F, 1.0F };
GLfloat specular[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, diffuse);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0F);
m_xRotation = 0.0f;
m_yRotation = 0.0f;
m_xTranslation = -0.0f;
m_yTranslation = 0.0f;
m_zTranslation = -0.0f;
m_xScaling = 0.8f;
m_yScaling = m_xScaling;
m_zScaling = m_xScaling;
m_LeftButtonDown=false;
m_RightButtonDown=false;
return TRUE;
}
void CWanGuanView::SetLogicalPalette()
{
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} logicalPalette = { 0x300, 256 };
BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE blues[] = {0, 85, 170, 255};
for (int colorNum=0; colorNum<256; ++colorNum)
{
logicalPalette.aEntries[colorNum].peRed =
reds[colorNum & 0x07];
logicalPalette.aEntries[colorNum].peGreen =
greens[(colorNum >> 0x03) & 0x07];
logicalPalette.aEntries[colorNum].peBlue =
blues[(colorNum >> 0x06) & 0x03];
logicalPalette.aEntries[colorNum].peFlags = 0;
}
m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
}
int CWanGuanView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//初始化OpenGL和设置定时器
m_pDC = new CClientDC(this);
// SetTimer(1, 500, NULL);
InitializeOpenGL(m_pDC);
return 0;
}
void CWanGuanView::drawCylinder(float fGuanR,YBCArray &YBCs,int iCurrentLine)
{
int iybcsnum,i;
float fGuanL;
GLUquadricObj *obj = gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_FILL);
gluQuadricNormals(obj,GLU_SMOOTH);
gluQuadricOrientation(obj,GLU_OUTSIDE);
float fb,fy,fc,ftr;
iybcsnum=YBCs.GetSize();
glPushMatrix();
for ( i=iybcsnum;i>iCurrentLine;i--)
{
fb=YBCs[i-1].fB;
fy=YBCs[i-1].fY;
fc=YBCs[i-1].fC;
//fc
glRotatef(-fc,0.0f,0.0f,1.0f);// 绕Z 轴旋转fc
if(i!=iCurrentLine+1){
//fb
glTranslatef(0.0,YBCs[i-1].fR,0.0f); //沿Y走fTR
glRotatef(-fb,1.0f,0.0f,0.0f);// 绕b 轴旋转fb
glTranslatef(0.0,-YBCs[i-1].fR,0.0f); //沿Y走-fTR
//fy
glTranslatef(fy,0,0.0f); //沿Y走fY
}
}
fGuanL=YBCs[iCurrentLine].fY;
ftr=YBCs[iCurrentLine].fR;
glPushMatrix();
glTranslatef(-1.0,ftr,0.0f); //沿Y走fTR
glRotatef(90.0f,0.0f,1.0f,0.0f);// 绕Y 轴旋转90
gluCylinder(obj,fGuanR,fGuanR,fGuanL+2,90,1);
glPopMatrix();
glPopMatrix();
gluDeleteQuadric(obj);
}
void CWanGuanView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// clear background
//调用OpenGL绘图函数进行图形绘制
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存
glPushMatrix();
glScalef(m_xScaling,m_yScaling,m_zScaling);
glTranslatef(m_xTranslation,m_yTranslation,m_zTranslation);
glRotatef(m_xRotation,1.0f,0.0f,0.0f);
glRotatef(m_yRotation,0.0f,1.0f,0.0f);
glDisable(GL_LIGHTING);
DrawAxis();
glEnable(GL_LIGHTING);
switch(iViewMode){
case 1:
CreatePipeView();
break;
case 2:
FormingProcessView();
break;
default:
{}
}
glPopMatrix();
glFlush();
SwapBuffers(m_pDC->GetSafeHdc()); //dc.m_ps.hdc
// Do not call CView::OnPaint() for painting messages
}
void CWanGuanView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_LeftButtonDown = TRUE;
m_LeftDownPos = point;
CView::OnLButtonDown(nFlags, point);
}
void CWanGuanView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_LeftButtonDown = FALSE;
CView::OnLButtonUp(nFlags, point);
}
void CWanGuanView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( m_bRotateOperation && m_LeftButtonDown)
{
m_xRotation += (float)(point.y - m_LeftDownPos.y) / 3.0f;
m_yRotation += (float)(point.x - m_LeftDownPos.x) / 3.0f;
m_LeftDownPos = point;
InvalidateRect(NULL,FALSE);
}
if( m_bMoveOperation && m_LeftButtonDown)
{
// m_yTranslation += (float)(-point.y + m_RightDownPos.y)/10.0f;
// m_xTranslation += (float)(point.x - m_RightDownPos.x)/10.0f;
// m_RightDownPos = point;
m_yTranslation += (float)(-point.y + m_LeftDownPos.y) / 10.0f;
m_xTranslation += (float)(point.x - m_LeftDownPos.x) / 10.0f;
m_LeftDownPos = point;
InvalidateRect(NULL,FALSE);
}
CView::OnMouseMove(nFlags, point);
}
void CWanGuanView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_RightButtonDown=TRUE;
m_RightDownPos = point;
CView::OnRButtonDown(nFlags, point);
}
void CWanGuanView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_RightButtonDown=FALSE;
CView::OnRButtonUp(nFlags, point);
}
void CWanGuanView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//添加窗口缩放时的图形变换函数
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100,100,-100,100,-1000,1000);
// gluPerspective(60.0, 1.0f, 1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
// TODO: Add your message handler code here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -