#include #include #include #include #include #include #include static float lineWidth = 0; void *font = GLUT_BITMAP_TIMES_ROMAN_24; #define MAX_LINE_WIDTH 9 void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); glOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0); // Here lies the cause of my troubles glEnable (GL_LINE_SMOOTH); glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // /glBlendFunc (GL_SRC_ALPHA, GL_ONE); glShadeModel (GL_FLAT); glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); float flt; //glGetFloatv(GL_LINE_WIDTH_RANGE, &flt); } char* itoa(int val, int base){ static char buf[32] = {0}; int i = 30; for(; val && i ; --i, val /= base) buf[i] = "0123456789abcdef"[val % base]; return &buf[i+1]; } void ftoa(float Value, char* Buffer) { union { float f; struct { unsigned int mantissa_lo : 16; unsigned int mantissa_hi : 7; unsigned int exponent : 8; unsigned int sign : 1; }; } helper; unsigned long mantissa; signed char exponent; unsigned int int_part; char frac_part[3]; int i, count = 0; helper.f = Value; //mantissa is LS 23 bits mantissa = helper.mantissa_lo; mantissa += ((unsigned long) helper.mantissa_hi << 16); //add the 24th bit to get 1.mmmm^eeee format mantissa += 0x00800000; //exponent is biased by 127 exponent = (signed char) helper.exponent - 127; //too big to shove into 8 chars if (exponent > 18) { Buffer[0] = 'I'; Buffer[1] = 'n'; Buffer[2] = 'f'; Buffer[3] = '\0'; return; } //too small to resolve (resolution of 1/8) if (exponent < -3) { Buffer[0] = '0'; Buffer[1] = '\0'; return; } count = 0; //add negative sign (if applicable) if (helper.sign) { Buffer[0] = '-'; count++; } //get the integer part int_part = mantissa >> (23 - exponent); //convert to string char* res= itoa(int_part, 10 ); strcpy(&Buffer[count] , res); //find the end of the integer for (i = 0; i < 8; i++) if (Buffer[i] == '\0') { count = i; break; } //not enough room in the buffer for the frac part if (count > 5) return; //add the decimal point Buffer[count++] = '.'; //use switch to resolve the fractional part switch (0x7 & (mantissa >> (20 - exponent))) { case 0: frac_part[0] = '0'; frac_part[1] = '0'; frac_part[2] = '0'; break; case 1: frac_part[0] = '1'; frac_part[1] = '2'; frac_part[2] = '5'; break; case 2: frac_part[0] = '2'; frac_part[1] = '5'; frac_part[2] = '0'; break; case 3: frac_part[0] = '3'; frac_part[1] = '7'; frac_part[2] = '5'; break; case 4: frac_part[0] = '5'; frac_part[1] = '0'; frac_part[2] = '0'; break; case 5: frac_part[0] = '6'; frac_part[1] = '2'; frac_part[2] = '5'; break; case 6: frac_part[0] = '7'; frac_part[1] = '5'; frac_part[2] = '0'; break; case 7: frac_part[0] = '8'; frac_part[1] = '7'; frac_part[2] = '5'; break; } //add the fractional part to the output string for (i = 0; i < 3; i++) if (count < 7) Buffer[count++] = frac_part[i]; //make sure the output is terminated Buffer[count] = '\0'; } void output(int x, int y, const char *string) { int len, i; glRasterPos2f(x, y); len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(font, string[i]); } } void display (void) { // Just draw a black line on white background glClear (GL_COLOR_BUFFER_BIT); glColor3f (0.0, 0.0, 100.0); char strLineWidth [10]; // std::string strstr(" line width = "); //strstr += lineWidth; ftoa(lineWidth, strLineWidth); char buff[100]; strcpy(buff ," line width = "); strcat(buff, strLineWidth); output(0, 0, buff); glFlush (); glLineWidth (lineWidth); glBegin (GL_LINE_STRIP); glVertex3f (0.50, 0.0, 1.0); glVertex3f (0.51, 1.0, 0.0); glEnd (); glFlush (); if(lineWidth > MAX_LINE_WIDTH) { output(20.0f, 20.0f, "READY!"); glFlush (); } } void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { lineWidth += 0.1; } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { lineWidth -= 0.1; } display(); } void timer(int v) { lineWidth += 0.1; if (lineWidth > MAX_LINE_WIDTH) { glutPostRedisplay(); return; } glutPostRedisplay(); glutTimerFunc(40000/60, timer, v); } int main(int argc, char** argv) { // Standard GLUT init glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutDisplayFunc (display); glutMouseFunc(mouse); glutTimerFunc(100, timer, 0); glutMainLoop (); return 0; }