OpenGL: Error moving object using keyboard -
i'm new opengl , i'm trying paint stickman , move using arrow-keys keyboard. idea use global variables stickman , change them when key pressed. afterwards draw-function (mydisplay()) called again. unfortunately following error-message: "error 10 error c2371: 'mydisplay' : redefinition; different basic types " when replace mydisplay()-call in keyboard-function glutpostredisplay() suggested in tutorials read error message disapears , build successful. stickman doesn't move when pressing keys.
here's code:
#include <gl/glut.h> glint x; glint y; glint d; //parameters stickman void mykeyboard(unsigned char key, int mx, int my) { int x1 = mx; int y1 = 480 - my; switch(key){ case glut_key_left : x = x-50; mydisplay(); break; case 'e' : exit(-1); break; default: break; } } void stickman () { glbegin(gl_lines); //body glvertex2i(x, y); glvertex2i(x, y-2*d); glvertex2i(x-d, y-d); glvertex2i(x+d, y-d); glvertex2i(x, y-2*d); glvertex2i(x-d, y-3*d); glvertex2i(x, y-2*d); glvertex2i(x+d, y-3*d); glend(); glbegin(gl_line_loop); //head glvertex2i(x,y); glvertex2i(x+0.5*d, y); glvertex2i(x+0.5*d, y+0.5*d); glvertex2i(x-0.5*d, y+0.5*d); glvertex2i(x-0.5*d, y); glend(); } void mydisplay() { glclear(gl_color_buffer_bit); glcolor3f(0.0, 0.0, 0.0); stickman(); glflush(); } void myinit() { glclearcolor(1.0, 1.0, 1.0, 1.0); glmatrixmode(gl_projection); glloadidentity(); gluortho2d(0.0, 640.0, 0.0, 480.0); } void main(int argc, char** argv) { x = 320; y = 350; d = 100; glutinit(&argc, argv); glutinitdisplaymode(glut_single | glut_rgb); glutinitwindowsize(640, 480); glutinitwindowposition(200, 50); glutcreatewindow("stickman"); glutdisplayfunc(mydisplay); glutkeyboardfunc(mykeyboard); glutpostredisplay(); myinit(); glutmainloop(); }
as found out (glut keyboard tutorial) problem wasn't repaint of picture, rather left arrow key wasn't recognized. needed write keyboard-function special keys such arrow keys.
now code looks , works:
void processnormalkeys (unsigned char key, int mx, int my) { if (key == 'e') exit(-1); } void processspecialkeys (int key, int mx, int my) { switch(key){ case glut_key_left : x = x-5; glutpostredisplay(); break; case glut_key_right : x = x+5; glutpostredisplay(); break; case glut_key_up : y = y+5; glutpostredisplay(); break; case glut_key_down : y = y-5; glutpostredisplay(); break; default: break; } }
i had adjust main function:
glutkeyboardfunc(processnormalkeys); glutspecialfunc(processspecialkeys);
but anyways!
Comments
Post a Comment