How to input and output in the openGL window, not console? C++

I am trying to create text output in the openGL window. I want to be able to click in a spot on the window and then type a word or phrase straight from the openGL window and not from the console. How do I do that?
Here is my code so far:

#include <iostream>
#include <GL/glut.h>// include GLUT library
#include <string>
//***********************************************************************************
float x, y;
std::string input;
float red = 1.0f, blue = 1.0f, green = 1.0f;
void drawPoints()
{

	//print text
	//glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
	glColor3f(red, blue, green);
	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 mouse(int button, int state, int mousex, int mousey)
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		x = mousex - 400 / 2;
		y = -mousey + 400 / 2;
		red = 0.0;
		green = 0.0;
		blue = 1.0;
		
	}

	else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)//undo(clear)the drawing
	{
		//x = mousex - 400 / 2;
		//y = -mousey + 400 / 2;
		red = 0.0;
		green = 1.0;
		blue = 0.0;
	}


	//glutPostRedisplay();
}
void keyPressed(unsigned char key, int x, int y) {
	
	std::getline(std::cin, input);
}
//***********************************************************************************
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
}

//***********************************************************************************
int main(int argc, char** argv)
{	glutInit(& argc, argv);                  // optional in our environment

	//std::cout << "Enter a string: ";
	

	glutInitWindowSize(400, 400);    // specify a window size
	glutInitWindowPosition(100, 0);   // specify a window position
	glutCreateWindow("Simple Point Drawing"); // create a titled window
	
	myInit();         // setting up
	
	glutDisplayFunc(myDisplayCallback);  // register a callback
	glutMouseFunc(mouse);
	glutKeyboardFunc(keyPressed);
	glutMainLoop();       // get into an infinite loop

	return 0;
}

OpenGL itself doesn’t have anything to do with user input. With GLUT, you can use glutKeyboardFunc and glutSpecialFunc to set callbacks to receive keyboard input (the former is for keys which generate characters, the latter for cursor keys etc). From that, at the simplest level you can simply append each printable character to a buffer. More complex line editing is possible if you want to make the effort (look at the source code for a text widget in any open-source GUI toolkit for reference), but you can’t implement e.g. inter-application copy/paste using GLUT.

If you need to support languages other than English, things start getting more complicated. While GLUT will typically handle Shift+<key> and AltGr+<key> combinations itself, it won’t handle compose sequences and is limited to some unspecified unibyte encoding (AFAICT, this seems to be ISO-8859-1 for FreeGLUT).

For simple text rendering with a limited repertoire and using a compatibility profile context, you can use GLUT’s text functions. If you want more choice of fonts or a wider range of characters, use FreeType to create bitmaps or textures then render those using either glBitmap (compatibility profile) or textured triangles (core profile).

So using glutKeyboardFunc how do I append each printable character to a buffer?

std::string buffer;

static void key(unsigned char key, int x, int y)
{
    buffer.push_back((char) key);
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
...
    glutKeyboardFunc(key);
...
}
1 Like

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