opengl - how to make my cube rotate using the key functions from the keyboard -


im creating simple textured cube im have problem keyboard functions not seem interact,i want cube move in x , y direction not moving,i used function

void specialkeys( int key, int x, int y ) {   if (key == glut_key_right)     rotate_y += 5;   else if (key == glut_key_left)     rotate_y -= 5;   else if (key == glut_key_up)     rotate_x += 5;   else if (key == glut_key_down)     rotate_x -= 5;   glutpostredisplay();  } 

and in main function used code

glutspecialfunc(specialkeys); 

this whole code

 #include <stdlib.h> #include <gl/glut.h> #include "rgbimage.h"  void specialkeys(); double rotate_y=0;  double rotate_x=0; glfloat xrotated, yrotated, zrotated; gluint  texture[1];         // storage 1 texture ( new ) void loadtexturefromfile(char *filename) {       glclearcolor (0.0, 0.0, 0.0, 0.0);    glshademodel(gl_flat);    glenable(gl_depth_test);    rgbimage thetexmap( filename );       glgentextures(1, &texture[0]);                  // create texture         glbindtexture(gl_texture_2d, texture[0]);    gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat);    gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat);    gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest);    gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest);          // typical texture generation using data bitmap          glteximage2d(gl_texture_2d, 0, 3, thetexmap.getnumcols(), thetexmap.getnumrows(), 0, gl_rgb, gl_unsigned_byte, thetexmap.imagedata() );  } void drawscene(void) {    glclear(gl_color_buffer_bit | gl_depth_buffer_bit);    glenable(gl_texture_2d);     glbindtexture(gl_texture_2d, texture[0]);    glloadidentity();    gltranslatef(0.0,0.0,-5);      glrotatef(yrotated, 0, 1, 0);      glrotatef(zrotated, 0, 0, 1);    glbegin(gl_quads);         // front face          // face         gltexcoord2f(1.0f, 0.0f); glvertex3f(-1.0f, -1.0f, -1.0f);         gltexcoord2f(1.0f, 1.0f); glvertex3f(-1.0f,  1.0f, -1.0f);         gltexcoord2f(0.0f, 1.0f); glvertex3f( 1.0f,  1.0f, -1.0f);         gltexcoord2f(0.0f, 0.0f); glvertex3f( 1.0f, -1.0f, -1.0f);         // top face          // bottom face         gltexcoord2f(1.0f, 1.0f); glvertex3f(-1.0f, -1.0f, -1.0f);         gltexcoord2f(0.0f, 1.0f); glvertex3f( 1.0f, -1.0f, -1.0f);         gltexcoord2f(0.0f, 0.0f); glvertex3f( 1.0f, -1.0f,  1.0f);         gltexcoord2f(1.0f, 0.0f); glvertex3f(-1.0f, -1.0f,  1.0f);         // right face         gltexcoord2f(1.0f, 0.0f); glvertex3f( 1.0f, -1.0f, -1.0f);         gltexcoord2f(1.0f, 1.0f); glvertex3f( 1.0f,  1.0f, -1.0f);         gltexcoord2f(0.0f, 1.0f); glvertex3f( 1.0f,  1.0f,  1.0f);         gltexcoord2f(0.0f, 0.0f); glvertex3f( 1.0f, -1.0f,  1.0f);         // left face         gltexcoord2f(0.0f, 0.0f); glvertex3f(-1.0f, -1.0f, -1.0f);         gltexcoord2f(1.0f, 0.0f); glvertex3f(-1.0f, -1.0f,  1.0f);         gltexcoord2f(1.0f, 1.0f); glvertex3f(-1.0f,  1.0f,  1.0f);         gltexcoord2f(0.0f, 1.0f); glvertex3f(-1.0f,  1.0f, -1.0f);     glend();     glflush();    gldisable(gl_texture_2d);  }  void resizewindow(int x, int y) {  if (y == 0 || x == 0) return;     glmatrixmode(gl_projection);       glloadidentity();          gluperspective(40.0,(gldouble)x/(gldouble)y,0.5,20.0);     glmatrixmode(gl_modelview);     glviewport(0,0,x,y); }  void specialkeys( int key, int x, int y ) {   if (key == glut_key_right)     rotate_y += 5;   else if (key == glut_key_left)     rotate_y -= 5;   else if (key == glut_key_up)     rotate_x += 5;   else if (key == glut_key_down)     rotate_x -= 5;   glutpostredisplay();  } char* filename = "./salt_on_spoon.bmp";  int main(int argc, char** argv) {    glutinit(&argc, argv);    glutinitdisplaymode(glut_single | glut_rgb | glut_depth);    glutinitwindowsize(240, 240);    glutinitwindowposition(100, 100);    glutcreatewindow(argv[0]);    loadtexturefromfile( filename );    glutdisplayfunc(drawscene);    glutspecialfunc(specialkeys);    glutreshapefunc(resizewindow);    glutmainloop();    return 0; } 

your callback modifying rotate_x , rotate_y, you're using different variables rotations:

glrotatef(yrotated, 0, 1, 0); glrotatef(zrotated, 0, 0, 1); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -