⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sphereworld32.c

📁 OpeNGL超级宝典源代码. OpeNGL超级宝典源代码.
💻 C
📖 第 1 页 / 共 3 页
字号:
			// Tell the application to terminate after the window
			// is gone.
			PostQuitMessage(0);
			break;

		// Window is resized.
		case WM_SIZE:
			// Call our function which modifies the clipping
			// volume and viewport
			ChangeSize(LOWORD(lParam), HIWORD(lParam));
			break;

		// The painting function.  This message sent by Windows 
		// whenever the screen needs updating.
		case WM_PAINT:
			{
            // Only poll keyboard when this window has focus
            if(GetFocus() == hWnd)
                {
                float fTime;
                float fLinear, fAngular;

        		// Get the time since the last time we rendered a frame
		        LARGE_INTEGER lCurrent;
		        QueryPerformanceCounter(&lCurrent);

		        fTime = (float)(lCurrent.QuadPart - CameraTimer.QuadPart) /
										(float)CounterFrequency.QuadPart;

                CameraTimer = lCurrent;

                // Camera motion will be time based. This keeps the motion constant
                // regardless of frame rate. Higher frame rates produce smoother
                // animation and motion, they should not produce "faster" motion.
                fLinear = fTime * 1.0f;
                fAngular = (float)gltDegToRad(60.0f * fTime);

                // Move the camera around, poll the keyboard
                if(GetAsyncKeyState(VK_UP))
                    gltMoveFrameForward(&frameCamera, fLinear);

                if(GetAsyncKeyState(VK_DOWN))
                    gltMoveFrameForward(&frameCamera, -fLinear);

                if(GetAsyncKeyState(VK_LEFT))
                    gltRotateFrameLocalY(&frameCamera, fAngular);
      
                if(GetAsyncKeyState(VK_RIGHT))
                    gltRotateFrameLocalY(&frameCamera, -fAngular);
                }

			// Call OpenGL drawing code
			RenderScene();

			// Call function to swap the buffers
			SwapBuffers(hDC);

            // Not validated on purpose, gives and endless series
            // of paint messages... this is akin to having
            // a rendering loop
            //ValidateRect(hWnd,NULL);
			}
			break;


		// Windows is telling the application that it may modify
		// the system palette.  This message in essance asks the 
		// application for a new palette.
		case WM_QUERYNEWPALETTE:
			// If the palette was created.
			if(hPalette)
				{
				int nRet;

				// Selects the palette into the current device context
				SelectPalette(hDC, hPalette, FALSE);

				// Map entries from the currently selected palette to
				// the system palette.  The return value is the number 
				// of palette entries modified.
				nRet = RealizePalette(hDC);

				// Repaint, forces remap of palette in current window
				InvalidateRect(hWnd,NULL,FALSE);

				return nRet;
				}
			break;

	
		// This window may set the palette, even though it is not the 
		// currently active window.
		case WM_PALETTECHANGED:
			// Don't do anything if the palette does not exist, or if
			// this is the window that changed the palette.
			if((hPalette != NULL) && ((HWND)wParam != hWnd))
				{
				// Select the palette into the device context
				SelectPalette(hDC,hPalette,FALSE);

				// Map entries to system palette
				RealizePalette(hDC);
				
				// Remap the current colors to the newly realized palette
				UpdateColors(hDC);
				return 0;
				}
			break;


        default:   // Passes it on if unproccessed
            return (DefWindowProc(hWnd, message, wParam, lParam));

        }

    return (0L);
	}



/////////////////////////////////////////////////////////////////////////////
// Dialog procedure for the startup dialog
BOOL APIENTRY StartupDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam)
	{
    switch (message)
    	{
		// Initialize the dialog box
	    case WM_INITDIALOG:
			{
            int nPF;
            HDC hDC;                        // Dialogs device context
            HGLRC hRC;
            DEVMODE devMode;
			unsigned int iMode;
			unsigned int nWidth;	// Current settings
			unsigned int nHeight;
			char cBuffer[64];
			HWND hListBox;
			
			PIXELFORMATDESCRIPTOR pfd = {   // Not going to be too picky
			sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
			PFD_TYPE_RGBA,		// Full color
			32,					// Color depth
			0,0,0,0,0,0,0,		// Ignored
			0,0,0,0,		    // Accumulation buffer
			16,					// Depth bits
			8,					// Stencil bits
			0,0,0,0,0,0 };		// Some used, some not


            // Initialize render options
			startupOptions.bFSAA = FALSE;
			startupOptions.bFullScreen = FALSE;
			startupOptions.bVerticalSync = FALSE;

			// Create a "temporary" OpenGL rendering context
			hDC = GetDC(hDlg);
		
			// Set pixel format one time....
			nPF = ChoosePixelFormat(hDC, &pfd);
			SetPixelFormat(hDC, nPF, &pfd);
			DescribePixelFormat(hDC, nPF, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

			// Create the GL context
			hRC = wglCreateContext(hDC);
			wglMakeCurrent(hDC, hRC);

			// Set text in dialog
			SetDlgItemText(hDlg, IDC_VENDOR, (const char *)glGetString(GL_VENDOR));
			SetDlgItemText(hDlg, IDC_RENDERER, (const char *)glGetString(GL_RENDERER));
			SetDlgItemText(hDlg, IDC_VERSION, (const char *)glGetString(GL_VERSION));
			
			// Vertical Sync off by default
			if(gltIsExtSupported("WGL_EXT_swap_control"))
				EnableWindow(GetDlgItem(hDlg, IDC_VSYNC_CHECK), TRUE);

			// Find a multisampled and non-multisampled pixel format
			FindBestPF(hDC, &startupOptions.nPixelFormat, &startupOptions.nPixelFormatMS);

			// Done with GL context
			wglMakeCurrent(hDC, NULL);
			wglDeleteContext(hRC);

			// Enumerate display modes
			iMode = 0;
			nWidth = GetSystemMetrics(SM_CXSCREEN);	// Current settings
			nHeight = GetSystemMetrics(SM_CYSCREEN);
			hListBox = GetDlgItem(hDlg, IDC_DISPLAY_COMBO);
			while(EnumDisplaySettings(NULL, iMode, &devMode))
                {
				//if(devMode.dmBitsPerPel == pfd.cColorBits)
				    {
                    int iItem;
					sprintf(cBuffer,"%d x %d x %dbpp @%dhz", devMode.dmPelsWidth,
						devMode.dmPelsHeight, devMode.dmBitsPerPel, devMode.dmDisplayFrequency);

					iItem = SendMessage(hListBox, CB_ADDSTRING, 0, (LPARAM)cBuffer);
					SendMessage(hListBox, CB_SETITEMDATA, iItem, iMode);

					if(devMode.dmPelsHeight == nHeight &&
						devMode.dmPelsWidth == nWidth)
						SendMessage(hListBox, CB_SETCURSEL, iItem, 0);
				    }
				iMode++;
                }
			

			// Set other defaults /////////////
			// Windowed or full screen
			CheckDlgButton(hDlg, IDC_FS_CHECK, BST_CHECKED);	

			// FSAA, but only if support detected
			if(startupOptions.nPixelFormatMS != 0)
				EnableWindow(GetDlgItem(hDlg, IDC_MULTISAMPLED_CHECK), TRUE);
				
			return (TRUE);
			}
			break;

		// Process command messages
	    case WM_COMMAND:      
			{
			// Validate and Make the changes
			if(LOWORD(wParam) == IDOK)
			    {
				// Read options ////////////////////////////////////////
				// Display mode
				HWND hListBox = GetDlgItem(hDlg, IDC_DISPLAY_COMBO);
				int iMode = SendMessage(hListBox, CB_GETCURSEL, 0, 0);
				iMode = SendMessage(hListBox, CB_GETITEMDATA, iMode, 0);
				EnumDisplaySettings(NULL, iMode, &startupOptions.devMode);
			
				// Full screen or windowed?
				if(IsDlgButtonChecked(hDlg, IDC_FS_CHECK))
					startupOptions.bFullScreen = TRUE;
				else
					startupOptions.bFullScreen = FALSE;
			
				
				// FSAA
				if(IsDlgButtonChecked(hDlg, IDC_MULTISAMPLED_CHECK))
					startupOptions.bFSAA = TRUE;
				else
					startupOptions.bFSAA = FALSE;

				// Vertical Sync.
				if(IsDlgButtonChecked(hDlg, IDC_VSYNC_CHECK))
					startupOptions.bVerticalSync = TRUE;
				else
					startupOptions.bVerticalSync = FALSE;


				EndDialog(hDlg,TRUE);
                }

			if(LOWORD(wParam) == IDCANCEL)
				EndDialog(hDlg, FALSE);
			}
			break;

		// Closed from sysbox
		case WM_CLOSE:
			EndDialog(hDlg,FALSE); // Same as cancel
			break;
		}

	return FALSE;
	}


///////////////////////////////////////////////////////////////////////////////
// Display the startup screen (just a modal dialog box)
BOOL ShowStartupOptions(void)
    {
	return DialogBox (ghInstance,
		MAKEINTRESOURCE(IDD_DLG_INTRO),
		NULL,
		StartupDlgProc);
    }


///////////////////////////////////////////////////////////////////////////////
// Select pixelformat with desired attributes
// Returns the best available "regular" pixel format, and the best available
// Multisampled pixelformat (0 if not available)
void FindBestPF(HDC hDC, int *nRegularFormat, int *nMSFormat)
    {
    *nRegularFormat = 0;
    *nMSFormat = 0;

    // easy check, just look for the entrypoint
    if(gltIsWGLExtSupported(hDC, "WGL_ARB_pixel_format"))
        if(wglGetPixelFormatAttribivARB == NULL)
            wglGetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)
                                 wglGetProcAddress("wglGetPixelFormatAttribivARB");

    // First try to use new extended wgl way
    if(wglGetPixelFormatAttribivARB != NULL)
        {
        // Only care about these attributes
        int nBestMS = 0;
        int i;
        int iResults[9];
        int iAttributes [9] = {    WGL_SUPPORT_OPENGL_ARB, // 0
                                   WGL_ACCELERATION_ARB,   // 1
                                   WGL_DRAW_TO_WINDOW_ARB, // 2
                                   WGL_DOUBLE_BUFFER_ARB,  // 3
                                   WGL_PIXEL_TYPE_ARB,     // 4
                                   WGL_DEPTH_BITS_ARB,     // 5
                                   WGL_STENCIL_BITS_ARB,   // 6
                                   WGL_SAMPLE_BUFFERS_ARB, // 7
                                   WGL_SAMPLES_ARB };      // 8

        // How many pixelformats are there?
        int nFormatCount[] = { 0 };
        int attrib[] = { WGL_NUMBER_PIXEL_FORMATS_ARB };
        wglGetPixelFormatAttribivARB(hDC, 1, 0, 1, attrib, nFormatCount);

        // Loop through all the formats and look at each one
        for(i = 0; i < nFormatCount[0]; i++)
            {
            // Query pixel format
            wglGetPixelFormatAttribivARB(hDC, i+1, 0, 9, iAttributes, iResults);

            // Match? Must support OpenGL AND be Accelerated AND draw to Window 
            if(iResults[0] == 1 && iResults[1] == WGL_FULL_ACCELERATION_ARB && iResults[2] == 1)
            if(iResults[3] == 1)                    // Double buffered
            if(iResults[4] == WGL_TYPE_RGBA_ARB)    // Full Color
            if(iResults[5] >= 16)                   // Any Depth greater than 16
            if(iResults[6] > 0)                     // Any Stencil depth (not zero)
                {
                // We have a candidate, look for most samples if multisampled
                if(iResults[7] == 1)	            // Multisampled
                    {
                    if(iResults[8] > nBestMS)       // Look for most samples
                        {
                        *nMSFormat = i;			// Multisamples	
                        nBestMS = iResults[8];	// Looking for the best
                        }
                    }
                else // Not multisampled
                    {
                    // Good enough for "regular". This will fall through
                    *nRegularFormat = i;
                    }
                }
            }
        }
    else
        {
        // Old fashioned way... 
        // or multisample
        PIXELFORMATDESCRIPTOR pfd = { 
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,      // Full color
        32,                 // Color depth
        0,0,0,0,0,0,0,      // Ignored
        0,0,0,0,            // Accumulation buffer
        24,                 // Depth bits
        8,                  // Stencil bits
        0,0,0,0,0,0 };      // Some used, some not

        *nRegularFormat = ChoosePixelFormat(hDC, &pfd);
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -