PDA

View Full Version : Click mouse to add - OpenGL



binarydude87
06-23-2014, 05:03 PM
My program displays an image and text when it is compiled. I am trying to add/modify objects each time I click the left or right button of the mouse. The reason all the time functions are similar is because I am trying to change/draw over the polygon with a same size polygon but a different time. When the program starts, the polygon displays 1:30:00 PM. When I click the first time, the polygon is replaced with a same size polygon with the time of 1:45:00 PM. When I click for a second time the polygon disappears. I am trying to loop the mouse click so I can keep replacing the polygon with a different time each time I click the left button. Any help is appreciated.



#include<windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<Gl/gl.h>
#include<GL/glut.h>

int n;
int m;
int onMouse = 1;
int *image;
void *font = GLUT_BITMAP_HELVETICA_12;

void mouseClicks(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON)
onMouse++;

}

void output(int x, int y, char *string)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}
void Time1 ()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0,0.8,0.0);
glBegin(GL_POLYGON);
glVertex2f(230.0,893.0);
glVertex2f(355.0,893.0);
glVertex2f(355.0,845.0);
glVertex2f(230.0,845.0);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(230.0,893.0);
glVertex2f(355.0,893.0);
glVertex2f(355.0,845.0);
glVertex2f(230.0,845.0);
glEnd();
output(235.0,880.0,"Time");
glRasterPos2f(235.0,855.0);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'1' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,':' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'3' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,':' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,' ');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'P' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'M' );
}

void Time2 ()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0,0.8,0.0);
glBegin(GL_POLYGON);
glVertex2f(230.0,893.0);
glVertex2f(355.0,893.0);
glVertex2f(355.0,845.0);
glVertex2f(230.0,845.0);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(230.0,893.0);
glVertex2f(355.0,893.0);
glVertex2f(355.0,845.0);
glVertex2f(230.0,845.0);
glEnd();
output(235.0,880.0,"Time");
glRasterPos2f(235.0,855.0);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'1' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,':' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'4' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'5' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,':' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,' ');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'P' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'M' );
}

void Time3 ()
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0,0.8,0.0);
glBegin(GL_POLYGON);
glVertex2f(230.0,893.0);
glVertex2f(355.0,893.0);
glVertex2f(355.0,845.0);
glVertex2f(230.0,845.0);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(230.0,893.0);
glVertex2f(355.0,893.0);
glVertex2f(355.0,845.0);
glVertex2f(230.0,845.0);
glEnd();
output(235.0,880.0,"Time");
glRasterPos2f(235.0,855.0);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'2' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,':' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,':' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'0' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,' ');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'P' );
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'M' );
}

void Draw ()
{
glDrawPixels(n,m,GL_RGB, GL_UNSIGNED_INT, image);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(0.0,0.8,0.0);
glBegin(GL_POLYGON);
glVertex2f(8.0, 893.0);
glVertex2f(210.0, 893.0);
glVertex2f(210.0, 645.0);
glVertex2f(8.0, 645.0);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(8.0, 893.0);
glVertex2f(210.0, 893.0);
glVertex2f(210.0, 645.0);
glVertex2f(8.0, 645.0);
glEnd();
glColor3f(0.0,0.0,0.0);
output(13,880,"Legend");
output(13,860,"* - Custer's Headquarters Unit");
output(13,847,"F - Custer's Command");
output(13,834,"C - Custer's Command");
output(13,821,"E - Custer's Command");
output(13,808,"I - Custer's Command");
output(13,795,"L - Custer's Command");
output(13,782,"A - Reno's Command");
output(13,769,"M - Reno's Command");
output(13,756,"G - Reno's Command");
output(13,743,"H - Benteen's Command");
output(13,730,"D - Benteen's Command");
output(13,717,"K - Benteen's Command");
output(13,704,"B - Packtrain Guard");
output(13,691,"Packtrain - Blank Cavalry Unit");
output(13,678,"G - Gall (Hunkpapa Warchief)");
output(13,665,"C - Crazy Horse (Oglala Warchief");
output(13,652,"IU(n) - 34 Blank Indian Units");
}

void myInit (void)
{
glClearColor ( 1.0, 1.0, 1.0 , 1.0);
glColor3f ( 1.0f, 0.0f, 0.0f );
glPointSize ( 4.0 );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
gluOrtho2D ( 0.0, 400.0, 0.0, 400.0 );
}

void myreshape(int h, int w)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLfloat) n, 0.0, (GLfloat) m);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,h,w);
}

void display(void)
{

glClear ( GL_COLOR_BUFFER_BIT );
glRasterPos2i(0,0);
Draw();
if (onMouse == 1)
Time1();
else if (onMouse == 2)
Time2();
else if (onMouse == 3)
Time3();


glFlush ();
}

int main(int argc, char**argv)
{
FILE *fd;
int k, nm;
char c;
char b[70];
float s;
char red, green, blue;
int x, y;

fd = fopen("map.ppm", "r");
if(fd == 0)
{
exit(0);
}


fscanf(fd, "%s", b);
if((b[0] != 'P') || (b[1] != '6'))
{
printf("%s is not a PPM file!\n", b);
exit(0);
}

fscanf(fd, "%c", &c);

fscanf(fd, "%c", &c);
while(c == '#')
{
fscanf(fd, "%[^\n]", b);
printf("%s\n", b);
fscanf(fd, "%c", &c);
printf("%c", c);
}

ungetc(c,fd);

fscanf(fd, "%d %d %d", &n, &m, &k);

printf("%d rows %d colums max value = %d\n", n, m, k);

nm = n*m;

image = (int*)malloc(3*sizeof(GLint)*nm);

s = 255./k;

for(x = 0; x < m; x++)
{
for(y = n-1; y >= 0; y--)
{
fscanf(fd, "%c", &red);
fscanf(fd, "%c", &green);
fscanf(fd, "%c", &blue);

image[3*nm - 3*(x*n +y) -3] = green;
image[3*nm - 3*(x*n +y) -2] = blue;
image[3*nm - 3*(x*n +y) -1] = red;

}
}


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(n,m);
glutInitWindowPosition(0,0);
glutCreateWindow("Little BigHorn");
glutReshapeFunc(myreshape);
glutMouseFunc(mouseClicks);
glutDisplayFunc(display);
glPixelTransferf(GL_RED_SCALE, s);
glPixelTransferf(GL_GREEN_SCALE, s);
glPixelTransferf(GL_BLUE_SCALE, s);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
glClearColor(1.0, 1.0, 1.0, 1.0);
myInit ( );

glutMainLoop ( );

}