關於我自己

2014年2月27日 星期四

OpenGL 光線與旋轉和滑鼠事件 Ch003

 (如有翻譯或解釋不太洽當,請多多指教)

glViewport

投影預覽


glMatrixMode

矩陣模式


gluPerspective

透視


gluLookAt

視點


glLightfv

光線


glLightModelfv

光線模式


glutSolidSphere

實心球面

 glRotatef

 旋轉


Code:


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

GLuint list; 
float spinx = 0; 
float spiny = 0; 
float spinz = 0;
GLfloat tdist = 0.0; 
GLfloat polyfactor = 1.0; 
GLfloat polyunits = 1.0; 
float delang = 5.0; 
int xAbs, yAbs;

void display (void) 

    GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 }; 
    GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 }; 
 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glPushMatrix (); 
    glTranslatef (0.0, 0.0, tdist); 
    glRotatef ((GLfloat) spinx, 1.0, 0.0, 0.0); 
    glRotatef ((GLfloat) spiny, 0.0, 1.0, 0.0); 
    glRotatef((GLfloat) spinz, 0.0, 0.0, 1.0);
  
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray); 
    glMaterialfv(GL_FRONT, GL_SPECULAR, black); 
    glMaterialf(GL_FRONT, GL_SHININESS, 0.0); 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_POLYGON_OFFSET_FILL); 
    glPolygonOffset(polyfactor, polyunits); 
    glCallList (list); 
    glDisable(GL_POLYGON_OFFSET_FILL); 
 
    glDisable(GL_LIGHTING); 
    glDisable(GL_LIGHT0); 
    glColor3f (1.0, 1.0, 0.0); 
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
    glCallList (list); 
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
 
    glPopMatrix (); 
    glFlush (); 

 
void gfxinit (void) 

    GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; 
    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; 
    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; 
 
    GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; 
 
    glClearColor (0.0, 0.0, 0.0, 1.0); 
 
    list = glGenLists(1); 
    glNewList (list, GL_COMPILE); 
       glutSolidSphere(1.0, 20, 12); 
    glEndList (); 
 
    glEnable(GL_DEPTH_TEST); 
 
    glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient); 
    glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse); 
    glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular); 
    glLightfv (GL_LIGHT0, GL_POSITION, light_position); 
    glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient); 

 
// 重塑
void reshape(int width, int height) 

    glViewport (0, 0, width, height); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity (); 
    gluPerspective(45.0, (GLdouble)width/(GLdouble)height, 
        1.0, 10.0); 
    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity (); 
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

 
void CheckRot()
{
    if (spinx > 360.0f)
        spinx = 0.0f;
    if (spinx < 0.0f)
        spinx = 360.0-delang;
  
    if (spiny > 360.0f)
        spiny = 0.0f;
    if (spiny < 0.0f)
        spiny = 360.0f-delang;
  
    if (spinz > 360.0f)
        spinz = 0.0f;
    if (spinz < 0.0f)
        spinz = 360.0f-delang;

// 滑鼠移動 
void MouseMove( int x, int y) { 
    spinx  += (y-yAbs);
    yAbs = y;
    spiny -= (x-xAbs);
    xAbs = x;
    CheckRot();
    glutPostRedisplay();

// 滑鼠按下
void MousePress(int button, int state, int x, int y)
{
    if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
    {
        xAbs = x;
        yAbs = y;
    }

int main(int argc, char** argv) 

    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); //設定初始顯示模組
    glutCreateWindow(argv[0]); 
    glutReshapeFunc(reshape);     // 重塑
    glutMotionFunc (MouseMove);  // 滑鼠移動事件
    glutDisplayFunc(display);    // 顯示
    gfxinit();                   
    glutMainLoop(); 
    return 0; 
}  

 

 =====================================================

 

 光源 glLightfv

   glLightfv( 光源, 光源屬性, RGBA );

   // 設定發光體的光源的特性
   glEnable(GL_DEPTH_TEST);                               //啟動深度測試 
   glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient);      //設定 環境光 GL_AMBIENT
   glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse);        //設定 散射光 GL_DIFFUSE
   glLightfv( GL_LIGHT0, GL_SPECULAR,light_specular);  //設定 反射光 GL_SPECULAR
  
   glLightfv( GL_LIGHT0, GL_POSITION,light_position);     //光的座標
   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);  // 光線模型


 

 glRotatef 旋轉


glRotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )

glRotatef(角度,X軸,Y軸,Z軸)

 

 

  glutMotionFunc (MouseMove);  // 滑鼠移動事件 

將涵式加入 glutMotionFunc

2014年2月24日 星期一

OpenGL 二維向量(glVertex2f) 與 三維向量(glVertex3f) Ch002

1.二維向量(glVertex2f)

說明: 二維向量就是指只有X軸跟Y軸  

  glVertex2f(X軸,Y軸) ;

 

程式碼:

#include <GL/glut.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>
void display()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_QUADS) ;
  glVertex2f(-0.5,-0.5) ;
  glVertex2f(-0.5,0.5) ;
  glVertex2f(0.5,0.5) ;
  glVertex2f(0.5,-0.5) ;
  glEnd() ;

glFlush();
}
/* Program entry point */
int main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
  glutInitWindowSize(500, 500); 
  glutCreateWindow("OpenGL");
  glutDisplayFunc( display );
  glutMainLoop();
  return 0;
}

執行後的圖:

2.三維向量(glVertex3f)

說明: 三維向量就是指只有X軸跟Y軸跟Z軸

glVertex3f(X軸,Y軸,Z軸);

Code:


#include <GL/glut.h>

#include <stdlib.h>
static GLfloat xRot = 45.0f;
static GLfloat yRot = 45.0f;



void RenderScene(void)
 {
 //清除
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 //保存矩陣
 glPushMatrix();

 //旋轉
 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
 glRotatef(yRot, 0.0f, 1.0f, 0.0f);



 glBegin(GL_QUADS);

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);

 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);



 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);

 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);

 
 
 
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);

 

 glEnd();

    glPopMatrix();

 glutSwapBuffers();
 }

void SetupRC()
 {
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

 glEnable(GL_DEPTH_TEST);
 glEnable(GL_DITHER);
 glShadeModel(GL_SMOOTH);
 }

void SpecialKeys(int key, int x, int y)
 {
 if(key == GLUT_KEY_UP)
  xRot-= 5.0f;

 if(key == GLUT_KEY_DOWN)
  xRot += 5.0f;

 if(key == GLUT_KEY_LEFT)
  yRot -= 5.0f;

 if(key == GLUT_KEY_RIGHT)
  yRot += 5.0f;

    if(key > 356.0f)
        xRot = 0.0f;

    if(key < -1.0f)
        xRot = 355.0f;

    if(key > 356.0f)
        yRot = 0.0f;

    if(key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
    }


void ChangeSize(int w, int h)
{
 GLfloat fAspect;

 if(h == 0)
  h = 1;

 glViewport(0, 0, w, h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 fAspect = (GLfloat)w / (GLfloat)h;
 gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -400.0f);
}

int main(int argc, char* argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(800,600);
 glutCreateWindow("RGB Cube");
 glutReshapeFunc(ChangeSize);
 glutSpecialFunc(SpecialKeys);
 glutDisplayFunc(RenderScene);
 SetupRC();
 glutMainLoop();

 return 0;
}

執行出來的圖: (故加線條讓使用者比較方便明白)

解說:因為用GL_QUADS,可以參考上一張的圖,這個用意

 

看一定很奇怪 明明圖只有八個角度,為什麼設定的參數有24個,

以下圖片一一為大家說明


 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);

 

首先紅色部分CODE執行 (有加入X跟Y跟Z做標指示方便了解)

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);


 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);

首先藍色部分CODE執行 (有加入X跟Y跟Z做標指示方便了解)

 

glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);


 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);

首先綠色部分CODE執行 (有加入X跟Y跟Z做標指示方便了解)

 glVertex3f(50.0f,50.0f,50.0f);
 glVertex3f(50.0f,50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,-50.0f);
 glVertex3f(50.0f,-50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,50.0f);
 glVertex3f(-50.0f,50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,-50.0f);
 glVertex3f(-50.0f,-50.0f,50.0f);

 所以才組成

 

 

2014年2月23日 星期日

OpenGL 基本介紹 Ch001

1.OpenGL 基本簡單主架構:

 

 Code :

#include <GL/glut.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除
     glLoadIdentity();              // 讀取特性 
     glBegin(GL_TRIANGLES);         // 開始繪畫 設定樣式                            
     glVertex3f( 0.0f, 1.0f, 0.0f);
     glVertex3f(-1.0f,-1.0f, 0.0f);
     glVertex3f( 1.0f,-1.0f, 0.0f);                          
     glEnd();                       // 結束
     glFlush();                     // 清空緩衝區
}
/* 程式進入點 */
int main(int argc, char *argv[])
{
  glutInit(&argc, argv);            // 初始化
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); // 設置顯示模式
  glutInitWindowSize(512, 512);     // 設定視窗大小 
  glutCreateWindow("AMI2000 Ch01"); // 設定標頭名稱
  glutDisplayFunc( display );       // 載入呈現涵式
  glutMainLoop();                   // 主程式迴圈
  return 0;
}

2.繪畫模式:

 

 

 GL_POINTS:繪畫點


GL_LINES:繪畫線


GL_LINE_STRIP:繪畫折線


GL_LINE_LOOP:繪畫閉合曲線


GL_TRIANGLES:繪畫三角形


GL_TRIANGLE_STRIP:繪畫三角形狀


GL_TRIANGLE_FAN:繪畫三角形扇


GL_QUADS:繪畫四邊形


GL_QUAD_STRIP:繪畫四邊形狀

 

3. glClearColor(255, 255, 255, 1) => RGB 顏色模式

說明:設置背景顏色

R:255  G:255 B:255   1  => 白色

R:0  G:255 B:0          1   =>綠色

R:0  G:0 B:255          1   =>藍色

以此類推


 

 

 

2014年2月22日 星期六

OpenGL with Dev-C++

1.下載 Dev-C++
網址:
http://www.bloodshed.net/dev/devcpp.html

我的平台:
Windows   7   64位元 




 2. 安裝 Dev-C++



 3.


4.


5.


6.


7.


8. 

9.

10.

11.

12.

13.在勾選glut


14.


15.


16.


17.


18.


19.


20. 


21.

22.

23.