banner



How To Do A Basic Animation In Opengl

Dirt Basic Animation With OpenGL

Hi once again! The next installment of my tutorial series on OpenGL for 2D games is a super brief, super unproblematic introduction to animation. But you should check out all my other tutorials first if you oasis't yet.

  • Merely Draw A Stupid Triangle With OpenGL
  • Making A Dopey Circumvolve Matter With OpenGL
  • Only Interpolate Already With OpenGL
  • Make A Stinking Sprite With OpenGL
  • Distorting Sprites' UV Mappings With OpenGL
  • Giddy Spiky Stars And Donuts With OpenGL
  • Rounding All The Corners With OpenGL

In fact, you won't even be able to run this code if you lot don't have the three.js library in its ain subfolder of your project binder every bit described in the showtime tutorial.

The protagonist of our tutorial volition exist this three-sided, spinny, floaty thing:

Spinny Floaty Triangle

Kind of reminds me of a actually onetime-school "educational programming language" called Logo. I wonder if anyone else remembers that.

And then Anyhow… Where'southward The Lawmaking?

Hither! It'due south here. Put it in another file in your opengl_examples folder called dirt_basic_animation.html. Ctrl + C. Ctrl + V.

                          <!DOCTYPE html>              <html              lang=              "en"              >              <head>              <title>Dirt Bones Blitheness With OpenGL</title>              <meta              charset=              "utf-8"              >              <meta              proper name=              "viewport"              content=              "width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"              >              <style>              body              {              color              :              #ffffff              ;              groundwork-color              :              #000000              ;              margin              :              0px              ;              overflow              :              hidden              ;              }              </style>              </head>              <body>              <div              id=              "container"              ></div>              <script                            src=              "js/three.js"              ></script>              <script                            src=              "js/Detector.js"              ></script>              <script>              var              webGlSupported              =              Detector              .              webgl              ;              if              (              !              webGlSupported              )              {              Detector              .              addGetWebGLMessage              ();              }              const              WHITE              =              new              THREE              .              Color              (              1.0              ,              1.0              ,              1.0              );              const              CYAN              =              new              Iii              .              Colour              (              0.0              ,              1.0              ,              ane.0              );              const              MAGENTA              =              new              THREE              .              Color              (              1.0              ,              0.0              ,              i.0              );              const              VERTEX_COLORS              =              [              WHITE              ,              CYAN              ,              MAGENTA              ];              var              container              =              undefined              ;              var              camera              =              undefined              ;              var              scene              =              undefined              ;              var              renderer              =              undefined              ;              var              clock              =              undefined              ;              var              mesh              =              undefined              ;              initializeCamera              ();              initializeScene              ();              initializeRenderer              ();              // 1              initializeClock              ();              onTick              ();              function              initializeScene              ()              {              scene              =              new              THREE              .              Scene              ();              var              geometry              =              getTriangleGeometry              ();              var              material              =              getMaterial              ();              mesh              =              new              THREE              .              Mesh              (              geometry              ,              fabric              );              mesh              .              position              .              set up              (              0.0              ,              0.0              ,              0.0              );              scene              .              add              (              mesh              );              }              role              getTriangleGeometry              ()              {              var              geometry              =              new              3              .              Geometry              ();              geometry              .              vertices              =              [              new              Three              .              Vector3              (              20.0              ,              0.0              ,              0.0              ),              new              THREE              .              Vector3              (              -              10.0              ,              ten.0              ,              0.0              ),              new              THREE              .              Vector3              (              -              10.0              ,              -              x.0              ,              0.0              ),              ];              var              normal              =              undefined              ;              var              face              =              new              THREE              .              Face3              (              0              ,              1              ,              2              ,              normal              ,              VERTEX_COLORS              );              geometry              .              faces              .              push button              (              face              );              return              geometry              ;              }              function              getMaterial              ()              {              render              new              THREE              .              MeshBasicMaterial              ({              vertexColors              :              Three              .              VertexColors              ,              side              :              3              .              DoubleSide              });              }              role              initializeCamera              ()              {              var              aspectRatio              =              window              .              innerWidth              /              window              .              innerHeight              ;              var              screenWidth              =              undefined              ;              var              screenHeight              =              undefined              ;              if              (              aspectRatio              >              1.0              )              {              screenWidth              =              320.0              *              aspectRatio              ;              screenHeight              =              320.0              ;              }              else              {              screenWidth              =              320.0              ;              screenHeight              =              320.0              /              aspectRatio              ;              }              var              nearPlane              =              one.0              ;              var              farPlane              =              grand.0              ;              camera              =              new              3              .              OrthographicCamera              (              -              0.5              *              screenWidth              ,              0.5              *              screenWidth              ,              0.5              *              screenHeight              ,              -              0.5              *              screenHeight              ,              nearPlane              ,              farPlane              );              var              distanceFromScene              =              500.0              ;              camera              .              position              .              set up              (              0.0              ,              0.0              ,              distanceFromScene              );              }              office              initializeRenderer              ()              {              renderer              =              new              Iii              .              WebGLRenderer              ();              renderer              .              setPixelRatio              (              window              .              devicePixelRatio              );              renderer              .              setSize              (              window              .              innerWidth              ,              window              .              innerHeight              );              container              =              document              .              getElementById              (              "container"              );              container              .              appendChild              (              renderer              .              domElement              );              }              function              renderScene              ()              {              renderer              .              return              (              scene              ,              camera              );              }              // 2              function              initializeClock              ()              {              clock              =              new              Iii              .              Clock              ();              }              // 4              function              updateMeshRotation              (              elapsedTime              )              {              var              radiansPerSecond              =              0.75              *              Math              .              PI              ;              var              angle              =              elapsedTime              *              radiansPerSecond              ;              mesh              .              rotation              .              fix              (              0.0              ,              0.0              ,              angle              );              }              // 5              function              updateMeshLocation              (              elapsedTime              )              {              var              amplitude              =              50.0              ;              var              frequency              =              0.55              ;              var              y              =              Math              .              sin              (              frequency              *              2.0              *              Math              .              PI              *              elapsedTime              )              *              aamplitude              ;              mesh              .              position              .              set              (              0.0              ,              y              ,              0.0              );              }              // 3              function              onTick              ()              {              var              elapsedTime              =              clock              .              getElapsedTime              ();              updateMeshRotation              (              elapsedTime              );              updateMeshLocation              (              elapsedTime              );              renderScene              ();              window              .              requestAnimationFrame              (              onTick              );              }              </script>              </body>              </html>                      

Cool. It'southward relatively short. And I'm not even going to spend fourth dimension going over stuff that we've already covered. We'll just talk near animation.

Let'south Talk Nearly Animation

Everyone knows about animation. I mean, we all watch TV and movies. Nosotros pretty much sympathize how they work. It'due south just a movie that changes over time. Around 30 times per 2d, your Idiot box screen updates the image it displays, and that's frequent enough that your brain can weave the frames together and fool itself into thinking there are actual humans on the other side who are talking to y'all. That's the basic concept behind computer animation as well, but of course with computers there are some boosted considerations.

1 of them is the frame charge per unit–the number of times per second that your screen gets updated. 30 frames per second may take been plenty for the early games… just back so the bar was much lower. With each new generation of computer hardware, the public has become accustomed to amend and ameliorate visuals. Today, annihilation less than lx FPS in a computer game is going to feel somewhat sluggish… and substantially subpar.

Fulfilling consumers' need for fluid, threescore FPS animation is rarely as like shooting fish in a barrel as flipping a switch. In earlier tutorials, we've touched on the mind-boggling mountain of calculations that get into rendering a single shape on the screen. Multiply that mount by the hundreds of shapes and sprites present in any mod 2nd game, and y'all accept quite the mountain range. Only that'south merely what's required to render a unmarried frame. That same colossal profusion of operations must be executed again and again and over again, sixty times each second, without the slightest pause. Obviously modern graphics hardware is upwards to the task, but information technology must apply all manner of optimizations to achieve such a feat. The instructions your game delivers to OpenGL must be arranged in a very item format. Otherwise the optimizations will fail, and even the about sophisticated cutting edge hardware will slow and eventually attain paralysis.

And that'south but to render the graphics. If your game logic is circuitous at all, you lot'll also need to be careful non to clog up your CPU. Information technology'due south ofttimes necessary to divide heavy calculations across several frames, and then that no individual frame takes longer than the allotted ane/60th of a 2d to process. Even when you do everything right, your game is rarely the only chore your computer is managing at one time. On an iPhone, your processor could be diverted at any time to tackle another abrasive push button notification.

To bargain with the inevitable fluctuations in resource availability, games have been forced to adopt a number of mitigation strategies. One of them is the variable frame rate. It may not ever be possible to return each frame in 0.01666667 seconds, just when a particular frame exceeds this limit, it's frequently possible to compensate somewhat past changing the amount that everything in the game moves proportionally. So if your spaceship normally moves 3.5 pixels each frame, simply one frame takes twice as long to render equally it's supposed to, y'all tin can just move it vii.0 pixels that frame instead. Of class, any drop in frame rate hurts the quality of your animation, just assuasive for a variable frame rate helps preserve some of its continuity.

So yeah… lots to consider when you lot're dealing with animation on computers. That'southward actually just the starting time. I could keep going if you'd like…

Plenty Jabbering. Become To The Code.

Fine. Let'south expect at part // 1.

            initializeCamera(); initializeScene(); initializeRenderer();  // i initializeClock(); onTick();                      

At that place's two new functions that nosotros're calling once we've ready our scene. The starting time creates a Clock object of some sort. Looks like it's implemented near the bottom.

Tick Tock

Yup. This is most insultingly uncomplicated.

            // 2 part initializeClock() {   clock = new Three.Clock(); }                      

The reason I marked this department is so that I could talk almost what a THREE.Clock does. It's just a handy utility that three.js puts out there so that y'all don't take to go on track of time yourself. Web browsers are pretty inconsistent beasts. They weren't originally designed with blitheness in mind, so they don't ever provide the same interface for tracking fourth dimension. The Clock abstracts the time implementation away so that yous can focus on writing your animation. Thank you, Clock.

Lights, Photographic camera, Activeness!

Later on initializeClock, the last part to become called is onTick. Pitiful that name isn't terribly intuitive. I'll explain in a 2d. Role // iii is down at the very, very bottom.

            // iii function onTick() {   var elapsedTime = clock.getElapsedTime();    updateMeshRotation(elapsedTime);   updateMeshLocation(elapsedTime);    renderScene();   window.requestAnimationFrame(onTick); }                      

This function is what we want to call each frame. At every tick of the blitheness. Spider web browsers are a piddling bizarre, so if you lot want to animate something, they brand you utilise this weird method… window.requestAnimationFrame. It informs the spider web browser that yous've only drawn part of an blitheness, and that the adjacent time it gets a risk to update, it should call the callback you requite it. Since nosotros're calling requestAnimationFrame from onTick, and the callback we're providing it is also onTick, we've basically created an endless cycle. From now on every time the browser is about to update, it will call onTick for us. Until the user gets bored and closes out of our page. 😛

But what well-nigh the other stuff before requestAnimationFrame? Well of form we're calling renderScene to update the image on the screen. The way that's implemented hasn't changed at all. But nosotros're likewise getting this elapsedTime value and passing it into updateMeshRotation and updateMeshLocation. Each frame nosotros move the mesh slightly, and and so we return it. Then echo. To infinity.

Spinny

In pretty much all 2D game engines (as well as in three.js), there are layer or node objects of some sort. In iii.js, they have Object3D, of which Mesh is a subclass. These are objects that can be rendered, and they also accept various backdrop that can be manipulated. Such backdrop generally include spatial characteristics like rotation, location, and scale, equally well as chromatic traits such as opacity and color. Most of the animations in a typical game will be produced but past altering combinations of these properties over time. Sure, information technology'southward possible to pay a existent creative person to pre-draw your animations so load them from a file, just fifty-fifty preloaded animations usually require some degree of massaging with property manipulation to make them match the feel of a game.

This is where an engine's activeness system typically comes in. An activity arrangement is a standardized fashion of defining transitions of property values over time that allows for sequencing and composition, among other gizmos. Deportment greatly simplify programmatic animation, and it's usually stupid non to use them. It should be noted that three.js has a pretty sophisticated blitheness system, and if you're planning on developing whatsoever sort of projection with the library, y'all should definitely take advantage of it. But today I want to focus on the subatomic level of animation, to make sure we're appreciating the near fundamental machinery of game development.

Our tricolored triangle is spins endlessly. How is this accomplished? Every frame we call this:

            // 4 function updateMeshRotation(elapsedTime) {   var radiansPerSecond = 0.75 * Math.PI;    var angle = elapsedTime * radiansPerSecond;    mesh.rotation.set(0.0, 0.0, angle); }                      

It's actually very unproblematic. elapsedTime is a value representing the verbal number of seconds since our clock started running. Each tick, it increases by almost 0.01666667. We multiply elapsedTime by a constant to derive the angle to which our mesh should be rotated. In this way, nosotros've bound our triangle's rotation to the very passage of time. Purple.

No uncertainty you've noticed at that place are two 0.0s that we're passing into the rotation vector. That's because three.js is designed for 3D models, which demand to be able to rotate on all 3 axes. In our 2D environment, we're merely concerned with rotation around the z centrality. But feel gratis to try rotating on the other axes, but to see what happens.

Do The Wave

I figured we should brand our triangle bladder upwards and down while we're at it. Why not? Here's part // 5.

            // five function updateMeshLocation(elapsedTime) {   var aamplitude = 50.0;   var frequency = 0.55;    var y = Math.sin(frequency * two.0 * Math.PI * elapsedTime) * amplitude;    mesh.position.gear up(0.0, y, 0.0); }                      

It's pretty similar. Nosotros're defining a formula to calculate a y coordinate. The formula happens to follow a sine wave pattern, which turns out to exist 1 of the most versatile functions in blitheness, and indeed in calculator graphics as a whole.

Nothing To It

Meet? This kind of blitheness is sublimely simple. We're merely linking rotation and location to fourth dimension with a formula. At present that yous get it, you should definitely effort to practise some fancier animations on your own. Can you make the triangle fly in a circle? How about making its rotation wobble back and forth? You lot could even mess with its scale property. The globe is your burrito.

Thy Downloadables, Your Highness

Click here to run the finished product!

And hither'south a link to a downloadable version of the source code: dirt_basic_animation.html. Note that you lot'll demand to run information technology from inside the folder structure that we set up up in the showtime tutorial.

Next Tutorial

Windows 95 Tiling Wallpaper With OpenGL

Source: http://oldsite.dartographer.com/dirt-basic-animation-with-opengl/

Posted by: embreetheatelf.blogspot.com

0 Response to "How To Do A Basic Animation In Opengl"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel