www.pudn.com > planeshoot.rar > RenderView.cpp


// RenderView.cpp 
#include "stdafx.h" 
#include "Tool.h" 
#include   
 
#include "ToolDoc.h" 
#include "RenderView.h" 
#include "glm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define WIDTH 32 // 纹理文件的宽度 
// 地形网格格点数,用来控制地形矩阵的下标 
#define MAP 512 
 
// This is the holding space for the landscape. 
GLfloat y[MAP+1][MAP+1]; 
char object[MAP+1][MAP+1]; 
 
typedef struct color { 
  GLfloat r, g, b; 
} color; 
 
// 地形网格点颜色 
color c[MAP+1][MAP+1]; 
 
static GLubyte texImg[WIDTH][WIDTH][4]; 
static GLuint texName; 
 
unsigned short int comp = 32; // Scale modifier. 
 
unsigned short int temp, texture_mapping = FALSE, 
                   land_fogging = TRUE, flat_shading = TRUE; 
 
// 初始视点位置和视角 
static GLfloat patriot_posx = 0, patriot_posy = 0, patriot_posz = 0; 
static GLfloat lookx=0, looky=0, lookz=20; 
static GLfloat speed = 0; 
 
// The following code for mouse routines was contributed. 
// These are used for the motion function. 
#define FORWARD 1 
#define UP 2 
#define TURNLEFT 3 
#define STRAFELEFT 4 
#define LOOKUP 5 
 
#define PI 3.14159f 
#define H 0.4f 
#define DT 2 
#define VN 20 
#define CN 16 
#define TAILL 4 
#define LIST  6 
#define ADDL 0.3f 
#define ADDH 0.1f 
#define MISSLE 2 
 
// Mouse position and button. 
int oldmx = 0, oldmy = 0, mb; 
int srnd; 
float L = 10.0f,tmp[CN][2],red_green[6][CN],tail[6][CN]; 
float vetxm[LIST][VN+1][CN][7]; 
double ttt; 
 
float vertex[213][3], normals[286][3]; 
int faces[187][2][41]; 
 
int loop1, counter = 0, flag=0; 
 
 
// CRenderView 
IMPLEMENT_DYNCREATE(CRenderView, CView) 
 
BEGIN_MESSAGE_MAP(CRenderView, CView) 
//{{AFX_MSG_MAP(CRenderView) 
ON_WM_DESTROY() 
ON_WM_SIZE() 
ON_WM_PAINT() 
ON_WM_TIMER() 
ON_WM_CREATE() 
//}}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() 
 
 
// CRenderView construction/destruction 
CRenderView::CRenderView() 
{ 
  // OpenGL 
  m_hGLContext = NULL; 
  m_GLPixelIndex = 0; 
} 
 
CRenderView::~CRenderView() 
{ 
	glmDelete(plane); 
	glmDelete(patriot); 
} 
 
void CRenderView::InitGeometry(void) 
{ 
  GLfloat fogColor[4] = {0.75, 0.75, 1.0, 1.0}; 
  patriot = glmReadOBJ("patriot.obj"); 
  plane = glmReadOBJ("plane.obj"); 
  glmUnitize(plane); 
  glmScale(plane, 8.0f); 
  patriot_posx = 3000.0; 
  patriot_posy = 256.0; 
  patriot_posz = 8500.0; 
 
  lookx = 15.0; 
  looky = -8.0;  
  lookz = -18.00; 
 
//  loop=-5.0; 
  Shoot = FALSE; 
  speed = 0; 
 
  srand(224); 
  GenScean(); 
  GenFire(); 
  MakeFireList(); 
  srand((unsigned)time(NULL)); 
  MakeTexture(); 
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
  glGenTextures(1, &texName); 
  glBindTexture(GL_TEXTURE_2D, texName); 
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, WIDTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImg); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
 
  glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
 
  glEnable(GL_DEPTH_TEST); 
  glShadeModel(GL_SMOOTH); 
 
  glFogi(GL_FOG_MODE, GL_LINEAR); 
  glFogfv(GL_FOG_COLOR, fogColor); 
  glFogf(GL_FOG_DENSITY, 0.8f); 
  glFogf(GL_FOG_START, 400.0f); 
  glFogf(GL_FOG_END, 500.0f);   
  glEnable(GL_TEXTURE_2D); 
  glClearColor(0.75f, 0.75f, 1.0f, 1.0f); 
} 
 
BOOL CRenderView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
  return CView::PreCreateWindow(cs); 
} 
 
// CRenderView drawing 
void CRenderView::OnDraw(CDC* pDC) 
{ 
} 
 
BOOL CRenderView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
  return DoPreparePrinting(pInfo); 
} 
 
void CRenderView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
} 
 
void CRenderView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
} 
 
// CRenderView diagnostics 
#ifdef _DEBUG 
void CRenderView::AssertValid() const 
{ 
  CView::AssertValid(); 
} 
 
void CRenderView::Dump(CDumpContext& dc) const 
{ 
  CView::Dump(dc); 
} 
 
CToolDoc* CRenderView::GetDocument() // non-debug version is inline 
{ 
  if (m_pDocument){ 
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CToolDoc))); 
    return (CToolDoc*)m_pDocument; 
  } 
  else return NULL; 
} 
#endif //_DEBUG 
 
// Create OpenGL rendering context  
int CRenderView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
  if (CView::OnCreate(lpCreateStruct) == -1) 
    return -1; 
   
  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_FILL); 
  glPolygonMode(GL_BACK,GL_FILL); 
  glShadeModel(GL_FLAT); 
   
  InitGeometry(); 
  SetTimer(1,50,NULL); 
  return 0; 
} 
 
BOOL CRenderView::SetWindowPixelFormat(HDC hDC) 
{ 
  PIXELFORMATDESCRIPTOR pixelDesc; 
   
  pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR); 
  pixelDesc.nVersion = 1; 
   
  pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 
    PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE; 
   
  pixelDesc.iPixelType = PFD_TYPE_RGBA; 
  pixelDesc.cColorBits = 32; 
  pixelDesc.cRedBits = 8; 
  pixelDesc.cRedShift = 16; 
  pixelDesc.cGreenBits = 8; 
  pixelDesc.cGreenShift = 8; 
  pixelDesc.cBlueBits = 8; 
  pixelDesc.cBlueShift = 0; 
  pixelDesc.cAlphaBits = 0; 
  pixelDesc.cAlphaShift = 0; 
  pixelDesc.cAccumBits = 64; 
  pixelDesc.cAccumRedBits = 16; 
  pixelDesc.cAccumGreenBits = 16; 
  pixelDesc.cAccumBlueBits = 16; 
  pixelDesc.cAccumAlphaBits = 0; 
  pixelDesc.cDepthBits = 32; 
  pixelDesc.cStencilBits = 8; 
  pixelDesc.cAuxBuffers = 0; 
  pixelDesc.iLayerType = PFD_MAIN_PLANE; 
  pixelDesc.bReserved = 0; 
  pixelDesc.dwLayerMask = 0; 
  pixelDesc.dwVisibleMask = 0; 
  pixelDesc.dwDamageMask = 0; 
   
  m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc); 
  if(m_GLPixelIndex == 0) // Choose default 
  { 
    m_GLPixelIndex = 1; 
    if(DescribePixelFormat(hDC,m_GLPixelIndex, 
      sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) 
      return FALSE; 
  } 
   
  if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc)) 
    return FALSE; 
   
  return TRUE; 
} 
 
// Create an OpenGL rendering context 
BOOL CRenderView::CreateViewGLContext(HDC hDC) 
{ 
  m_hGLContext = wglCreateContext(hDC); 
   
  if(m_hGLContext==NULL) 
    return FALSE; 
   
  if(wglMakeCurrent(hDC,m_hGLContext)==FALSE) 
    return FALSE; 
   
  return TRUE; 
} 
 
// Cleanup every OpenGL rendering context 
void CRenderView::OnDestroy()  
{ 
  if(wglGetCurrentContext() != NULL) 
    wglMakeCurrent(NULL,NULL); 
   
  if(m_hGLContext != NULL) 
  { 
    wglDeleteContext(m_hGLContext); 
    m_hGLContext = NULL; 
  } 
  CView::OnDestroy(); 
} 
 
void CRenderView::OnSize(UINT nType, int cx, int cy)  
{ 
  CView::OnSize(nType, cx, cy); 
   
  // Set OpenGL perspective, viewport and mode 
  CSize size(cx,cy); 
  double aspect; 
  aspect = (cy == 0) ? (double)size.cx : (double)size.cx/(double)size.cy; 
 
  glViewport(0, 0, (GLsizei) cx, (GLsizei) cy); 
  glMatrixMode(GL_PROJECTION); 
  glLoadIdentity(); 
  gluPerspective(60.0, (GLfloat) cx/(GLfloat) cy, 1.0f, 5000.0f); 
 
  glMatrixMode(GL_MODELVIEW); 
  glLoadIdentity(); 
} 
 
void CRenderView::OnPaint()  
{ 
  // Device context for painting   
  CPaintDC dc(this);  
   
  // Useful in singledoc templates 
  HWND hWnd = GetSafeHwnd(); 
  HDC hDC = ::GetDC(hWnd); 
  wglMakeCurrent(hDC,m_hGLContext); 
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
  glClearColor(0.75f, 0.75f, 1.0f, 1.0f); 
  glMatrixMode(GL_MODELVIEW); 
  glLoadIdentity(); 
  CheckHeight(); 
  gluLookAt(patriot_posx, patriot_posy, patriot_posz,patriot_posx+lookx, patriot_posy+looky, patriot_posz+lookz, 0.0f,1.0f,0.0f); 
  RenderTerrain(); 
  TrackObj(); 
  glFlush(); 
  SwapBuffers(hDC); 
} 
 
void CRenderView::RenderTerrain() 
{ 
  int x, z, position_x, position_z; 
  GLfloat x1, x2, z1, z2; 
 
  glPushMatrix();   
  position_x = (int)patriot_posx/comp; 
  position_z = (int)patriot_posz/comp; 
  for(x=position_x-16; x0 && x0 && z0) 
					object[x][z] = 't'; 
				else 
					object[x][z] = 'n'; 
			} 
		} 
  
		for(x=0; x<=MAP; x++){ 
			for(z=0; z<=MAP; z++) { 
				gradient = ((GLfloat) (y[x][z]-y[x+1][z])) / 100.0f; 
				if(y[x][z]>64) { 
					if((y[x][z]-y[x][z+1])<8 && (y[x][z]-y[x][z+1])>-8 && (y[x][z]-y[x+1][z])<8 && (y[x][z]-y[x+1][z])>-8) { 
					c[x][z].g = gradient + 0.75f; 
					c[x][z].r = c[x][z].g; 
					c[x][z].b = c[x][z].g; 
				} 
				else { 
					c[x][z].r = gradient + 0.75f; 
					c[x][z].g = c[x][z].r*0.8f; 
					c[x][z].b = c[x][z].r*0.5f; 
				} 
			}  else 
			if(y[x][z]>0){ 
				c[x][z].g = gradient + 0.75f; 
				c[x][z].r = c[x][z].g * 0.6f; 
				c[x][z].b = 0.0f; 
			} 
            else { 
				c[x][z].r = gradient + 1.0f; 
				c[x][z].g = c[x][z].r * 0.75f; 
				c[x][z].b = c[x][z].r * 0.5f; 
			} 
		} 
	} 
} 
 
void CRenderView::MakeTexture(void) { 
  int i, j; 
  FILE *fp; 
  unsigned char *image; 
  int rc; 
 
  fp = fopen("res\\texture.bmp","rb"); 
  if(!fp) return; 
  fseek(fp,54,SEEK_SET); 
  image = (unsigned char *)malloc(WIDTH*WIDTH*3); 
  rc=fread(image,sizeof(unsigned char),WIDTH*WIDTH*3,fp); 
  fclose(fp); 
  for(i=0; i12 || L<10) flag=1-flag; 
	if (flag) L-=0.1f; else L+=0.1f; 
	counter++; 
	patriot_posx=patriot_posx-(float)cos(thita)*14.0f; 
	patriot_posz=patriot_posz+(float)sin(thita)*14.0f; 
	CheckHeight(); 
	glDisable(GL_BLEND); 
    glPopMatrix(); 
} 
 
void CRenderView::GenFire() 
{ 
	float r,dt,dy,t,y; 
	int k, l, ff, i, j, list; 
 
	for (list=0; list