CGL.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:6k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // CGL.cpp: implementation of the CGL class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CGL.h"
  6. #include "GameSetting.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CGL::CGL()
  11. {
  12. m_hRC=NULL; // Permanent rendering context
  13. m_hDC=NULL; // Private GDI device context
  14. m_hWnd=NULL; // Window to wich the RC is attached
  15. m_fov=60;
  16. }
  17. CGL::~CGL()
  18. {
  19. }
  20. bool CGL::SetGLMenuState()
  21. {
  22. glClearColor(0,0,0,1);
  23. glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
  24. glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST ); // Really Nice Perspective Calculations
  25. glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  26.     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  27. glDisable(GL_CULL_FACE);
  28. glDisable(GL_FOG);
  29. glDisable(GL_LIGHTING);
  30. glDepthFunc(GL_LEQUAL);
  31. glEnable(GL_DEPTH_TEST);
  32. glEnable(GL_TEXTURE_2D);
  33. glColor3f(1,1,1);
  34. glReadBuffer(GL_FRONT);
  35. return true;
  36. }
  37. bool CGL::SetGLMissionState()
  38. {
  39.     glCullFace(GL_BACK);
  40. glEnable(GL_CULL_FACE);
  41.     /////////fog
  42.     glFogf(GL_FOG_MODE,GL_LINEAR);
  43. GLfloat fogcolor[]={0.77f,0.77f,0.77f,1};
  44.     glFogfv(GL_FOG_COLOR,fogcolor);
  45. glFogf(GL_FOG_DENSITY,0.2f);
  46.     glFogi(GL_FOG_START,1600-20*CGameSetting.m_iFogDensity);
  47.     glFogi(GL_FOG_END,2000-10*CGameSetting.m_iFogDensity);
  48. glEnable(GL_FOG);
  49. GLfloat mat_ambient[]={1,1,1,1.0f};
  50. GLfloat mat_diffuse[]={1,1,1,1.0f};
  51. // GLfloat mat_emission[]={0.0f,0.0f,0.0f,1.0f};
  52. GLfloat mat_shininess[]={0.0f};
  53. GLfloat mat_specular[]={0.0f,0.0f,0.0f,1.0f};
  54. /*********************************************/
  55. glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
  56. glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
  57. // glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,mat_emission);
  58. glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
  59. glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
  60. GLfloat light0_diffuse[]={0.75f,0.6f,0.5f,1.0f};
  61. GLfloat light0_ambient[]={0.5f,0.5f,0.5f,1.0f};
  62. GLfloat light0_emission[]={0.0f,0.0f,0.0f,0.0f};
  63. // GLfloat light0_position[]={1500.0f,150.0f,1500.0f,1.0f};
  64. glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
  65. glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
  66. glLightfv(GL_LIGHT0,GL_EMISSION,light0_emission);
  67.   glEnable(GL_LIGHT0);
  68. // glEnable(GL_LIGHTING);
  69. // glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,1);
  70. // glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.1f);
  71. // glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,2);
  72. glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  73. glAlphaFunc(GL_GEQUAL,0.1f);
  74.     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  75. glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
  76. glEnable(GL_DEPTH_TEST);
  77. // glEnable(GL_TEXTURE_2D);
  78. glReadBuffer(GL_BACK);
  79. return TRUE;
  80. }
  81. void CGL::Resize(int iWidth, int iHeight)
  82. {
  83. // Handle window resizing
  84. float Aspect;
  85. if (iHeight == 0)iHeight=1;
  86. Aspect = (float) iWidth / (float) iHeight;
  87. glViewport(0, 0, iWidth, iHeight);
  88. glMatrixMode(GL_PROJECTION);
  89. glLoadIdentity();
  90. gluPerspective(60, Aspect, 0.8f, 100000);
  91. glMatrixMode(GL_MODELVIEW);
  92. glLoadIdentity();
  93. }
  94. bool CGL::InitGL( HWND hWnd,int iColorBits)
  95. {
  96. // Pixel format storage
  97. unsigned int PixelFormat;
  98. static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
  99. {
  100. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  101. 1, // Version Number
  102. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  103. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  104. PFD_DOUBLEBUFFER, // Must Support Double Buffering
  105. PFD_TYPE_RGBA, // Request An RGBA Format
  106. iColorBits, // Select Our Color Depth
  107. 0, 0, 0, 0, 0, 0, // Color Bits Ignored
  108. 0, // No Alpha Buffer
  109. 0, // Shift Bit Ignored
  110. 0, // No Accumulation Buffer
  111. 0, 0, 0, 0, // Accumulation Bits Ignored
  112. 32, // 16Bit Z-Buffer (Depth Buffer)  
  113. 0, // No Stencil Buffer
  114. 0, // No Auxiliary Buffer
  115. PFD_MAIN_PLANE, // Main Drawing Layer
  116. 0, // Reserved
  117. 0, 0, 0 // Layer Masks Ignored
  118. };
  119. m_hWnd=hWnd;
  120. if (!(m_hDC=GetDC(hWnd))) // Did We Get A Device Context?
  121. { // Reset The Display
  122. MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  123. return FALSE; // Return FALSE
  124. }
  125. if (!(PixelFormat=ChoosePixelFormat(m_hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
  126. {
  127. // Reset The Display
  128. MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  129. return FALSE; // Return FALSE
  130. }
  131. if(!SetPixelFormat(m_hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
  132. {
  133. MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  134. return FALSE; // Return FALSE
  135. }
  136. if (!(m_hRC=wglCreateContext(m_hDC))) // Are We Able To Get A Rendering Context?
  137. {
  138. MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  139. return FALSE; // Return FALSE
  140. }
  141. if(!wglMakeCurrent(m_hDC,m_hRC)) // Try To Activate The Rendering Context
  142. {
  143. MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  144. return FALSE; // Return FALSE
  145. }
  146. // SetGLState();
  147. return TRUE;
  148. }
  149. void CGL::DestroyGL()
  150. {
  151. if (m_hRC) // Do We Have A Rendering Context?
  152. {
  153. if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
  154. {
  155. MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  156. }
  157. if (!wglDeleteContext(m_hRC)) // Are We Able To Delete The RC?
  158. {
  159. MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  160. }
  161. m_hRC=NULL; // Set RC To NULL
  162. }
  163. if (m_hDC && !ReleaseDC(m_hWnd,m_hDC)) // Are We Able To Release The DC
  164. {
  165. MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  166. m_hDC=NULL; // Set DC To NULL
  167. }
  168. }
  169. void CGL::SwapBuffers()
  170. {
  171. // Swap the buffers
  172. ::SwapBuffers(m_hDC);
  173. }