Как переместить один объект из другого класса с помощью клавиатуры в OpenGL?

Как я могу перенести с клавиатуры свою сферу в OpenGL, если моя сфера - это один класс?

главный:

#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);
}

Сфера класса:

#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();
    }
};

Я пытаюсь переместить его с помощью glutSpecialFunc(sfera1.keyboardown) или с помощью auxKeyFunc(AUX_RIGHT,phere1.MutaDreapta1), но в обоих случаях я не могу... может кто-нибудь сказать мне, как это сделать?

1 ответ

Действительно ли перенасыщение и вспомогательное взаимодействие совместимы? или glutSpecialFunc - опечатка для auxSpecialFunc?;-)

в любом случае часто лучше использовать (перенасыщение)KeyboardFunc (в некоторых системах Up/Down/Special делают странные вещи).

Другие вопросы по тегам