Archive Pages Design$type=blogging

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 func...

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 -


COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: Tutorial How to display strings in OpenGL mini projects
Tutorial How to display strings in OpenGL mini projects
http://1.bp.blogspot.com/-IwnvKmxnXMk/T4ltPC5PEgI/AAAAAAAABKE/2VJSsLJ-wNw/s320/drawstringdemo.JPG
http://1.bp.blogspot.com/-IwnvKmxnXMk/T4ltPC5PEgI/AAAAAAAABKE/2VJSsLJ-wNw/s72-c/drawstringdemo.JPG
OpenGL Projects
http://www.openglprojects.in/2012/04/tutorial-how-to-display-strings-in.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2012/04/tutorial-how-to-display-strings-in.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago