Song, thanks again for your quick reply. Here is my code so far. if I have glRasterPos2i(x, y) in the keyboard function the characters will print out on top of each other. I need to get the position before that hence the passive function. If I use the mouse function it will miss the first character I print.
#include <GL/glut.h>
#include <stdio.h>
/* globals */
GLsizei wh = 500, ww = 500; /* initial window size */
GLfloat size = 3.0; /* half side length of square */
static int submenu_shape;
static int submenu_fill;
static int menu_id;
void keyboard(unsigned char key, int x, int y)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, key);
glFlush();
}
void passive(int x, int y) //find the current position of the mouse
{
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position */
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
realy = viewport[3] - (GLint) y - 1;
glRasterPos2f(x, realy);
}
/* Right mouse button click - its menu system*/
void myRightClickMenu(int id)
{
//Right click menu options
if(id==0) //Exit has been clicked
{
exit(0);
}
if(id==1) //clear screen has been clicked
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
if(id==2) //shape->square
{
//not working yet
}
if(id==3) //shape->circle
{
//not working yet
}
if(id==4) //shape->triangle
{
//not working yet
}
if(id==5) //shape->bezier curve
{
//not working yet
}
if(id==6) //Fill->On
{
//not working yet
}
if(id==7) //Fill->Off
{
//not working yet
}
if(id==8) //Add text; with this -> where ever your mouse is is where u start typing.
{
glutPassiveMotionFunc(passive);
glutKeyboardFunc(keyboard);
}
}
/* reshaping routine called whenever window is resized or moved */
void myReshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* adjust viewport and clear */
glViewport(0,0,w,h);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
/* set global size for use by drawing routine */
ww = w;
wh = h;
}
/* myinit function */
void myinit()
{
glClearColor (0.0, 0.0, 0.0, 1.0);
glViewport(0,0,ww,wh);
/* Pick 2D clipping window to match
size of screen window. This choice avoids having to scale object
coordinates each time window is resized. */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);
/* set clear color to black and clear window */
glClearColor (0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
/* display callback required by GLUT */
void display()
{}
/* Add menu function called from Main*/
void addmenu()
{
submenu_shape = glutCreateMenu(menu_id);
glutAddMenuEntry("Square",2);
glutAddMenuEntry("Circle",3);
glutAddMenuEntry("Triangle",4);
glutAddMenuEntry("Bezier Curve",5);
submenu_fill = glutCreateMenu(menu_id);
glutAddMenuEntry("On",6);
glutAddMenuEntry("Off",7);
menu_id = glutCreateMenu(myRightClickMenu);
glutAddMenuEntry("Clear Screen", 1);
glutAddSubMenu("Shape",submenu_shape);
glutAddSubMenu("Fill",submenu_fill);
glutAddMenuEntry("Add Text",8);
glutAddMenuEntry("Exit", 0);
//menu attached to right mouse button
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
/* Main */
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Assignment 1: Rory Carroll - D04102698");
myinit ();
glutReshapeFunc (myReshape);
glutDisplayFunc(display);
addmenu(); /* call function to add menu to right mouse button */
glutMainLoop();
}