如何在 OpenGL 中为 3D 对象设置原点
how to set origin in opengl for 3d objects
我正在尝试在设置顶点和窗口位置时绘制立方体,但我没有得到正确的立方体。我在右上角得到立方体,尺寸不像我给出的那样。我是opengl的新手。请帮忙。
#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#else
#endif
void display();
void specialKeys();
double rotate_y=0;
double rotate_x=0;
void display(){
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rotate_x and rotate_y
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
// side - FRONT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0, 0, 0);
glVertex3f( 40,0,0);
glVertex3f(40,40,0 );
glVertex3f(0,40,0 );
glEnd();
// side - BACK
glBegin(GL_POLYGON);
glColor3f( 1.0,0.0,1.0 );
glVertex3f( 0,0,40 );
glVertex3f( 0,40,40);
glVertex3f( 40,40,40 );
glVertex3f( 40,0,40 );
glEnd();
// side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 40,40,0 );
glVertex3f( 40,0,0 );
glVertex3f( 40,0,40 );
glVertex3f( 40,40,40 );
glEnd();
// side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 0,0,0 );
glVertex3f( 0,40,0 );
glVertex3f( 0,40,40 );
glVertex3f( 0,0,40 );
glEnd();
// side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0,0.0,1.0 );
glVertex3f( 0,40,0);
glVertex3f( 0,40,40 );
glVertex3f( 40,40,40 );
glVertex3f( 40,40,0 );
glEnd();
// side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.5, 0.0 );
glVertex3f( 0,0,0 );
glVertex3f( 40,0,0 );
glVertex3f( 40,0,40 );
glVertex3f( 0,0,40);
glEnd();
glFlush();
glutSwapBuffers();
}
void init()
{
glClearColor(0.5,0.5,0.0, 0.0);
glColor3f(1,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-1.0,1.0,-1.0,1.0);
glOrtho(0, // left
1000, // right
0, // bottom
1000, // top
0, // zNear
1000 // zFar
);
}
void specialKeys( int key, int x, int y ) {
// Right arrow - increase rotation by 5 degree
if (key == GLUT_KEY_RIGHT)
rotate_y += 5;
// Left arrow - decrease rotation by 5 degree
else if (key == GLUT_KEY_LEFT)
rotate_y -= 5;
else if (key == GLUT_KEY_UP)
rotate_x += 5;
else if (key == GLUT_KEY_DOWN)
rotate_x -= 5;
// Request display update
glutPostRedisplay();
}
int main(int argc, char* argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(10, 10);
// Create window
glutCreateWindow("Awesome Cube");
// Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
init();
glutMainLoop();
return 0;
}
这是因为您没有指定相机位置和LookAt位置。所以 opengl 假设您正在寻找原点 (0,0,0)。 因此,您的立方体将显示在屏幕上的右上角是正常的。
如果你想在中心看到它,你可以在旋转调用之前应用translatef函数:
glTranslatef(-20.0f,-20.0f,-20.0f); //Shift your object to the center
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
...
相关文章:
- 如何在自删除后将对象设置为nullptr
- 在 c++ 中将对象设置为等于同一类的构造函数是否有效?
- std::用派生类对象设置,但用gcc8.1设置基类比较器
- 如何在 gdb 中的所有构造函数上同时为 C++ 对象设置断点?
- 如何将C++ time_t对象设置为纪元?
- 使用PYBIND11,如何为Array_t对象设置基础内存的所有权
- 如何使用基类对象设置派生类变量的值
- C 不能将对象设置为方法的返回对象
- 一旦对象设置为 null,用于声明对象的堆大小的变量就设置为 null?
- 无法在openGL和GLUT中为我想要的确切对象设置动画
- 如何将2D向量中的对象设置为空指针[C ]
- 如何用类别的consumetrized构造函数声明使用类的比较对象设置的STL设置
- 为什么重载运算符不将主对象设置为值集
- 如何在 OpenGL 中为 3D 对象设置原点
- C++是否可以将fstream对象设置为在每次调用<<
- 如何从C#将对象设置为C++
- 哪个更好?如果子对象是类的成员,则为子对象设置父对象
- c++无法为包含静态变量的对象设置NULL指针
- 将数组中的对象设置为新对象
- 在OpenGL中为不同的对象设置单独的材质属性