• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            Build 3D Environment

            Posted on 2011-11-20 15:22 eryar 閱讀(2059) 評論(0)  編輯 收藏 引用 所屬分類: 2.OpenCASCADE

            利用OpenGL創建一個3D空間,便于觀察三維的曲面。程序實現:

            1. 設置投影使用 glOrtho(),而不是gluOrtho2D();

            2. 增加光照效果;

            3. 畫出三維坐標軸,且可通過方向鍵來旋轉視圖;

            源程序有三個文件:Main.cpp、CordinateAxis.h、CordinateAxis.cpp;其中類CCordinateAxis用來繪坐標軸。

            程序代碼如下:

               1:  // Main.cpp
               2:  // OpenGL 3D Environment
               3:   
               4:  #include <gl\glut.h>
               5:  #include <iostream>
               6:  #include "CordinateAxis.h"
               7:  using namespace std;
               8:   
               9:  void    Initialize(void);
              10:  void    DrawScene(void);
              11:  void    myReshape(GLsizei w, GLsizei h);
              12:  void    myKeyFunc(GLubyte key, GLint x, GLint y);
              13:  void    mySpecFunc(GLint  key, GLint x, GLint y);
              14:   
              15:  GLint    leftRight    =    0;
              16:  GLint    upDown        =    0;
              17:   
              18:  void main(int argc, char* argv[]) {
              19:      glutInit(&argc, argv);                // Initialize GLUT
              20:      glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);    // Set display mode
              21:      glutInitWindowPosition(50,100);            // Set top-left display window position
              22:      glutInitWindowSize(400, 300);            // set display window width and height
              23:      glutCreateWindow("OpenGL 3D");            // Create display window
              24:   
              25:      Initialize();                    // Execute initialization procedure
              26:      glutDisplayFunc(DrawScene);            // Send graphics to display window
              27:      glutReshapeFunc(myReshape);            // 
              28:      glutKeyboardFunc(myKeyFunc);
              29:      glutSpecialFunc(mySpecFunc);
              30:   
              31:      glutMainLoop();                    // Display everything and wait
              32:  }
              33:   
              34:  void    Initialize(void) {
              35:      glClearColor(0.0, 0.0, 0.0, 0.0);        // Set Display-window color to white
              36:      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
              37:      glMatrixMode(GL_PROJECTION);            // Set projection parameters
              38:      glLoadIdentity();
              39:   
              40:      // Set light
              41:      GLfloat ambientLight[]  = {1.0f, 1.0f, 1.0f, 1.0f};
              42:      GLfloat diffuseLight[]  = {1.0f, 0.2f, 1.0f, 1.0f};
              43:      GLfloat specularLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
              44:      GLfloat shininess[]     = {90.0f};
              45:      GLfloat lightPos[]      = {800.0f, 800.0f, 900.0f, 1.0f};
              46:      
              47:      // Setup and enable light 0
              48:  //     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
              49:  //     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
              50:  //     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
              51:   
              52:      glMaterialfv(GL_FRONT, GL_AMBIENT, ambientLight);
              53:      glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseLight);
              54:      glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
              55:      glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
              56:      glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
              57:   
              58:      // Enable lighting
              59:      glEnable(GL_LIGHTING);
              60:      glEnable(GL_LIGHT0);
              61:      glDepthFunc(GL_LESS);
              62:      glEnable(GL_DEPTH_TEST);
              63:      
              64:      // Enable color tracking
              65:      glEnable(GL_COLOR_MATERIAL);    
              66:   
              67:      //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
              68:  } // Initialize
              69:   
              70:  void    myReshape(GLsizei w, GLsizei h) {
              71:      // Reset viewport and projection parameter
              72:      glViewport(0, 0, w, h);
              73:      glMatrixMode(GL_PROJECTION);
              74:      glLoadIdentity();
              75:      glOrtho(0, w, 0, h, -500, 500);
              76:      glMatrixMode(GL_MODELVIEW);
              77:  } // myReshape
              78:   
              79:  void    myKeyFunc(GLubyte key, GLint x, GLint y) {
              80:      cout<<key<<x<<y<<endl;
              81:  } // myKeyFunc
              82:   
              83:  void    mySpecFunc(GLint  key, GLint x, GLint y) {
              84:   
              85:      switch(key) {
              86:      case    GLUT_KEY_LEFT:
              87:          leftRight    -=    1;
              88:          break;
              89:   
              90:      case    GLUT_KEY_RIGHT:
              91:          leftRight    +=    1;
              92:          break;
              93:   
              94:      case    GLUT_KEY_UP:
              95:          upDown        -=    1;
              96:          break;
              97:   
              98:      case    GLUT_KEY_DOWN:
              99:          upDown        +=    1;
             100:          break;
             101:   
             102:      default:
             103:          glutPostRedisplay();
             104:      }
             105:   
             106:      glutPostRedisplay();
             107:  } // mySpecFunc
             108:   
             109:  void    DrawScene(void) {
             110:      glClear(GL_COLOR_BUFFER_BIT);            // Clear display window
             111:      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
             112:      glEnable(GL_AUTO_NORMAL);
             113:   
             114:      glPushMatrix();
             115:   
             116:      glRotatef((GLfloat)leftRight, 0.0, 1.0, 0.0);
             117:      glRotatef((GLfloat)upDown, 1.0, 0.0, 0.0);
             118:   
             119:      // 
             120:      glTranslatef(50.0, 50.0, 0.0);
             121:      CCordinateAxis  axis;
             122:      axis.SetAxisPosition(0, 0, 0);
             123:      axis.SetAxisSize(100);
             124:      axis.Show();
             125:   
             126:      glTranslatef(100.0f, 100.0f, 100.0f);
             127:      glColor3f(1.0f, 0.8f, 0.0f);
             128:      //glutWireCube(100.0);
             129:      glutSolidTeapot(80);
             130:   
             131:      glPopMatrix();
             132:   
             133:      glFlush();                    // Process all OpenGL routines as quickly possible
             134:  } // DrawScene
               1:  // CordinateAxis.h: interface for the CCordinateAxis class.
               2:  //
               3:  //////////////////////////////////////////////////////////////////////
               4:   
               5:  #ifndef _CORDINATEAXIS_H_
               6:  #define _CORDINATEAXIS_H_
               7:   
               8:  #pragma once
               9:   
              10:  #include <GL/glut.h>
              11:  #include <iostream>
              12:  using namespace std;
              13:   
              14:  class CCordinateAxis  
              15:  {
              16:  public:
              17:      CCordinateAxis();
              18:      CCordinateAxis(double x, double y, double z);
              19:      virtual ~CCordinateAxis();
              20:   
              21:      void    SetAxisPosition(double x, double y, double z);
              22:      void    SetAxisSize(double size);
              23:      void    Show(void);
              24:   
              25:  private:
              26:      double  m_xPos;
              27:      double  m_yPos;
              28:      double  m_zPos;
              29:   
              30:      double  m_Size;
              31:  };
              32:   
              33:  #endif // _CORDINATEAXIS_H_
            .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
            .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

             

               1:  // CordinateAxis.cpp: implementation of the CCordinateAxis class.
               2:  //
               3:  //////////////////////////////////////////////////////////////////////
               4:   
               5:  #include "CordinateAxis.h"
               6:   
               7:  CCordinateAxis::CCordinateAxis()
               8:  {
               9:      m_xPos  = 0;
              10:      m_yPos  = 0;
              11:      m_zPos  = 0;
              12:   
              13:      m_Size  = 80;
              14:  }
              15:   
              16:  CCordinateAxis::CCordinateAxis( double x, double y, double z )
              17:  {
              18:      m_xPos  = x;
              19:      m_yPos  = y;
              20:      m_zPos  = z;
              21:  }
              22:   
              23:  CCordinateAxis::~CCordinateAxis()
              24:  {
              25:   
              26:  }
              27:   
              28:  void CCordinateAxis::SetAxisPosition( double x, double y, double z )
              29:  {
              30:      m_xPos  = x;
              31:      m_yPos  = y;
              32:      m_zPos  = z;
              33:  }
              34:   
              35:  void CCordinateAxis::SetAxisSize( double size )
              36:  {
              37:      m_Size  = size;
              38:  }
              39:   
              40:  void CCordinateAxis::Show( void )
              41:  {
              42:      GLdouble    radius  = m_Size / 12;
              43:      GLdouble    height  = radius * 2.2;
              44:   
              45:      glPushMatrix();
              46:      glTranslatef(m_xPos, m_yPos, m_zPos);
              47:   
              48:      glBegin(GL_LINES);
              49:          glVertex3f(0, 0, 0);
              50:          glVertex3f(m_Size, 0, 0);
              51:   
              52:          glVertex3f(0, 0, 0);
              53:          glVertex3f(0, m_Size, 0);
              54:   
              55:          glVertex3f(0, 0, 0);
              56:          glVertex3f(0, 0, m_Size);
              57:      glEnd();
              58:   
              59:      glutSolidSphere(radius/2, 10, 10);
              60:   
              61:      // x axis arrow
              62:      glTranslatef(m_Size, 0, 0);
              63:      glRotatef(90, 0, 1, 0);
              64:      glColor3f(1, 0, 0);
              65:      glutSolidCone(radius, height, 10, 10);
              66:   
              67:      // y axis arrow
              68:      glTranslatef(0, m_Size, -m_Size);
              69:      glRotatef(-90, 1, 0, 0);
              70:      glColor3f(0, 1, 0);
              71:      glutSolidCone(radius, height, 10, 10);
              72:   
              73:      // z axis arrow
              74:      glTranslatef(-m_Size, 0, -m_Size);
              75:      glRotatef(-90, 0, 1, 0);
              76:      glColor3f(0, 0, 1);
              77:      glutSolidCone(radius, height, 10, 10);
              78:   
              79:      glPopMatrix();
              80:  }
            .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

             

             

            OpenGL 3D

            程序效果圖

            一本一道久久综合狠狠老| 日韩欧美亚洲综合久久影院d3| 香港aa三级久久三级老师2021国产三级精品三级在 | 香蕉久久一区二区不卡无毒影院 | 一本色道久久88综合日韩精品 | 久久精品中文字幕大胸| 欧美日韩精品久久免费| www久久久天天com| 久久本道综合久久伊人| 伊人久久久AV老熟妇色| 久久se精品一区二区影院 | 久久免费国产精品一区二区| 亚洲v国产v天堂a无码久久| 久久婷婷五月综合色高清| 久久久不卡国产精品一区二区| 亚洲中文久久精品无码ww16| 精品久久久久久无码人妻热| 国产精品99久久免费观看| 亚洲欧洲中文日韩久久AV乱码| 狠狠色丁香婷婷久久综合不卡| 欧美亚洲国产精品久久高清| 久久99国产精品成人欧美| 亚洲国产精品久久久天堂| 久久99精品久久久久久噜噜| 亚洲中文久久精品无码ww16| 一本一道久久a久久精品综合| 精品久久久久久国产| 亚洲精品乱码久久久久久按摩| 久久久噜噜噜久久中文字幕色伊伊| 国产精品无码久久久久久| 中文字幕精品久久| 久久久久无码精品| 亚洲国产精品久久久久网站| 91精品国产9l久久久久| 久久棈精品久久久久久噜噜| 99久久综合国产精品免费| 欧美国产精品久久高清| 久久一本综合| 一本综合久久国产二区| 99久久综合国产精品免费| 久久久久亚洲国产|