I took two classes yesterday and handed in my VR homework. I found that OpenGL is still very powerful. Basically I want to write it in drawglscene. Because it needs to be different from the example given by the teacher, he made the red-blue missile into a triangle with texture maps, so he learned more.
Operation instructions:
Run vr_1.exe in double-hitting mode,
After selecting whether to enable full screen. (If full screen is selected, ESC exits)
You can see two triangular images that are displayed alternately in space.
The rotation speed can be controlled up and down by the direction key (after testing, it seems that if the duration is too long, it will also produce the opposite effect, which is not related to up and down overflow)
As shown in the following figure: QQ seems to be able to only capture static images. The two images are displayed and rotated alternately.
The Code is as follows:
// VR First Job // group name: ignorant us // # 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> // standard input header file for reading textures # 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 hinstancehinstance; // Save the program's instance boolkeys [256]; // The array boolactive = true for keyboard routines; // The Window activity flag, the default value is trueboolfullscreen = true; // The full screen flag is set to lresultcallback wndproc (hwnd, uint, wparam, lparam) by default; // wndproc defines glintfspeed = 5; // The speed of switching frame rate. The greater the speed, the slower the glintframespeed = fspeed; glfloatrspeed = 2.5f; // The higher the rotation speed, the faster the glfloatrot; // you can set the rotation gluint filter; // filter type gluint texture [2]; // sets the storage space of the two textures, which is displayed alternately. // loads the bitmap image aux_rgbimagerec * loadbmp (char * Filename) {file * file = NULL; // file handle if (! Filename) // make sure the file name is provided. {Return NULL; // if not provided, return NULL} file = fopen (filename, "R"); // try to open the file if (File) // does the file exist? {Fclose (File); // close the handle return auxdibimageload (filename); // load the bitmap and return the pointer} return NULL; // If loading fails, return NULL} // load the bitmap (call the code above) and convert it to the texture int loadgltextures () {int status = false; // Status Indicator aux_rgbimagerec * textureimage [2]; // create a texture bucket memset (textureimage, 0, sizeof (void *) * 2); // set the pointer to null // load bitmap 1 to check whether there are any errors, exit if the bitmap is not found. If (textureimage [0] = loadbmp ("Data/1.bmp") {status = true; // set status to trueglgentextures (1, & texture [0]); // create a texture glbindtexture (gl_texture_2d, texture [0]); // use the typical texture gltexparameteri (texture, dimensions, gl_nearest) generated from the bitmap data; gltexparameteri (gl_texture_2d, dimensions, gl_nearest); glteximage2d (gl_texture_2d, 0, 3, textureimage [0]-> sizex, textureimage [0]-> sizey, 0, gl_rgb, gl_un Signed_byte, textureimage [0]-> data);} If (textureimage [0]) // whether the texture exists {If (textureimage [0]-> data) // check whether the texture image exists {free (textureimage [0]-> data); // release the memory occupied by the texture image} Free (textureimage [0]); // release the image structure} // load bitmap 2 to check whether there are any errors. If the bitmap is not found, exit. If (textureimage [1] = loadbmp ("Data/2.bmp") {status = true; // set status to trueglgentextures (1, & texture [1]); // create a texture glbindtexture (gl_texture_2d, texture [1]); // use the typical texture gltexparameteri (texture, dimensions, gl_nearest) generated from the bitmap data; gltexparameteri (gl_texture_2d, dimensions, gl_nearest); glteximage2d (gl_texture_2d, 0, 3, textureimage [1]-> sizex, textureimage [1]-> sizey, 0, gl_rgb, gl_un Signed_byte, textureimage [1]-> data);} If (textureimage [1]) // whether the texture exists {If (textureimage [1]-> data) // check whether the texture image exists {free (textureimage [1]-> data); // release the memory occupied by the texture image} Free (textureimage [1]); // release image structure} return status;} // reset and initialize the GL window size glvoid resizeglscene (glsizei width, glsizei height) {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 GL Loadidentity (); // reset the projection matrix gluperspective (45.0f, (glfloat) width/(glfloat) height, 0.1f, 100366f); // calculate the display ratio of the window glmatrixmode (gl_modelview ); // select the model observation matrix glloadidentity (); // reset the model observation matrix} // start to initialize OpenGL and set int initgl (glvoid) {If (! Loadgltextures () // call the texture loading subroutine {return false; // if not loaded, return false} gl_texture_2d; // enable texture ing glshademodel (gl_smooth ); // enable shadow smooth glclearcolor (0.0f, 0.0f, 0.0f, 0.5f); // set the black background glcleardepth (1.0f); // set the deep cache glable (gl_depth_test ); // enable gldepthfunc (gl_lequal) for the deep test; // glhint (gl_perspective_correction_hint, gl_nicest) for the type of the deep test; // true fine-grained perspective correction return true; // initialize OK} // use a texture to draw a triangle void drawtriangles (glint number) {GLB Indtexture (gradient, texture [number]); // select the texture glbegin (gl_triangles); // draw the triangle gltexcoord2f (0.0f, 0.0f); glvertex3f (0.0f, 1.0f, 0.0f ); // The bottom left gltexcoord2f (1.0f, 0.0f) of the texture and quadrilateral; glvertex3f (-1.0f,-1.0f, 0.0f); // the bottom right gltexcoord2f (1.0f, 1.0f) of the texture and quadrilateral ); glvertex3f (1.0f,-1.0f, 0.0f); // glend () on the right of the texture and quadrilateral; // draw the end of the triangle} // draw the function, all is to modify this function int drawglscene (glvoid) {glclear (gl_color_buffer_bit | gl_depth_buffer _ Bit); // clear the screen and depth cache glloadidentity (); // reset the current model observation matrix gltranslatef (0.0f, 0.0f,-2.0f ); // move the glfloat POs to the screen [3]; // The position glint Number of the object; POS [1] =-0.1f; POS [2] =-3.0f; if (framespeed <= (fspeed/2) {pos [0] =-0.02f; number = 0;} else {pos [0] = 0.02f; number = 1 ;} glpushmatrix (); // This function must be used; otherwise, the newly drawn graph will produce an offset gltranslatef (Pos [0], POS [1], POS [2]); glrotatef (rot, 1.0f, 0.0f, 0.0f); drawtriangles (number); glpopmatrix (); framespeed --; // frames 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) {If (fullscreen) before exiting the program. // 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} // create a window function bool createglwindow (char * Title, int width, int height, int bits, bool fullscreenflag) {gluintpixelformat; // save wndclasswc; // dworddwexstyle of the window class structure; // extend the window style dworddwstyle; // rectwindowrect of the window style; // 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 = fullscreenflag; // set the global full screen flag hinstance = getmodulehandle (null ); // obtain the WC 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. h Icon = 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. lpszmenuname = NULL; // The Menu WC is not required. lpszclassname = "OpenGL"; // 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. 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 ;}} 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 if (! (Hwnd = createjavaswex (dwexstyle, // extended form style "OpenGL", // class name "ignorant US-VR alternate ", // 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. "," er Ror ", mb_ OK | mb_iconexclamation); Return false; // return false} // describe the pixel format staticpixelformatdescriptor PFD = {sizeof (pixelformatdescriptor), // The appeal format descriptor size 1, // version pfd_draw_to_window | // The format must support the window pfd_support_opengl | // The format must support openglpfd_doublebuffer, and // The Double Buffer pfd_type_rgba. // 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, // master 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;} 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} // process all messages in the window. 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 return defwindowproc (hwnd, umsg, wparam, lparam);} // The Int winapi winmain (hinstancehinstance, // instance hinstancehprevinstance, // The previous instance lpstrlpcmdline, // command line parameter intnc Mdshow) // window display status {msgmsg; // windowsx message structure booldone = false; // The bool variable used to exit the loop // enables the user to select whether to check whether full screen if (MessageBox (null, "enable full screen mode? "," Window mode selection? ", Mb_yesno | mb_iconquestion) = IDNO) {fullscreen = false;} // create an OpenGL window if (! Createglwindow ("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 ); // switch cache (dual cache) if (Keys [vk_up]) // is the UP direction key pressed? {Rspeed-= 0.01f; // If yes, reduce xspeed} If (Keys [vk_down]) // is the direction key down? {Rspeed + = 0.01f; // If yes, add xspeed }}if (Keys [vk_f1]) // has the F1 key been 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 ("OpenGL framework", 640,480, 16, fullscreen) {return 0; // if the window cannot be created, exit the program }}} killglwindow (); // destroy window return (MSG. wparam); // exit the program}
Finally, I hope Miss Li Fengxia will not see it. Otherwise, we copied this article. Isn't it funny?