17 Şubat 2014 Pazartesi

Neden "Debugging" deniyor?

1951 yılında Grace Hopper isimli bilgisayar bilimcisi UNIVAC adındaki devasa bilgisayarı programlıyordu. Bir gün sistemin çalışması aniden durdu. Hopper, problemin bir kelebek yüzünden oluştuğunu buldu. Sistem kayıt defterine bu hatayı "bug" olarak kaydetti. Bu zamandan beridir bilgisayar problemleri "bug" olarak isimlendirilmektedir.

Defterdeki o kayıt National Museum of American History'de sergilenmektedir.




Computer Graphics Rendering Pipeline

If you're a newbie in Computer Graphics and OpenGL probably your mind is confused about rendering pipeline. In this post I'll simply and fundamentally try to mixture mathematical basics of computer graphics and OpenGL rendering logic.

Think of an artist who tries to paint a view. What should he do? At first he should decide his sitting position (adjusting the camera) and secondly he should try to paint that 3D view onto a 2D paper (projection). Last thing he should decide is which part of the paper the view will be drawn (viewport). What an OpenGL programmer should do is a very similar to this process:

In computer graphics we represent assets as array of points. Every point is represented with 4 parameters in homogeneous coordinates, in mathematics it's represented with matrices like:


Pay attention it is not the representation on the screen. It is the representation of the asset in the real world coordinates. Screen is a 2D plane so that we need to project the asset on it. In order to do that we need to multiply it with a Transformation Matrix called Projection Matrix. We have basically two kind of projections in computer  graphics:

  1. Perspective Projection 
  2. Orthographic Projection

Ortographic projection can be used in computer aided applications. You can think of Autocad or Solidworks type of applications. Perspective projections can be used in especially in games. Basically in perspective projections you can view assets relatively. As an example if you're in a plane and looking below you'll see road lines which are all the same size. But you're in a car you'll see the farthermost line as the shortest one. After you make decision between ortographic and perspective projections you need to multiply your asset (array of points) with  this projection matrix. The result of this operation will give you the asset's 2D represented coordinates. Then remember that you should decide which part of the screen you want to draw this object. It is another transformation type called viewport. When you multiply the last result with your viewport matrix then you obtain the drawing of your asset on the screen.

And lastly what if you want to move the object on the screen(Translation) or want to show your asset bigger/smaller (Scaling), or want to rotate your asset? These are called viewing transformations as a whole. In OpenGL we have modelview matrix in order to do these operations. Model is our asset and view our camera. Think why both of them represented as one matrix? Because, there is no difference between rotating model about left side 45 degrees or rotating camera about right side 45 degrees. This example is valid with other situations as well. (Translation,Scaling)

I think it's been a good introduction about Computer Graphics and OpenGL.

4 Şubat 2014 Salı

Reassambling TCP Packets

If you read tcp messages like files it would work properly on local but it may not work properly with other situations. So that you should reassamble the your tcp package on client side. Here is a great article on that problem.

As a summary there are three cases:
First case, you can assume that you read perfect amount of data.
Second case, you might not read data enough.
Third case, you might read more than one message.

So, according to these cases you should examine these situations. If you read perfect amount there is no problem. If you don't read enough you should wait for the next message and merge it appropriate amount. If you read more than one message you should hold the overflow into some variable and merge it with next income packet.

3 Şubat 2014 Pazartesi

Implementing A Circular Buffer (Ring Buffer)

A ring buffer is generally used in streaming applications such as music streaming or video streaming. When you stream media from somewhere the bytes of the media will be written on a ring buffer and after it received by the client new data will be written on the buffer. So that you will use a fixed-size data structure and will be able to do real-time streaming without using a big data structure which is not very easy to handle when it comes from the socket.

General implementation of a ring buffer

There are two cases while reading/writing from/to a ring buffer:
First one is as you guess, is to write/read directly as if our ring buffer is an ordinary buffer. This case is suitable if income data doesn't causes overflow. If ring buffer overflows after incoming data you must have another case to handle it. This is the second case and it is very comprehensible as well as the first case. If your data overflows then push it from the beginning of the data structure. And in both cases you should adjust your begin/end indexes (or you can use pointers). These indexes holds the address/index of the last data on the buffer.

I implemented my own ring buffer data structure using C++ language like this: