Sandundhammika's Blog











I really hate to write political suff in my tech blog, This is the one and only blog that I was maintaining for my pure tech interests.

 

But today he owned it because what he have done.If I didn’t write this on my blog then it’s not fair thing. Because I felt this is too important and this is a big step [1000 steps] forward to a advanced civilization.

http://lankacnews.com/sinhala/news/25221/?replytocom=75094#respond

whatever he done today is going thousand steps forward to the direction where civilization getting advanced. So we should respect him and if we didn’t show respect then that means we not belongs to that advanced socieaty.

1. Thousands of Animal lives were saved.

2. The ppl who doing sin and who enjoying that inicident were saved from doing that sin.

3. The next generation and the whole chain of generations after that who will come there and do the same sin , are saved as well.

4. The new ppl who will follow this and become vicitims to this have been saved.

This is a big forward step to the direction that civilization going advanced. Good Luck and long live Mervin Silva sir, today you proved you owned that ‘sir’. I alologize I’m a person who never refer you with the word ‘sir’.Sir , now you are not a member of Avanced civilization , you are a leader for us and the civilization too.



when we store data in computer memory, there are basically two ways of storing them. Simply one way is store the big byte first or storing the small byte first.

The term “big-ending” or “little-ending” is comming from the Gulliver’s Travel Advanture movie,where the “liliputians” are divided into two camps , who start to ate the egg from little and and who start to ate the egg from the big end.

The same meaning is there on the computing world. In little ending, you first store the lowerst byte , and in the big ending you store the most significiant byte first. Computing world also politically discriminated into these two categories, discrimination is everywhere in the world where computing is not just exception to it.

To make this clear , let’t take a look at a example. Suppose you need to store 12345678H (hexadecimal) value in compuer memory. In C programming language you can simply write.

int i = 0×12345678;

If you took a logic probe and hack your computer memory and see , and if your computer is little ending you can find it like this. Please suppose that &i=0x0100h or address of i is situated at the memory location 0x0100h.

Image

 

value 0x12345678h was stored in a little ending machine’s memory.

On the other hand if your machine is a big ending machine then you will see your memory like follows.

Image

value 0x12345678h was stored in a big ending machine’s memory

And if you’r not touching assembly language/system programming or file types and I/O and you programming in a high level language like C/C++/Java probablly you don’t need to familar with the difference between little-big ends.

If you’r writing the network protocols or writing a device driver code for a networking interface then you should probablly know that all the networking communication is goes in big-ending representation. When you doing programming with sockets or higher level API you’ll met very rare cases you need to deal with this fact.

However if you want to deal with the file formats ,again this counts.For a example BMP,GIF and RTF are file formats that are examples for the “little ending” format. and JPEG is a example for “big ending”. Some file formats like AVI, WAV,TIFF supports both endings.



Here in this note I’m going to explain the first program  that I published before.For the sake of convenient I’m going to list that program again here.

However before  we go depth I need to explain what is Xlib. Well Xlib is a client library to the X-Server.Technically speaking like a JavaScript but not like PHP or  JSP that which is runs on the server. What Xlib does is just preprocess the commands from application programs to the X Server and open a communication channel ,encode them using X protocol and send them to the server.X protocol can operate through TCP/IP where the remote machine is located at other side in the world or in the case of local machine it will use the interprocess communication mechanism.No matter what mechanism that it use it won’t violate the basic server client architecture of the windowing system.

So as I already told you Xlib is like JavaScript that is running on your web-browser not in the web server.But the difference is it’s not written with JavaScript,But with C. So it’s actually a C library.In the helder file /usr/include/ X11/ directory you can find the Xlib.h,which is the declares the main functions that you needed to talk with the X-server.By the way the gcc compilers main include path is /usr/include so therefore what we need is to add the following line in order to include the /usr/inlude/X11/Xlib.h.

 

#define <X11/Xlib.h>

 

So then we are going to write the program , below I have listed the program again.

 

#include <stdio.h>
#include <X11/Xlib.h>

void *__gxx_personality_v0 =0;

int main (int argc,char**argv)
{
    // open a display
    Display *dpy;
    Window win;
    Visual *vis ;
    dpy = XOpenDisplay(NULL);
    vis = DefaultVisual(dpy,0);
    win = XCreateWindow( dpy ,
                         DefaultRootWindow(dpy),
                         100,100,
                         500,500,
                         0,
                         CopyFromParent,
                         CopyFromParent,
                         vis,
                         0,
                         NULL);
    XMapWindow(dpy,win);

    // then say what keys to be notified.
    XSelectInput(dpy, win , ExposureMask|KeyPressMask|ButtonPressMask);
    XEvent report;
    // enter the message processing loop
    while(1)
    {
          XNextEvent(dpy,&report);
          switch(report.type)
          {
                case Expose:
                     fprintf(stdout,"Expose Event.\n");
                     break ;
                case KeyPress:
                     fprintf(stdout,"KeyPress Event.\n");
                     break;

                default:
                     break;

          }
    }
    return 0;
}

Well you may be wonder why is this __gxx_personality_v0 ? I include that line here because some it will get a linking problem when linking with old “C” libraries.Actually it’s a patch.If you compile without this and obtains no error, then



{July 28, 2010}   A simple X window using Xlib

Here today ‘m going to demonstrate you how to write a simple X application using

Xlib X-libraries.

down here is the source code.

#include <stdio.h>
#include <X11/Xlib.h>

void *__gxx_personality_v0 =0;

int main (int argc,char**argv)
{
 // open a display
 Display *dpy;
 Window win;
 Visual *vis ;
 dpy = XOpenDisplay(NULL);
 vis = DefaultVisual(dpy,0);
 win = XCreateWindow( dpy ,
 DefaultRootWindow(dpy),
 100,100,
 500,500,
 0,
 CopyFromParent,
 CopyFromParent,
 vis,
 0,
 NULL);
 XMapWindow(dpy,win);

 // then say what keys to be notified.
 XSelectInput(dpy, win , ExposureMask|KeyPressMask|ButtonPressMask);
 XEvent report;
 // enter the message processing loop
 while(1)
 {
 XNextEvent(dpy,&report);
 switch(report.type)
 {
 case Expose:
 fprintf(stdout,"Expose Event.\n");
 break ;
 case KeyPress:
 fprintf(stdout,"KeyPress Event.\n");
 break;

 default:
 break;


 }
 }
 return 0;
}

And save this as ch01.cc and execute this bellow command to compile this program.


$gcc -o ch01 ch01.cc -lm -L/usr/lib/X11 -lX11

and thereafter to run.


$./ch01

It will simply receive the events KeyPress event and Expose event and just write that it’s exposed

to the stdout by using fprintf.

I’ll willing to write another full page about digging and explaining this program.

–Happy Coding–



{July 12, 2010}   glRotatef

let’s write a simple program to demonstrate the glRotatef function.

here is the documentation from OpenGL SDK.

http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml

study the source code bellow.Remember that every time that you draw again should need to load the identity matrix into the GL_MODELVIEW matrix stack.There are three matrix stacks that internally maintained by the OpenGL.the glRotatef() function will apply the rotation to the current matrix stack.But we didn’t use the glMatrixMode() to select appropriate matrix stack.So all the changes will be applied to the initial matrix stack which is GL_MODELVIEW.But as a practice try to check what matrix stack that we are going to apply some change to the current matrix mode.


#define GLUT_DISABLE_ATEXIT_HACK ;; this is a dirty hack,but we have to do this.
#include <GL/gl.h>

#include <GL/glut.h>
#include <stdio.h>

/* This variable will keep the state of the rotation information. */
double rotation = 0;

 /* The Display Function */
void display_func()
{

 // clear the buffer
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 // load the identity matrix
 glLoadIdentity();
 // do rotation around the vector k/root(3)
 glRotatef(rotation,0,0,1);
 glBegin(GL_LINES);
 // we are drawing a square that spans -0.5,-0.5 to 0.5,0.5
 glVertex3f(-0.5,-0.5,0.0);
 glVertex3f(0.5,-0.5,0.0);

 // right vertical line
 glVertex3f(0.5,-0.5,0.0);
 glVertex3f(0.5,0.5,0.0);

 // the top horizontal line
 glVertex3f(0.5,0.5,0.0);
 glVertex3f(-0.5,0.5,0.0);

 // the left vertical line.
 glVertex3f(-0.5,0.5,0.0);
 glVertex3f(-0.5,-0.5,0.0);
 glEnd();
 glutSwapBuffers();     // at the end,we are swapping the buffers because
 // we enabled the double buffering.

}
/* The keyboard function */
void keyboard_func(unsigned char key ,int x,int y)
{
 if( key == 'q' || key=='Q')    {
 printf("Exit from the OpenGL program.......\n");
 exit(0);

 }

 if ( key == 'a')
 {
 printf("rotation =%f \n",rotation);
 rotation = rotation +1 ;
 if (rotation >360)
 rotation =0;
 glutPostRedisplay();

 }
}

/* The main function of this program */
int main(int argc,char** argv)
{
 // initialize the glut
 glutInit(&argc,argv);
 glutInitWindowPosition(100,100);
 glutInitWindowSize(500,500);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH |GLUT_RGBA);

 // Enable some appropriate modes in the gl
 glEnable(GL_DEPTH_TEST);

 // create the window
 glutCreateWindow("Ch10");
 // set up the display function.
 glutDisplayFunc(display_func);
 // set up the keyboard function
 glutKeyboardFunc(keyboard_func);
 glutMainLoop();

 return 0;
}

–press the ‘a’ key on the keyboard to experience the rotation effect of the rectangle.

Happy Coding.



hi today I’m going to demonstrate how to write a simple win32 program using C++

to display screen resolution values using a simple Message Box.

Here is the source code.


#include <windows.h>
#include <tchar.h>


TCHAR  gResultOutBuffer [80];

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
 // get the vertical resolution.
 int vert_res = GetSystemMetrics(SM_CYSCREEN);
 // get the horizontal resolution.
 int hori_res = GetSystemMetrics(SM_CXSCREEN);

 // then create use wsprintf to print it to an
 // appropriate buffer.
 wsprintf(gResultOutBuffer,_T("Resolution is : %dx%d \n" ),hori_res,vert_res );

 // Then Create a MessageBox and show it to the user.
 MessageBox(NULL,gResultOutBuffer,_T("screen resolution"),MB_OK);

 return 0;
}

And here is how you should compile this,

and for more information head to the http://msdn.microsoft.com/en-us/library/ms724385%28VS.85%29.aspx

–Happy Coding–



{April 12, 2010}   OpenGL depth buffer

welll what is the opengl depth buffer

Opengl is supporting the drawing 3D objects. So there is something called depth in the 3rd dimension.

Therefore OpenGL uses this depth buffer to represent the depth information about the each pixel.For

a example consider the bellow code and it’s drawing two triangles with different depths. One is at depth

1.0(red one)  and one is at depth 0.0(green one).So the blue one is on the red one.


glBegin(GL_TRIANGLES);
// set hte color
glColor3f(1.0,0.0,0.0);

// the triangle with the depth 1
glVertex3f(0.6,0.6,1.0);
glVertex3f(0.2,-0.2,1.0);
glVertex3f(-0.8,0.2,1);

// set the second color
glColor3f(0.0,1.0,0.0);
// traingle with the depth 0
glVertex3f(0.2,1.0,0.0);
glVertex3f(0.8,0.0,0.0);
glVertex3f(-0.8,0.0,0.0);
glEnd();

So when OpenGL is drawing each triangle it will compare the current depth buffer value and update if it is occlude

with the new value.So simply it contains the depth value of the pixel from the triangle that is visible.Simply that’s

how it’s working.

To enable this depth buffer you should enable two properties  form the windowing system, and the OpenGL.

If you are using GLUT(GL utility toolkit) then use this function to enable that.


glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);

and to enable the property on OpenGL you should invoke the bellow line.


glEnable(GL_DEPTH_TEST);

so after that you can use the depth buffer.

here is a simple program that just draw two triangles and demonstrates the depth buffer.


#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>


void init()
{
/* performs the init routines for the
* GLUT window
*/
// set the clear color
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}

void draw_loop()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
// set hte color
glColor3f(1.0,0.0,0.0);

// the triangle with the depth 1
glVertex3f(0.6,0.6,1.0);
glVertex3f(0.2,-0.2,1.0);
glVertex3f(-0.8,0.2,1);

// set the second color
glColor3f(0.0,1.0,0.0);
// traingle with the depth 0
glVertex3f(0.2,1.0,0.0);
glVertex3f(0.8,0.0,0.0);
glVertex3f(-0.8,0.0,0.0);
glEnd();
}

void keyboard_loop(unsigned char key, int x,int y)
{
if(key=='q')
{
printf("Exiting.......\n");
exit(0);
}
}

int main( int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(300,300);
glutInitWindowPosition(100,100);
glutCreateWindow("gl depth buffer testing");
glutDisplayFunc(draw_loop);
glutKeyboardFunc(keyboard_loop);
glutMainLoop();

return 0;
}

To compile this under linux just save the source code as main.cc and invoke these commands on the bash shell.


$gcc -c main.cc

$gcc -o main main.o -lglut

And heres how it works.

Do IT YOURSELF.

and turn off the depth buffer and see what will happens.

–HAPPY CODING–



{April 6, 2010}  

TEXTAREA_ID



Last night I get hard time with my finding a bug.
that bug is hided by the C preprocessor.I just only use a simple macro shown
bellow.

#define drawOneLine(x1,y1,x2,y2) \
	glBegin(GL_LINES); \
	glVertex2d((x1),(y1)); \
	glVertex2d((x2),(y2)); \
	glEnd();

and this is the program that I tries to correct.

#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

#define drawOneLine(x1,y1,x2,y2) \
	glBegin(GL_LINES); \
	glVertex2d((x1),(y1)); \
	glVertex2d((x2),(y2)); \
	glEnd();
	
void init()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);
}

void display()
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glEnable(GL_LINE_STIPPLE);
	
	glLineStipple(1,0x0101); /* dotted */
	drawOneLine(50.0 ,125.0,150.0,125.0);
	glLineStipple(1,0x00FF);
	drawOneLine(150.0,125.0,250.0,125.0);
	glLineStipple(1,0x1C47);
	drawOneLine(250.0,125.0,350.0,125.0);
	

	/* in 2d row , 3 wide lines , each with different stipple */
	glLineWidth(5.0);
	glLineStipple(1,0x0101);
	drawOneLine(50.0,100.0,150.0,100.0);
	glLineStipple(1,0x00FF);
	drawOneLine(150.0,100.0,250.0,100.0);
	glLineStipple(1,0x1C47);
	drawOneLine(250.0,100.0,350.0,100.0);
	glLineWidth(1.0);
	
	/* in third row , 6 lines with dash/dot/dash stipple */
	/* as part of a singl connected line strip */
	glLineStipple(1.0,0x1C47);
	glBegin(GL_LINE_STRIP);
		for ( i=0;i < 7;i++)
			glVertex2f(50.0+((GLfloat) i * 50.0),75.0);
			
	glEnd();
	
	i=0;
	
	/* in  4th row , 6 independent lines with same stipple */
	for (i=0;i<6;i++)
		drawOneLine(50.0+((GLfloat)i*50.0),50.0,50.0 +((GLfloat)(i+1)*50.0),50.0);
	
	/* in 5th row, one line with dash/dot/dash stipple */
	/* and a stipple repeat factor of 5 */
	glLineStipple(5,0x1C47);
	drawOneLine(50.0,25.0,350.0,25.0);
	glDisable(GL_LINE_STIPPLE);
	glFlush();

}


void reshape(int w,int h)
{
	glViewport(0,0,(GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, (GLdouble) w,0.0, (GLdouble)h,-1.0,1.0);
}


int main(int argc,char**argv)
{
	
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(400,150);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}

The bug is marked in red.That line should be aligned with the rest of 5 lines too.
So everything in the program looks fine,and according to that imaginary something like this
cannot be happen.So where is the wrong.Can you guess?

Actually to pinpoint the error you need to lookup the preprocessor output.
This is how you should get the preprocessor output.and open it by notepad to view it.

J:\EXERCISES\opengl\glut\ch07>gcc -E ch07.c > preprocessed.cpp
ch07.c:90:2: warning: no newline at end of file

J:\EXERCISES\opengl\glut\ch07>notepad preprocessed.cpp

The -E does simply said stop in the preprocessor stage.and this will output the output to the
standard output, using ‘>’ operator I just simply redirect that output to a file called preprocessed.cpp.

So let’s go and see what is the wrong at preprocessed.cpp file.Preprocessed.cpp file is around 2000
line of code.Because every opengl helder file context is there.Anyway I will show where the error
is located.


 for (i=0;i<6;i++)
  glBegin(0x0001); glVertex2d((50.0+((GLfloat)i*50.0)),(50.0)); glVertex2d((50.0 +((GLfloat)(i+1)*50.0)),(50.0)); glEnd();;

So asking for you, do you get the error?still not get it? Oky then let me correct it.


 for (i=0;i<6;i++){
  glBegin(0x0001); glVertex2d((50.0+((GLfloat)i*50.0)),(50.0)); glVertex2d((50.0 +((GLfloat)(i+1)*50.0)),(50.0)); glEnd();;
}

So the curly braces should be applied , otherwise the glBegin(0×0001) only be nested
inside the for loop. so the source code file should be corrected as bellow.

	/* in  4th row , 6 independent lines with same stipple */
	for (i=0;i<6;i++){
		drawOneLine(50.0+((GLfloat)i*50.0),50.0,50.0 +((GLfloat)(i+1)*50.0),50.0);
	}

And finally it runs well.

So one coding advice , be careful with the preprocessor when you do code with C/C++

–happy coding–



hi today I’m going to share my sourcecode with you how to draw a
circle using GL_LINE_LOOP.

This program simply does following.
1. define the PI constant
2. define number of steps that circle should be drawn.
3. divide 2*PI by number of steps and get the value of a one sector angle(angle).
4. within a for loop add another angle to the current angle(angle1).
5. Use the glVertex2d(sine(angle1),cos(angle1)
6. after the for loop come to the glEnd() and flush the buffers using glFlush().

here is the source code for the program.

#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>

#include <math.h>
#define PI 3.1415926535898
GLint circle_points =100;
// This is the draw function.
void draw()
{
	glClear(GL_COLOR_BUFFER_BIT);
	double angle = 2*  PI/circle_points ;
	glBegin(GL_LINE_LOOP);
		double angle1=0.0;
		glVertex2d( cos(0.0) , sin(0.0));
		int i;
		for ( i=0 ; i< circle_points ;i++)
		{
			printf( "angle = %f \n" , angle1);
			glVertex2d(cos(angle1),sin(angle1));
			angle1 += angle ;
		}
	glEnd();
	glFlush();
}



void init()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}

void keyboard (unsigned char key , int x, int y)
{
	exit(0);

}

void main( int argc,char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
	glutInitWindowSize(250,250);
	glutInitWindowPosition(100,100);
	glutCreateWindow("ch06");
	init();
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(draw);
	glutMainLoop();
}

To compile ,Just execute following commands , note that glut32.dll should be in the folder that
you executing this.

J:\EXERCISES\opengl\glut\ch06>gcc -o ch06.exe ch06.c -lGLUT32 -lOPENGL32

and here’s how this was running.

–Happy Coding–



et cetera
Follow

Get every new post delivered to your Inbox.