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

游戏引擎

开发平台:

Visual C++

  1. // CGL.cpp: implementation of the CGL class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CGL.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CGL::CGL()
  10. {
  11. m_ScrWidth=800;
  12. m_ScrHeight=600;
  13. m_FOV=60;
  14. }
  15. CGL::~CGL()
  16. {
  17. }
  18. bool CGL::InitGLState()
  19. {
  20. // Set default values the state machine
  21. // Enable Texture Mapping
  22. glClearColor(0.0f,0.0f,0.0f,1); // This Will Clear The Background Color To Black
  23. // glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
  24. glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
  25. glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST ); // Really Nice Perspective Calculations
  26. /*    glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
  27.     glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);
  28.    
  29. // glEnable(GL_LINE_SMOOTH);
  30.     glEnable(GL_POLYGON_SMOOTH);
  31. */
  32.     glCullFace(GL_BACK);
  33. glEnable(GL_CULL_FACE);
  34.     /////////fog
  35.     glFogf(GL_FOG_MODE,GL_LINEAR);
  36. GLfloat fogcolor[]={0.7f,0.7f,0.7f,1};
  37.     glFogfv(GL_FOG_COLOR,fogcolor);
  38. glFogf(GL_FOG_DENSITY,1.78f);
  39.     glFogf(GL_FOG_START,1300);
  40.     glFogf(GL_FOG_END,2000);
  41. // glEnable(GL_FOG);
  42. GLfloat mat_ambient[]={1,1,1,1.0f};
  43. GLfloat mat_diffuse[]={1,1,1,1.0f};
  44. // GLfloat mat_emission[]={0.0f,0.0f,0.0f,1.0f};
  45. GLfloat mat_shininess[]={0.0f};
  46. GLfloat mat_specular[]={0.0f,0.0f,0.0f,1.0f};
  47. /*********************************************/
  48. glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
  49. glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
  50. // glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,mat_emission);
  51. glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
  52. glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
  53. GLfloat light0_diffuse[]={0.75f,0.6f,0.5f,1.0f};
  54. GLfloat light0_ambient[]={0.5f,0.5f,0.5f,1.0f};
  55. GLfloat light0_emission[]={0.0f,0.0f,0.0f,0.0f};
  56. // GLfloat light0_position[]={1500.0f,150.0f,1500.0f,1.0f};
  57. glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
  58. glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
  59. glLightfv(GL_LIGHT0,GL_EMISSION,light0_emission);
  60.   glEnable(GL_LIGHT0);
  61. glEnable(GL_LIGHTING);
  62. // glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,1);
  63. // glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.1f);
  64. // glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,2);
  65. glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
  66.     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  67.     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  68. ///////////////////////////////////////////
  69. glAlphaFunc(GL_GEQUAL,0.1f);
  70. glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do
  71. glEnable(GL_DEPTH_TEST);
  72.     glEnable(GL_TEXTURE_2D);
  73. glReadBuffer(GL_FRONT);
  74. return TRUE;
  75. }
  76. void CGL::Resize(int iFOVAngle,int iHeight)
  77. {
  78. // Handle window resizing
  79. double Aspect;
  80. m_FOV=iFOVAngle;
  81. if(m_FOV>90)m_FOV=60;
  82. if (iHeight == 0)
  83. Aspect = (double) m_ScrWidth;
  84. else
  85. Aspect = (double) m_ScrWidth / (double) m_ScrHeight;
  86. glViewport(0, 0, m_ScrWidth, m_ScrHeight);
  87. glMatrixMode(GL_PROJECTION);
  88. glLoadIdentity();
  89. gluPerspective(m_FOV, Aspect, 1.0f, 100000);
  90. glMatrixMode(GL_MODELVIEW);
  91. glLoadIdentity();
  92. // glDrawBuffer(GL_BACK);
  93. }
  94. void CGL::ChangeFOVAngle(int state)//0: RButtonUp 1:RButtonDown
  95. {
  96. static int  FOVAngle=60;
  97. static bool bZooming=false;
  98. static bool bZoomed=false;
  99.     if(state==1 && !bZoomed)
  100. {
  101. bZooming=true;
  102.     FOVAngle-=2;
  103. }
  104.     if(state==0 )
  105. {
  106. if(!bZoomed)FOVAngle=60;
  107. else 
  108. {
  109. if(!bZooming)FOVAngle=60;
  110. }
  111.         bZooming=false;
  112. }
  113. if(FOVAngle<20)bZoomed=true;
  114. else bZoomed=false;
  115. if(FOVAngle>60)FOVAngle=60;
  116. if(FOVAngle<2)FOVAngle=2;
  117. glViewport(0, 0, m_ScrWidth, m_ScrHeight);
  118. glMatrixMode(GL_PROJECTION);
  119. glLoadIdentity();
  120. gluPerspective(FOVAngle, (double) m_ScrWidth / (double) m_ScrHeight,1.0f, 100000);
  121. glMatrixMode(GL_MODELVIEW);
  122. glLoadIdentity();
  123. }
  124. bool CGL::InitGL(unsigned int iWidth, unsigned int iHeight, bool bFullscreen, HWND hWnd)
  125. {
  126.     m_ScrWidth=iWidth;
  127.     m_ScrHeight=iHeight;
  128. // Pixel format storage
  129. unsigned int PixelFormat;
  130. // Pixel format descriptor
  131. PIXELFORMATDESCRIPTOR pfd=
  132. {
  133. sizeof(PIXELFORMATDESCRIPTOR), // Size of this pixel format descriptor
  134. 1, // Version number (?)
  135. PFD_DRAW_TO_WINDOW | // Format must support window
  136. PFD_SUPPORT_OPENGL | // Format must support OpenGL
  137. PFD_DOUBLEBUFFER, // Must support double buffering
  138. PFD_TYPE_RGBA, // Request an RGBA format
  139. 16, // Select 16 bit color depth
  140. 0, 0, 0, 0, 0, 0, // Color bits ignored (?)
  141. 0, // No alpha buffer
  142. 0, // Shift bit ignored (?)
  143. 0, // No accumulation buffer
  144. 0, 0, 0, 0, // Accumulation bits ignored (?)
  145. 16, // 16 bit Z-buffer (depth buffer)  
  146. 0, // No stencil buffer
  147. 0, // No auxiliary buffer (?)
  148. PFD_MAIN_PLANE, // Main drawing layer
  149. 0, // Reserved (?)
  150. 0, 0, 0 // Layer masks ignored (?)
  151. };
  152. // Save passed hWnd
  153. m_hWnd = hWnd;
  154. // Get a device context for the window
  155. m_hDC = GetDC(m_hWnd);
  156. // Find the closest match to the pixel format we set above
  157. PixelFormat = ChoosePixelFormat(m_hDC, &pfd);
  158. // No matching pixel format ?
  159. if (!PixelFormat)
  160. {
  161. MessageBox(0,"Can't find a suitable pixelformat", "Error", MB_OK | MB_ICONERROR);
  162. // Failed
  163. return FALSE;
  164. }
  165. // Can we set the pixel mode?
  166. if (!SetPixelFormat(m_hDC,PixelFormat,&pfd))
  167. {
  168. MessageBox(0,"Can't set the pixelformat", "Error", MB_OK | MB_ICONERROR);
  169. // Failed
  170. return FALSE;
  171. }
  172. // Grab a rendering context
  173. m_hRC = wglCreateContext(m_hDC);
  174. // Did we get one?
  175. if (!m_hRC)
  176. {
  177. MessageBox(0, "Can't create a GL rendering context", "Error", MB_OK | MB_ICONERROR);
  178. // Failed
  179. return FALSE;
  180. }
  181. // Can we make the RC active?
  182. if (!wglMakeCurrent(m_hDC, m_hRC))
  183. {
  184. MessageBox(0, "Can't activate GLRC", "Error", MB_OK | MB_ICONERROR);
  185. // Failed
  186. return FALSE;
  187. }
  188. // Change the resolution
  189. if (bFullscreen)
  190. if (!ChangeResolution(iWidth, iHeight))
  191. {
  192. MessageBox(0, "Can't switch resolution", "Error", MB_OK | MB_ICONERROR);
  193. // Failed
  194. return FALSE;
  195. }
  196. // Init the state machine
  197. if (!InitGLState())
  198. {
  199. MessageBox(0, "Can't init the state machine", "Error", MB_OK | MB_ICONERROR);
  200. // Failed
  201. return FALSE;
  202. }
  203. return TRUE;
  204. }
  205. bool CGL::ChangeResolution(unsigned int iWidth, unsigned int iHeight)
  206. {
  207. // Change the resolution
  208.     m_ScrWidth=iWidth;
  209.     m_ScrHeight=iHeight;
  210. DEVMODE dmScreenSettings;
  211. // Zero ram to store settings
  212. memset(&dmScreenSettings, 0, sizeof(DEVMODE));
  213. dmScreenSettings.dmSize = sizeof(DEVMODE); // Size of the devmode structure
  214. dmScreenSettings.dmPelsWidth = iWidth; // Screen width
  215. dmScreenSettings.dmPelsHeight = iHeight; // Screen height
  216. dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; // Pixel mode
  217. // Switch to full screen
  218. if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  219. return FALSE;
  220. return TRUE;
  221. }
  222. void CGL::DestroyGL()
  223. {
  224. // Switch resolution back and shutdown GL
  225. ChangeDisplaySettings(NULL, 0); // Disable fullscreen mode
  226. wglMakeCurrent(m_hDC, NULL); // Make te DC crrent
  227. wglDeleteContext(m_hRC); // Kill the RC
  228. ReleaseDC(m_hWnd, m_hDC); // Free the DC
  229. }
  230. void CGL::SwapBuffers()
  231. {
  232. // Swap the buffers
  233. ::SwapBuffers(m_hDC);
  234. }