c++ - 如何在 opengl 中为 3d 对象设置原点

标签 c++ opengl isometric

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

最佳答案

这是因为您没有指定相机位置和注视位置。所以 opengl 假设你正在寻找 Origin (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 );
                     ...

关于c++ - 如何在 opengl 中为 3d 对象设置原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917783/

相关文章:

c++ - 2D 等距 - SFML - 正确的公式,错误的坐标范围

c++ - 与可变参数模板相结合的成员指针数组的定义

c++ - msvc预编译头警告抑制

c++ - 在 Tile Map 上绘制 Sprite

java - OpenCV + OpenGL : copy gl texture, 使用 OpenCV 修改并渲染

javascript - 在 2D 等距网格中点击检测?

C++ 在 Linux 中访问 Oracle

c++ - 在模板和非模板代码之间进行交互时替换 switch 语句

opengl - 如何查询 SSBO 结构的对齐/步幅?

c++ - 使用 tmx-parser 和 SDL 的 2d tile 渲染