Monday, April 22, 2013

OpenGl Transition Effects with the Stencil Buffer


Swapping between objects or even entire 3D scenes is something that graphics programmers will always have to deal with.  Deciding exactly how to accomplish such a task leaves room for innovation from not only a visual standpoint but a technical one as well.  Screen fades, wipes, and dissolve effects are all common forms of transitions used in graphical programming.  For this project I chose to recreate a dissolve effect and wipe transition in C++ OpenGL using the Stencil Buffer to control it.
                
The Stencil Buffer is an interesting tool that most modern graphics hardware takes advantage of, it opens the door to a wide variety of effects like the ones I chose to create as well as much more. The Stencil buffer acts very similarly like an actual stencil in real world would.  OpenGL allows us to directly interact with the Stencil Buffer manipulating how each pixel show within the effect area will be affected by it.  What this means for us is we can literally draw out a patter to be displayed through the Stencil Buffer. But how do we accomplish such a task? It is not as hard as you might think, but there are definitely a few things to make sure are turned on before you start to battle with the Stencil Buffer.

By default the Stencil Buffer is not being used so in order to start manipulating it need to enable it, by simply calling glEnable(GL_STENCIL_TEST);  now that we are using the Stencil Buffer we need to make sure to clear it out each frame otherwise it will overwrite itself.  By calling glClear(GL_STENCIL_BUFFER_BIT); we also want to turn off depth and color mask otherwise the Stencil buffer will interfere with those values when and in this case we don’t want that to happen so similarly disable them by passing in GL_FALSE to both. 

Now this is where the fun starts, We want to set up the pattern we want to be seen through the stencil buffer, so if we were doing a transition the first frame would basically be empty, but as the transition progressed more and more of the screens design would be drawn until the other object is fully visible and the first one is gone.  We do this by setting the stencil buffer to always fail everywhere we draw a pixel.  We then draw whatever pattern we want displayed through the stencil buffer.  An example would be in a shooter, when a bullet it’s a wall they don’t replace the entire texture on the building or even the model with one that has a bullet hole, no they use the stencil buffer and choose a few pixels on the wall to reveal the bullet hole texture through it.  We can also use this to draw interesting shapes and patters like spirals or zig zags.  Once we are satisfied with the pattern we drawn either by pixel using something like glDrawPixels() or possibly a whole object (to make a star shape or something drawing as you would with any other model) we then turn off writing to the stencil buffer.  We now set the stencil buffer to pass wherever it did not previously fail as well as turning back on our depth and color masks so our display objects are drawn correctly.  We now draw our first object, the one we want to be displayed everywhere the stencil pattern isn’t.  Once that image has been drawn we set the stencil buffer to only draw where the it previously failed meaning the second object will only appear where the stencil pattern was drawn.



 The image below is a perfect example of what we are trying to do.  The 1’s are where the stencil buffer was set to fail and the blank spaces is where it passed.  When we draw the objects over one another we can see based on the value different parts of the red and green rectangles are revealed. 


Congratulations you are now using the stencil buffer from here, each frame you can change the stencil buffer pattern to allow more and more of the second object to be seen or hidden based on what you are trying to accomplish. Those are the basics of how to use the stencil buffer to switch between, see through, and draw over objects in specific locations.  When creating my two effects (wipe and dissolve) I used the same basic technique but handled drawing to my stencil buffer differently.


For the wipe effect I picked a starting location in this specific example I used bottom left and slowly drew a larger and larger square expanding outwards until the stencil pattern completely covered the screen revealing the second object underneath.  I could then reverse the process to re reveal the first object.
The dissolve effect was a little bit more difficult, I had to gain access to different parts of the screen and randomly draw pixels into the stencil buffer thus causing random bits of the object to change until finally the entire screen was covered in stencil buffer pixels and the second object is revealed.  This gives the impression that the object is dissolving into the second object as you saw chunks of it slowly switch over until the new object is visible. 

Overall this process allowed me to gain a much better understanding of how the stencil buffer is used and what types of effects can be accomplished with them like I mentioned above.  It allows us to represent other objects, color, or patterns without changing textures or swapping out whole models and makes for great for screen transitions.  The Stencil Buffer is a powerful tool which I will definitely be using more of, and I hope you will too.


References:
1.       Advanced Graphics Programming Using OpenGL By Tom McRynolds, David Blyth (Pages 196-199)

Used for basic tutorial of how to use a Stencil Buffer.

2.       http://www.opengl.org/sdk/docs/man2/xhtml/glDrawPixels.xml

How to use glDrawPixels.

3.       http://www.opengl.org/sdk/docs/man2/xhtml/glRasterPos.xml

How to use glRasterPos

4.       http://psysal.livejournal.com/67933.html

More Stencil Buffer How To.







Friday, January 11, 2013

Back to School and hit the ground running!

Ahh it feels great to not only be back at school but to be back to work!  Classes this semester look like a challenge that I can't wait to challenge.  This is my first blog post of the semester and my Intentions from this point forward I hope to do a quick overview of each class and show off what I have accomplished.  But for now since things are just starting I'll give a little overview of each class and what I have on my plate for the first week of classes!

Production II:

This will be the most creative, fun, hard, and time consuming class by far.  I've been teamed up with with 2 designers, an artist and a half with the only goal of the semester is to create a game.  We will be working in an agile production cycle most likely scrum to keep us organized and on task.  Each week  we will present to the class our current iteration and be critiqued on it so there is a lot on the line.   Our team has settled on using Unity for our project (being one of my first real attempt at making a 3D game)  and we have more ideas buzzing around in our heads then we can handle.  No spoilers yet but I'm really excited about our first project, there are a lot of seemingly hard challenges that I already have tons of Ideas on how to crack.  The most exciting part is I'll be using the techniques from AI last semester to simulate crowd behaviors, flee behaviors and chase behaviors.  It's one of the best feelings in the world to see things I learned being put to use! Expect to see a rough prototype next week!

Graphics II:

Math, math, math, and more math.  This class is going to be a struggle but one of the most beneficial ones I'm taking.  For the first half of the year I'll be working in OpenGL to create 3D graphics in C++.  My first attempt at such a thing.  The second half we will change gears and start working on DirectX  that way we will be able to experience both major ways of coding 3D graphics.  Our first assignment is to create compact disk and one other complex shape. Shouldn't be too bad just a few math equations and the computer will do the rest for me. A snapshot will be present next week. Come on Geometry don't fail me now!  

System Software Analysis and Design:  

Architecture!  Our first assignment gives the impression of a blow over class, especially since we are working in C#  however I have a hunch that this class will escalate quickly.  Our first project is to make a contact book really simple, however this class is all about following standards, good architecture,  efficient methods, and design patterns.  So this fairly simple program becomes a little more time consuming as I make sure to cross my T's and dot my I's.  Each week we will present our assignment and then the following week we will re factor them to make it better. A lot of work but by the end of the semester I'm sure my code will be looking better then ever!

Our of class Projects:

Besides class the other good news I bring is after Emailing the correct Legal Teams I've been given the green light to start on my Application!  I'm not going to share what it is just yet but I'll keep posted on any updates.  For now I'll be prototyping in Titanium, a java script based shell which promotes multi-platform production so i can hit both Android and Apple.  However if all goes well I'll end up switching from Titanium once I figure everything out!

That's all for now I gotta get to work!

See you next week!

-Ian

Monday, December 10, 2012

Game AI - Predicting Physics

     Sports, Shooters, Platformers, Adventure, Racing, Arcade, and even games like pong all has some sort of physics in them no matter how simple they may be. These Physics effect objects in the game that directly impact how AI should act in different situations.  Learning to use Physics can help make it so your AI goes from a clueless loser to a competitive challenge.  But I'm not talking about just using Physics to make a game look and feel more realistic, what I'm here to talk about is using our knowledge of in game Physics to allow our AI's to predict the correct outcome based on the world around it, this way our AI looks like it is actually making intelligent decisions.  

     Us humans make judgement based on physics all the time, for example when you are playing catch, when you  go to throw the ball you don't sit there and take the time to accurately calculate how hard to throw the ball and at what angle you should throw it, you have a general feel and understanding that you have picked up just from living in this world of physics everyday.  While the AI in our games don't the those "gut feelings" about how hard to throw a ball, they have the possibility of something I would consider much better, Cold, Hard, Math.  The AI is built on a computer and there is nothing a computer is better at then solving equations, with the correct equations at its side and precise data from the world around it, our AI's have the possibility to never miss a catch, have the perfect shot, and always dodge the projectile. Very quickly you can see just how powerful this type of information can be for an AI. (2)

     This is where the fun starts, the math behind the physics, and accuracy vs efficiency when and what trades off you should make based on the game. Predicting Physics isn't an all or nothing system,  you can tailor how much accuracy you want/need in your game, because in all honestly perfect accuracy isn't always a good as it sounds.  Imaging playing a FPS game where whenever you came into range of an enemy who had you in line of sight you got head shot.  It would make the player very angry very quickly, so while we have the math to make this possible its definitely not the best route to take.  Projectile Math the most common for Games considering to take care of any projectile whether it be a bullet, snowball, football, or something of the likes. Projectile math takes the following variables Gravity(9.81 m/s ^2 on Earth, is usually doubled in games), Angle of Launch, Initial Velocity  initial height, time, and distance traveled. with them you can find any other one if it is unknown.  Considering we control the world in which this AI is running  we should have easy access to gravity, angle, velocity,height and time.  We would normally use this to find out where our projectile is going to land. (2)

For a 2D space (http://www.iforce2d.net/b2dtut/projected-trajectory) has a great tutorial explaining the math behind the magic.  When looking to find where our object will land we use the following equation  

 p(n) = po + nv + (n^2 + n)a/2


p(n) is the position on the nth time step
n is time step
p0 is the starting position at the 0th time step
v is the starting velocity per time step
a is the acceleration due to gravity per time step


Something to keep in mind when calculating where the projectile will land is not always what you want to use.  Using the last time point in the equation is where the bullets,ball,objects are landing.  It would be very terrible in a sport game like football if the AI always went to the point where the ball would hit its feet, it would look very strange catching the ball or miss it all together.  You want to make sure in situations like this as well as shooters that the projectiles end somewhere around chest height.  Obviously this is all based on the projectile being used and isn't a constant rule.(1)

The Equation above is a basic form of the projectile trajectory equation  and is the one most used by games it is recommended for Platforms that don't have the best computing power if you still want realish looking projectiles in that game.  The more worried about reality you are the more processing intensive it becomes.  The example above leaves out a few variables that are definitely present in the real world.  I speak of Drag and Rotation.  These projectiles like projectiles in most games pretend there is no resistances around them, I speak of Air resistance.  While  this is only a slight influences especially on things like bullet projectiles if you want reality  like for a sports game you are going to have to include it and at this point the math becomes much more complicated and processing intensive.  This also goes for rotation, most if not all projectile do rotate at least slightly which will add to lift causing the projects to go higher or farther, once again on low end platforms i would recommend leaving this out as well because the more variables you add the harder the math, the harder the math the more CPU intense it is for you AI to figure out every so many frames and react to it possibly causing the game to slow.(3)

A good example with Air Resistance
 http://phet.colorado.edu/sims/projectile-motion/projectile-motion_en.html

When worrying about air resistance you take the equation that we have above and where we apply the effects of gravity we take the air resistance force and add it with the acting gravity force.  This way the total acting force is now also including Air Resistance.  An example of which is shown here (http://www.riotstories.co.uk/science/projectile-motion-ii-air-resistance-linear-in-speed/)(3)

Just as these equations can become more complicated they can also be simplified for lower end platforms or if the game just doesn't care about looking real.  Below is an example of accurate prediction of shots if there is no negative force acting upon it.  No gravity or resistances,  see how the AI leads in front of the player when there is no obstruction.  This is because the AI is calculating with a similar version of the above formula if the shot will hit the target and when.  In this example the programmer added a slight error margin which allows the AI to not always hit its target, this makes it more playable for the player not always being hit but still making it feel like a challenge.  (2)


This is the core essence and purpose of predicting physics, it allows accurate "smart" seeming AI because they look like they actually know how to aim/throw/block.  They move in a timely matter and if the error margin is small enough it will cause quite a challenge.  (2)









Works Cited
  1. "Box2D Tutorials - Projected Trajectories." Box2D Tutorials - Projected Trajectories -                                                        Iforce2d. Box 2D, 6 Nov. 2011. Web. 11 Dec. 2012.
  2. Millington, Ian, and John David Funge. Artificial Intelligence for Games. Burlington, M          MA: Morgan Kaufmann/Elsevier, 2009. Print.
  3. Sinclair, Steve. "Riotstories." Riotstories. ITest Solutions Ltd, 13 Mar. 2012. Web. 11 Dec. 2012.

Hello World!

Nothing is more fitting for a first blog post of a programmer then the two first words we see printed out in every new language.