Sandundhammika's Blog











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–



{March 20, 2010}   QT mingw gcc 3.4.2 problem

Hi all , I’m using g++ 3.4.2 and I had downloaded the latest QT SDK (with source code) from

Nokia. And just type ‘QT tutorial’ in google and get this page.

http://doc.trolltech.com/4.3/tutorial.html

and I add the g++ and QT /bin folder to PATH environment variable and write the first program

and save it as tutorial1.cc.


/****************************************************************************
* Qt tutorial
*
*
*****************************************************************************/
#include <qapplication.h>
#include <qpushbutton.h>

int main ( int argc , char ** argv )
{
QApplication a(argc,argv);

QPushButton hello( "hello World!",0);
hello.resize(100,30);

//a.setMainWidget(&hello);
hello.show();
return a.exec();
}

and to compile and link this I hit ,


J:\EXERCI~1\QT4>qmake

J:\EXERCI~1\QT4>make -f Makefile.debug

Then I had received these errors.

I get discouraged, but finally I google  those symbols,those symbols are ,


C:\iwmake\build_mingw_opensource\src\winmain/qtmain_win.cpp:93: undefined refere
nce to `_Unwind_Resume'
C:\iwmake\build_mingw_opensource\src\winmain/qtmain_win.cpp:135: undefined refer
ence to `_Unwind_Resume'
c:/Qt/4.6.2/lib/libqtmaind.a(qtmain_win.o):C:\iwmake\build_mingw_opensource\src\
winmain/../../include/QtCore/../../src/corelib/tools/qvector.h:482: undefined re
ference to `_Unwind_Resume'
c:/Qt/4.6.2/lib/libqtmaind.a(qtmain_win.o):C:\iwmake\build_mingw_opensource\src\
winmain/../../include/QtCore/../../src/corelib/tools/qvector.h:483: undefined re
ference to `_Unwind_Resume'
c:/Qt/4.6.2/lib/libqtmaind.a(qtmain_win.o):qtmain_win.cpp:(.eh_frame+0x12): unde
fined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Just two symbols,

__gxx_personality_v0

_Unwind_Resume

So googling resulted me these things,

http://stackoverflow.com/questions/329059/what-is-gxx-personality-v0-for

as as they tell , they told to declare those symbols as void * , then I change the source code

like this.


/****************************************************************************
 * Qt tutorial
 *
 *
*****************************************************************************/
#include <qapplication.h>
#include <qpushbutton.h>

void * __gxx_personality_v0=0;
void * _Unwind_Resume =0;

int main ( int argc , char ** argv )
{
 QApplication a(argc,argv);

 QPushButton hello( "hello World!",0);
 hello.resize(100,30);

 //a.setMainWidget(&hello);
 hello.show();
 return a.exec();
}

Then it resolved the linked errors, and in the .\debug directory I got the QT4.exe file, and it running nicely. BINGO.

BUT not BINGO , because those are some two functions that used for exception handling and

stack unwinding. So we declare them to zero. If exception occurred or somehow will invoke

these functions it will crash with the error message Invalid Memory access ( because we

can’t access the memory location at 0x00000000h ,in the protected mode under win32 no !

anyway this is a KLUDGE hack ,the right thing is implement these functions.

–Happy Coding–



{March 11, 2010}   Hello world!

Hi,


My name is Sandun Dhammika Perera.
I am a EXTERN undergraduate student of UCSC(university of colombo school of computing)

View Larger Map
I’m still an undergraduate but I want to tell you that my imaganation is beyond the president of my sweet little
island called sri lanka. Oh yes I’m living at sri lanka.

My Contract Information:

No 29,

Dias Place,

Panadura,

Sri Lanka

My email: sandundhammikaperera@gamil.com/sandundhammikaperera@yahoo.com
My Telephone: (+094)382236254
Mobil: -will update later-

And about my family background, my mother is a teacher and father is a ex-engineer and now he is running his own
business,and I have a one sister and no brothers and my sister was already married.And I’m alone at my home with
my computer,that’s all.

–Have A Nice Day–



et cetera
Follow

Get every new post delivered to your Inbox.