c++ - How to move one object from another class with keyboard in OpenGL? -
how can move keyboard sphere in opengl if sphere 1 class?
main:
#include "glos.h" #include <gl.h> #include <glu.h> #include <glut.h> #include <glaux.h> #include <math.h> #include"sphere.cpp" using namespace std; void myinit(void); void callback myreshape(glsizei w, glsizei h); void callback display(void); sphere sfera1(0, 0, 0, 1); sphere sfera2(5, 0, 0, 1); void myinit(void) { glenable(gl_lighting); // activare iluminare glenable(gl_light0); // activare sursa 0 gldepthfunc(gl_less); glenable(gl_depth_test); } void callback display(void) { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); sfera1.draw(); sfera2.draw(); glflush(); } void callback myreshape(glsizei w, glsizei h) { if (!h) return; glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); gluperspective(65.0, (glfloat)w / (glfloat)h, 1.0, 20.0); glmatrixmode(gl_modelview); glloadidentity(); glrotated(25, 0.0, 1.0, 1.0); gltranslatef(0.0, -1.0, -6.0); } int main(int argc, char** argv) { auxinitdisplaymode(aux_single | aux_rgb | aux_depth16); auxinitposition(0, 0, 850, 850); auxinitwindow("iluminarea"); myinit(); glutspecialfunc(sfera1.keyboardown); auxreshapefunc(myreshape); auxmainloop(display); return(0); }
sphere class:
#include "glos.h" #include <glut.h> #include <math.h> class sphere{ public: float x, y, z, r; bool testcolision = false; sphere(float x, float y, float z, float r){ this->x = x; this->y = y; this->z = z; this->r = r; } bool colision(sphere sfera){ float d = sqrt(pow(++x - ++sfera.x, 2) + pow(++y - ++sfera.y, 2) + pow(++z - ++sfera.z, 2)); if (d <= r + sfera.r){ return true; } else{ return false; } } void draw(){ glpushmatrix(); gltranslatef(x, y, z); glutsolidsphere(r, 100, 100); glpopmatrix(); } void keyboardown(int key, int x, int y) { if (key == 'w') this->x += x; if (key == 'a') this->x -= x; if (key == 'd') this->y += y; if (key == 's') this->y -= y; glutpostredisplay(); } };
i try move glutspecialfunc(sfera1.keyboardown) or auxkeyfunc(aux_right, sphere1.mutadreapta1) in both case cant... can tell me how that?
are glut , aux supposed inter-operable ? or glutspecialfunc typo auxspecialfunc ? ;-)
anyway it's better use (glut)keyboardfunc (on systems, up/down/special doing bizarre things).
Comments
Post a Comment