Writing text in OpenGL C++

I am trying to be able to write text in the openGL window. I want to first make it so that where ever the cursor is, that is where it will start writing my input text. So far I have only been able to make it start typing but the words stack on top of each other. How do I fix this? Next I would like to make a menu so that I can have the options to change colors and font and also quite the program. How do I do that?

Below is my code:

#include <iostream>
#include <GL/glut.h>// include GLUT library
#include <string>
//***********************************************************************************
float x, y;
double speed = 10;
float mousePressed = true;
float firstPress = true;
std::string input;
float red = 1.0f, green = 1.0f, blue = 1.0f;
//***********************************************************************************
void drawPoints()
{
	//print text
	//glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
	glColor3f(red, green, blue);
	glRasterPos2i(x, y);
	for (std::size_t x = 0; x < input.size(); x++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, input[x]);
	}

	//axis labels
	glColor3f(0, 0, 0);
	glRasterPos2i(155, -5);
	glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'X');
	glRasterPos2i(-5, 155);
	glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, 'Y');

	glPointSize(1);
	glBegin(GL_POINTS); // use points to form X-/Y-axes

	  // change drawing color to black
	for (int x = -150; x <= 150; x++) // draw X-axis
		glVertex2i(x, 0);
	for (int y = -150; y <= 150; y++) // draw Y-axis
		glVertex2i(0, y);
	glEnd();
}
//***********************************************************************************
void myInit()
{
	glClearColor(1, 1, 1, 0);   // specify a background clor: white
	gluOrtho2D(-200, 200, -200, 200);  // specify a viewing area
}

//***********************************************************************************
void myDisplayCallback()
{
	glClear(GL_COLOR_BUFFER_BIT); // draw the background

	drawPoints();

	glFlush(); // flush out the buffer contents
}
//***********************************************************************************
void mouse(int button, int state, int mousex, int mousey)
{

	//x = mousex - 400 / 2;
	//y = -mousey + 400 / 2;


	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		/*if (firstPress == true) {		//First mouse click set position of typed word or phrase.

			firstPress = false;
		}*/

		red = 0.0;						//Change color to blue
		green = 0.0;
		blue = 1.0;

		mousePressed = true;

	}

	else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
	{
		red = 0.0;
		green = 1.0;					//Change color to green
		blue = 0.0;
	}
	myDisplayCallback();
	//glutPostRedisplay();
}
//***********************************************************************************
void keyPressed(unsigned char key, int mousex, int mousey) {
	x = mousex - 400 / 2;
	y = -mousey + 400 / 2;
	input.push_back((char)key);
	glColor3f(red, green, blue);
	if (firstPress == true) {
		glRasterPos2i(x, y);
		firstPress = false;

	}
	else if (firstPress == false) {
		mousex += 1;
		glRasterPos2i(x, y);
	}
	glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, key);

	glFlush();
	//glutPostRedisplay();

}
//***********************************************************************************
void myMotionCallBack(int mousex, int mousey)
{

	if (mousePressed == true)
	{

		x = mousex - 400 / 2;
		y = -mousey + 400 / 2;
		myDisplayCallback();

		glColor3f(red, green, blue);
		glRasterPos2i(x, y);
		for (std::size_t x = 0; x < input.size(); x++) {
			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, input[x]);
		}
	}
	glFlush(); // flush out the buffer contents
}
//***********************************************************************************
int main(int argc, char** argv)
{
	glutInit(&argc, argv);                  // optional in our environment

	glutInitWindowSize(400, 400);    // specify a window size
	glutInitWindowPosition(100, 0);   // specify a window position
	glutCreateWindow("Drawing Window"); // create a titled window

	myInit();         // setting up

	glutDisplayFunc(myDisplayCallback);  // register a callback
	glutMouseFunc(mouse);
	glutMotionFunc(myMotionCallBack);
	glutKeyboardFunc(keyPressed);
	glutMainLoop();       // get into an infinite loop

	return 0;
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.