Red-Blue alternation in VR assignments of Beijing University of Technology

Source: Internet
Author: User
Tags alternation

The Graduate Student offered the virtual reality course and chose it with a try. Because the "Interactive Graphic programming" (taught by Li hongsong) offered by the Software Institute ), at that time, I used DX9 + vs2008 to develop a set of Microsoft things. Today, this course requires OpenGL, and I am not used to it. Of course, I have asked Mr. Li Fengxia that DX9 can fully implement OpenGL. However, since we all use OpenGL, try it.

The first step is to configure the development environment. I searched the internet and found that the following files are required:

GLUT. h

GLUT. Lib

Glu32.lib

GLUT. dll

Glu32.dll

Glaux. h

Glaux. Lib

Put these files in the vs2008 folder. My files are D: \ visual_studio_2008 \ Vc. put the H file into D: \ visual_studio_2008 \ Vc \ include \ GL ;. put the Lib file in D: \ visual_studio_2008 \ Vc \ Lib ;. DLL to c: \ windows \ system32. After completing these steps, you only need to configure them in vs2008.

Create a new Win32 console program and enter the project menu item for configuration as follows:

After completing these configurations, create a new CPP file and paste the student code given by instructor Li:

# Pragma comment (Lib, "opengl32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "Glaux. lib ") # include <windows. h> // windows header file # include <stdio. h> // header file of the standard input/output Library (Added) # include <GL \ GL. h> // the header file of the opengl32 library # include <GL \ Glu. h> // the header file of the glu32 library # include <GL \ Glaux. h> // hglrchrc = NULL in the header file of the Glaux Library; // permanent coloring description table hdchdc = NULL; // hwndhwnd = NULL in the private GDI device description table; // save our window handle hinstanceh Instance; // The instance boolkeys for saving the program [256]; // The array boolactive = true for the keyboard routine; // The activity flag of the window. The default value is trueboolfullscreen = true; // The full-screen flag is set to the full-screen mode intfspeed = 3 by default; // the speed of the red basket switching frame rate, the more slow the intframespeed = fspeed; floatrspeed = 4.0f; // the rotation speed, faster floatrot = 0.0f; lresultcallback wndproc (hwnd, uint, wparam, lparam); // wndproc defines glvoid resizeglscene (glsizei width, glsizeheight I) // reset and initialize the GL window size {If (Height = 0) // prevent division by zero {Height = 1; // set height to 1} glviewport (, Width, height); // reset the current viewport glmatrixmode (gl_projection); // select the projection matrix glloadidentity (); // reset the projection matrix // calculate the appearance ratio of the window gluperspective (45.0f, (glfloat) width/(glfloat) height, 0.1f, 100366f); glmatrixmode (gl_modelview ); // select the model observation matrix glloadidentity (); // reset the model observation matrix} int initgl (glvoid) // All OpenGL settings are started here {glshademodel (gl_smooth ); // enable the shadow smooth glclearcolor (0.0f, 0.0f, 0.0f, 0.5f); // The black background glcleardepth (1.0f); // sets the deep cache glable (gl_de Pth_test); // enable gldepthfunc (gl_lequal); // glhint (gl_perspective_correction_hint, gl_nicest) of the type of the deep test; // return true for a truly fine-grained perspective correction; // initialize OK} // draw the missile model void drawmissile (void) {float scale = 0.06; float P [3] = {0 * scale, 0 * scale, 13 * scale}; // missile vertex // float PR [3] = {9, 0, 0}; // Missile tail point float a [3] = {1 * scale, 1.732 * scale, 7 * scale}; // front hexagonal float B [3] = {2 * scale, 0 * scale, 7 * scale }; float C [3] = {1 * scale,-1.732 * scale, 7 * scale}; float d [3] = {-1 * scale,-1.732 * scale, 7 * scale}; float E [3] = {-2 * scale, 0 * scale, 7 * scale }; float f [3] = {-1 * scale, 1.732 * scale, 7 * scale}; float ar [3] = {1 * scale, 1.732 * scale, -7 * scale}; // float BR [3] = {2 * scale, 0 * scale,-7 * scale }; float cr [3] = {1 * scale,-1.732 * scale,-7 * scale}; float Dr [3] = {-1 * scale,-1.732 * scale, -7 * scale}; float er [3] = {-2 * scale, 0 * scale,-7 * scale}; float FR [3] = {-1 * scale, 1.732 * scale,-7 * scale}; // 4 Four tails, float WY [4] [4] [3] ={{ 2 * scale, 0 * scale, 3 * scale }, {3.5 * scale, 0 * scale,-5.5 * scale}, {4 * scale, 0 * scale,-9 * scale}, {2 * scale, 0 * scale, -8 * scale }},{ {0 * scale, 1.732 * scale, 3 * scale}, {0 * scale, 3.5 * scale,-5.5 * scale }, {0 * scale, 4 * scale,-9 * scale}, {0 * scale, 1.732 * scale,-8 * scale }},{-2 * scale, 0 * scale, 3 * scale}, {-3.5 * scale, 0 * scale,-5.5 * scale}, {-4 * scale, 0 * scale, -9 * scale}, {-2 * scale, 0 * scale,-8 * SCA Le }},{ {0 * scale,-1.732 * scale, 3 * scale}, {0 * scale,-3.5 * scale,-5.5 * scale }, {0 * scale,-4 * scale,-9 * scale}, {0 * scale,-1.732 * scale,-8 * scale }}}; // 1 // glcolor3f (1, 0, 0); glbegin (gl_triangles ); // draw top cover ==============================/// glshademodel (gl_smooth ); glvertex3fv (p); glvertex3fv (a); glvertex3fv (f); glend (); glbegin (gl_triangles); glvertex3fv (p); glvertex3fv (f); glvertex3fv (E ); glend (); glbegin (gl_triangles); glvertex3fv (P); glvertex3fv (E); glvertex3fv (d); glend (); glbegin (gl_triangles); glvertex3fv (p); glvertex3fv (d); glvertex3fv (C ); glend (); glbegin (gl_triangles); glvertex3fv (p); glvertex3fv (c); glvertex3fv (B); glend (); glbegin (gl_triangles); glvertex3fv (P ); glvertex3fv (B); glvertex3fv (a); glend (); // draw a bullet body =============================================== // glcolor3f (0.5, 0.5, 0.5); glbegin (gl_quads); glvertex3fv (a); glvertex3fv (AR); glvertex3fv (FR); GLV Ertex3fv (f); glend (); glbegin (gl_quads); glvertex3fv (f); glvertex3fv (FR); glvertex3fv (ER); glvertex3fv (E); glend (); glbegin (gl_quads); glvertex3fv (E); glvertex3fv (ER); glvertex3fv (DR); glvertex3fv (d); glend (); glbegin (gl_quads); glvertex3fv (d ); glvertex3fv (DR); glvertex3fv (CR); glvertex3fv (c); glend (); glbegin (gl_quads); glvertex3fv (c); glvertex3fv (CR); glvertex3fv (BR ); glvertex3fv (B); glend (); glbegin (gl_quads); glvertex3f V (B); glvertex3fv (BR); glvertex3fv (AR); glvertex3fv (a); glend (); // glbegin (gl_polygon); glvertex3fv (AR ); glvertex3fv (BR); glvertex3fv (CR); glvertex3fv (DR); glvertex3fv (ER); glvertex3fv (FR); glend (); // draw the tail ================================================== ===/// glcolor3f (0.1, 0.5, 1.0); For (INT I = 0; I <= 3; I ++) {glbegin (gl_quads); glvertex3f (WY [I] [0] [0], WY [I] [0] [1], WY [I] [0] [2]); glvertex3f (WY [I] [1] [0], WY [I] [1] [1], WY [I] [1] [2]); glver Tex3f (WY [I] [2] [0], WY [I] [2] [1], WY [I] [2] [2]); glvertex3f (WY [I] [3] [0], WY [I] [3] [1], WY [I] [3] [2]); glend ();}} //************************************** * ***************** int drawglscene (glvoid) // start from here to draw all {glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and depth cache glloadidentity (); // reset the current model observation matrix float POS [3]; // object position float color [3]; // object color POS [1] =-0.1f; pos [2] =-3.0f; If (framespeed <= (fspeed/2) {pos [0] =-0.02f; Color [0] = 1.0f; color [1] = 0.0f; color [2] = 0.0f;} else {pos [0] = 0.02f; color [0] = 0.0f; color [1] = 0.0f; color [2] = 1.0f;} glpushmatrix (); gltranslatef (Pos [0], POS [1], POS [2]); glcolor3f (color [0], color [1], color [2]); glrotatef (rot, 1, 1, 1 ); // degree x, y, z/* glbegin (gl_triangles); glvertex3f (0.0f, 1.0f, 0.0f); then (-0.5f, 0.0f, 0.0f); glvertex3f (0.5f, 0.0f, 0.0f); glend (); */drawmissile (); glpopmatrix (); framespeed --; // frame minus one if (Framespeed <0) framespeed = fspeed; rot + = rspeed; If (Rot> = 360.0f) rot = 0.0f; return true; // All OK} // This Code only calls glvoid killglwindow (glvoid) before exiting the program // normal destruction window {If (fullscreen) // are we in full screen mode? {Changedisplaysettings (null, 0); // Yes, switch back to the desktop showcursor (true); // display the mouse pointer} If (HRC) // do we have a coloring description table? {If (! Wglmakecurrent (null, null) // can we release the DC and RC description tables? {MessageBox (null, "Release of DC and RC failed.", "shutdown error", mb_ OK | mb_iconinformation);} If (! Wgldeletecontext (HRC) // can we delete RC? {MessageBox (null, "Release rendering context failed. "," shutdown error ", mb_ OK | mb_iconinformation);} HRC = NULL; // set RC to null} If (HDC &&! Releasedc (hwnd, HDC) // can we release DC? {MessageBox (null, "release device context failed. "," shutdown error ", mb_ OK | mb_iconinformation); HDC = NULL; // set HDC to null} If (hwnd &&! Destroywindow (hwnd) // can I destroy the window? {MessageBox (null, "cocould not release hwnd.", "shutdown error", mb_ OK | mb_iconinformation); hwnd = NULL; // set hwnd to null} If (! Unregisterclass ("OpenGL", hinstance) // can I deregister a class? {MessageBox (null, "cocould not unregister class. "," shutdown error ", mb_ OK | mb_iconinformation); hinstance = NULL; // set hinstance to null}/* This Code creates our OpenGL window. parameters are: ** title-title to appear at the top of the window ** width-width of the GL window or fullscreen mode ** height-height of the GL window or fullscreen mode ** bits -Number of BITs to use for color (8/16/24/32) ** fullsc Reenflag-use fullscreen mode (true) or invalid wed mode (false) This process returns a Boolean variable (true or false ). It also has five parameters: the title bar of the window, the width of the window, the height of the window, the number of colors (16/24/32), and the full screen logo (true-Full Screen mode, false-window mode ). The returned Boolean value indicates whether the window is successfully created. */Bool createglwindow (char * Title, int width, int height, int bits, bool fullscreenflag) {gluintpixelformat; // Save the matching result wndclasswc; // dworddwexstyle; // extended window style dworddwstyle; // window style rectwindowrect; // obtain the coordinate value windowrect in the upper left corner and lower right corner of the rectangle. left = (long) 0; // set left to 0windowrect. right = (long) width; // set right to the required width windowrect. top = (long) 0; // set top to 0windowrect. bottom = (long) height; // set bottom to the required height. fullscreen = fullscr Eenflag; // set the global full screen flag hinstance = getmodulehandle (null); // obtain the WC of the instance in our window. style = cs_hredraw | cs_vredraw | cs_owndc; // redraw when moving and obtain dcwc for the window. lpfnwndproc = (wndproc) wndproc; // wndproc processes the message WC. cbclsextra = 0; // no extra window data WC. cbwndextra = 0; // no extra window data WC. hinstance = hinstance; // set the WC of the instance. hicon = loadicon (null, idi_winlogo); // load the default icon WC. hcursor = loadcursor (null, idc_arrow); // load the WC pointer. hbrbackground = NULL; // GL does not require background WC. lpszmenun Ame = NULL; // menu WC. lpszclassname = "OpenGL" is not required; // set the class name if (! Registerclass (& WC) // try to register the window class {MessageBox (null, "failed to register the window class. "," error ", mb_ OK | mb_iconexclamation); Return false; // exit and return false} If (fullscreen) // do you want to try full screen mode? {Devmode dmscreensettings; // device mode memset (& dmscreensettings, 0, sizeof (dmscreensettings); // ensure that the memory is allocated dmscreensettings. dmsize = sizeof (dmscreensettings); // the size of the devmode structure dmscreensettings. dmpelswidth = width; // The selected screen width dmscreensettings. dmpelsheight = height; // specifies the screen height dmscreensettings. dmbitsperpel = bits; // The color depth dmscreensettings selected for each pixel. dmfields = dm_bitsperpel | dm_pelswidth | dm_pelsheight; // set the display mode and return the result. Note: the status bar is removed from cds_fullscreen. If (changedisplaysettings (& dmscreensettings, cds_fullscreen )! = Disp_change_successful) {// If the mode fails, two options are provided: exit or run in the window. If (MessageBox (null, "the requested fullscreen mode is not supported by \ Nyour video card. Use protected wed mode instead? "," Kingwise Gl ", mb_yesno | mb_iconexclamation) = idyes) {fullscreen = false; // select the window mode (fullscreen = false )} else {// pop up a message box lew.user know the program is closing. messageBox (null, "program will now close. "," error ", mb_ OK | mb_iconstop); Return false; // return false }}if (fullscreen) // is it still in full screen mode? {Dwexstyle = ws_ex_appwindow; // extended form style dwstyle = ws_popup; // form style showcursor (false); // hide the mouse pointer} else {dwexstyle = ws_ex_appwindow | ws_ex_windowedge; // extended form style dwstyle = ws_overlappedwindow; // form style} adjustwindowrectex (& windowrect, dwstyle, false, dwexstyle ); // adjust the size of the window to meet the actual requirements. // create the required WIF (! (Hwnd = createjavaswex (dwexstyle, // extended form style "OpenGL", // class name title, // window title dwstyle | // defined window stylews_clipsiblings | // required form style attributes ws_clipchildren, // required form style attributes 0, 0, // window position windowrect. right-WindowRect.left, // calculate the adjusted window width windowrect. bottom-WindowRect.top, // calculate the adjusted window height null, // No parent window null, // no menu hinstance, // instance null ))) // do not pass any stuff to wm_create {killglwindow (); // reset the MessageBox (null, "window creation error. "," error ", MB _ OK | mb_iconexclamation); Return false; // return false} staticpixelformatdescriptor PFD = // PFD tell the window what we want (sizeof (pixelformatdescriptor ), // appeal format descriptor size 1, // version pfd_draw_to_window | // The format must support window pfd_support_opengl | // The format must support openglpfd_doublebuffer, and // The Double Buffer pfd_type_rgba must be supported, // apply for the rgba format bits, // select the color depth 0, 0, 0, 0, 0, 0, // ignore the color bit 0, // No Alpha cache 0, // ignore shift bit0, // No aggregation cache 0, 0, 0, 0, // ignore the aggregation bit 16, // 16-bit Z-Cache (deep cache) 0, // No template cache 0, // no secondary cache pfd_main_plane, // primary Drawing Layer 0, // retain 0, 0, 0 // ignore layer mask}; If (! (HDC = getdc (hwnd) // get the device description table? {Killglwindow (); // reset the MessageBox (null, "can't create a GL device context. "," error ", mb_ OK | mb_iconexclamation); Return false; // return false} If (! (Pixelformat = choosepixelformat (HDC, & PFD) // does Windows find the corresponding pixel format? {Killglwindow (); // reset the displaymessagebox (null, "can't find a suitable pixelformat. "," error ", mb_ OK | mb_iconexclamation); Return false; // return false} If (! Setpixelformat (HDC, pixelformat, & PFD) // can I set the pixel format? {Killglwindow (); // reset the displaymessagebox (null, "can't set the pixelformat. "," error ", mb_ OK | mb_iconexclamation); Return false; // return false} If (! (HRC = wglcreatecontext (HDC) // can the colored description table be obtained? {Killglwindow (); // reset the displaymessagebox (null, "can't create a GL rendering context. "," error ", mb_ OK | mb_iconexclamation); Return false; // return false} If (! Wglmakecurrent (HDC, HRC) // try to activate the coloring description table {killglwindow (); // reset the displaymessagebox (null, "can't activate the GL rendering context. "," error ", mb_ OK | mb_iconexclamation); Return false; // return false} showwindow (hwnd, sw_show); // display window setforegroundwindow (hwnd ); // slightly higher priority setfocus (hwnd); // sets the keyboard focus to the current window resizeglscene (width, height); // sets the perspective GL screen if (! Initgl () // initialize the newly created GL window {killglwindow (); // reset the displaymessagebox (null, "initialization failed. "," error ", mb_ OK | mb_iconexclamation); Return false; // return false} return true; // success} lresult callback wndproc (hwndhwnd, // window handle uintumsg, // window message wparamwparam, // additional message content lparamlparam) // additional message content {Switch (umsg) // check Windows message {Case wm_activate: // monitoring window activation message {If (! Hiword (wparam) // check the minimal state {active = true; // program is active} else {active = false; // program is no longer active} return 0; // return to the message loop} case wm_syscommand: // interrupt the system command intercept system commands {Switch (wparam) // check the system call check system CILS {Case SC _screensave: // is screensaver running? Case SC _monitorpower: // is the monitor going to power-saving mode? Return 0; // prevent occurrence} break; // exit} case wm_close: // receive the close message? {Postquitmessage (0); // return 0 when an Exit message is sent; // jump back} case wm_keydown: // is there a key pressed? {Keys [wparam] = true; // If yes, set it to truereturn 0; // jump back} case wm_keyup: // is there a key to open it? {Keys [wparam] = false; // If so, mark it as falsereturn 0; // jump back} case wm_size: // resize the OpenGL window {resizeglscene (loword (lparam), hiword (lparam); // loword = width, hiword = heightreturn 0; // jump back }}// send all unprocessed messages to defwindowproc (hwnd, umsg, wparam, lparam);} int winapi winmain (hinstancehinstance, // instance hinstancehprevinstance, // previous instance lpstrlpcmdline, // command line parameter intncmdshow) // window display Status {msgmsg; // windowsx message structure booldone = false; // The bool variable used to exit the loop // prompt the user to select the running mode if (MessageBox (null, "wocould you like to run in fullscreen mode? "," Start fullscreen? ", Mb_yesno | mb_iconquestion) = IDNO) {fullscreen = false; // define wed mode} // create an OpenGL window *****************************!! If (! Createglwindow ("kingwise's OpenGL framework", 640,480, 16, fullscreen) {return 0; // quit if window was not created} while (! Done) // keep the loop until done = true {If (peekmessage (& MSG, null, pm_remove) // is there a message waiting? {If (msg. Message = wm_quit) // receives the Exit message? {Done = true; // if so done = true} else // No, processing window message {translatemessage (& MSG); // translation message dispatchmessage (& MSG ); // send message} else // if there are no messages {// draw scenario. Monitor the ESC key and Exit message from drawglscene () if (active) // program active? {If (Keys [vk_escape]) // was ESC pressed? {Done = true; // ESC signalled a quit} else // when not exiting, refresh the screen {drawglscene (); // draw the swapbuffers (HDC) scenario ); // exchange cache (Double Cache)} If (Keys [vk_f1]) // is the F1 key pressed? {Keys [vk_f1] = false; // If yes, set the value in the corresponding key array to falsekillglwindow (); // destroy the current window fullscreen =! Fullscreen; // switch to full screen/window mode // recreate the OpenGL window if (! Createglwindow ("kingwise's OpenGL framework", 640,480, 16, fullscreen) {return 0; // if the window cannot be created, program exit }}}// close the program killglwindow (); // destroy the window return (MSG. wparam); // exit the program}

Compile, found 19 errors, Internet search, found that someone has encountered such a problem, http://www.lampthneo.com/error-c2664-messageboxw-const-char-lpcwstr.html estimates this person is not my alumni, the solution is to change the character set, in addition, switch the sub-system options below the linker.

Then re-compile and run the program. After the black screen, you can see the red and blue arrows. I feel that the effect of viewing in win7 is very poor. Microsoft really liked the black OpenGL.

In the end, I have to say that it is not a matter of writing what people have written. I have to write it myself. Teacher Li said that plagiarism is acceptable, but I must understand it. Let's take a look.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.