#include #include #include #include "prim.h" #include "waxloader.h" #include class Pos { public: float x,y,z; Pos() { x=0; y=0; z=0; } }; static HGLRC hRC; static HDC hDC; BOOL keys[256]; GLsizei w,h; Wax * wax; GLfloat X, Z; GLfloat HeadMovement, HeadMovAngle; POINT mpos; POINT ompos; GLfloat camy,camx; Pos pos; GLvoid InitGL(GLsizei Width, GLsizei Height) { glClearColor(0, 0, 0, 0.0f); glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glMatrixMode(GL_PROJECTION); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); } GLvoid ReSizeGLScene(GLsizei Width, GLsizei Height) { if (Height==0) Height=1; glViewport(0, 0, Width, Height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); } GLvoid DrawGLScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(-camx, 1, 0, 0); glRotatef(-camy, 0, 1, 0); glTranslatef(pos.x,pos.y,pos.z); for(int i=0; icount; i++) { wax->p[i].draw(); } } LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { GLuint PixelFormat; static PIXELFORMATDESCRIPTOR pfd= { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; switch (message) { case WM_CREATE: ShowCursor(false); wax=new Wax((char*)"room.wax"); hDC = GetDC(hWnd); PixelFormat = ChoosePixelFormat(hDC, &pfd); if (!PixelFormat) { MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK|MB_ICONERROR); PostQuitMessage(0); break; } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) { MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK|MB_ICONERROR); PostQuitMessage(0); break; } hRC = wglCreateContext(hDC); if(!hRC) { MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK|MB_ICONERROR); PostQuitMessage(0); break; } if(!wglMakeCurrent(hDC, hRC)) { MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR); PostQuitMessage(0); break; } InitGL(w, h); break; case WM_DESTROY: case WM_CLOSE: ChangeDisplaySettings(NULL, 0); wglMakeCurrent(hDC,NULL); wglDeleteContext(hRC); ReleaseDC(hWnd,hDC); PostQuitMessage(0); break; case WM_KEYDOWN: keys[wParam] = TRUE; break; case WM_KEYUP: keys[wParam] = FALSE; break; case WM_SIZE: ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); break; default: return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS wc; HWND hWnd; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = "OpenGL WinClass"; if(!RegisterClass(&wc)) { MessageBox(0,"Failed To Register The Window Class.","Error",MB_OK|MB_ICONERROR); return FALSE; } w=GetSystemMetrics(SM_CXSCREEN); h=GetSystemMetrics(SM_CYSCREEN); hWnd = CreateWindow( "OpenGL WinClass", "Wax", WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, w, h, NULL, NULL, hInstance, NULL); if(!hWnd) { MessageBox(0,"Window Creation Error.","Error",MB_OK|MB_ICONERROR); return FALSE; } DEVMODE dmScreenSettings; // Режим работы memset(&dmScreenSettings, 0, sizeof(DEVMODE)); // Очистка для хранения установок dmScreenSettings.dmSize = sizeof(DEVMODE); // Размер структуры Devmode dmScreenSettings.dmPelsWidth = w; // Ширина экрана dmScreenSettings.dmPelsHeight = h; // Высота экрана dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; // Режим Пиксела ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN); ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); SetFocus(hWnd); while (1) { // Обработка всех сообщений while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { if (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { return TRUE; } } DrawGLScene(); // Нарисовать сцену SwapBuffers(hDC); // Переключить буфер экрана GetCursorPos(&mpos); camy=camy-(mpos.x-ompos.x)/2; camx=camx-(mpos.y-ompos.y)/2; if(camx>90) camx=90; if(camx<-90) camx=-90; ompos=mpos; if(mpos.x>w-20) { ompos.x=20; SetCursorPos(20,mpos.y); } if(mpos.x<20) { ompos.x=w-20; SetCursorPos(w-20,mpos.y); } if (keys[VK_ESCAPE]) SendMessage(hWnd,WM_CLOSE,0,0); // Если ESC - выйти if(keys['w']||keys['W']||keys['ц']||keys['Ц']) { pos.x=pos.x+cos(-camy* 0.01745329F+90); pos.z=pos.z+sin(-camy* 0.01745329F+90); } if(keys['s']||keys['S']||keys['ы']||keys['Ы']) { pos.x=pos.x-cos(-camy* 0.01745329F+90); pos.z=pos.z-sin(-camy* 0.01745329F+90); } if(keys['a']||keys['A']||keys['ф']||keys['Ф']) { pos.x=pos.x+sin(-camy* 0.01745329F+90); pos.z=pos.z-cos(-camy* 0.01745329F+90); } if(keys['d']||keys['D']||keys['в']||keys['В']) { pos.x=pos.x-sin(-camy* 0.01745329F+90); pos.z=pos.z+cos(-camy* 0.01745329F+90); } } }