Lets learn pixel shaders - Chap 2 - Basics

Posted by Quakeboy Comments

Important - To move forward open another tab in your browser and open the URL http://www.iquilezles.org/apps/shadertoy/, I will refer to this tab simply as 'Shader Toy'

Chapter 2 - Basics of pixel shader with 'Shader toy'

Let us write some code, switch to the shader toy and notice that the "Deform" shader is already loaded and is running by default. On the right side bottom you see the source code, and on the left is the rendered effect. Take some time to notice the various details/controls shown to you like

1) fps, time, play/pause button, restart button on top of render area (place where the effect is played).

2) Dimensions panel - Width and height of the render area on top it. You can change it if you want.

3) The textures passed to the pixel shader are given right to the width/height box. You can pass up to 4 textures and use them in the pixel shader. The texture used in deform shader is given in the box 'unit 0'. Copy paste that URL path in a new tab and check the texture for yourself, or you can just click this link.

4) Also you can check the other shaders by going through presets drop down list. Don't forget to click 'Load'. There are some cool effects which you have no idea how to create, but at the end of this series you will learn to make most of them.

5) Most important thing we need to notice is the compile button. It is given above the source code area along with 'help' button.

n00b note - Every time we write new code, we need to press the compile button to see new source code taking effect.





6) Now let us write some code. We will begin with a blank program. Copy paste the following code in the code window.


#ifdef GL_ES
precision highp float;
#endif
uniform float time;
uniform vec2 resolution;
uniform vec4 mouse;
uniform sampler2D tex0;
uniform sampler2D tex1;
void main(void)
{
}

--- Now hit the compile button and see the output on the left side ---

7) Beautiful isn't it !
It will be a blank screen. Basically for now we have not written any code. Being a programmer I'm sure  you can notice that main function is empty. If you don't know what main() function is, then stop reading right away, don't waste your time. Grab a good C or C++ book or tutorial.

8) Now let us write our first line. If you recap I said - "pixel shaders are programs that are run for every pixel on the screen and they can do one thing, set the pixel color". So let us do that now. Inside the void main function block write the following line
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
gl_FragColor is a built-in variable through which we can write the pixel color. vec4 is a custom data type just like int, float, bool etc. But vec4 is a vector of 4 float numbers. The numbers are the colors - R, G, B and A. So here we say R, G, B are zero and Alpha is one which works out to black color. Hit compile button and make sure it is not paused on the left side, you will see the screen turning black. Now try replacing that line with the following.
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
Click compile and run again, you get the idea? As the same program is running for each pixel on the rendering area, and what we are doing is just setting it to white, the whole screen looks white.

Now how do we make things interesting? Wait for next chapter :)

Lets learn pixel shaders! - New Series - Chapter 1

Posted by Quakeboy Comments

This is the beginning of a series of tutorials, about pixel shaders (aka fragment shaders) with WebGL. I will start by assuming you are someone who has "absolutely no experience in graphics programming", but you should know what pixels are, what their components are. And optionally if you have basic 3d texturing knowledge, it will help a lot, but again not compulsory.

Without more blah, blah, let us start learning fellow programmers!

without which I would have never even started this! All our tutorials will be explain with reference to that web app. You will need one of the latest browser to run it.

Chapter 1 - Pre-requisite (for the n00b graphics programmer)

Pixels (skip if you know)

Everything you see on your monitor is made of pixels. If you zoom into your computer monitor, basically it is made of basic blocks (usually square shaped) called pixels. But as they are very small and closely packed, you are not able to notice them. Every monitor doesn't have the same number of pixels.

Resolution (skip if you know)

It is (number of pixels lengthwise) x (number of pixels widthwise). For eg. iPhone 3GS has a resolution of 320 x 480 when you hold the phone in portrait mode. iPhone 4 / 4S has 960 x 640 as their resolution. A full HD (High Definition) monitor has 1920 x 1080 pixels. More the resolution, more is the image detail/clarity. Will you be able to show more details with a 10x10 grid of dots or a 100x100 grid of dots? Think about it.

Pixel components (skip if you know)

Each pixel can do only one thing - "They can display a color". A pixel has three basic components - R, G and B. They are the primary colors, and combining them in various ways, we can make thousands (or millions, in latest monitors) of other colors. Usually each color is represented with one byte (8 bits, so value range is 0 - 255). So put together for each pixel you will have total of 3 x 8 bits = 24 bits or 3 bytes of color information.

For eg.
black (R = 0, G = 0, B = 0)
white (R = 255, G = 255, B = 255)

Introducing Alpha (skip if you know)

Apart from R, G and B, there is another component for a pixel, its called Alpha. Alpha = Transparency. Often pixels can be overwritten on top of each other, when that happens they are "mixed" according to their alpha levels. So R, G, B and A are the components of a pixel, which takes 32 bits per pixel. Understand there are more ways to represent a pixel, its not always 32 bits, but it is the most advanced pixel format, and enables an individual pixel to represent the max number of colours.

So, what are pixel shaders ?

Now that you know what pixels are, pixel shaders are nothing but programs, very similar to a C program. Next paragraph is very important.
A pixel shader is run for every pixel, before getting displayed on the screen. So let us say we have 1920 x 1080 pixels for a full screen game, which is a total of 2073600 pixels, which means a pixel shader is run that many times! But that is okay because they are all run on the GPU, instead of the CPU (Graphics processing unit, a special hardware dedicated to graphics, present nowadays on almost all computers).


Now that we know the basics, let us get started with
1) what can be done in a pixel shader,
2) how it can be done and
3) what information we have access to in the next chapter !

Any doubts are welcome in the comments section.

Common mistakes done by every (game) programmer

Posted by Quakeboy Comments

This is a re-blog of a recent article in programming section of Gamasutra. It was titled "Programming Sins - Common Errors From Down In The Trenches". I would like to summarize it, but I definitely recommend you to read and start adding some points to your coding standards document.

Here is what I thought are the main points from that article

Coding standards for Games.

1. Always code for NULL cases when loading resources and using it. Expect NULL data where ever possible and code.
2. When you write functions DO NOT use ambiguous variable names, function names and parameter names.
3. Do NOT use abbreviations when naming variables/ functions/ parameters.
4. Validate pointers at least once before accessing them.
5. Validate data (clip them to valid ranges etc.)
6. Have a default state (like a default texture)
7. Consider Memory fragmentation when planning your code.
8. AVOID copy pasting code, try to always make them into functions when reusing.

You should definitely read the original article here though, as it convinces and gives excellent real world examples of why you should follow the above points.

XCode 4 icon

Posted by Quakeboy Comments


Like me if you have installed both XCode3 and XCode4, and you wanna have both in the dock, you will find having the same icon is confusing, so here is a custom icon which you can use.

Right click XCode.app and click "Show Package Contents" and after that navigate to "Contents/Resources" and replace the existing Xcode.icns with this one.

Download Custom Xcode.icns file.

Download Android SDK standalone for offline installation

Posted by Quakeboy Comments

Update :- Ice Cream Sandwich links (4.0) updated - thanks Vikram!




How to install Android SDK without internet connection


I searched all over the internet and found no posts like this, hence I'm making one hoping it would be helpful for a lot of people.

The magic URL used to be - http://dl-ssl.google.com/android/repository/repository.xml (Outdated)
That is the XML file from which the URL for downloading the SDK packages are obtained.

Update :- The previous URL is now invalid, the new URL is given below
https://dl-ssl.google.com/android/repository/repository-5.xml


For e.g. if you want to download Android SDK for version 4.0.3 for all platforms, you could look up that XML file. You will find a block under tag SDK 4.0.3 like this

<sdk:archive arch="any" os="any">

<sdk:size>44758833</sdk:size>
<sdk:checksum type="sha1">f2aa75133c29916b0b0b984974c2d5e46cb09e9c</sdk:checksum>
<sdk:url>android-15_r01.zip</sdk:url>
</sdk:archive>



So the URL will be is http://dl-ssl.google.com/android/repository/android-15_r01.zip



If all the above sounds too complex

You can directly download from the below links. If this page does turn out to be useful, then I might update it regularly. Do leave a comment to let me know if it has helped you.


Android SDK Platform 4.0.3 (*** NEW ***)
All Platforms

Android SDK Platform 4.0
All Platforms

Android SDK Platform 3.2 revision 1
All Platforms

Android SDK Platform 3.1 revision 3
All Platforms

Android SDK Platform 3.0, revision 2
All Platforms

Android SDK Platform 2.3.3_r2
All Platforms

Android SDK Platform 2.3.1_r2
All Platforms

Android SDK Platform 2.2_r3
All Platforms

--- OLDER LINKS (Still working) ---

Android SDK Platform Honeycomb Preview, revision 1
All Platforms

Android SDK Platform 2.3.3_r1 (Offline complete download) - Approx 80 MB
All Platforms

Android SDK Platform 2.3_r1 (Offline complete download) - Approx 80 MB
All Platforms

Android SDK Platform 2.2_r2 (Offline complete download) - Approx 80 MB
Windows
Mac
Linux

Android SDK Platform 2.1_r1 (Offline complete download) - Approx 80 MB
Windows
Mac
Linux

Android SDK Platform 2.0.1_r1 (Offline complete download) - Approx 80 MB
Windows
Mac
Linux

Android SDK Platform 2.0, revision 1 (Offline complete download) - Approx 80 MB
Windows
Mac
Linux

Android SDK Platform 1.6_r2  (Offline complete download) - Approx 63 MB
Windows
Mac
Linux

Android SDK Platform 1.5_r3  (Offline complete download) - Approx 54 MB
Windows
Mac
Linux

Android SDK Docs for Android API 7, revision 1 - Approx 48 MB
All Platforms

Android SDK Tools, revision 5  (16 to 23 MB)
Windows
Mac
Linux
Labels:

Pointer not declared even when declared !

Posted by Quakeboy Comments

A Strange problem encountered yesterday which left us puzzled for a few mins :) (Check image below)


So now, we tried everything possible. Until at last when we saw that there was a backslash after "Plist" in the comment line. As backslash is an escape sequence, it made the whole next line invalid, more like a comment.

Once we deleted that backslash, things were back to normal. But hey, aren't comments supposed to suppress anything in them including escape characters
Labels: ,

How to ignore build directory in SVN from command line

Posted by Quakeboy Comments

This works for Windows/Mac/Linux, but its tested on Mac. On Windows, you can do it easily with Free TortoiseSVN Shell context menu, so this method will be useful on Mac/Linux.

Lets suppose your folder structure is
SVNfolder->trunk->development->Projectfolder->build

Now open terminal and cd to development. (Now if you 'ls' it wil' list Projectfolder)
type svn propset svn:ignore "build" Projectfolder/  and press Enter.

and that is it, your build folder will be ignored.
Labels:

Simflex Image Gallery - Full Free Flex Image Gallery

Posted by Quakeboy Comments



I am releasing my source code for a full fledged Flex 3 image gallery along with a PHP based Minimal Image file manager page with upload, delete and automatic thumbnail generation features. I had made this while learning Flex, but it is very usable in many projects.

To see it in action, download, extract and put the bin-debug folder under your web server. There is a folder called "Admin" inside "bin-debug" folder which has the PHP part responsible image upload, XML file generation and auto thumbnail code. To manage the images, use filelist.php in admin folder. Tested with JPG and PNG images.

Download Source code - 8.78 MB (along with sample images)
Please drop me a line if you are using this anywhere.
Labels: , , ,

3d Tunnel OpenGL Source code - iPhone OpenGL ES compatible !

Posted by Quakeboy Comments




I am releasing source code of the iPhone OpenGL ES compatible Never-Ending Tunnel Source Code. The code is done using SDL on Windows only. But the OpenGL elements use only Triangle strips and all OpenGL ES compatible functions and porting to iPhone is a cakewalk.

I have searched on the web and never found a ES compatible version which is easily portable, so here we go enjoy. I have intentionally left a small glitch in Texture Coordinate generation, which actually looks pretty according to me. If you need I can help you to fix is easily.


Some important notes :-
  • No Models present - The Tunnel is a torus which is completely procedurally generated.. so the torus properties can be changed easily.

  • The Texture generation part is platform dependent. The files RTexture.h and RTexture.cpp is almost completely copy-paste reusable :D

  • DO NOT REMOVE - #define QUAKEBOY_IS_AWESOME (!)

Download Source code (along with SDL Library and Binaries included - 1.36 MB)

Andre Michelle's Tile based Scrolling ported to AS 3

Posted by Quakeboy Comments

Update:- I have found something that has to be added to the existing code. The SWF embedded here runs only @ around 60 FPS on a Safari or Firefox on a Mac. I will work on a more consistent render loop code adding some timer calculation elements soon.


I have ported the Tile based scrolling in Flash code by Andre Michelle to use Actionscript 3.0
Main challenge I faced was the tearing issue which was removed after I used Timer Event and updateAfterEvent() method. Thanks to 8bitRocket site for the tip.




I am getting a blazing 160 FPS in browser.. OMG Flash rocks ! If anyone wants me to provide comments and documentation for the code. I will happily do it on request.

Click here to get the full source code.

P.S. If its slow, its because you might be having the static noise swf playing below (scroll page down). View on the post page by click the title to ensure it plays smoothly.