Text3D.c
上传用户:cz2828
上传日期:2007-01-14
资源大小:25k
文件大小:11k
源码类别:

OpenGL

开发平台:

C/C++

  1. // GLPalette.c
  2. // OpenGL SuperBible, Chapter 17
  3. // Program by Richard S. Wright Jr.
  4. // This program demonstrates creation and use of a 332 palette for OpenGL
  5. #include <windows.h>
  6. #include <glgl.h>
  7. #include <glglu.h>
  8. // Palette Handle
  9. HPALETTE hPalette = NULL;
  10. static LPCTSTR lpszAppName = "Text3D";
  11. GLint nFontList;
  12. // Light values and coordinates
  13. GLfloat  whiteLight[] = { 0.4f, 0.4f, 0.4f, 1.0f };
  14. GLfloat  diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  15. GLfloat  specular[] = { 0.9f, 0.9f, 0.9f, 1.0f};
  16. GLfloat lightPos[] = { -100.0f, 200.0f, 50.0f, 1.0f };
  17. // Declaration for Window procedure
  18. LRESULT CALLBACK WndProc( HWND  hWnd,
  19. UINT message,
  20. WPARAM wParam,
  21. LPARAM lParam);
  22. // Set Pixel Format function - forward declaration
  23. void SetDCPixelFormat(HDC hDC);
  24. void ChangeSize(GLsizei w, GLsizei h)
  25. {
  26. GLfloat nRange = 100.0f;
  27. GLfloat fAspect;
  28. // Prevent a divide by zero
  29. if(h == 0)
  30. h = 1;
  31. fAspect = (GLfloat)w/(GLfloat)h;
  32. // Set Viewport to window dimensions
  33. glViewport(0, 0, w, h);
  34. glMatrixMode(GL_PROJECTION);
  35. // Reset coordinate system
  36. glLoadIdentity();
  37. // Setup perspective for viewing
  38. gluPerspective(17.5f,fAspect,1,300);
  39. // Viewing transformation
  40. glMatrixMode(GL_MODELVIEW);
  41. glLoadIdentity();
  42. glTranslatef(-1.8f, 0.0f, -15.0f);
  43. glRotatef(-20.0f, 0.0f, 1.0f,0.0f);
  44. glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  45. }
  46. void RenderScene(void)
  47. {
  48. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  49. // Blue 3D Text
  50. glColor3ub(0, 0, 255);
  51.     glPushMatrix();
  52. glListBase(nFontList);
  53. glCallLists (6, GL_UNSIGNED_BYTE, "OpenGL"); 
  54.     glPopMatrix();
  55. }
  56. void SetupRC(HDC hDC)
  57. {
  58. // Setup the Font characteristics
  59. HFONT hFont;
  60. GLYPHMETRICSFLOAT agmf[128]; // Throw away
  61. LOGFONT logfont;
  62. logfont.lfHeight = -10;
  63. logfont.lfWidth = 0;
  64. logfont.lfEscapement = 0;
  65. logfont.lfOrientation = 0;
  66. logfont.lfWeight = FW_BOLD;
  67. logfont.lfItalic = FALSE;
  68. logfont.lfUnderline = FALSE;
  69. logfont.lfStrikeOut = FALSE;
  70. logfont.lfCharSet = ANSI_CHARSET;
  71. logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  72. logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  73. logfont.lfQuality = DEFAULT_QUALITY;
  74. logfont.lfPitchAndFamily = DEFAULT_PITCH;
  75. strcpy(logfont.lfFaceName,"Arial");
  76. // Create the font and display list
  77. hFont = CreateFontIndirect(&logfont);
  78. SelectObject (hDC, hFont); 
  79. //create display lists for glyphs 0 through 128 with 0.1 extrusion 
  80. // and default deviation. The display list numbering starts at 1000 
  81. // (it could be any number). 
  82. nFontList = glGenLists(128);
  83. wglUseFontOutlines(hDC, 0, 128, nFontList, 0.0f, 0.5f, 
  84. WGL_FONT_POLYGONS, agmf); 
  85. DeleteObject(hFont);
  86. glEnable(GL_DEPTH_TEST); // Hidden surface removal
  87. glEnable(GL_COLOR_MATERIAL);
  88. glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  89. glEnable(GL_LIGHTING);
  90. glLightfv(GL_LIGHT0,GL_AMBIENT,whiteLight);
  91. glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  92. glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  93. glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  94. glEnable(GL_LIGHT0);
  95. glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  96. glMaterialfv(GL_FRONT, GL_SPECULAR,specular);
  97. glMateriali(GL_FRONT,GL_SHININESS,128);
  98. }
  99. // If necessary, creates a 3-3-2 palette for the device context listed.
  100. HPALETTE GetOpenGLPalette(HDC hDC)
  101. {
  102. HPALETTE hRetPal = NULL; // Handle to palette to be created
  103. PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
  104. LOGPALETTE *pPal; // Pointer to memory for logical palette
  105. int nPixelFormat; // Pixel format index
  106. int nColors; // Number of entries in palette
  107. int i; // Counting variable
  108. BYTE RedRange,GreenRange,BlueRange;
  109. // Range for each color entry (7,7,and 3)
  110. // Get the pixel format index and retrieve the pixel format description
  111. nPixelFormat = GetPixelFormat(hDC);
  112. DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  113. // Does this pixel format require a palette?  If not, do not create a
  114. // palette and just return NULL
  115. if(!(pfd.dwFlags & PFD_NEED_PALETTE))
  116. return NULL;
  117. // Number of entries in palette.  8 bits yeilds 256 entries
  118. nColors = 1 << pfd.cColorBits;
  119. // Allocate space for a logical palette structure plus all the palette entries
  120. pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));
  121. // Fill in palette header 
  122. pPal->palVersion = 0x300; // Windows 3.0
  123. pPal->palNumEntries = nColors; // table size
  124. // Build mask of all 1's.  This creates a number represented by having
  125. // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  126. // pfd.cBlueBits.  
  127. RedRange = (1 << pfd.cRedBits) -1;
  128. GreenRange = (1 << pfd.cGreenBits) - 1;
  129. BlueRange = (1 << pfd.cBlueBits) -1;
  130. // Loop through all the palette entries
  131. for(i = 0; i < nColors; i++)
  132. {
  133. // Fill in the 8-bit equivalents for each component
  134. pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  135. pPal->palPalEntry[i].peRed = (unsigned char)(
  136. (double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  137. pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  138. pPal->palPalEntry[i].peGreen = (unsigned char)(
  139. (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  140. pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  141. pPal->palPalEntry[i].peBlue = (unsigned char)(
  142. (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  143. pPal->palPalEntry[i].peFlags = (unsigned char) NULL;
  144. }
  145. // Create the palette
  146. hRetPal = CreatePalette(pPal);
  147. // Go ahead and select and realize the palette for this device context
  148. SelectPalette(hDC,hRetPal,FALSE);
  149. RealizePalette(hDC);
  150. // Free the memory used for the logical palette structure
  151. free(pPal);
  152. // Return the handle to the new palette
  153. return hRetPal;
  154. }
  155. // Select the pixel format for a given device context
  156. void SetDCPixelFormat(HDC hDC)
  157. {
  158. int nPixelFormat;
  159. static PIXELFORMATDESCRIPTOR pfd = {
  160. sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
  161. 1, // Version of this structure
  162. PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
  163. PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
  164. PFD_DOUBLEBUFFER, // Double buffered mode
  165. PFD_TYPE_RGBA, // RGBA Color mode
  166. 32, // Want 32 bit color
  167. 0,0,0,0,0,0, // Not used to select mode
  168. 0,0, // Not used to select mode
  169. 0,0,0,0,0, // Not used to select mode
  170. 16, // Size of depth buffer
  171. 0, // Not used to select mode
  172. 0, // Not used to select mode
  173. 0, // Draw in main plane
  174. 0, // Not used to select mode
  175. 0,0,0 }; // Not used to select mode
  176. // Choose a pixel format that best matches that described in pfd
  177. nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  178. // Set the pixel format for the device context
  179. SetPixelFormat(hDC, nPixelFormat, &pfd);
  180. }
  181. // Entry point of all Windows programs
  182. int APIENTRY WinMain( HINSTANCE  hInstance,
  183. HINSTANCE  hPrevInstance,
  184. LPSTR  lpCmdLine,
  185. int nCmdShow)
  186. {
  187. MSG msg; // Windows message structure
  188. WNDCLASS wc; // Windows class structure
  189. HWND hWnd; // Storeage for window handle
  190. // Register Window style
  191. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  192. wc.lpfnWndProc = (WNDPROC) WndProc;
  193. wc.cbClsExtra = 0;
  194. wc.cbWndExtra = 0;
  195. wc.hInstance  = hInstance;
  196. wc.hIcon = NULL;
  197. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  198. // No need for background brush for OpenGL window
  199. wc.hbrBackground = NULL;
  200. wc.lpszMenuName = NULL;
  201. wc.lpszClassName = lpszAppName;
  202. // Register the window class
  203. if(RegisterClass(&wc) == 0)
  204. return FALSE;
  205. // Create the main application window
  206. hWnd = CreateWindow(
  207. lpszAppName,
  208. lpszAppName,
  209. // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  210. WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  211. // Window position and size
  212. 100, 100,
  213. 250, 250,
  214. NULL,
  215. NULL,
  216. hInstance,
  217. NULL);
  218. // If window was not created, quit
  219. if(hWnd == NULL)
  220. return FALSE;
  221. // Display the window
  222. ShowWindow(hWnd,SW_SHOW);
  223. UpdateWindow(hWnd);
  224. // Process application messages until the application closes
  225. while( GetMessage(&msg, NULL, 0, 0))
  226. {
  227. TranslateMessage(&msg);
  228. DispatchMessage(&msg);
  229. }
  230. return msg.wParam;
  231. }
  232. // Window procedure, handles all messages for this program
  233. LRESULT CALLBACK WndProc( HWND  hWnd,
  234. UINT message,
  235. WPARAM wParam,
  236. LPARAM lParam)
  237. {
  238. static HGLRC hRC; // Permenant Rendering context
  239. static HDC hDC; // Private GDI Device context
  240. switch (message)
  241.     {
  242. // Window creation, setup for OpenGL
  243. case WM_CREATE:
  244. // Store the device context
  245. hDC = GetDC(hWnd);
  246. // Select the pixel format
  247. SetDCPixelFormat(hDC);
  248. // Create the rendering context and make it current
  249. hRC = wglCreateContext(hDC);
  250. wglMakeCurrent(hDC, hRC);
  251. // Create the palette
  252. hPalette = GetOpenGLPalette(hDC);
  253. SetupRC(hDC);
  254. break;
  255. // Window is being destroyed, cleanup
  256. case WM_DESTROY:
  257. // Kill the timer that we created
  258. KillTimer(hWnd,101);
  259. glDeleteLists(nFontList, 128);
  260. // Deselect the current rendering context and delete it
  261. wglMakeCurrent(hDC,NULL);
  262. wglDeleteContext(hRC);
  263. // Delete the palette
  264. if(hPalette != NULL)
  265. DeleteObject(hPalette);
  266. // Tell the application to terminate after the window
  267. // is gone.
  268. PostQuitMessage(0);
  269. break;
  270. // Window is resized.
  271. case WM_SIZE:
  272. // Call our function which modifies the clipping
  273. // volume and viewport
  274. ChangeSize(LOWORD(lParam), HIWORD(lParam));
  275. break;
  276. // The painting function.  This message sent by Windows 
  277. // whenever the screen needs updating.
  278. case WM_PAINT:
  279. {
  280. // Call OpenGL drawing code
  281. RenderScene();
  282. // Call function to swap the buffers
  283. SwapBuffers(hDC);
  284.             ValidateRect(hWnd,NULL);
  285. }
  286. break;
  287. // Windows is telling the application that it may modify
  288. // the system palette.  This message in essance asks the 
  289. // application for a new palette.
  290. case WM_QUERYNEWPALETTE:
  291. // If the palette was created.
  292. if(hPalette)
  293. {
  294. int nRet;
  295. // Selects the palette into the current device context
  296. SelectPalette(hDC, hPalette, FALSE);
  297. // Map entries from the currently selected palette to
  298. // the system palette.  The return value is the number 
  299. // of palette entries modified.
  300. nRet = RealizePalette(hDC);
  301. // Repaint, forces remap of palette in current window
  302. InvalidateRect(hWnd,NULL,FALSE);
  303. return nRet;
  304. }
  305. break;
  306. // This window may set the palette, even though it is not the 
  307. // currently active window.
  308. case WM_PALETTECHANGED:
  309. // Don't do anything if the palette does not exist, or if
  310. // this is the window that changed the palette.
  311. if((hPalette != NULL) && ((HWND)wParam != hWnd))
  312. {
  313. // Select the palette into the device context
  314. SelectPalette(hDC,hPalette,FALSE);
  315. // Map entries to system palette
  316. RealizePalette(hDC);
  317. // Remap the current colors to the newly realized palette
  318. UpdateColors(hDC);
  319. return 0;
  320. }
  321. break;
  322.         default:   // Passes it on if unproccessed
  323.             return (DefWindowProc(hWnd, message, wParam, lParam));
  324.         }
  325.     return (0L);
  326. }