Wednesday, July 18, 2012

GRAFIC libraries

2012/07/19
做一個匯入3D模型之簡易動畫。

GRAFIC is a set of libraries of C and C++ callable routines for programming interactive
graphics applications on workstations and personal computers. Versions of the GRAFIC library are
available for workstations running X Windows™, Apple Macintosh™ PowerPC personal
computers, and Intel compatible personal computers running Microsoft Windows 95, 98 and NT.
Except for minor differences, the same source code will function as expected for these platforms
simply by re-compiling and linking with the appropriate GRAFIC library. The GRAFIC package
was developed to provide basic, cross-platform, multi-language support for programming windowbased
raster graphics applications without the complexities and language dependencies typical of
window graphics systems. The libraries can be accessed at URL www.cadlab.ecn.purdue.edu.
The next sections introduce the basic concepts for GRAFIC programming, followed by
descriptions of the routines. Information specific to programming using the Windows and
Macintosh versions of GRAFIC is provided in the closing sections.

1.
http://hungming.ct.ntust.edu.tw/
GRAFIC 是用來控制視窗,
利用GLUT需要設定步驟,
GLUT現在已經改為freeglut因此以前教學中glut32.lib全部要改為freeglut.lib,一些相關的連結檔案也要注意。

2.
已經可以跑出畫面,現在開始找功能強大以及可以匯入3DS模組的函式庫,
http://code.google.com/p/lib3ds/
可惜lib3DS說明文件太少,本身也未附帶函式之說明文件。

3.
嘗試使用開放源碼之3D引擎ORGE作為匯入3DS及建置場景之工具。
參考此篇網路文章所得ORGE之訊息
http://www.programmer-club.com/showSameTitleN/game/3494.html
以下ORGE首頁
http://www.ogre3d.org/
一篇中文教學網誌
http://chia0418.wordpress.com/2009/03/11/%E8%88%87ogre%E5%85%B1%E8%88%9E%EF%BC%9A%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%8C%E5%AE%89%E8%A3%9D-ogre-161-%E4%BD%BF%E7%94%A8-vc-20052008/

4.
授權分為BSD、MIT、GNU、GPL等等,ORGE是較為寬鬆的MIT license可以用

5.
匯入3DS失敗了,改成匯入OBJ檔案,先將3DS轉檔成OBJ
WINGS3D
匯入方法有以下教學
http://www.programmer-club.com/showsametitlen/opengl/1144.html
http://www.gamelife.idv.tw/viewtopic.php?t=211
http://lawlietmoon.pixnet.net/blog/post/27177462-%E8%AE%80obj%E6%AA%94(opengl,visual-c%2B%2B6.0)
貼出匯入範例程式檔如下

/////////////////////////
// glutTest08.cpp
//
// Created by Gary Ho, ma_hty@hotmail.com, 2005
//


#include <stdio.h>
#include <math.h>

#include "glut.h"

#include "glm.h"

#define G_PI 3.14159265358979323846f

void prepare_lighting();
void display();
void keyboard(unsigned char key, int x, int y);

float theta, phi;

GLuint list_id;

void main()
{
  theta = G_PI/2;
  phi = -G_PI/2;

  glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
  glutInitWindowSize( 640, 640 );
  glutCreateWindow( "glutTest08" );

  glutDisplayFunc(display);
  glutKeyboardFunc( keyboard );

  {
    GLMmodel *glm_model;
     glm_model= glmReadOBJ( "castle.obj" );
     glmUnitize( glm_model );
     glmScale( glm_model, .1 );
     glmFacetNormals( glm_model );
     glmVertexNormals( glm_model, 90 );

     list_id = glmList( glm_model, GLM_MATERIAL | GLM_SMOOTH );

     glmDelete( glm_model );
  }

  prepare_lighting();

  glutMainLoop();
}

void keyboard(unsigned char key, int x, int y)
{
  switch( key )
  {
    case 'w':
     theta -= .05;
     prepare_lighting();
     glutPostRedisplay();
    break;

    case 's':
     theta += .05;
     prepare_lighting();
     glutPostRedisplay();
    break;

    case 'a':
     phi -= .05;
     prepare_lighting();
     glutPostRedisplay();
    break;

    case 'd':
     phi += .05;
     prepare_lighting();
     glutPostRedisplay();
    break;
  };
}


void display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective( 20, 1, 0.1, 10 );

    glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     gluLookAt(
     0,0,1,
     0,0,0,
     0,1,0 );

    glEnable( GL_LIGHTING );
    glEnable( GL_DEPTH_TEST );
 
    glCallList( list_id );

  glutSwapBuffers();
}

void prepare_lighting()
{
  theta = fmodf( theta, 2*G_PI );
  phi = fmodf( phi, 2*G_PI );

  float light_diffuse[4] = {1.0, 1.0, 1.0, 1.0};
  float mat_diffuse[4] = {1.0, 1.0, 1.0, 1.0};
  float light_position[4] = { sinf(theta) * cosf(phi), cosf(theta), -sinf(theta) * sinf(phi), 0 };

  //glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  glEnable( GL_LIGHT0 );
}

6.弳度或弧度




單位弧度定義為圓弧長度等於半徑時的圓心角

7.發現一個很容易查GLUT函式的網站
http://msdn.microsoft.com/en-us/library/windows/desktop/dd318369(v=vs.85).aspx

8.