【问题标题】:How to move one object from another class with keyboard in OpenGL?如何在OpenGL中使用键盘从另一个类中移动一个对象?
【发布时间】:2015-08-12 19:18:01
【问题描述】:

如果我的 Sphere 是一类,我如何才能在 OpenGL 中从键盘移动我的 Sphere?

主要:

#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, sphere1.MutaDreapta1) 移动它,但在这两种情况下我都无法...谁能告诉我该怎么做?

【问题讨论】:

    标签: c++ opengl glut opengl-1.x


    【解决方案1】:

    glut 和 aux 真的应该可以互操作吗? 还是 glutSpecialFunc 是 auxSpecialFunc 的错字? ;-)

    无论如何,使用 (glut)KeyboardFunc 通常会更好(在某些系统上,Up/Down/Special 会做一些奇怪的事情)。

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • 几分钟前我没有发表评论的特权;-)。
    • ...无论如何,为什么我的答案“不要混合 aux 和 glut”不是一个有效的答案?
    • glut 和 aux 真的应该可以互操作吗?”看起来更像是一个问题。 “Don't mix aux and glut”会是一个更好的答案,如果那是你写的。您应该编辑您的答案,以便更明显地提供解决方案。谢谢。
    • 不:我从来没有尝试过混合 glut 和 aux,所以我不会让自己断言“这是错误的”,即使 99% 肯定。因此,适当的方法是指出,如果我错了并且那个人知道他在做什么,那么问题可能是 xxx。想想你在这里得到的所有反对票,人们对事情的理解比你少,或者是某种编码风格的 ayatollah肯定的。
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多