Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在opengl/glut中使用gluLookAt和鼠标更改视角_C++_Opengl_Glut - Fatal编程技术网

C++ 在opengl/glut中使用gluLookAt和鼠标更改视角

C++ 在opengl/glut中使用gluLookAt和鼠标更改视角,c++,opengl,glut,C++,Opengl,Glut,您好,我正在尝试使用鼠标功能移动gluLookAt的透视图到目前为止我已经尝试根据鼠标位置调整upX和upY,但是我希望程序能够根据鼠标移动围绕对象进行整个360度旋转,并且希望在鼠标移动到窗口停止。任何帮助都将不胜感激,我仍在学习中 #include <iostream> #include <math.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> flo

您好,我正在尝试使用鼠标功能移动gluLookAt的透视图到目前为止我已经尝试根据鼠标位置调整upX和upY,但是我希望程序能够根据鼠标移动围绕对象进行整个360度旋转,并且希望在鼠标移动到窗口停止。任何帮助都将不胜感激,我仍在学习中

#include <iostream>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

float posX=4, posY=6, posZ=5, targetX=0, targetY=0, targetZ=0, upX=0, upY=1, upZ=0;

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, 4.0/3.0, 1, 40);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(posX, posY, posZ, targetX, targetY, targetZ, upX, upY, upZ);  

    glColor3f(1.0, 1.0, 1.0);
    glutWireTeapot(1.5);

    glBegin(GL_LINES);
      glColor3f(1, 0, 0); glVertex3f(0, 0, 0); glVertex3f(10, 0, 0);
      glColor3f(0, 1, 0); glVertex3f(0, 0, 0); glVertex3f(0, 10, 0);
      glColor3f(0, 0, 1); glVertex3f(0, 0, 0); glVertex3f(0, 0, 10);
    glEnd();

    glFlush();
}

void usage(){
    std::cout<< "\n\n\
          q,Q: Quit\n\n" ;
          std::cout.flush();
}

void onMouseMove(int x, int y)
{
    posX = x*cos(posY) + PosY*sin(PosX)*sin(yRot) - dz*cos(xRot)*sin(yRot) 
    glutPostRedisplay();
}

void KeyboardFunc (unsigned char key, int eyeX, int eyeY)
{
    switch (key)
    {
       case 'q':
       case 'Q':
           exit(0);
           break;
    }
    glutPostRedisplay();
}

void init() 
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    usage();
}

int main(int argc, char** argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(300,250);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Final");
    init();
    glutDisplayFunc(display);
    glutPassiveMotionFunc(&onMouseMove);
    glutKeyboardFunc(&KeyboardFunc); 
    glutMainLoop();
}
#包括
#包括
#包括
#包括
#包括
浮点数posX=4,posY=6,posZ=5,targetX=0,targetY=0,targetZ=0,upX=0,upY=1,upZ=0;
无效显示(){
glClear(GLU颜色缓冲位);
glMatrixMode(GL_投影);
glLoadIdentity();
gluPerspective(60.0,4.0/3.0,1,40);
glMatrixMode(GLU模型视图);
glLoadIdentity();
gluLookAt(posX、posY、posZ、targetX、targetY、targetZ、upX、upY、upZ);
GL3F(1.0,1.0,1.0);
茶壶(1.5);
glBegin(GL_行);
glColor3f(1,0,0);glVertex3f(0,0,0);glVertex3f(10,0,0);
glColor3f(0,1,0);glVertex3f(0,0,0);glVertex3f(0,10,0);
glColor3f(0,0,1);glVertex3f(0,0,0);glVertex3f(0,0,10);
格伦德();
glFlush();
}
无效用法(){

std::cout如果您想要实现一个arcball摄像头,并且您想要使用固定的函数管道矩阵堆栈,那么实际上不使用
gllookat()
而使用
glRotate/glTranslate
会更简单,如下所示:

glTranslatef(0f,0f,-半径);
glRotatef(angX,1f,0f,0f);
glRotatef(angY,0f,1f,0f);
glTranslatef(-targetX,-targetY,-targetZ);
其中,
radius
是“相机”到观察点的距离,
angX
是围绕X轴的角度,
angY
是围绕Y轴的角度,
(targetX,targetY,targetZ)
是观察点的位置(你的
targetX/Y/Z


您不必自己计算
sin/cos
(它由
glRotatef
计算)你所要做的就是在你的运动函数中设置/增加angX和angY。如果你想实现一个arcball摄像机,并且你想使用固定的函数管道矩阵堆栈,实际上不使用
gluLookAt()
而是
glRotate/glTranslate
,这样做会更简单:

glTranslatef(0f,0f,-半径);
glRotatef(angX,1f,0f,0f);
glRotatef(angY,0f,1f,0f);
glTranslatef(-targetX,-targetY,-targetZ);
其中,
radius
是“相机”到观察点的距离,
angX
是围绕X轴的角度,
angY
是围绕Y轴的角度,
(targetX,targetY,targetZ)
是观察点的位置(你的
targetX/Y/Z


您不必自己计算
sin/cos
(它由
glRotatef
计算)你所要做的就是在运动功能中设置/增加
angX
angY

请忽略当前的onMouseMove代码,当前正在尝试随机操作。你想使用lookAt在场景周围移动以始终保持场景在中心还是旋转相机?你想使用什么坐标变换来映射鼠标移动到一个球体?我不确定你所说的坐标变换是什么意思,但形状是一个茶壶,我希望能够根据鼠标在窗口中的移动,将相机在茶壶周围移动360度。实际上,你应该使用模型矩阵来做这项工作,根据鼠标移动来改变旋转角度。我认为是最好的应用程序。通常将滚动设置为零,这是一个很好的建议。请忽略当前正在尝试随机操作的onMouseMove代码。是否要使用lookAt在场景周围移动以始终保持场景在中心或旋转摄影机?要使用什么坐标变换将鼠标移动映射到球体?我不知道你所说的坐标变换是什么意思,但是形状是一个茶壶,我希望能够根据鼠标在窗口中的移动,将相机在茶壶周围移动360度。实际上,你应该使用模型矩阵来完成这项工作,根据鼠标移动来改变旋转角度。我认为这是最好的应用程序n、 通常将滚动设置为零,这是一个很好的建议。这将进入onMouseMove函数,以便在鼠标移动时使用,还是将其放置在代码中的其他位置?顺便感谢您的建议!它将进入您需要转换顶部modelview矩阵的任何位置,例如您当前对
gluLookAt()
(因此,代替
gluLookAt()
)。这将进入onMouseMove函数,以便在鼠标移动时使用,还是将其放置在代码中的其他位置?顺便感谢您的建议!它将进入您需要转换顶部modelview矩阵的任何位置,例如您当前调用的
gluLookAt()
(因此,而不是
gluLookAt())
)。