如何将"camera"放入OpenGL中的多维数据集中
How to put the "camera" inside a cube in OpenGL
我想出了如何将"相机"放在我创建的立方体中,以便我可以以类似 FPS 的风格移动。我尝试使用gluLookAt和gluPerspective,但我显然缺少一些步骤。在gluLookAt之前我应该做什么?
这是到目前为止编写的代码:
int rotate_Y; //used to rotate the cube about the Y-axis
int rotate_X; //used to rotate the cube about the X-axis
//the display function draws the scene and redraws it
void display(){
//clear the screen and the z-buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //resets the transformations
glRotatef(rotate_X, 1.0, 0.0, 0.0);
glRotatef(rotate_Y, 0.0, 1.0, 0.0);
//Front Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
//Back Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
//Right Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
//Left Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.7, 0.7, 0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
//Upper Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.7, 0.7, 0.3);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glEnd();
//Bottom Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.2, 0.2, 0.8);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
glFlush();
glutSwapBuffers(); //send image to the screen
}
//the special keys function allows interaction via keys (also special ones)
void specialKeys(int key, int x, int y){
switch (key){
case GLUT_KEY_F1:
exit(0);
break;
case GLUT_KEY_LEFT:
rotate_Y -= 5;
break;
case GLUT_KEY_UP:
rotate_X -= 5;
break;
case GLUT_KEY_RIGHT:
rotate_Y += 5;
break;
case GLUT_KEY_DOWN:
rotate_X += 5;
break;
}
glutPostRedisplay(); //request a screen refresh to see changes
}
int main(int argc, char*argv[]){
//initialize GLUT
glutInit(&argc, argv);
//request double buffering, RGB colors window and a z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
//create a window
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Space");
//enable depth
glEnable(GL_DEPTH_TEST);
//callback functions
glutDisplayFunc(display); //display - redraws the scene
glutSpecialFunc(specialKeys); //special - allows interaction with specialkeys
//pass control to GLUT for events
glutMainLoop();
return 0; //this line is never reached
}
您需要
使用 glTranslatef(float x,float y,float z) 来移动相机。
请注意,由于OpenGL的工作方式,转换实际上适用于世界其他地方,而不是相机。因此,上述函数实际上会将之后绘制的所有内容(而不是相机)移动指定的量。
要让相机移动到某个位置,您需要在将位置的所有组件传递给函数之前否定它们。这将使世界朝着相反的方向移动,将相机放在您想要的地方。
相关文章:
- 如何实现高效的算法来计算大型数据集的多个不同值?
- HDF5Cpp 扩展复合数据集超板问题
- HDF5 简单读取数据集失败
- std::bad_alloc 在大数据集的 Dijkstra 计算期间
- C++中的多维数据集:从 std::vector 的 2D 数据到 std::vector 的 2D 网格的最干净方法?
- C++创建空 HDF5 文件而不是数据集的代码
- 跨线程共享数据集与拆分每个线程的数据
- pybind11:用可选数据表示并集
- 多维数据集ai中的ai_float是什么
- 适用于频繁更改的大型数据集的最佳数据结构
- 使用程序集嵌入数据时"Undefined reference"错误,使用适用于窗口的 mingw-w64 编译(COFF 而不是 ELF)
- 将数据集从 R 传递到C++(使用 .呼叫)
- 将数据集几乎平均分配给工作线程
- 我将 c++ 矢量数据集转换为 c 数组.我将相同的数据集传递到一个应该给出相同输出的函数中
- 使用 194 点海伦数据集训练 DLib 形状预测器
- 现代 OpenGL 多维数据集旋转未按预期工作
- 构建大型无序集,并在开始时使用所有可用数据
- 数据集(和样本)何时在 boost::test 中销毁?
- 从数据集 Blob 字段中打开和编辑文件
- 用于创建 HDF5 数据集的 4 维 c++ 数组的动态内存分配