Hello,
I’m working on a program that I need to be able to draw circles and squares and be able to pick the color of them and also be able to clear the screen. I have all of this done, but I also need to be able to limit the number of objects drawn to 10 and also be able to keep track of the objects so that I can select them and delete them in any order and if I delete say for example the 10th object then I can draw another object if I want. I’m having trouble doing this and I would appreciate any help possible.
Sincerely yours;
jdm2
code:
using namespace std;
#include <iostream>
#include <GLUT/glut.h>
#include <cmath>
int minsquaresize = 10;
int maxsquaresize = 100;
int currentsize = 10;
int xA,yA,xB,yB;
int looprun = 0;
void myMouse(int,int, int, int);
void myDisplay();
void myReshape(int, int);
void myinit();
double PI = 3.14159;
bool square = true;
bool circle = false;
bool drawSquare = true;
bool drawCircle = false;
GLsizei winheight = 500, winwidth = 500; /* initial window size */
void myDisplay()
{
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
winheight = h;
winwidth = w;
glutPostRedisplay();
}
void myinit()
{
/* set clear color to grey */
glClearColor(0.75, 0.75, 0.75, 1.0);
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
}
void drawbox(int x, int y, int x1, int y1)
{
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x, y1);
glVertex2f(x1, y1);
glVertex2f(x1, y);
glEnd();
glFlush();
}
void glCircle3i(GLint x, GLint y, GLint x1, GLint y1)
{
int radius = sqrt(fabs(((x1-x)(x1-x))+((y1-y)(y1-y))));
glPushMatrix();
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glLineWidth(1.0f);
glBegin(GL_POLYGON);
float xmod, ymod, step = PI / (4* radius); //don't compute much more than is necessary for a solid circle
for(float theta = 0; theta <= PI/4; theta += step)
{
xmod = (cos(theta) * radius);
ymod = (sin(theta) * radius);
//create solids by vertices
glVertex2f(x + xmod, y - ymod);
//and using glpolygon for filling
glVertex2f(x + xmod, y + ymod);
glVertex2f(x - xmod, y - ymod);
glVertex2f(x - xmod, y + ymod);
//reflect points through the center and over a 45 degree (PI/4) diagonal
glVertex2f(x + ymod, y - xmod);
//for 1/2 loop time
glVertex2f(x + ymod, y + xmod);
glVertex2f(x - ymod, y - xmod);
glVertex2f(x - ymod, y + xmod);
}
glEnd();
glFlush();
glEnable(GL_TEXTURE_2D);
glPopMatrix();
}
void myMouse(int btn, int state, int x, int y)
{
y = winheight - y;
switch(btn)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
if(looprun == 0)
{
xA = x;
yA = y;
looprun++;
}
else if(looprun == 1)
{
xB = x;
yB = y;
if(drawSquare)
drawbox(xA, yA, xB, yB);
else if(drawCircle)
glCircle3i(xA, yA, xB, yB);
looprun = 0;
}
}
break;
}
}
void mainmenu(int id)
{
switch (id)
{
case 1:
drawCircle = true;
drawSquare = false;
break;
case 2:
drawSquare = true;
drawCircle = false;
break;
case 3: glClear(GL_COLOR_BUFFER_BIT);
glFlush();
break;
case 4: exit(0);
}
}
void color_menu(int id)
{ switch(id)
{
//Violet
case 1: glColor3ub(255, 62, 150);
break;
//Indigo
case 2: glColor3ub( 75, 0, 130);
break;
//Blue
case 3: glColor3ub(0,0,255);
break;
//Green
case 4: glColor3ub(0,255,0);
break;
// Yellow
case 5: glColor3ub(255, 255, 0);
break;
// Orange
case 6: glColor3ub(255, 165, 0);
break;
// Red
case 7: glColor3ub(255,0,0);
break;
// White
case 8: glColor3ub(255,255,255);
break;
//Black
case 9: glColor3ub(0,0,0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(winwidth, winheight);
glutCreateWindow(“Basic Menu and Mouse Functions”);
glutDisplayFunc(myDisplay);
int cmenu = glutCreateMenu(color_menu);
glutAddMenuEntry("Violet",1);
glutAddMenuEntry("Indigo",2);
glutAddMenuEntry("Blue",3);
glutAddMenuEntry("Green",4);
glutAddMenuEntry("Yellow",5);
glutAddMenuEntry("Orange",6);
glutAddMenuEntry("Red",7);
glutAddMenuEntry("White",8);
glutAddMenuEntry("Black",9);
glutCreateMenu(mainmenu);
glutAddMenuEntry("Make Circles", 1);
glutAddMenuEntry("Make Squares", 2);
glutAddSubMenu("Colors",cmenu);
glutAddMenuEntry("Clear",3);
glutAddMenuEntry("Quit",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
myinit ();
glutReshapeFunc(myReshape);
glutMouseFunc (myMouse);
glClear(GL_COLOR_BUFFER_BIT);
glutMainLoop();
}