Search Projects

Saturday, April 14, 2012

Tutorial How to display strings in OpenGL mini projects

In the previous post, which highlights on making a front screen for the OpenGL mini projects a function called drawstring was shown but didn't explained well as it was assumed to be understood easily by the students but few of them demand it's code, which was not there. To help them out and sort the error they have,  here I made a simple tutorial to show, how we can display a string in the projects.

To display strings in the project we can make function in different ways, call the defined front in OpenGL API. How to code is depend on the coder, what he want to do with the string and how he want to display it. Matter of fact is that the necessary things is to make sure once a function is made it must be flexible enough that Front sizes and the colours can be changed easily when ever needed, wherever needed. Keeping in the mind that A simple function is code, of which stepwise process is defined below. You can also see the different fonts demo.



First declared  a indentifire that will hold the current font type -

void *currentfont;

After the declaration of this, we define a function which will change the fonts or say set the font which we can to to display with the strings. Code for this is easy, just make a function called setFont(), code given below -


void setFont(void *font)
{
currentfont=font;                      // Set the currentfont to the font
}




Now the most important part, Code a function drawstring(), it should contain four arguments - x, y, z coordinates and a string. While the coordinate assumed to be the space where string to be placed and the string is the value to be display.  Code is below -


void drawstring(float x,float y,float z,char *string)
{
char *c;
glRasterPos3f(x,y,z);
for(c=string;*c!='\0';c++)
{ glColor3f(0.0,0.0,0.0);
glutBitmapCharacter(currentfont,*c);
}
}

Now everything has been done. Let display something in our program. Let started with five simple sentence. You can display whatever but I have code like below -


void text(void)
{
setFont(GLUT_BITMAP_HELVETICA_18);
glColor3f(0.0,1.0,1.0);
drawstring(150.0,455.0,1.0,"* * * * * * * * * * THE DRAWSTRING FUNCTION DEMO * * * * * * * * * *");

glColor3f(0.0,0.0,1.0); // RGB color R=0 ; G=0 ; B =1; so blue!
drawstring(220.0,405.0,1.0,"I am GLUT_BITMAP_HELVETICA_18 in blue");

setFont(GLUT_BITMAP_HELVETICA_12); //Font set to helvetica with size 12
glColor3f(1.0,0.0,0.0);
drawstring(220.0,350.0,1.0,"I am GLUT_BITMAP_HELVETICA_12 in red");

      setFont(GLUT_BITMAP_TIMES_ROMAN_24);
      glColor3f(0.0,1.0,0.0);
     drawstring(220.0,295.0,1.0,"I am GLUT_BITMAP_TIMES_ROMAN_24 in green");

setFont(GLUT_BITMAP_8_BY_13);
glColor3f(1.0,1.0,1.0);
drawstring(220.0,240.0,1.0,"I am GLUT_BITMAP_8_BY_13 in white");

setFont(GLUT_BITMAP_9_BY_15);
glColor3f(1.0,0.0,1.0);
drawstring(220.0,185.0,1.0,"I am GLUT_BITMAP_9_BY_15 in pink");
glFlush();

}


Below is the Whole Program -


7 comments:

  1. i merged the above program with mine... but i am getting a white screen.. please help me.. 

    ReplyDelete
  2. Can you place the text clearly, even it is difficult to read.
    How you have paste it here, try to give line by line code. It will took alot of time to seperate different functions in proper way.



    what is this? 
    it like html tags! 
    remove it!

    ReplyDelete
  3. hey.. i hv mailed the code to you.. please check your yahoo id and please reply :)

    ReplyDelete
  4. Supreethak1 can you mail me your code to sachin.dev.v@gmail.com

    ReplyDelete
  5. please mail me the code to patilpsd15@gmail.com

    ReplyDelete
  6. give me any idae for 3d text

    ReplyDelete
  7. I had an project code for 3D text but it have some error which need to be sorted out. If you want i can post it here but i can't help you out correcting the errors as i have my own project and i also have some other works that why now i am out of work for those projects. Also now the time had came and most of the students had either build the project or got it from somewhere so nothing for me to do.

    ReplyDelete