Wednesday, September 02, 2009

OpenGL: Diffuse, Specular, Ambient, Emissive lights

In the OpenGL world there are different types of light types

Diffuse
--------
These type come from one direction bu are scattered equally in all directions.

Specular
--------
This type of light comes in one directions and is also bounced off in one direction

Ambient
--------
This type of light source comes from different light sources and is also scattered in different directions

Emissive
---------
This represents light originating from the object

So in general A light source has two types of properties associated with it
LR,LG,LB => the RGB components that make up the light and
MR,MG,MB => the RGB components that are reflected away
Hence the net effect of the RGB is given by a vector

(LR.MR, LG.MG, LB.MB)

if light is coming from two or more sources the light is added and next effect is
of the form

(R1 + R2, G1 + G2, B1 + B2)

Tuesday, September 01, 2009

Opengl : Hidden Surface Removal and LIghting

Hidden surface removal is a common technique in the graphics world. The technique says
we should draw only parts of objects that are visible to the user.

What support does OpenGL have for hidden surface removal.OpenGL supports hidden surface removal through a combination of DEPTH BUFFER and COLOR BUFFER.

Depth Buffer and Color Buffer
------------------------------
Our screen is flat basically we see a flat 2D rectangular screen made up of pixels. Associated with these pixels we have a corresponding value in the Depth Buffer and
Color Buffer. So what pixel appears forward or backward depends on its value in the depth buffer. The color value it shows depends on its value in the color buffer.

to enable the depth buffer we need use something like the following:

glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);

Material And Ambient Light
---------------------------
OpenGL understands RGB. A material quality decides the amount of RGB that is reflected or absorbed. An area can be lit by many light sources.Say in a room the bulb can the primary source of light.But there might be light reflected from walls which can be called secondory sources, the light contributed by such sources is called ambient light.

ps options

If you invoke the ps command like this

ps -C -o etime,pid,pri,cmd

this will show processes that match the command name and show output using the
fields followed after -o i.e

etime - how long the process is running
pid - the pid of the process
pri - priority of the process
cmd - the full command line of the command

Sunday, August 30, 2009

Looking back at mergesort

For mergesort the recurrence relation is

T(N) = 1 if N == 1
T(N) = cn + 2T(N/2) if N != 1

so if we draw the recursion tree for such a recurrence relation we get
Height of tree = lg N
Number of leaves = N
--------------
Leaves at level 0 -> 1
Leaves at level 1 -> 2
Leaves at level 2 -> 4
Leaves at level 3 -> 8
...
Leaves at level lg N -> N

Total Work Done
----------------
Work done at level 0 -> cn
Work done at level 1 -> cn
Work done at level 2 -> cn
....
Work done at level lg N -> cn

So total work done => cn * lg N => O(n lg n)

Similarity between objects

Lets say we need to find the similarity between people based on how they rank different items. Based on the that collected data we need to recommend some new
items to the users.People who are similar to each other have a higher likelihood of
liking things that we recommend them.

Now lets consider a Set of Users.A Set of Items and a set of Rankings which users give to these Items.

Given this information how can we find the similarity between 2 Users.

Approach 1)
------------
Take user A and user B
Calculate union of items chosen by user A & B , call it A_UNION_B
Calculate the intersection of the items chosen between A & b, call it A_INT_B
then the value A_INT_B/A_UNION_B represents the similarity of the 2 users A and B

Approach 2)
------------
For every item that is common between A & B we can calculate the Euclidean distance between the rankings of the 2 items.
D = RA (ranking A gave to an item) - RB (ranking B gave to an item)
Sum = Sum + D^2

The once we have the sum we can call it Similarity Sum and use a formula
like

Similarity = 1/(1 + Sum)

The 1 is added to prevent the 1/0 off case, where our magic factor factor is 1
we can choose an arbitrary magic factor say N

Then Similarity = N/(N + Sum)

Approach 3)
-----------
Use the Sum that we got in Approach 2 above
Similarity = Sum / N where N is the number of items common between A and B that have the same rating
Similarity = Sqrt(Similarity)
then to make Similarity fall between the range 0 and 1

Similarity = 1 - tanh(Similarity)

Approach 4)
-----------
Use the Similarity that we calculated in Approach 3
NN = Number of items common between A and B
Similarity = Similarity * (N / NN)