附上效果图和一小段代码(程序入口)以供参观:
// 1506号宇宙R.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "rbase.h"
#include "dfs3d.h"
#include "rsphere.h"
#include "3dview.h"
#include "creator.h"
#include "resloader.h"
#include "settings.h"
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
void TextureMode();
extern RWorld Main;
extern Camera3D camera;
extern Pnt3D MidPnt, Toward;
extern _flt Towardz, MinDist;
extern _int Mid;
_int CAPHEI;
GLuint Texture[6];
GLuint Control[6];
_int Mode = V_MAIN;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CAPHEI = GetSystemMetrics(SM_CYCAPTION);
srand(time(NULL));
rand();
// TODO: Place code here.
WNDCLASS wc;
HWND hWnd;
HGLRC hRC;
HDC hDC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC | CS_DBLCLKS;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "no1506";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"no1506", "1506号宇宙",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE/* | WS_MAXIMIZEBOX*/ | WS_MINIMIZEBOX,
0, 0, SCR_WID, SCR_HEI+CAPHEI,
NULL, NULL, hInstance, NULL);
Main.Create(20.0*Ly, 200);
Mid = 0;
Toward = MidPnt = Main.sphrs[Mid].P;
Towardz = camera.z = 1.5e11;
MinDist = Main.sphrs[Mid].R;
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
//glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glEnableClientState(GL_VERTEX_ARRAY);
glViewport(-(SCR_WID-SCR_HEI)/2, 0, SCR_HEI*2, SCR_HEI);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -0.5, 0.5, 1.0, Ly);
glClearDepth(1.0f);
InitSettings(Q_HIG);
InitMainCtrl();
AUX_RGBImageRec *bmp[2];
DFI_FILE * dfi[16];
memset(bmp, NULL, sizeof(AUX_RGBImageRec *));
memset(dfi, NULL, sizeof(DFI_FILE *));
LoadBMP(bmp, 0, "texture\\star.bmp", "加载纹理.\\texture\\star.bmp时发生了错误!");
LoadBMP(bmp, 1, "texture\\gasplanet.bmp", "加载纹理.\\texture\\gasplanet.bmp时发生了错误!");
LoadBMP(bmp, 2, "texture\\rockyplanet.bmp", "加载纹理.\\texture\\rockyplanet.bmp时发生了错误!");
LoadDFI(dfi, 0, "control\\settings.dfi", "加载图片.\\control\\settings.dfi时发生了错误!");
LoadDFI(dfi, 1, "control\\vkA.dfi", "加载图片.\\control\\vkA.dfi时发生了错误!");
LoadDFI(dfi, 2, "control\\vkS.dfi", "加载图片.\\control\\vkS.dfi时发生了错误!");
glGenTextures(6, Texture);
glGenTextures(6, Control);
for (int i = 0; i < 3; i ++)
{
glBindTexture(GL_TEXTURE_2D, Texture[i]);
TextureMode();
glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp[i]->sizeX, bmp[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp[i]->data);
}
for (i = 0; i < 3; i ++)
{
glBindTexture(GL_TEXTURE_2D, Control[i]);
TextureMode();
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, dfi[i]->sizeX, dfi[i]->sizeY, 0, GL_ALPHA, GL_UNSIGNED_BYTE, dfi[i]->data);
}
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
/* OpenGL animation code goes here */
if (Mode & V_MAIN)
{
calcMain();
drawMainView();
}
if (Mode & V_SET)
{
calcSettingsView();
drawSettingsView();
}
SwapBuffers (hDC);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
if (Mode & V_SET)
SettingsOnKeyDown(wParam);
else
OnKeyDown(wParam);
return 0;
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
if (Mode & V_SET)
SettingsCtrl(message, wParam, lParam);
else
OnMouse(message, wParam, lParam);
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
void TextureMode()
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,0x812F);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,0x812F);
}