2013年2月23日 星期六

Setting Up the Android Device for Development on Windows7

since: 2013/02/23
update: 2013/02/23

reference:
1. OEM USB Drivers | Android Developers2. Using Hardware Devices | Android Developers


A. 安裝 Google USB Driver
     1. Eclipse > Window > Android SDK Manager:

     2. 確認 Google USB Driver 為 Installed.

     3. 關閉 Eclipse.
-------------------------------------------------------------------------------------------

B. 開啟 Android 設備上的 USB 測試功能
     1. 打開 Android 設備(在此為 Nexus 7 版本: 4.2.2)

     2. 設定 > 關於平板電腦 > 版本號碼
         連續點擊 "版本號碼" 數次後,Android 會回應你再點擊三次就表示你是
         開發人員的訊息(訊息會出現在螢幕下方,而且會有次數)。
         此時繼續點擊,點擊到 Android 告訴你 "開發人員選項" 已開啟為止。

     3. 回到上一層,點選 "開發人員選項".
         > 勾選 "USB 偵錯".

     4. 接著用 USB 跟電腦連接,這時電腦會出現安裝驅動程式,先不用理會.

-------------------------------------------------------------------------------------------

C. 安裝 Android 設備的驅動程式
     1. 電腦 > 內容 > 裝置管理員 > 可攜式裝置 > "顯示驚嘆號的設備"

     2. "顯示驚嘆號的設備" > 右鍵 > "更新驅動程式軟體"
         > "瀏覽電腦上的驅動程式軟體"
         > 選取剛剛安裝的 Google USB Driver:
            C:\Lanli\EclipseAndroidDev\android-sdk-windows\extras\google\usb_driver
         > 下一步, 接著電腦就會自動安裝.

-------------------------------------------------------------------------------------------

D. 在 Eclipse 裡, 啟用 Debug 功能

     1. 開啟專案後, 打開 AndroidManifest.xml 檔案, 在 <application> 標籤裡,
         加入以下的屬性設定: android:debuggable="true"
         備註: a. 可以不需要設定, 因為使用 Eclipse 來啓動 App 會自動啓用 Debug 模式.
                  b. 如果手動啟用了, 記得在 buildrelease App 之前, 將其停用.

     2. Run > Run Configurations > Android Application > 專案名稱:
         > 右邊 Target 頁籤
         > Deploy Target Selection Mode: Always prompt to pick device
    備註: 如此一來, 每次執行時皆可選擇設備.

-------------------------------------------------------------------------------------------

E. 執行應用程式
     1. 選取專案 > 右鍵 >  "Run As" > "Android Application"

     2. 選取 Android 設備 > OK

-------------------------------------------------------------------------------------------

F. 備註: 在 Mac OS X 上實機測試
    1. 不需要安裝 USB Driver

    2. 只需要 B, DE 的步驟即可.

2013年2月11日 星期一

Installing the Android SDK on the Mac OS X


since: 2013/02/11
update: 2013/02/21


A. 新增軟體開發的資料夾
     新增一個用來放置所有開發軟體的資料夾(Java 除外)
       ex: AndroidDevMac (在此, 資料夾位置為: /Lanli/AndroidDevMac)

-------------------------------------------------------------------------------------------

B. 安裝JAVA
     下載 JAVA SDK: http://www.oracle.com/technetwork/java/javase/downloads/
     , 並安裝. (在此為: jdk-7u13-macosx-x64.dmg)

-------------------------------------------------------------------------------------------

C. Eclipse

    1. 下載 Eclipse Classic: http://www.eclipse.org/downloads/
        採用版本: eclipse-SDK-4.2.1-macosx-cocoa-x86_64.tar.gz

    2. 解壓縮後將整個 eclipse 資料夾放到 /Lanli/AndroidDevMac 下

-------------------------------------------------------------------------------------------

D. Android SDK
   1. 下載 Android SDK: http://developer.android.com/sdk/
      > USE AN EXISTING IDE
         採用版本: android-sdk_r21.0.1-macosx.zip

   2. 解壓縮後將整個 android-sdk-macosx 資料夾放到 /Lanli/AndroidDevMac 下

   3. 將工具的路徑, 加到系統的 PATH 變數裡.
       開啟終端機
       $ cd ~
       $ sudo vi .profile
       $ export PATH=/Lanli/AndroidDevMac/android-sdk-macosx/tools:/Lanli/AndroidDevMac/android-sdk-macosx/platform-tools:$PATH
      
       關閉終端機, 再重新開啓:
       $ echo $PATH

-------------------------------------------------------------------------------------------

E. 安裝 Android Development Tools (ADT) plugin
     1. 開啟 Eclipse (選擇 workspace, 在此為 /Lanli/workspace)

     2. Help > Install New Software... > Add >

        Name: ADT
        Location: https://dl-ssl.google.com/android/eclipse/

        > OK
        > Select All (會將 Developer ToolsNDK Plugins 全選)
        > Next > Next
        > 勾選: I accept the terms of the license agreements
        > Finish (開始下載並安裝)
        > Security Warning > OK
        > Restart Eclipse > Yes

     3. Eclipse 重開後,
         a. 先關閉出現的警告(按 Close)
         b. 與取消安裝新的 Android SDK(按 Cancel)
             (之後會再自行設定)

-------------------------------------------------------------------------------------------

F. Android SDK Setting
   1. 開啟 Eclipse
 
   2. Eclipse > 偏好設定 >
       Android > SDK Location: Browse...
     
      /Lanli/AndroidDevMac/android-sdk-macosx
     
      > OK
      > Close

-------------------------------------------------------------------------------------------

G. Android SDK Manager
   1. 開啟 Eclipse
 
   2. Window > Android SDK Manager
 
   3. 可以勾選所有的項目, 在此勾選: Tools, Android 2.2, Android 4.2Extras
      > 按下 "Install xx packages..."
      > Accept All
      > Install
      (開始下載並安裝)
    
      > 完成後, 關閉 Eclipse 

-------------------------------------------------------------------------------------------

H. Android Virtual Device (AVD) Setting
    1. 開啟 Eclipse
 
    2. Window > Android Virtual Device Manager
       > New
       > AVD Name: AVD-2.2-480x800
          Device: 4.0" WVGA(480x800:hdpi)
          Target: Android 2.2 - API Level 8
          取消勾選: Hardware keyboard present
                            Display a skin with hardware controls
          SD Card: size: 16 MiB
       > OK
       > Start > Lanuch (啟動)

-------------------------------------------------------------------------------------------

I. UTF-8 Setting
   1. Eclipse > 偏好設定 > General > Workspace

   2. Text file encoding: Other: UTF-8
      > OK

-------------------------------------------------------------------------------------------

J. 備註:
   當更改整個 AndroidDevMac 資料夾位置時, 須再次檢查 Eclipse:
   1. Eclipse > 偏好設定 > Android > SDK Location:
 
   2. Eclipse > 偏好設定 > Java > Install JREs:   

2013年2月10日 星期日

Seting up an Android Development Environment


since: 2013/02/10
update: 2013/02/10


A. 新增軟體開發的資料夾
   1. 在任意槽新增一個用來放置所有開發軟體的資料夾
       ex: EclipseAndroidDev
     
   2. 在此, 資料夾位置為: C:\Lanli\EclipseAndroidDev
       並在 EclipseAndroidDev 下新增 JDK 資料夾.

-------------------------------------------------------------------------------------------


B. Java SDK
   1. 下載 JAVA SDK: http://www.oracle.com/technetwork/java/javase/downloads/
       採用版本: Java SE 7u13 及 Java SE 7 Documentation
     
   2. 預設安裝位置: C:\Program Files\Java\jdk1.7.0_13
      
   3. 將 C:\Program Files\Java\jdk1.7.0_13 下的全部檔案及
       jdk-7u11-apidocs.zip(Java文件檔)
       copy 到 C:\Lanli\EclipseAndroidDev\JDK
       p.s. 至此, 便可以將 JAVA SDK 從控制台中移除.
              (可以只保留額外安裝的 JRE C:\Program Files\Java\jre7, 如果有的話)

-------------------------------------------------------------------------------------------
     
C. Eclipse
   1. 下載 Eclipse IDE for Java Developers: http://www.eclipse.org/downloads/
       採用版本: eclipse-java-juno-SR1-win32-x86_64
     
   2. 解壓縮到 C:\Lanli\EclipseAndroidDev 下(會自動產生 eclipse 資料夾)

-------------------------------------------------------------------------------------------

D. Android SDK
   1. 下載 Android SDK: http://developer.android.com/sdk/
      > DOWNLOAD FOR OTHER PLATFORMS > Windows >
         android-sdk_r21.0.1-windows.zip
         採用版本: Android SDK r21.0.1
     
   2. 解壓縮 android-sdk_r21.0.1-windows.zip 到 C:\Lanli\EclipseAndroidDev 下
       (會自動產生 android-sdk-windows 資料夾)

-------------------------------------------------------------------------------------------

E. 新增批次檔: 
   1. 在 C:\Lanli\EclipseAndroidDev 下新增一個 AndroidDevStart.bat 檔案, 內容如下:
set WORK_HOME=%cd%
set JAVA_HOME=%WORK_HOME%\JDK
set JRE_HOME=%WORK_HOME%\JDK\jre
set ANDROID_SDK_HOME=%WORK_HOME%\android-sdk-windows
set PATH=%JAVA_HOME%\bin;%JRE_HOME\bin;%PATH%
cd eclipse
start eclipse


   2. AndroidDevStart.bat > 傳送到 > 桌面(建立捷徑)

-------------------------------------------------------------------------------------------

F. 安裝 Android Development Tools (ADT) plugin
   1. 點二下 AndroidDevStart.bat 檔案, 開啟 Eclipse
      (選擇 workspace, 在此為 C:\Lanli\workspace)
     
   2. Help > Install New Software... >
      Work with: https://dl-ssl.google.com/android/eclipse/ > Add >

      Name: ADT
      Location: https://dl-ssl.google.com/android/eclipse/

      > OK
      > Select All (會將 Developer Tools 與 NDK Plugins 全選)
      > Next > Next
      > 勾選: I accept the terms of the license agreements
      > Finish (開始下載並安裝)
      > Security Warning > OK
      > Restart Eclipse > Yes
     
   3. Eclipse 重開後,
      a. 先關閉出現的警告(按 Close)
      b. 與取消安裝新的 Android SDK(按 Cancel)
         (之後會再自行設定)

-------------------------------------------------------------------------------------------

G. Android SDK Setting
   1. 開啟 Eclipse
  
   2. Window > Preferences >
      Android > SDK Location: Browse...
      C:\Lanli\EclipseAndroidDev\android-sdk-windows
      > OK
      > Close

-------------------------------------------------------------------------------------------

H. Android SDK Manager 
   1. 開啟 Eclipse
  
   2. Window > Android SDK Manager
  
  
   3. 可以勾選所有的項目, 在此勾選: Tools, Android 2.2, Android 4.2Extras
      > 按下 "Install xx packages..."
      > Accept All
      > Install
      (開始下載並安裝)
     
      > 完成後, 關閉 Eclipse

-------------------------------------------------------------------------------------------

I. Android Virtual Device (AVD) Setting 
   1. 開啟 Eclipse
  
   2. Window > Android Virtual Device Manager
      > New
      > AVD Name: AVD-2.2-480x800
         Device: 4.0" WVGA(480x800:hdpi)
         Target: Android 2.2 - API Level 8
         取消勾選: Hardware keyboard present
                            Display a skin with hardware controls
         SD Card: size: 16 MiB
      > OK
      > Start > Lanuch (啟動)

-------------------------------------------------------------------------------------------

J. UTF-8 Setting
   1. Window > Preferences > General > Workspace

   2. Text file encoding: Other: UTF-8
      > OK

-------------------------------------------------------------------------------------------
           
K. 備註:
   當更改整個 EclipseAndroidDev 資料夾位置時, 須再次檢查 Eclipse:
   1. Window > Preference > Android > SDK Location:
  
   2. Window > Preferences > Java > Install JREs:   

2012年10月11日 星期四

Use OpenGL in Windows Form

since: 2012/10/11
update: 2012/10/11

reference:
1. 將OpenGL加入Windows Forms(C++/CLI)
2. (VS2005,OpenGL)WinForm建立OpenGL视场

A. 說明:
     本篇文章參考上面的二篇文章, 擷取不同的特點:

      1. OpenGL 的繪圖作業, 獨立成一個類別來處理.

      2.Form 加入以下的元件:
          a. Panel: 作為 Device Context,  之後提供給 OpenGL 來建立 Render Context.
          b. Timer: 計時器, 藉著啟用 Tick 事件, 定時作繪製的動作, 來達到動畫的效果.

     3.Form 加入以下的事件:
          a. Paint: 首次的繪製. (如果不需要動畫的話, 可將 Timer 停用, 只畫此一次)
          b. ClientSizeChanged: 當 Form 的大小改變時, 需要再重新繪製.

-------------------------------------------------------------------------------------

B. 前置作業:
     1. 安裝 GLUT  與 GLEW: 參考這篇CD.

-------------------------------------------------------------------------------------

C. 新增專案:
     1. Visual Studio 2010 > 檔案 > 新增 > 專案

     2. Visual C++ > CLR > Windows Form 應用程式
         名稱: GLForm_Hello
         位置: C:\Lanli\projects\WindowsForm_My\
         為方案建立目錄: 取消勾選
          > 確定

     3. 新增給 OpenGL 使用的類別檔案:

         點選 "GLForm_Hello" 專案 >  原始程式檔 > 滑鼠右鍵 > 加入 > 新增項目:

         Visual C++ > 標頭檔 (.h)
         名稱: OpenGL.h
          > 新增

         Visual C++ > C++ 檔 (.cpp)
         名稱: OpenGL.cpp
          > 新增

-------------------------------------------------------------------------------------

D. 專案屬性設定:
     點選 "GLForm_Hello" 專案 > 滑鼠右鍵
     > 屬性
     > 組態屬性

     1. > 一般: (Debug Release)
         字元集: 使用 Unicode 字元集
         Common Language Runtime 支援: Common Language Runtime 支援 (/clr)
          > 確定

     2. > 連結器 > 輸入 > 其他相依性: (Debug Release)
             opengl32.lib
             glut32.lib
             glew32.lib
             glew32s.lib
             glu32.lib


            > 確定

-------------------------------------------------------------------------------------

E. 撰寫 OpenGL 類別檔案
     1. 開啟 OpenGL.h 檔案, 加入以下程式碼:
#pragma once

#include <windows.h>
#include <GL/glut.h>


// OpenGL 繪圖類別
class OpenGL
{
public:
    OpenGL(); // 建構子
    bool Init(HDC hdc); // 傳入參數: device context, 來建立 render context
    void InitOpenGL(); // 初始化 OpenGL 環境
    void InitScene(int x, int y, int width, int height); // 初始化場景
    void RenderScene(); // 繪製場景

public:
    ~OpenGL(void); // 解構子

private:
    HDC m_hDC; // device context handler
    HGLRC m_rc; // render context handler

    // -------------------- lights(光源)
    GLfloat gLightDiffuse[4]; // light diffuse (散射光)
    GLfloat gLightPosition[4]; // light position

    // -------------------- rotate angle(旋轉角度)
    float m_rAngle;
};


***************************************************

     2. 開啟 OpenGL.cpp 檔案, 加入以下程式碼:
#include "stdafx.h"
#include <iostream>
#include "OpenGL.h"


// 建構子
OpenGL::OpenGL()
{
    m_rc = NULL;
}

// 解構子
OpenGL::~OpenGL(void)

    wglMakeCurrent(0,0);
    wglDeleteContext(m_rc);
}

// 傳入參數: device context, 來建立 Render Context
bool OpenGL::Init(HDC hdc)
{  
    m_hDC = hdc;

    // Set pixel format
    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;

    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 32;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int cpf = ChoosePixelFormat(m_hDC, &pfd);
    if(!cpf)
    {
        std::cerr << "Choose Pixel Format Error\n";
        return false;
    }

    SetPixelFormat(m_hDC, cpf, &pfd);

    m_rc = wglCreateContext(m_hDC);
    if(!m_rc)
    {
        std::cerr << "Create Render Context Error\n";
        return false;
    }

    if(!wglMakeCurrent(m_hDC, m_rc))
    {
        std::cerr << "wglMakeCurrent Error\n";
        return false;
    }

    return true;
}

// 初始化 OpenGL 環境
void OpenGL::InitOpenGL()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White Background
    //glColor3f(0.0, 0.0, 0.0); // 筆畫顏色: 黑

    /*******************************************************/
    // 光源設定
    // light diffuse (散射光): 紅色

    gLightDiffuse[0] = 1.0;
    gLightDiffuse[1] = 0.0;
    gLightDiffuse[2] = 0.0;
    gLightDiffuse[3] = 1.0;

    // light position (光源位置)
    gLightPosition[0] = 1.0;
    gLightPosition[1] = 1.0;
    gLightPosition[2] = 0.0;
    gLightPosition[3] = 1.0;

    // 啟用第一個光源設定
    glLightfv(GL_LIGHT0, GL_DIFFUSE, gLightDiffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, gLightPosition);
    glEnable(GL_LIGHT0);
    /*******************************************************/

    glClearDepth(1.0f); // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST); // Enables Depth Testing
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH); // Enable Smooth Shading

    glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

// 初始化場景
void OpenGL::InitScene(int x, int y, int width, int height)
{
    glViewport(x, y, width, height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    glLoadIdentity(); // Reset The Projection Matrix

    // 透視投影
    /*
    gluPerspective(
        45.0f, // field of view in degree
        (GLfloat)width/(GLfloat)height, // aspect ratio
        0.1f, // Z near
        100.0f); // Z far
    */

    // 正交投影
    //glOrtho(-5, 5, -5, 5, 5, 15);


    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    glLoadIdentity(); // Reset The Modelview Matrix

    // 眼睛(攝影機) 位置與觀看方向
    gluLookAt(
        0.0, 0.0, 10.0, // eye is at (0,0,10.0)
        0.0, 0.0, 0.0, // eye look at (0,0,0)
        0.0, 1.0, 0.0); // up is in positive Y direction

}

// 繪製場景
void OpenGL::RenderScene()
{

    // Clear Screen And Depth Buffer   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
    glLoadIdentity(); // Reset The Current Modelview Matrix

    // -------------------- 啟用光源設定
    glEnable(GL_LIGHTING);

    // render the 2D/3D object
   
    /*
    glBegin(xxx);   
    ....
    glEnd();   
    */


    // 3. 再位移
    //glTranslatef(3.0f,0.0f,0.0f);    // Move Right 3 Units


    // 2. 再旋轉角度
    m_rAngle = m_rAngle + 1;
    if(360 < m_rAngle) m_rAngle -= 360;
    glRotatef(m_rAngle, 0.0f, 1.0f, 0.0f); // 對 Y 軸(垂直軸)作旋轉

    // 1. 先畫圖
    //glPointSize(1.0f);
    glColor3f(0.4, 0.6, 0.7);
    glutWireTeapot(0.5);

    // -------------------- 停用光源設定
    glDisable(GL_LIGHTING);

    SwapBuffers(m_hDC);
}


-------------------------------------------------------------------------------------

F. 為專案的 Form 添加成員變數
    開啟 Form1.h 檔案 ( Form1.h > 滑鼠右鍵 > 檢視程式碼), 修改如下:

// step 01:
#pragma once


#include "OpenGL.h"
OpenGL w_openGL; // w: 代表由 wgl 所建立的 GL
....

// step 02:
    private:
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        System::ComponentModel::Container ^components;

        //@add for OpenGL handle

        HDC  m_hDC; // Device Context Handler
        int m_width;
        int m_height;


-------------------------------------------------------------------------------------

G.為專案的 Form 加入新元件與事件:
     開啟 Form1.h 設計工具 ( Form1.h > 滑鼠右鍵 > 設計工具檢視):

     1. 更改 Form 的顯示名稱:
          > 屬性 > Text: GLForm Hello
                           Size: 400, 400

     2. 幫 Form 加入 Paint 事件:
          > 屬性 > 事件(閃電圖示):
          點二下 Paint 事件

     3. 幫 Form 加入 ClientSizeChanged 事件:
         > 屬性 > 事件(閃電圖示):
         點二下 ClientSizeChanged 事件

     4. 幫 Form 加入 Panel 元件:
         > 工具箱 > 將 Panel 拖拉到 Form 上
         > 屬性:
            (Name): OpenGLPanel
            Duck: Fill (填滿)

     5. 幫 Form 加入 Timer 元件與 Tick 事件:
         > 工具箱 > 將 Timer 拖拉到 Form 上
         > 點二下 Tick 事件

-------------------------------------------------------------------------------------

H. 為專案的 Form 加入 OpenGL 的處理:
      開啟 Form1.h 檔案 ( Form1.h > 滑鼠右鍵 > 檢視程式碼), 修改如下:
....
// step 01:
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: 在此加入建構函式程式碼
            //


            //@add for OpenGL
            OpenGLInit();

        }

        //@add for OpenGL
        void OpenGLInit(void)
        {
            // Get Device Context
            //m_hDC = GetDC((HWND)(this->Handle.ToInt32()));

            m_hDC = GetDC((HWND)(this->OpenGLPanel->Handle.ToInt32()));

            if(!w_openGL.Init(m_hDC))
            {
                MessageBox::Show("OpenGL Init Error");
            }

            w_openGL.InitOpenGL();

            m_width = this->ClientSize.Width;
            m_height = this->ClientSize.Height;
            w_openGL.InitScene(0, 0, m_width, m_height);

            // 下面二行註解的話, 就不會產生動畫效果,
            // 只會執行一次 Form1_Paint 事件裡的 w_openGL.RenderScene();

            timer1->Interval = 20;
            timer1->Enabled = true;
        }

....

// step 02:
    private: System::Void Form1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
                 //@add for OpenGL
                 w_openGL.RenderScene();

             }
   
    private: System::Void Form1_ClientSizeChanged(System::Object^  sender, System::EventArgs^  e) {
                 //@add for OpenGL
                 int width=this->ClientSize.Width;
                 int height=this->ClientSize.Height;
                 w_openGL.InitScene(0,0,width,height);
                 w_openGL.RenderScene();

             }

    private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
                 //@add for OpenGL
                 w_openGL.RenderScene();

             }
    };

....

-------------------------------------------------------------------------------------

I. 建置並啟動::

    繞著 Y 軸(垂直軸)旋轉的 Teapot

2012年10月7日 星期日

Read mesh file and display wireframe in OpenGL

since: 2012/10/07
update: 2012/10/07

reference:
1. Openmesh 读入off文件并用opengl显示

A. 說明:
     1. 主要參考上方的資料, 並作適度的簡化與調整.
     2. 調整為: 不加入光源效果, 只顯示線框圖(wireframe), 採用正交投影.

----------------------------------------------------------------------------------------

B. 前置作業:
     1. 安裝 GLUT  與 GLEW: 參考這篇CD.

     2. 取得 OpenMesh Libraries: 參考這篇, 在此會使用 C. 方式二所產生的
         Libraries, 即:        
          OpenMesh Libraries 目錄位置
          C:\Lanli\projects\OpenMesh-2.2\lib
          內含: libOpenMeshCore.lib
                    libOpenMeshCored.lib
                    libOpenMeshTools.lib
                    libOpenMeshToolsd.lib
                    (有 d 的為 debug 版本)

          OpenMesh Headers 目錄位置
          C:\Lanli\projects\OpenMesh-2.2\src

----------------------------------------------------------------------------------------

C. 新增專案:
     1. Visual Studio 2010 > 檔案 > 新增 > 專案

     2. Visual C++ > Win32 > Win32 主控台應用程式
         名稱: OpenMesh_Hello
         位置: C:\Lanli\projects
         為方案建立目錄: 取消勾選
          > 確定

     3. 下一步
          > 空專案: 勾選
          > 完成

     4. 新增主程式檔案
         點選 "OpenMesh_Hello" 專案 > 滑鼠右鍵 > 加入 > 新增項目:
         Visual C++ > C++ 檔 (.cpp)
         名稱: main.cpp
          > 新增

----------------------------------------------------------------------------------------

D. 為專案加入 OpenMesh 的 Libraries 與 Header Files:
     1. OpenMesh_Hello 專案的根目錄, 新增 meshlib 資料夾.

     2. B. 前置作業2. 的:
         a. OpenMesh Libraries 目錄下所有檔案 copy 到 lib 目錄下.
              即 lib 目錄下的檔案為:
              ----------------------------
              libOpenMeshCore.lib
              libOpenMeshCored.lib
              libOpenMeshTools.lib
              libOpenMeshToolsd.lib
              (有 d 的為 debug 版本)

         b. OpenMesh Headers 目錄下所有檔案 copy 到 mesh 目錄下.
             即 mesh 目錄下的檔案為:
             ----------------------------
             OpenMesh 資料夾
             Unittests 資料夾

----------------------------------------------------------------------------------------

E. 專案屬性設定:
     點選 "OpenMesh_Hello" 專案 > 滑鼠右鍵
     > 屬性
     > 組態屬性

     1. > 一般: (Debug Release)
         字元集: 使用 Unicode 字元集
         Common Language Runtime 支援: Common Language Runtime 支援 (/clr)
          > 確定

     2. > VC++ 目錄: (Debug Release)
            Include 目錄: mesh
            程式庫目錄: lib
            > 確定

            備註: 也可以使用以下的方式
            Include 目錄: C:\Lanli\projects\OpenMesh-2.2\include
            程式庫目錄: C:\Lanli\projects\OpenMesh-2.2\lib


     3. > C/C++ 前置處理器: (Debug Release)
             前置處理器定義: _USE_MATH_DEFINES

     4. > 連結器 > 輸入 > 其他相依性:
             Debug: (檔案有 d 的版本)
             -----------------------------------
             libOpenMeshCored.lib
             libOpenMeshToolsd.lib
             glut32.lib
             glew32.lib
             glew32s.lib
            > 確定

             Release:
             -----------------------------------
             libOpenMeshCore.lib
             libOpenMeshTools.lib
             glut32.lib
             glew32.lib
             glew32s.lib
            > 確定

----------------------------------------------------------------------------------------

F.下載模型檔:
     網址: models
     檔名: teapot.off
     下載後, copy 到 OpenMesh_Hello 專案的根目錄下.
     用 MeshLab 看一下模型檔:

----------------------------------------------------------------------------------------

G. 撰寫程式碼:
     開啟 main.cpp 檔案, 修改如下:
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <string.h>


// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>


// -------------------- OpenGL
#include <GL/glut.h>

// -------------------- typedef
typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
MyMesh mesh;

// -------------------- global variables
float gWidth; // width of the render window
float gHeight; // height of the render window

// -------------------- lights
//GLfloat gLightDiffuse[] = {0.0, 0.0, 1.0, 1.0}; // light diffuse
//GLfloat gLightPosition[] = {0.0, 0.0, 1.0, 1.0}; // light position

// -------------------- function declare
void display();
void drawFace();
void drawLine();
void glInit();
void idle();
void render();
void reshape(GLint width, GLint height);

// ----------------------------------------------------------------------------

int main(int argc, char* argv[])
{
    // read mesh object file
    if (!OpenMesh::IO::read_mesh(mesh, "teapot.off"))
    {
        std::cerr << "read error\n";
        exit(1);
    }

    // initialize the render window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(640, 640);
    glutCreateWindow("Hello, OpenMesh!");

    // initialize the render environment
    glInit();

    // callback function
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);

    glutMainLoop();

    return 0;
}

// initialize the render environment
void glInit()
{
    // set color
    glClearColor(0.8, 0.8, 0.8, 1.0); // 背景: 灰色
    glColor3f(0.0, 0.0, 0.0); // 筆畫顏色: 黑

    // 光源設定
    /*
    glLightfv(GL_LIGHT0, GL_DIFFUSE, gLightDiffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, gLightPosition);
    glEnable(GL_LIGHT0);
    */


    glEnable(GL_DEPTH_TEST); // 深度測試
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);

    // set OpenGL projection mode
    glMatrixMode(GL_PROJECTION);
   
    // 透視投影
    /*
    gluPerspective(
        45.0, // field of view in degree
        1.0, // aspect ratio
        1.0, // Z near
        10.0); // Z far
    */
    // 正交投影

    glOrtho(-5, 5, -5, 5, 5, 15);

    // set OpenGL nodel view mode
    glMatrixMode(GL_MODELVIEW);

    // 眼睛(攝影機) 位置與觀看方向
    gluLookAt(
        0.0, 0.0, 10.0, // eye is at (0,0,10.0)
        0.0, 0.0, 0.0, // eye look at (0,0,0)
        0.0, 1.0, 0.0); // up is in positive Y direction
}

void display()
{
    glViewport(0, 0, gWidth, gHeight);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    render();

    glutSwapBuffers();
}

void render()
{
    //glEnable(GL_LIGHTING);

    //drawFace();

    drawLine();

    //glDisable(GL_LIGHTING);
}

void reshape(GLint width, GLint height)
{
    // record the width and height of the current render window
    gWidth = width;
    gHeight = height;
}

void idle()
{
    // refresh the screen
    glutPostRedisplay();
}

void drawFace()
{
    glPointSize(1.0f);
    glColor3f(1.0, 1.0, 0.0);

    for (MyMesh::FaceIter f_it = mesh.faces_begin(); f_it != mesh.faces_end(); ++f_it)
    {
        glBegin(GL_TRIANGLES);
        for(MyMesh::FaceVertexIter fv_it = mesh.fv_iter(f_it); fv_it; ++fv_it)
        {
            glVertex3fv(mesh.point(fv_it).data());
        }
        glEnd();
    }
}

void drawLine()
{
    glPointSize(1.0f);
    glColor3f(0.0, 0.0, 0.0);
    for (MyMesh::FaceIter f_it = mesh.faces_begin(); f_it != mesh.faces_end(); ++f_it)
    {
        glBegin(GL_LINE_LOOP);
        for(MyMesh::FaceVertexIter fv_it = mesh.fv_iter(f_it); fv_it; ++fv_it)
        {
            glVertex3fv(mesh.point(fv_it).data());
        }
        glEnd();
    }
}


----------------------------------------------------------------------------------------

H. 建置並啟動:
     

2012年10月2日 星期二

OpenMesh: First Steps - Building a cube

since: 2012/10/02
update: 2012/10/03

reference:
1. OpenMesh: First Steps - Building a cube

A. 新增專案
     1. Visual Studio 2010 > 檔案 > 新增 > 專案

     2. Visual C++ > Win32 > Win32 主控台應用程式
         名稱: OpenMesh_Cube
         位置: C:\Lanli\projects
         為方案建立目錄: 取消勾選
          > 確定

     3. 下一步
          > 空專案: 勾選
          > 完成

-----------------------------------------------------------------------------------

B. 專案屬性設定
     1. 點選 "OpenMesh_Cube" 專案 > 滑鼠右鍵 > 屬性:
         組態屬性 > VC++ 目錄 >
         Include 目錄: C:\Program Files (x86)\OpenMesh 2.2\include
         程式庫目錄: C:\Program Files (x86)\OpenMesh 2.2\lib
          > 確定

     2. 點選 "OpenMesh_Cube" 專案 > 滑鼠右鍵 > 屬性:
         組態屬性 > 連結器 > 輸入:
         其他相依性: OpenMeshCored.lib
                             OpenMeshToolsd.lib
           > 確定


     3. Debug 模式的設定好後, 換設定 Release 模式1. 2.
          其中 2. 的 "其他相依性", 改成: (沒有 d 的版本)
           OpenMeshCore.lib
           OpenMeshTools.lib

-----------------------------------------------------------------------------------

C. 撰寫程式碼
     1. 點選 "OpenMesh_Cube" 專案 > 滑鼠右鍵 > 加入 > 新增項目:
         Visual C++ > C++ 檔 (.cpp)
         名稱: main.cpp
          > 新增

     2. main.cpp 加入以下的程式碼:
#include <iostream>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>

// ----------------------------------------------------------------------------
typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
// ----------------------------------------------------------------------------
// Build a simple cube and write it to std::cout


int main()
{
    MyMesh mesh;

    // generate vertices
    MyMesh::VertexHandle vhandle[8];

    vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
    vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1, 1));
    vhandle[2] = mesh.add_vertex(MyMesh::Point( 1, 1, 1));
    vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
    vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
    vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
    vhandle[6] = mesh.add_vertex(MyMesh::Point( 1, 1, -1));
    vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1, -1));


    // generate (quadrilateral) faces
    std::vector<MyMesh::VertexHandle> face_vhandles;

    face_vhandles.clear();
    face_vhandles.push_back(vhandle[0]);
    face_vhandles.push_back(vhandle[1]);
    face_vhandles.push_back(vhandle[2]);
    face_vhandles.push_back(vhandle[3]);
    mesh.add_face(face_vhandles);

    face_vhandles.clear();
    face_vhandles.push_back(vhandle[7]);
    face_vhandles.push_back(vhandle[6]);
    face_vhandles.push_back(vhandle[5]);
    face_vhandles.push_back(vhandle[4]);
    mesh.add_face(face_vhandles);

    face_vhandles.clear();
    face_vhandles.push_back(vhandle[1]);
    face_vhandles.push_back(vhandle[0]);
    face_vhandles.push_back(vhandle[4]);
    face_vhandles.push_back(vhandle[5]);
    mesh.add_face(face_vhandles);

    face_vhandles.clear();
    face_vhandles.push_back(vhandle[2]);
    face_vhandles.push_back(vhandle[1]);
    face_vhandles.push_back(vhandle[5]);
    face_vhandles.push_back(vhandle[6]);
    mesh.add_face(face_vhandles);

    face_vhandles.clear();
    face_vhandles.push_back(vhandle[3]);
    face_vhandles.push_back(vhandle[2]);
    face_vhandles.push_back(vhandle[6]);
    face_vhandles.push_back(vhandle[7]);
    mesh.add_face(face_vhandles);

    face_vhandles.clear();
    face_vhandles.push_back(vhandle[0]);
    face_vhandles.push_back(vhandle[3]);
    face_vhandles.push_back(vhandle[7]);
    face_vhandles.push_back(vhandle[4]);
    mesh.add_face(face_vhandles);


    // write mesh to output.off
    try
    {
        if ( !OpenMesh::IO::write_mesh(mesh, "output.off") )
        {
            std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
            return 1;
        }
    }
    catch( std::exception& x )
    {
        std::cerr << x.what() << std::endl;
        return 1;
    }

    return 0;
}


     3. 建置 > 建置方案
         錯誤訊息:
          "You have to define _USE_MATH_DEFINES in the compiler settings!"   

     4. 解決方式: (同時對 Debug Release 版本作修改)
         點選 "OpenMesh_Cube" 專案 > 滑鼠右鍵 > 屬性:
         組態屬性 > C/C++ > 前置處理器:
         _USE_MATH_DEFINES
         > 確定

     5. 建置 > 建置方案
         偵錯 > 啟動但不偵錯
         (Debug 與 Release 版皆成功)

     6.檢視結果:
        用文字編輯器開啟專案根目錄下的 output.off 檔案, 如下所示:
OFF
8 6 0
-1 -1 1
1 -1 1
1 1 1
-1 1 1
-1 -1 -1
1 -1 -1
1 1 -1
-1 1 -1
4 0 1 2 3
4 7 6 5 4
4 1 0 4 5
4 2 1 5 6
4 3 2 6 7
4 0 3 7 4


-----------------------------------------------------------------------------------

D. 檢視 3D 圖形 (update: 2012/09/03)
     1. 下載: MeshLab 並安裝.

     2. 用 MeshLab 開啟 output.off 檔案.