- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
main.cpp
资源名称:brm2.rar [点击查看]
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:16k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "main.h"
- void RenderScene();
- bool InitializeGL();
- void SetupPixelFormat(HDC hDC);
- HDC g_HDC;
- HWND hwnd;
- CSceneRoot sRoot;
- CPhysic physic;
- CSound sound;
- CTimer timer;
- CMotionBlur mblur;
- CTakeTimer tt_phys(&timer, 100, "physics limit exceeded");
- CTakeTimer tt_draw(&timer, 100, "drawing limit exceeded");
- CTakeTimer tt_snd(&timer, 50, "sound limit exceeded");
- int obj_count = 100;
- int tex_id = 0;
- CVector cam(70, -7.0f, 40);
- float cam_add = 1.0f;
- float i1 = 100;
- float i1add = 100;
- float i2 = 63.0f;
- float i2add = 20;
- float i3 = 2.2f;
- float i4 = -3.0f;
- void brm();
- void abc();
- void TitleInfo();
- // init floor
- void initFlr()
- {
- CPhysicObject *flr = new CPhysicObject;
- flr->vColour.Set(0.0f, 0.6f, 0.0f, 0.5f);
- flr->SetPosition(CVector(0, -10, 0));
- flr->size.Set(100, 1, 100);
- flr->weight = 0;
- flr->isBox = 1;
- flr->piTexID = &tex_id;
- physic.AddObject(flr);
- sRoot.AddChild(flr);
- }
- // 1 object rnd position
- void objectRndPosition(CPhysicObject *o)
- {
- float min_y = 20, max_y = 30;
- float min_ = -50, max_ = 50;
- o->SetNewtonPosition(CVector(rnd(min_, max_), rnd(min_y, max_y), rnd(min_, max_)));
- }
- // 1 object init
- void initObject(CPhysicObject *o)
- {
- o->isBox = rand()%2;
- o->size = 0.5f;
- if (!o->isBox) o->size /= 2;
- o->weight = 1;
- o->piTexID = &tex_id;
- if (o->pBody) o->ReleaseFromNewton();
- o->mMatrix.Reset();
- o->vColour.Randomize();
- physic.AddObject(o);
- objectRndPosition(o);
- }
- // init all objects
- void initObjects()
- {
- for (int i=0 ; i < obj_count ; i++)
- {
- CPhysicObject *o = new CPhysicObject;
- initObject(o);
- sRoot.AddChild(o);
- CParticlesObject *po = new CParticlesObject();
- po->pPSystem->SetMaxLifeClrVelGrvPosEboxVvarTex(50, 0.5f, CVector(0.9f, 0.5f, 0.0), CVector(0),
- CVector(0, 10, 0), CVector(0, -0.98f, 0), CVector(0), CVector(0.2f), 45, 3);
- o->AddChild(po);
- }
- }
- // objects random position
- void objectsRndPosition()
- {
- for (vqCSNi i = sRoot.vKids.begin() ; i != sRoot.vKids.end() ; i++)
- {
- if (!*i) continue;
- if (!((CPhysicObject*)(*i))->weight) continue;
- objectRndPosition((CPhysicObject*)*i);
- }
- }
- bool Init()
- {
- if (!InitializeGL()) return 0;
- sound.Init();
- sound.SetListener(&cam[0]);
- sound.AddSound("1", "snds/1.mp3");
- sound.AddSound("2", "snds/2.mp3");
- sound.AddSound("3", "snds/3.mp3");
- sound.AddSound("4", "snds/4.mp3");
- sound.AddSound("5", "snds/5.mp3");
- sound.AddSound("6", "snds/6.mp3");
- sound.AddSound("7", "snds/7.mp3");
- sound.AddSound("8", "snds/8.mp3");
- sound.loadAndPlayLoop("snds/fire.mp3");
- physic.Init();
- initFlr();
- initObjects();
- return 1;
- }
- // The pixel format is an extension to the Win32 API that is provided
- // for support of OpenGL functionality. To be honest I've never change
- // this code every time I use it. If you don't understand pixel format
- // then don't worry. Just know that you need this exactly how it is and
- // it will most likely never change or would not change much. I will
- // go over it in more detail in later tutorials.
- void SetupPixelFormat(HDC hDC)
- {
- int nPixelFormat;
- static PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // size of structure.
- 1, // always 1.
- PFD_DRAW_TO_WINDOW | // support window
- PFD_SUPPORT_OPENGL | // support OpenGl
- PFD_DOUBLEBUFFER, // support double buffering
- PFD_TYPE_RGBA, // support RGBA
- 16, // 32 bit color mode
- 0, 0, 0, 0, 0, 0, // ignore color bits
- 0, // no alpha buffer
- 0, // ignore shift bit
- 0, // no accumulation buffer
- 0, 0, 0, 0, // ignore accumulation bits.
- 16, // number of depth buffer bits.
- 0, // number of stencil buffer bits.
- 0, // 0 means no auxiliary buffer
- PFD_MAIN_PLANE, // The main drawing plane
- 0, // this is reserved
- 0, 0, 0 }; // layer masks ignored.
- // this chooses the best pixel format and returns index.
- nPixelFormat = ChoosePixelFormat(hDC, &pfd);
- // This set pixel format to device context.
- SetPixelFormat(hDC, nPixelFormat, &pfd);
- // Remember that its not important to fully understand the pixel format,
- // just remember to include in all of your applications and you'll be
- // good to go.
- }
- // This is the Windows procedure (WndProc). This handles messages that the
- // windows operating sends to your application. This is similar to the
- // message map in MFC. There are a lot of different messages that could
- // be responded to that you will see little by litle as we go on. The
- // LRESULT is a long integer and CALLBACK is the calling convention used
- // with functions that are called by the Windows operating system. Every
- // program will need a Windows procedure whether it is simple or complex.
- // Lets take a look at the Windows procedure...
- LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- static HGLRC hRC;
- static HDC hDC; // Device context.
- int width, height; // The window width and height.
- switch(message)
- {
- case WM_CREATE: // Windows creation.
- hDC = GetDC(hwnd); // This gets the device context for our window.
- g_HDC = hDC; // Assigns the global device context to this one.
- SetupPixelFormat(hDC); // Call the pixel format function.
- hRC = wglCreateContext(hDC);
- wglMakeCurrent(hDC, hRC);
- return 0;
- break;
- case WM_CLOSE: // Close message.
- case WM_DESTROY:
- wglMakeCurrent(hDC, NULL);
- wglDeleteContext(hRC);
- PostQuitMessage(0); // Says close the program.
- return 0;
- break;
- case WM_SIZE: // re-size message.
- height = HIWORD(lParam); // This gets the height of the window.
- width = LOWORD(lParam); // This gets the width of the window.
- if(height==0) // we don't want it to be possible for a
- { // height of 0. If it is 0 me make it 1.
- height = 1;
- }
- glViewport(0, 0, width, height);// resets the viewport to new dimensions.
- glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
- glLoadIdentity(); // Reset the modelview matrix.
- // calculate the aspect ratio of the window.
- gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 10000.0f);
- glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
- glLoadIdentity(); // Reset the modelview matrix.
- glClear(GL_ACCUM_BUFFER_BIT);
- return 0;
- break;
- // This will be used to close the program when the keydown message is sent
- // to your application if the key is escape. If you wanted to check for
- // other messages then you would add more case statements to it. We will
- // do this in future tutorials. For now to exit when the user presses escape
- // you do this...
- case WM_KEYDOWN:
- {
- switch(wParam)
- {
- case VK_ESCAPE:
- PostQuitMessage(0);
- break;
- case 'Z': physic.SwitchSlowMode(); break;
- case 'X': objectsRndPosition(); break;
- case 'T': brm(); break;
- case 'L': sound.PauseAll(); break;
- case 'K': sound.UnpauseAll(); break;
- case 'B': mblur.Switch(); break;
- case 'W': cam.x -= cam_add; break;
- case 'S': cam.x += cam_add; break;
- case 'A': cam.z += cam_add; break;
- case 'D': cam.z -= cam_add; break;
- case 'Q': cam.y += cam_add; break;
- case 'E': cam.y -= cam_add; break;
- case 'U': tex_id = CreateOneColourTexture(); break;
- case 'I': tex_id = CreateChaosTexture(); break;
- case 'O': tex_id = CreateStarTexture(); break;
- case 'P': tex_id = CreateLineObjectTexture(); break;
- case '1': sound.PlaySound("1", 0); break;
- case '2': sound.PlaySound("2", 0); break;
- case '3': sound.PlaySound("3", 0); break;
- case '4': sound.PlaySound("4", 0); break;
- case '5': sound.PlaySound("5", 0); break;
- case '6': sound.PlaySound("6", 0); break;
- case '7': sound.PlaySound("7", 0); break;
- case '8': sound.PlaySound("8", 0); break;
- case '9': i1 += i1add; break;
- case '0': i1 -= i1add; break;
- case 'N': i2 += i2add; break;
- case 'M': i2 -= i2add; break;
- }
- break;
- }
- default: // Always have a default in case.
- break;
- }
- // What this does is pass all of the unhandled messages to DefWindowProc
- return (DefWindowProc(hwnd, message, wParam, lParam));
- }
- // This is the WinMain class. Remember that every C and C++ program required
- // a main() function, well every Windows program require a WinMain().
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- MSG msg; // A message variable.
- WNDCLASSEX windowClass; // Your Window class.
- // HWND hwnd; // The Window handle.
- bool isFinished; // This will be used to check if the program is done or not.
- // This is the Window class. Each attribute is defined for the creation of the
- // window. I will try to go over more in future tutorials but if you want the
- // complete list of all the different values you can pass, check out the MSDN.
- windowClass.cbSize = sizeof(WNDCLASSEX); // size of the WNDCLASSEX structure.
- windowClass.style = CS_HREDRAW | CS_VREDRAW; // style of the window.
- windowClass.lpfnWndProc = WndProc; // Address to the windows procedure.
- windowClass.cbClsExtra = 0; // Extra class information.
- windowClass.cbWndExtra = 0; // Extra window information.
- windowClass.hInstance = hInstance; // Handle of application Instance.
- windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);// Handle of application Icon.
- windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);// mouse cursor
- windowClass.hbrBackground = NULL; // background color.
- windowClass.lpszMenuName = NULL; // name of the main menu.
- windowClass.lpszClassName = "UltimateGameProgrammingClass";// window class name.
- windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);// icon when minimized.
- // You must register you class with Windows. What this does is if the class is
- // not registered, then the program will close right away.
- if(!RegisterClassEx(&windowClass))
- return 0;
- // After your class has been registered then you are ready to create your
- // window. To create the window you call the CreateWindowEx function
- // and assign it to hwnd.
- hwnd = CreateWindowEx(NULL,// The extended window style.
- "UltimateGameProgrammingClass",// window Class name.
- "brm", // window name.
- WS_OVERLAPPEDWINDOW | WS_VISIBLE |// The window style.
- WS_SYSMENU | WS_CLIPCHILDREN |// window style.
- WS_CLIPSIBLINGS,// window style.
- 110, 80,// window x, y coordinate.
- 800, 600,// window width and height.
- NULL,// handle to parent window.
- NULL,// menu.
- hInstance,// handle to app instance.
- NULL); // pointer to window creation data.
- // For more window styles check the MSDN but for now use this to create your
- // windows. I will go over this in more detail in future tutorials.
- // If there was an error with creating the window, then close the program.
- if(!hwnd)
- return 0;
- ShowWindow(hwnd, SW_SHOW); // This shows the window.
- UpdateWindow(hwnd); // This forces a paint message.
- isFinished = false; // False = running, True = not running.
- // Since our program is running we will of cource
- // set this to false. For this tutorials it has
- // no real purpose but in future tutorials we will
- // use this for when the user presses the esc key
- // the program will stop.
- // If the initialization of OpenGL fail we can't run the program.
- if(!Init())
- isFinished = true;
- while(!isFinished)// While the program is running...
- {
- PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
- if(msg.message == WM_QUIT)// If the application gets a quit message...
- {
- isFinished = true; // Then quit the program.
- }
- else
- {
- RenderScene();
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return msg.wParam;// End of the program.
- }
- void TitleInfo()
- {
- char title[256];
- char q[16];
- strcpy(title, "brm | fps: "); itoa((int)timer.GetFPS(), q, 10); strcat(title, q);
- strcat(title, " . tmrs: "); itoa(timer.GetUsed(), q, 10); strcat(title, q);
- strcat(title, " . actv: "); itoa(physic.GetActive(), q, 10); strcat(title, q);
- strcat(title, " . phys: "); itoa(tt_phys.GetTaken(), q, 10); strcat(title, q);
- strcat(title, " . draw: "); itoa(tt_draw.GetTaken(), q, 10); strcat(title, q);
- strcat(title, " . snd: "); itoa(tt_snd.GetTaken(), q, 10); strcat(title, q);
- strcat(title, " . snds.real: "); itoa(sound.vSounds.GetRealSize(), q, 10); strcat(title, q);
- strcat(title, " . play.size: "); itoa(sound.vPlayed.size(), q, 10); strcat(title, q);
- strcat(title, " . play.real: "); itoa(sound.vPlayed.GetRealSize(), q, 10); strcat(title, q);
- strcat(title, " . i1: "); itoa((int)i1, q, 10); strcat(title, q);
- float fps = timer.GetFPS();
- float mspf = timer.GetMSPF();
- // errfi("", fps);
- // erri("", mspf);
- // errfi("fps, mspf", fps, mspf);
- SetWindowText(hwnd, title);
- }
- bool InitializeGL()
- {
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Clear the screen to black.
- glShadeModel(GL_SMOOTH); // Smooth shading in our scenes.
- glEnable(GL_DEPTH_TEST); // Enable desth testing for hidden surface removal.
- glDisable(GL_LIGHTING); // Enable desth testing for hidden surface removal.
- glDisable(GL_BLEND);
- glBlendFunc(GL_SRC_COLOR, GL_SRC_ALPHA);
- glEnable(GL_DEPTH_TEST);
- CreateOneColourTexture();
- CreateChaosTexture();
- CreateStarTexture();
- CreateLineObjectTexture();
- tex_id = CreateOneColourTexture();
- glEnable(GL_TEXTURE_2D);
- glClear(GL_ACCUM_BUFFER_BIT);
- srand(GetTickCount());
- // If all went well we return true.
- return true;
- }
- void rndSound()
- {
- if (rand() > 42+23) return;
- CPhysicObject *o;
- o = (CPhysicObject*)(sRoot.vKids.GetRandom());
- int snd_i = rand() % 9;
- char str[4];
- itoa(snd_i, str, 10);
- sound.PlaySound(str, &o->mMatrix.m_posit[0]);
- }
- void RenderScene()
- {
- rndSound();
- timer.Frame();
- TitleInfo();
- CSceneNode *n;
- n = sRoot.vKids.begin()[2];
- CMatrix m = n->mMatrix;
- /**/
- tt_draw.start();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen.
- glLoadIdentity(); //Reset modelview matrix for new frame.
- gluLookAt(cam.x, cam.y, cam.z, m.m_posit.x, m.m_posit.y, m.m_posit.z, 0, 1, 0);
- glxMatrix(1);
- glBindTexture(GL_TEXTURE_2D, tex_id);
- mblur.step(1);
- sRoot.Update(timer.GetSPF());
- sRoot.Render();
- mblur.step(23);
- SwapBuffers(g_HDC); // Display the new frame by swapping the
- tt_draw.stamp();
- /**/
- /**/
- tt_snd.start();
- sound.Frame();
- tt_snd.stamp();
- /**/
- /**/
- tt_phys.start();
- physic.Frame(timer.GetSPF()*3);
- tt_phys.stamp();
- /**/
- }
- void abc()
- {
- }
- void brm()
- {
- }