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

📄 quadrics.cpp

📁 一本关于OPenGL的很好的电子书
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
 quadrics.cpp
 
 In this demo, a 3D world filled with quadric objects is created for the
 player to explore.
  
 Author   :   Dave Astle
 Date     :   1/5/2001

 Written for OpenGL Game Programming
*****************************************************************************/


/********************************* Includes *********************************/
#define WIN32_LEAN_AND_MEAN   // get rid of Windows things we don't need

#include <windows.h>					// included in all Windows apps
#include <winuser.h>          // Windows constants
#include <gl/gl.h>						// OpenGL include
#include <gl/glu.h>						// OpenGL utilty library
#include <stdlib.h>
#include <math.h>
#include "bitmap.h"
#include <stdio.h>
#include "HiResTimer.h"

#define WND_CLASS_NAME  "OpenGL Window Class"


/*************************** Constants and Macros ***************************/
const int   SCREEN_WIDTH    = 1000;
const int   SCREEN_HEIGHT   = 400;
const int   SCREEN_BPP      = 32;
const bool  USE_FULLSCREEN  = false; 
const char  *APP_TITLE      = "Quadric World";

const float DEFAULT_SPEED   = 0.4f;
const int   WORLD_SIZE      = 250;
// produce a random number between 0.0 and 1.0
#define FRAND (((GLfloat)rand())/RAND_MAX)

#define KEY_DOWN(vk_code)  ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)    ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)


/********************************* Globals **********************************/
HDC       g_hdc;								  // device context
HGLRC     g_hrc;                  // rendering context
BOOL      g_isFullscreen = TRUE;  // toggles fullscreen and windowed display
BOOL      g_isActive = TRUE;      // false if window is minimized
HWND      g_hwnd = NULL;          // main window handle
HINSTANCE g_hInstance;            // application instance

// "player" orientation information
GLdouble  g_playerPos[] = { 0.0, 0.5, 10.0 };
GLdouble  g_lookAt[] = { 0.0, 0.0, 0.0 };
GLfloat   g_viewAngle = -90.0;
GLfloat   g_elevationAngle = 0.0;


// kill this if we can
GLboolean g_keys[256];

// texture names
GLuint  g_fireTexture;
GLuint  g_rocketTexture;
GLuint  g_starsTexture;

// quadric objects
GLUquadricObj *g_normalObject     = NULL;
GLUquadricObj *g_wireframeObject  = NULL;
GLUquadricObj *g_texturedObject   = NULL;
GLUquadricObj *g_flatshadedObject = NULL;


/******************************** Prototypes ********************************/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

BOOL    SetupWindow(const char *title, int width, int height, int bits, bool isFullscreen);
BOOL    KillWindow();

GLvoid  ResizeScene(GLsizei width, GLsizei height);
BOOL    InitializeScene();
BOOL    DisplayScene();
BOOL    Cleanup();

GLvoid  DrawGround();
GLvoid  DrawStars();
GLvoid  DrawNormalObjects(GLfloat rotation);
GLvoid  DrawWireframeObjects(GLfloat rotation);
GLvoid  DrawTexturedObjects(GLfloat rotation);
GLvoid  DrawFlatshadedObjects(GLfloat rotation);

void    LoadTexture(char *filename, GLuint &texture);


/*****************************************************************************
 WinMain()

 Windows entry point
*****************************************************************************/
int WINAPI WinMain(HINSTANCE g_hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
  MSG   msg;       // message
  BOOL  isDone;    // flag indicatingen the app is done


  // if the window is set up correctly, we can proceed with the message loop
  if (SetupWindow(APP_TITLE, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, USE_FULLSCREEN))
    isDone = FALSE;
  // otherwise, we need to never enter the loop and proceed to exit
  else
    isDone = TRUE;

  CHiResTimer timer;
  timer.Init();

  int nFrames = 0;

  // main message loop
  while (!isDone)
  {
    if(PeekMessage(&msg, g_hwnd, NULL, NULL, PM_REMOVE))
    {
      if (msg.message == WM_QUIT)		// do we receive a WM_QUIT message?
      {
        isDone = TRUE;				// if so, time to quit the application
      }
      else
      {
        TranslateMessage(&msg);		// translate and dispatch to event queue
        DispatchMessage(&msg);
      }
    }

    // don't update the scene if the app is minimized
    if (g_isActive && g_hrc)
    {
      // update the scene every time through the loop
      DisplayScene();
      // switch the front and back buffers to display the updated scene
      SwapBuffers(g_hdc);
    }
			nFrames++;
			if(nFrames > 100)
				{
				char cOutBuffer[32];

				// Display the fps
				sprintf(cOutBuffer,"%0.1f",timer.GetFPS(100));
				SetWindowText(g_hwnd, cOutBuffer);
				nFrames = 0;
				}
  }

  Cleanup();
  KillWindow();

  return msg.wParam;
} // end WinMain()


/*****************************************************************************
 WndProc()

 Windows message handler
*****************************************************************************/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch(message)
  {
  case WM_ACTIVATE:  // watch for the window being minimized and restored
    {
      if (!HIWORD(wParam))
      {
        // program was restored or maximized
        g_isActive = TRUE;
      }
      else
      {
        // program was minimized
        g_isActive=FALSE;
      }

      return 0;
    }

  case WM_SYSCOMMAND:  // look for screensavers and powersave mode
    {
      switch (wParam)
			{
				case SC_SCREENSAVE:     // screensaver trying to start
				case SC_MONITORPOWER:   // monitor going to powersave mode
          // returning 0 prevents either from happening
  				return 0;
        default:
          break;
      }
    } break;

  case WM_CLOSE:    // window is being closed
    {
      // send WM_QUIT to message queue
      PostQuitMessage(0);

      return 0;
    }

  case WM_SIZE:
    {
      // update perspective with new width and height
      ResizeScene(LOWORD(lParam), HIWORD(lParam));
      return 0;
    }

  case WM_KEYDOWN:
    {
      switch (toupper(wParam))
      {
      case VK_ESCAPE:
        {
          // send WM_QUIT to message queue
          PostQuitMessage(0);
          return 0;
        }
      default:
        {
          g_keys[wParam] = GL_TRUE;
          return 0;
        }
      };
    }

  case WM_KEYUP:
    {
      g_keys[wParam] = GL_FALSE;
      return 0;
    }
  default:
    break;
  }

  return (DefWindowProc(hwnd, message, wParam, lParam));
} // end WndProc()


/*****************************************************************************
 SetupWindow()

 Create the window and everything else we need, including the device and
 rendering context. If a fullscreen window has been requested but can't be
 created, the user will be prompted to attempt windowed mode. Finally,
 InitializeScene is called for application-specific setup.

 Returns TRUE if everything goes well, or FALSE if an unrecoverable error
 occurs. Note that if this is called twice within a program, KillWindow needs
 to be called before subsequent calls to SetupWindow.
*****************************************************************************/
BOOL SetupWindow(const char *title, int width, int height, int bits, bool isFullscreen)
{
  // set the global flag
  g_isFullscreen = isFullscreen;

  // get our instance handle
  g_hInstance	= GetModuleHandle(NULL);

  WNDCLASSEX  wc;    // window class

  // fill out the window class structure
  wc.cbSize         = sizeof(WNDCLASSEX);
  wc.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  wc.lpfnWndProc    = WndProc;
  wc.cbClsExtra     = 0;
  wc.cbWndExtra     = 0;
  wc.hInstance      = g_hInstance;
  wc.hIcon          = LoadIcon(NULL, IDI_APPLICATION);	// default icon
  wc.hIconSm        = LoadIcon(NULL, IDI_WINLOGO);		  // windows logo small icon
  wc.hCursor        = LoadCursor(NULL, IDC_ARROW);		  // default arrow
  wc.hbrBackground  = NULL;     // no background needed
  wc.lpszMenuName   = NULL;     // no menu
  wc.lpszClassName  = WND_CLASS_NAME;
  
  // register the windows class
  if (!RegisterClassEx(&wc))
  {
    MessageBox(NULL,"Unable to register the window class", "Error", MB_OK | MB_ICONEXCLAMATION);

    // exit and return FALSE
    return FALSE;       
  }

  // if we're in fullscreen mode, set the display up for it
  if (g_isFullscreen)
  {
    // set up the device mode structure
    DEVMODE screenSettings;
    memset(&screenSettings,0,sizeof(screenSettings));

    screenSettings.dmSize       = sizeof(screenSettings);	
    screenSettings.dmPelsWidth  = width;			// screen width
    screenSettings.dmPelsHeight = height;			// screen height
    screenSettings.dmBitsPerPel = bits;				// bits per pixel
    screenSettings.dmFields     = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

    // attempt to switch to the resolution and bit depth we've selected
    if (ChangeDisplaySettings(&screenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
    {
      // if we can't get fullscreen, let them choose to quit or try windowed mode
      if (MessageBox(NULL, "Cannot run in the fullscreen mode at the selected resolution\n"
                           "on your video card. Try windowed mode instead?",
                           "OpenGL Game Programming",
                           MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
      {
        g_isFullscreen = FALSE;	
      }
      else
			{
				return FALSE;
			}
    }
  }

	DWORD dwExStyle;
	DWORD dwStyle;

  // set the window style appropriately, depending on whether we're in fullscreen mode
	if (g_isFullscreen)
	{
		dwExStyle = WS_EX_APPWINDOW;
		dwStyle = WS_POPUP;						// simple window with no borders or title bar
		ShowCursor(FALSE);            // hide the cursor for now
	}
	else
	{
		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwStyle = WS_OVERLAPPEDWINDOW;
	}

  // set up the window we're rendering to so that the top left corner is at (0,0)
  // and the bottom right corner is (height,width)
  RECT  windowRect;
  windowRect.left = 0;
  windowRect.right = (LONG) width;
  windowRect.top = 0;
  windowRect.bottom = (LONG) height;

  // change the size of the rect to account for borders, etc. set by the style
  AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);

  // class registered, so now create our window
  g_hwnd = CreateWindowEx(dwExStyle,          // extended style
                          WND_CLASS_NAME,     // class name
                          title,              // app name
                          dwStyle |           // window style
                          WS_CLIPCHILDREN |   // required for
                          WS_CLIPSIBLINGS,    // using OpenGL
                          0, 0,               // x,y coordinate
                          windowRect.right - windowRect.left, // width
                          windowRect.bottom - windowRect.top, // height
                          NULL,               // handle to parent
                          NULL,               // handle to menu
                          g_hInstance,        // application instance
                          NULL);              // no extra params

  // see if our window handle is valid
  if (!g_hwnd)
	{
		MessageBox(NULL, "Unable to create window", "Error", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

  // get a device context
	if (!(g_hdc = GetDC(g_hwnd)))
	{

⌨️ 快捷键说明

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