簡體   English   中英

如何在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假設您正在尋找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 );
                     ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM