projects

Sunday, August 28, 2005

Scanline and flat shading


Finally some scanlinen conversion with flat shading..The light direction is down the positive z-axis (0, 0, 1). Thanks to Wang I now know a bit more about the mathematics of perspective projection. The scene consists of a icosahedron and a box both of which have back face culled which means that only the front face are rendered. The z-buffer for hidden surface removal is also present but is not required here since backface culling is done.... but it will be required when object are rendered one infront of the other (the box infront of the icosahedron)
The matrix for perpective calculation...
float nearP = -1.0;
Matrix4 p( nearP, 0, 0, 0,
0, nearP, 0, 0,
0, 0, 1, 0,
0, 0, -1, 15 );

transformVertices(p);
normalizeTransformedVertices();

Friday, August 26, 2005

Phong Shading.... from the net


Phong Shading overcomes some of the disadvantages of Gouraud Shading and specular reflection can be successfully incorporated in the scheme. The first stage in the process is the same as for the Gouraud Shading - for any polygon we evaluate the vertex normals. For each scan line in the polygon we evaluate by linear intrepolation the normal vectors at the end of each line. These two vectors Na and Nb are then used to interpolate Ns. we thus derive a normal vector for each point or pixel on the polygon that is an approximation to the real normal on the curved surface approximated by the polygon. Ns , the interpolated normal vector, is then used in the intensity calculation. The vector interpolation tends to restore the curvature of the original surface that has been approximated by a polygon mesh. We have :



Source

Wednesday, August 24, 2005



Hidden surface removal using software z-buffer (depth buffer)... It is not exactly working perfectly.. as you can see there are some holes made.. not sure why. Maybe because i am using floating point to store the depth values. I am going to try with double. If the problem exists then something is wronge with my algo (logical error!!). Hope i can fix this today and then i can concentrate on phong model.
:) There is now no lines that was present previously... i fixed it.
The box looks like they are rendered inside out. Thats because there is no perspective projection.

Tuesday, August 23, 2005


Colors are randomly added for each triangle. As you can see the picture dont make any sense... only the outline looks like a box. This is because there is no depth testing or any sort of backface culling... which i plan to implement now.
I'm going to use a software z-buffer... later on I may use other techniques available for hidden surface removal if the sofware z-buffer does not work.

A box revolving in some axis.. software rendered using scanline technique. The box rotates realtime with very good fps. The result of the scanline is saved in a software buffer. Then the image from the buffer which is in RAM is drawn to the frame buffer (color buffer) using glDrawPixel(...) in 2D orthographic mode.
//rotate the box
box->RHAnimate(3.8);

//clear the software buffer..
BUFFER->ClearColorBuffer(0, 0, 255);

//do the scanline conversion..
scanline->ScanObject(box);

//draw to frame buffer...
glRasterPos2f(-1, -1);
glDrawPixels(600, 600, GL_RGB, GL_UNSIGNED_BYTE, BUFFER->GetColorBuffer());
Pressing the s or the p key will save the current frame as tga file.
  • p - from the software buffer.
  • s - from the opengl frame buffer
Both will produce the same tga file... but saving from the software buffer will be faster since the RGB information is not obtained from the frame buffer but from the processor memory (RAM) itself.

Still need to fix one more thing before procedding further..
In the picture there are two lines visible. This lines ofcourse should not be there.... and is occuring due to loss of information when a floating point is converted to inetger value.

Saturday, August 20, 2005


Scanline conversion now works..:) It is still in the initial stages.. i can only parse through triangles and render them in just one color. If you look closely.. there is a line going through from the leftmost edge to the other side. This is due some error involved when floating point numbers are converted to interger. A few tweek in finding the edge of the line should eliminate it.

Steps to follow:
  • Fix the line through the triangle as shown in the pic above
  • Test with more triangles... of different sizes and shape
  • Rendering at real time.. a box rotating along some axis..
  • Finally add color and light information.. this will be hard ;)

Two more classes have been added. The scanline and the buffer class. The buffer class is a singleton class and is therefor accessible to all classes..:) For now the buffer class only support rgb- 8 bits (1 byte) per pixel color.... unsigned char. It also wrote a few lines about the depth buffer. For now it is of type float.

Friday, August 19, 2005

Software rendering...Scanline conversion... finally some screen shot

Tga file containing a box and a triangle in wireframe mode... in parallel projection with no backface culling. Something is wronge with my maths for perspective projection..does not work ;)

Finally some screenshot... tga file reader wrtiter class is working. The tga class only supports uncompressed 8 or 24 bit gray scale or rgb image format. Very simple... with a header and cpp file.. no lib files :) The above image is rendered by reading the frame buffer and saving as tga file. Still no scanline... should start tonight :)
GLbyte *buffer = (GLbyte *)malloc(sizeof(GLbyte) * windowHeight * windowWidth * 3);
glReadPixels(0, 0, windowWidth, windowHeight, GL_RGB, GL_UNSIGNED_BYTE, buffer);
IMAGEPROC->WriteImage("screenshot.tga", (unsigned char *)buffer, windowWidth, windowHeight, 3 * 8, TGA);

The project now has two singleton objects defined in SingletonDefinitions.h file...

CImageProcessor *IMAGEPROC;
CRenderer *RENDERER;

//this function should be called atleast once.. at the beginning of the application..
void defineSingletonObjects(void)
{
IMAGEPROC = RHSingleton::Instance();
RENDERER = RHSingleton::Instance();
}

I need to implement a buffer class... need two buffer objects.. one for the final color value and one for storing depth values (depth buffer). Thinking of making it a singleton class also:)

My next objective is to implement the very basics of scanline.. and should test it with a simple triangle.

Tuesday, August 16, 2005

scanline

i have to start doing the scanline conversion... i have been saying this for the past few days