c++ - 如何在 3d 中将原点设置在左下角

标签 c++ opengl isometric

我想在左下角画一个边长为 40 的立方体。我的 glortho 函数是

 glOrtho(0,      // left
        1000,  // right
        0, // bottom
        1000,      // top
        0,      // zNear
        1000       // zFar
        );

x、y、z 轴的长度最大为 1000。因此立方体应位于左下角,尺寸应与我给定的一样。 gluLookAt(); 应该是什么?功能。我没有得到正确的输出。如果代码中有任何错误,请更正它以及应该在代码中添加什么功能。

#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
        1001       // zFar
        );
        gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -1000.0, 0.0, 1000.0, 0.0); 
}

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; 
}      

最佳答案

你搞砸了你的转变。在 Init() 中,您将当前矩阵模式设置为 GL_PROJECTION 并加载一些正交矩阵。然后将 lookAt 矩阵乘以它。这在原则上是错误的,因为 lookAt 矩阵应该应用于 GL_MODELVIEW 堆栈。 (您选择的 lookAt 参数实际上会产生一个恒等 lookAt 矩阵,因此该调用没有任何效果,但这只是旁注)。

但是,真正的错误是在display() 中。你有 glLoadIdentity() ,它只会用单位矩阵覆盖你以前的矩阵,所以你失去了你设置的正交变换,因为你仍然有 GL_PROJECTION 矩阵堆栈活跃。

正确的方法应该是这样的:

void init()
{
    // ... your other stuff
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( /* your ortho params */ );
    glMatrixMode(GL_MODELVIEW); // switch back to the modelView matrix stack
}

void display()
{
    glLoadIdentity();
    gluLookAt( /* your Lookat parameters */ );
    glRotate/Scale/Translate(...); // your local transformations
    // ...
}

请注意,所有这些内容 已完全弃用,并且已从现代 OpenGL 版本的核心配置文件中删除。现在学习 OpenGL 时,您应该考虑不要学习 20 年前的那些老东西。

关于c++ - 如何在 3d 中将原点设置在左下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28985242/

相关文章:

c++ - 如何从 vector 打印嵌套结构对象?

c++ - 使用 std::string 在 switch-case block 中返回字符串常量

c++ - 在 Visual Studio 2005 中调试多项目 (C++) 解决方案

c++ - 纯 C 语言中的 glRotatef

opengl - 如何在 PyOpenGL 中将纹理复制到 pbo 中?

javascript - 带有半透明点击图 block 的 HTML/JS/CSS 等距网格

iphone - 使用cocos2d的等距平铺游戏中的问题

c++ - 错误消息 "expected expression"....有人知道为什么这么说吗?

c - 整个窗口的 glReadPixels (OpenGL)

algorithm - 如何从鼠标坐标接收细胞的等距索引?