如何在 OpenGL 中为 3D 对象设置原点



我正在尝试在设置顶点和窗口位置时绘制立方体,但我没有得到正确的立方体。我在右上角得到立方体,尺寸不像我给出的那样。我是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 );
                     ...

相关内容

  • 没有找到相关文章

最新更新