我的程序在编译时显示图像和文本。每次单击鼠标左键或右键时,我都会尝试添加/修改对象。我试图循环它所以每次我点击鼠标按钮添加或移动的东西。我尝试了但是在第二次点击后没有出现任何内容。
#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();
if (onMouse == 2)
Time2();
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 ( );
}
答案 0 :(得分:0)
应实现鼠标点击回调,例如那样:
void mouseClicks(int button, int state, int x, int y) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
if(++onMouse > 3) {
onMouse = 1;
}
}
}
下次我高度建议固定问题而不是发布代码墙,而不清楚问题本身的描述。虽然这个网站确实是问题和答案,但即使在最简单的问题上你会被卡住,下次如何编写代码?你看到你的绘图大概是不正确的;它取决于什么? onMouse
值。printf
当你看到问题时,它现在有什么价值? (至少你可以{{1}}它,但我建议开始使用调试器;它更加棒极了)。这个值应该被接受吗?哪里变了?变化是否正确?这是您在发现问题时应该问自己的所有问题,或者不要期待任何改进。