MyTerrainWnd.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:5k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // MyTerrainWnd.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "MyColorPaletteWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrainWnd
- IMPLEMENT_DYNAMIC(CMyTerrainWnd, CMyglWnd)
- CMyTerrainWnd::CMyTerrainWnd()
- {
- // Set our camera to Orthographic view and
- // initialize our camera variables
- m_Camera.m_bPerspective = TRUE;
- m_Camera.m_fOrigin[X] = 0.0f;
- m_Camera.m_fOrigin[Y] = 0.0f;
- m_Camera.m_fOrigin[Z] = 50.0f;
- m_Camera.m_fRotation[X] = -75.0f;
- m_Camera.m_fRotation[Y] = 0.0f;
- m_Camera.m_fRotation[Z] = -15.0f;
- m_Camera.m_fNear = 0.0f;
- m_Camera.m_fFar = 300.0f;
- m_pTerrainObject = new C3dObjectTerrain;
- }
- CMyTerrainWnd::~CMyTerrainWnd()
- {
- if(m_pTerrainObject)
- delete m_pTerrainObject;
- }
- BEGIN_MESSAGE_MAP(CMyTerrainWnd, CWnd)
- //{{AFX_MSG_MAP(CMyTerrainWnd)
- ON_WM_PAINT()
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_WM_PALETTECHANGED()
- ON_WM_QUERYNEWPALETTE()
- ON_WM_ERASEBKGND()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BOOL CMyTerrainWnd::Create(DWORD dwExStyle, LPCTSTR lpszClassName,
- LPCTSTR lpszWindowName, DWORD dwStyle,
- const RECT& rect, CWnd* pParentWnd, UINT nID,
- CCreateContext* pContext)
- {
- // if(lpszClassName == NULL)
- // // Register a class with its own device context and the 'cross' cursor
- // lpszClassName = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
- // ::LoadCursor(NULL, IDC_CROSS));
- return CMyglWnd::Create(dwExStyle, lpszClassName, lpszWindowName,
- dwStyle, rect, pParentWnd, nID, pContext);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrainWnd messages forwarded to CMyglWnd base class
- void CMyTerrainWnd::OnDestroy()
- {
- // Forward the message to the base class
- CMyglWnd::OnDestroy();
- }
- void CMyTerrainWnd::OnSize(UINT nType, int cx, int cy)
- {
- // Forward the message to the base class
- CMyglWnd::OnSize(nType, cx, cy);
- }
- void CMyTerrainWnd::OnPaletteChanged(CWnd* pFocusWnd)
- {
- // Forward the message to the base class
- CMyglWnd::OnPaletteChanged(pFocusWnd);
- }
- BOOL CMyTerrainWnd::OnQueryNewPalette()
- {
- // Forward the message to the base class
- return CMyglWnd::OnQueryNewPalette();
- }
- BOOL CMyTerrainWnd::OnEraseBkgnd(CDC* pDC)
- {
- // Forward the message to the base class
- return CMyglWnd::OnEraseBkgnd(pDC);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyTerrainWnd message handlers
- int CMyTerrainWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CMyglWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- return 0;
- }
- void CMyTerrainWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // Make this view the current OpenGL rendering context...
- if(!the3dEngine.EnableRC(dc, m_hRC, TRUE))
- return;
- // Call OpenGL drawing code
- RenderPreviewWnd();
- // Exchange the front and back buffers
- SwapBuffers(dc);
- // Releases the device context that is used by the rendering context
- // to allow other rendering contexts to co-exist.
- the3dEngine.EnableRC(NULL, NULL, FALSE);
- // Do not call CWnd::OnPaint() for painting messages
- }
- void CMyTerrainWnd::RenderPreviewWnd()
- {
- GLfloat fOrigin [4] = {-13.0f, 34.0f, 10.0f, 1.0f};
- GLfloat fColorAmbient [4] = {0.2f, 0.2f, 0.2f, 1.0f};
- GLfloat fColorDiffuse [4] = {0.7f, 0.7f, 0.7f, 1.0f};
- GLfloat fColorSpecular [4] = {0.9f, 0.9f, 0.9f, 1.0f};
- // Enable color material
- glEnable(GL_COLOR_MATERIAL);
- // Enable lighting
- glEnable(GL_LIGHTING);
- // If we have a palette, enable dithering of color before
- // writing to the color buffer
- if(the3dEngine.GetPalette())
- glEnable(GL_DITHER);
- // We want specular reflections to take the direction of our
- // cameras' position
- glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
- glEnable(GL_DEPTH_TEST);
- glShadeModel(GL_SMOOTH);
- // Set the background color to black (default)
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- // Clear the window with current clearing color
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Save the current Modelview matrix
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- // Position our camera
- m_Camera.PositionCamera();
- // Set the Lights Parameters and Enable...
- glLightfv(GL_LIGHT0, GL_AMBIENT, fColorAmbient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, fColorDiffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, fColorSpecular);
- glLightfv(GL_LIGHT0, GL_POSITION, fOrigin);
- glEnable(GL_LIGHT0);
- m_pTerrainObject->SetAttributes(NULL, NULL);
- m_pTerrainObject->Build(NULL, NULL);
- // Restore the Modelview matrix
- glPopMatrix();
- // Flush drawing commands
- glFlush();
- }