Text3DView.cpp
上传用户:tengyuc
上传日期:2007-08-14
资源大小:722k
文件大小:10k
- // Text3DView.cpp : implementation of the CText3DView class
- //
- #include "stdafx.h"
- #include "Text3D.h"
- #include "Text3DDoc.h"
- #include "Text3DView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define GL_3D_FONT_LIST 1000
- #define glRGB(x, y, z) glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z)
- /////////////////////////////////////////////////////////////////////////////
- // CText3DView
- IMPLEMENT_DYNCREATE(CText3DView, CView)
- BEGIN_MESSAGE_MAP(CText3DView, CView)
- //{{AFX_MSG_MAP(CText3DView)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- 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()
- /////////////////////////////////////////////////////////////////////////////
- // CText3DView construction/destruction
- CText3DView::CText3DView()
- {
- // TODO: add construction code here
- }
- CText3DView::~CText3DView()
- {
- }
- BOOL CText3DView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- ////////////////////////////////////////////////////////////////
- //设置窗口类型
- cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
- ////////////////////////////////////////////////////////////////
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CText3DView drawing
- void CText3DView::OnDraw(CDC* pDC)
- {
- CText3DDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- //////////////////////////////////////////////////////////////////
- RenderScene(); //渲染场景
- //////////////////////////////////////////////////////////////////
- }
- /////////////////////////////////////////////////////////////////////////////
- // CText3DView printing
- BOOL CText3DView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CText3DView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CText3DView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CText3DView diagnostics
- #ifdef _DEBUG
- void CText3DView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CText3DView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CText3DDoc* CText3DView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CText3DDoc)));
- return (CText3DDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CText3DView message handlers
- int CText3DView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- // TODO: Add your specialized creation code here
- //////////////////////////////////////////////////////////////////
- //初始化OpenGL和设置定时器
- m_pDC = new CClientDC(this);
- SetTimer(1, 20, NULL);
- InitializeOpenGL(m_pDC);
- //////////////////////////////////////////////////////////////////
-
- return 0;
- }
- void CText3DView::OnDestroy()
- {
- CView::OnDestroy();
-
- // TODO: Add your message handler code here
- /////////////////////////////////////////////////////////////////
- //删除调色板和渲染上下文、定时器
- ::wglMakeCurrent(0,0);
- ::wglDeleteContext( m_hRC);
- if (m_hPalette)
- DeleteObject(m_hPalette);
- if ( m_pDC )
- {
- delete m_pDC;
- }
- KillTimer(1);
- /////////////////////////////////////////////////////////////////
-
- }
- void CText3DView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
-
- // TODO: Add your message handler code here
- /////////////////////////////////////////////////////////////////
- //添加窗口缩放时的图形变换函数
- if(cy==0)
- cy=1;
- glViewport(0,0,cx,cy);
- /////////////////////////////////////////////////////////////////
- GLfloat nRange = 125.0f;
- // 恢复坐标系
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- // 设置正交投影
- if (cx <= cy)
- glOrtho (-nRange, nRange, -nRange*cy/cx, nRange*cy/cx, -nRange*2.0f, nRange*2.0f);
- else
- glOrtho (-nRange*cx/cy, nRange*cx/cy, -nRange, nRange, -nRange*2.0f, nRange*2.0f);
- glTranslatef(-110.0f, 0.0f, -5.0f) ;
- glScalef(60.0f, 60.0f, 60.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void CText3DView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- /////////////////////////////////////////////////////////////////
- //添加定时器响应函数和场景更新函数
- Invalidate(FALSE);
- /////////////////////////////////////////////////////////////////
-
- CView::OnTimer(nIDEvent);
- }
- /////////////////////////////////////////////////////////////////////
- // 设置逻辑调色板
- //////////////////////////////////////////////////////////////////////
- void CText3DView::SetLogicalPalette(void)
- {
- 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);
- }
- //////////////////////////////////////////////////////////
- // 初始化openGL场景
- //////////////////////////////////////////////////////////
- BOOL CText3DView::InitializeOpenGL(CDC* pDC)
- {
- m_pDC = pDC;
- SetupPixelFormat();
- //生成绘制描述表
- m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
- //置当前绘制描述表
- ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
- // 光源值和位置坐标
- GLfloat whiteLight[] = { 0.4f, 0.4f, 0.4f, 1.0f };
- GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- GLfloat specular[] = { 0.9f, 0.9f, 0.9f, 1.0f};
- GLfloat lightPos[] = { -100.0f, 200.0f, 50.0f, 1.0f };
- // 设置字体特性
- HFONT hFont;
- GLYPHMETRICSFLOAT agmf[128];
- LOGFONT logfont;
- logfont.lfHeight = -10;
- logfont.lfWidth = 0;
- logfont.lfEscapement = 0;
- logfont.lfOrientation = 0;
- logfont.lfWeight = FW_BOLD;
- logfont.lfItalic = FALSE;
- logfont.lfUnderline = FALSE;
- logfont.lfStrikeOut = FALSE;
- logfont.lfCharSet = ANSI_CHARSET;
- logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
- logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
- logfont.lfQuality = DEFAULT_QUALITY;
- logfont.lfPitchAndFamily = DEFAULT_PITCH;
- strcpy(logfont.lfFaceName,"Arial");
- // 创建字体和显示列表
- hFont = CreateFontIndirect(&logfont);
- SelectObject (m_pDC->GetSafeHdc(), hFont);
- // 生成显示列表
- wglUseFontOutlines(m_pDC->GetSafeHdc(), 0, 128, GL_3D_FONT_LIST, 0.0f, 1.0f,
- WGL_FONT_POLYGONS, agmf);
- DeleteObject(hFont);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_COLOR_MATERIAL);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
- glEnable(GL_LIGHTING);
- glLightfv(GL_LIGHT0,GL_AMBIENT,whiteLight);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
- glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
- glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
- glEnable(GL_LIGHT0);
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
- glMaterialfv(GL_FRONT, GL_SPECULAR,specular);
- glMateriali(GL_FRONT,GL_SHININESS,128);
- // 颜色
- glRGB(255, 0, 0);
- // 黑色背景
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
- return TRUE;
- }
- //////////////////////////////////////////////////////////
- // 设置像素格式
- //////////////////////////////////////////////////////////
- BOOL CText3DView::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 CText3DView::RenderScene()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- // 显示字符串
- glListBase(GL_3D_FONT_LIST);
- glPushMatrix();
- glRotatef(1.0f, 1.0f, 0.0f, 0.0f);
- glRotatef(350.0f, 0.0f, 1.0f, 0.0f);
- // 显示字符串中的每个字符
- glCallLists (100, GL_UNSIGNED_BYTE, "OpenGLI");
-
- glPopMatrix();
- ::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
- return TRUE;
- }