Monday 24 December 2012

Barycentric Coordinates

Hi,

This article mainly concentrates on showing what Barycentric coordinates are, and how they are used in Computer Graphics.

Consider the following example:(This can also apply to lines)


Now, Barycentric coordinates allow us to express any point within this triangle in terms of the Vertices (P1, P2, P3). Let's see how this can be done.

Now, starting from P1, we can easily say that
P = P1 + (a)*(vector from P1 to P2) + (b)*(vector from P1 to P3)

Now, this is the same as:
P = P1 + a(P2 - P1) + b(P3 - P1)

Now, this can be written as:
P = (1 - a - b)P1 + aP2 + bP3

Now, let (1 - a - b) = c

Thus, we get:

P = c(P1) + b(P3 - P1) + a(P2 - P1)

Thus, any point within the triangle can be expressed via a combination of a, b, c! Also, we can see that:

a + b + c = 1   

So, we thus have a coordinate system(consisting of 3 numbers a, b, c) w.r.t to a triangle. This is called as a Barycentric coordinate system.

Now, let us see an example in Computer Graphics. Suppose we want to rasterize a triangle and assign pixel colors to each of the pixel within the triangle, provided we already have the per vertex colors with us, then we can use this concept to assign pixel values to all pixels in the triangle.

In that case, the points would actually be colors, instead of positions. But, the colors would be linearly interpolated across the triangle.   
   


  

Sunday 18 November 2012

Coordinate System transformation(OpenGL)

Hi,

This article mainly focuses on explaining the following:

0) Transforming between Coordinate Systems
1) Difference betweeen Eye Space and World Space.

These concepts confused me a lot, but thanks to Prof Ravi Ramamoorthi 's Lecture Series where the concepts are very well explained. I would like to just brief a little about the same and with more explanation. Hope this helps.

--------------------------------------------------------------------------------------------------------------
0) Transforming between Coordinate Systems:

So, let's begin! Generally speaking, a coordinate system (referring to Cartesian coordinate system) is a set of three perpendicular vectors and an origin and this system providing a way to address all points contained in the space.

Now, in the same space, there can be multiple Coordinate systems! This is where things get a little confusing(at least to me, it was). Now, let's take an example:



 Here, we have two coordinate systems here (CSa, CSb). Now, we can say that the origin of CSb is (0, 0, 0) w.r.t it's own coordinate system, AND (xO, yO, zO) w.r.t CSa. Now, let's consider an arbitrary point P.





Now, this point P can be described in two ways:
1) (xa, ya, za)  -> w.r.t CSa
2) (xb, yb, zb)  -> w.r.t CSb

Now, what we are interested in is, how a point represented in one coordinate system is transformed into another coordinate system.
So,  (xa, ya, za) -> (xb, yb, zb)  is the transformation what we need. We will shortly see how this can be achieved. Now, let's move onto how these concepts are used within OpenGL.

--------------------------------------------------------------------------------------------------------------

1) Difference betweeen Eye Space and World Space:

                In OpenGL, the modelview matrix transforms a vertex from object space to eye(view) space. The pipeline as shown in the Orange book, and more details added:



Fig (b) is the more detailed part of what is shown in the Orange Book(Fig (a)). Clearly, the ModelView matrix transforms a coordinate in object space to eye space. Now, revisiting the figure shown earlier, we have:



The tilted coordinate system is the camera(eye) space and the camera is at its origin. We have 3 vectors defining this space, (look, up, right). Now, notice that mentioning a random point say (1, 0, 0) here doesn't make sense unless we specify which coordinate system it belongs to. So, it could be (1, 0, 0) in world coordinate system or it could be (1, 0, 0) in eye coordinate system.

Now, the camera is located at some coordinate in world space (let's say this is camLoc) w.r.t the origin of the world. Thus, camera is located at camLoc in WorldSpace BUT, it is at (0, 0, 0) w.r.t EyeSpace. (similar as shown in CSa, CSb).

So, the view matrix basically does a transformation of a coordinate from world space to eye space. This can be done by building a ViewMatrix, or by simply using the glu function gluLookAt()
So, a point in world space Pw is transformed to eye space Pe by using the view matrix.

Now, there is an important point to be noted here. "moving a camera" is essentially the same as "moving the object" in reverse. They both modify the same matrix. Thus, OpenGL has it as just one matrix. How we visualize is more or less a personal choice, but the end result is that we update the same matrix. Thus, the Model and View Transforms are integrated into one -> the ModelView Matrix.

Interestingly, Direct3D still has two different matrices for the same: Direct3D matrices

Another interesting observation would be, the View Transformation can also be thought of as the camera being brought into WorldSpace (0, 0, 0). The other way to visualize the same would be, the WorldSpace brought into CameraSpace (0, 0, 0). But, both are the same.
--------------------------------------------------------------------------------------------------------------

I hope the pics have been helpful. Kindly let me know if there are mistakes in the article. I will correct them.

Sunday 28 October 2012

__COUNTER__ : An interesting macro

Hi,

Recently, i came across a macro, the __COUNTER__. In the past, i had looked for a way to do something that this macro allows us to do, but i wasn't aware of it at that time. So, i assumed its simply not possible to do it. 
I was searching for an easy way to generate variable names and this macro allows us to do just that.

Here is what the macro does. Everytime this macro is expanded, there is an internal count and the preprocessor writes this number.

To explain it further, here is an example:

 int main()  
 {  
   __COUNTER__  
   __COUNTER__  
   __COUNTER__  
   return 0;  
 }  

So, if you see the preprocessor output, you can see __COUNTER__ being expanded to 0, 1, 2.

1) In Visual Studio, to view the preprocessor output, you can goto Project Properties->C/C++->Preprocessor.  There you can select if you want to generate preprocessor output. It generates a ".i" file that contains the preprocessor output.

2) In GCC, you can give -E option to obtain the preprocessor output.

Now, here is an example how you can use it to obtain variable names. Its just a neat way to use them, instead of use giving foo, bar, baz...

#define var1(x, y)  x##y;  
 #define var(x, y)  var1(x, y)  
 int main()  
 {  
   int var(a, __COUNTER__)  
   int var(a, __COUNTER__)  
   return 0;  
 }   

This generates names like a0, a1 etc. Its just a nice way to use it. Well there could be several cases where this could come in handy. This was some instance where i wanted a neat way to generate names.

 

Sunday 29 July 2012

Android and NDK[ VS Android ]

Hello World!
Well, i don't know what introduction to give to this topic, but the main idea is to use C/C++ code to develop Android applications with Visual Studio. This post is not about what the NDK is or how to write applications. This is a post just on how to get started.

I googled around a little, found some videos on YouTube. Most of them suggest using the typical Eclipse way. But i am more used to Visual Studio and was looking for a way use it for writing code for Android. And there is a way to do it!

  \m/   VS Android  \m/

There was some searching i had to do in order to get this working. But all in all, it was quite easy to actually get started and build an Android app. All thanks to Google(not referring to the search engine :D) and the guys who develop the Android SDK(oh and of course VS Android guys!),  I was able to get stared on a simple app in about 3 days(including time for downloading all necessary stuff, plus i wasted some time too.). So, quite an easy task. But, i had not found a single place that explained(and provided links) in order to get started. So, i thought of writing a post here!

So, here it goes:

(i) Downloading the SDK and other dependencies:

1) Download the SDK

2) Download the NDK
3) Download Cygwin

4) Download the JDK

5) Download VS 2010.  This version is very essential because VS Android is not compatible with any other VS version

6) Download VS Android

7) Apache Ant download

8) Set environment variables:
  [ To set environment variables, do:
    MyComputer->Right click->Properties->Advanced->Environment variables. 
  ]
      a) ANDROID_NDK_ROOT = <install_path>:\android-ndk-r8-windows\android-ndk-r8
      b) ANT_HOME  = <install_path>:\apache-ant-1.8.4-bin\apache-ant-1.8.4
      c) JAVA_HOME = <install_path>\Java\jdk1.7.0_05
      d) _JAVA_OPTIONS = -Xms256m -Xmx512m

Make sure you have set your environment variables correctly.

And that is it! Using VS Android, things are so much easy!

---------------------x ---------------------

(ii) Download samples from the VS Android download site:
http://code.google.com/p/vs-android/downloads/detail?name=vs-android_samples.zip

Put your device into debug mode and then plug it via USB and build your app. It should run on the device.

---------------------x ---------------------

(iii) Errors that you might face:

When i tried to build my first program, i.e the sample provided by VS Android, i got the following error.
Unable to resolve target 'android-4'
This can be solved by changing the following in the project.properties file:
target=android-xx
Set this to the proper version to get rid of the error.
 Well, this was the only error i got. In fact i had another error. But that was solved by changing an environment variable. I had to edit _JAVA_OPTIONS to add an entry. But since i have already provided both the environment variables, it should work just fine.
---------------------x ---------------------

 (iv) Some tools that might come in handy:

1) ADB pusher:  This can be used to transfer files to your device very easily:

2) You can find some nice tools at
<install_path>\Android\android-sdk\platform-tools
---------------------x ---------------------

Well, i hope that this was of some help. Using VS Android was certainly a LOT easier than the usual Eclipse path. It feels "at home" using Visual Studio :D. Many of you maybe used to using Visual Studio and this is the best solution(i feel) available.


I have added a video of a sample program that i wrote: I used the the hello-gl2 project. wrote code to do some simple animation. The phone is a Samsung Galaxy Ace.
---------------------x ---------------------

I hope this helps. If there is something wrong in the information, or if there are errors you face after following this, kindly let me know. I will modify this. If there is anything else, please leave a comment.

Bye!

---------------------x ---------------------
(v)Some useful links:
1) http://www.youtube.com/watch?v=5yorhsSPFG4
  A Google I/O video