openGL三維網格座標,旋轉,縮放,燈光設置,紋理讀取,模型讀取(MFC單文檔)

 

 openGL三維網格座標,旋轉,縮放,燈光設置,紋理讀取,模型讀取(MFC單文檔)


源碼下載鏈接:鏈接:http://pan.baidu.com/s/1slANShZ 密碼:hbwj

1.三維網格座標建立 
2.基本3維圖形創建 
3.鼠標相應旋轉縮放 
4.鍵盤相應旋轉縮放 
5.燈光設置 
6.紋理載入映射 
7.讀取模型


關於MFC配置編寫openGL網上有很多教程 
需要的函數創建一般是:


OnCreat() 
OnDestroy() 
Onsize() 
PreCreateWindow() 
OnDraw()


這裏寫圖片描述


在我的MFC單文檔項目中enableview.h和enableview.cpp負責上面的窗口建立,myopenglview.h和myopenglView.cpp主要是功能的實現


1.三維網格建立: 
這裏寫圖片描述

void GLGrid(float pt1x, float pt1y, float pt1z, float pt2x, float pt2y, float pt2z, int num)

{

    const float _xLen = (pt2x - pt1x) / num;
    const float _yLen = (pt2y - pt1y) / num;
    const float _zLen = (pt2z - pt1z) / num;
    glLineWidth(2.f);
    glLineStipple(1, 0x0303);//線條樣式

    glBegin(GL_LINES);
    glEnable(GL_LINE_SMOOTH);

    int xi = 0;
    int yi = 0;
    int zi = 0;

    //繪製平行於X的直線
    for (zi = 0; zi <= num; zi++)
    {
        float z = _zLen * zi + pt1z;
        for (yi = 0; yi <= num; yi++)
        {
            float y = _yLen * yi + pt1y;
            glVertex3f(pt1x, y, z);
            glVertex3f(pt2x, y, z);
        }
    }
    //繪製平行於Y的直線
    for (zi = 0; zi <= num; zi++)
    {
        float z = _zLen * zi + pt1z;
        for (xi = 0; xi <= num; xi++)
        {
            float x = _xLen * xi + pt1x;
            glVertex3f(x, pt1y, z);
            glVertex3f(x, pt2y, z);
        }
    }
    //繪製平行於Z的直線
    for (yi = 0; yi <= num; yi++)
    {
        float y = _yLen * yi + pt1y;
        for (xi = 0; xi <= num; xi++)
        {
            float x = _xLen * xi + pt1x;
            glVertex3f(x, y, pt1z);
            glVertex3f(x, y, pt2z);
        }
    }
    glEnd();
}
void CmyopenglView::ordination() {

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glEnable(GL_POINT_SMOOTH);                   //設置反走樣
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);       //設置反走樣
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    glRotatef(-45, 0.0, 1.0, 0.0);
    //網格
    glPushMatrix();
    glColor3f(0.9f, 0.9f, 0.9f);
    glTranslatef(-4, -4, -4);
    GLGrid(0,0,0,8,0,8,20);
    glPopMatrix();


    glPushMatrix();
    glTranslated(-4,4, -4);
    glRotatef(90, 1.0, 0.0, 0.0);
    glColor3f(0.9f, 0.9f, 0.0f);
    GLGrid(0, 0, 0, 8, 0, 8, 20);
    glPopMatrix();


    glPushMatrix();
    glTranslatef(-4, -4, -4);
    glRotatef(90, 0.0, 0.0, 1.0);
    glColor3f(0.0f, 0.9f, 0.0f);
    GLGrid(0, 0, 0, 8, 0, 8, 20);
    glPopMatrix();




    glDisable(GL_BLEND);
    glDisable(GL_LINE_SMOOTH);
    glDisable(GL_POINT_SMOOTH);
    glDisable(GL_POLYGON_SMOOTH);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

我們在ordination()函數中增加繪製x,y,z座標的代碼

void CmyopenglView::ordination() {

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glEnable(GL_POINT_SMOOTH);                   //設置反走樣
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);       //設置反走樣
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    glRotatef(-45, 0.0, 1.0, 0.0);
    //網格
    glPushMatrix();
    glColor3f(0.9f, 0.9f, 0.9f);
    glTranslatef(-4, -4, -4);
    GLGrid(0,0,0,8,0,8,20);
    glPopMatrix();


    glPushMatrix();
    glTranslated(-4,4, -4);
    glRotatef(90, 1.0, 0.0, 0.0);
    glColor3f(0.9f, 0.9f, 0.0f);
    GLGrid(0, 0, 0, 8, 0, 8, 20);
    glPopMatrix();


    glPushMatrix();
    glTranslatef(-4, -4, -4);
    glRotatef(90, 0.0, 0.0, 1.0);
    glColor3f(0.0f, 0.9f, 0.0f);
    GLGrid(0, 0, 0, 8, 0, 8, 20);
    glPopMatrix();

    //x
    //glTranslatef(-2, -2, -2);
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(3.5, 0.0f, 0.0f);
    glEnd();
    glPushMatrix();
    glTranslatef(3.5, 0.0f, 0.0f);
    glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    glutWireCone(0.027, 0.09, 10, 10);
    glPopMatrix();


    //y
    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0, 3.5, 0.0f);
    glEnd();
    glPushMatrix();
    glTranslatef(0.0, 3.5, 0.0f);
    glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
    glutWireCone(0.027, 0.09, 10, 10);
    glPopMatrix();


    //z
    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0, 0.0f, 3.5);
    glEnd();
    glPushMatrix();
    glTranslatef(0.0, 0.0f, 3.5);
    glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
    glutWireCone(0.027, 0.09, 10, 10);
    glPopMatrix();


    glDisable(GL_BLEND);
    glDisable(GL_LINE_SMOOTH);
    glDisable(GL_POINT_SMOOTH);
    glDisable(GL_POLYGON_SMOOTH);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

這裏寫圖片描述


2.基本三維圖形創建 
點模型/線模型/面模型

glColor3f(1.0f, 1.0f, 1.0f);
    if (model == 1)
    {
        if (type == 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        if (type == 2)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        if (type == 3)
            glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
        auxSolidCube(4);

    }
    if (model == 2)
    {
        if (type == 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        if (type == 2)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);      
        if(type == 3)
            glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
        auxSolidSphere(3.0);
    }

    if (model == 3)
    {
        glPushMatrix();
        glRotatef(90, -1.0, 0.0, 0.0);
        if (type == 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        if (type == 2)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        if (type == 3)
            glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
        glutSolidCone(3, 3, 100, 100);
        glPopMatrix();

    }

    if (model == 4)
    {
        if (type == 1)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        if (type == 2)
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        if (type == 3)
            glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
        glutSolidTeapot(2.5);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述


3.鼠標相應旋轉縮放


BOOL enableview::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    // TODO: 在此添加消息處理程序代碼和/或調用默認值
    double a = zDelta / 120;
    if ((scale + a * 0.1)  <  10)
        scale += a * 0.1;

    this->InvalidateRect(NULL, FALSE);
    return CView::OnMouseWheel(nFlags, zDelta, pt);
}


void enableview::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息處理程序代碼和/或調用默認值
    if (nFlags & MK_LBUTTON == TRUE) {

        //MessageBox("mouse move function triggered!", "attentino", MB_OK);
        du += point.x - oldmx;              //鼠標在窗口x軸方向上的增量加到視點繞y軸的角度上,這樣就左右轉了
        h += 0.03f*(point.y - oldmy);       //鼠標在窗口y軸方向上的改變加到視點的y座標上,就上下轉了
        if (h>15.0f) h = 15.0f;             //視點y座標作一些限制,不會使視點太奇怪
        else if (h<-5.0f) h = -5.0f;
        oldmx = point.x, oldmy = point.y;   //把此時的鼠標座標作爲舊值,爲下一次計算增量做準備
                                            /*CString debug;
                                            debug.Format(_T("h,du= %0.3f %3d\n"), h, du);
                                            OutputDebugString(debug);*/
                                            //OnPaint();
        this->OnDraw(this->GetDC());    //重繪界面
    }
    else if (nFlags & MK_RBUTTON == TRUE)
    {
        oldmx += point.x - oldmx;
        oldmy += point.y - oldmy;
        glTranslatef(oldmx, oldmy, -0.1f);
        this->OnDraw(this->GetDC());
        oldmx = point.x, oldmy = point.y;
    }
    else {
        oldmx = point.x, oldmy = point.y;
        //OutputDebugString(_T("mouse up\n"));
    }
    //CView::OnMouseMove(nFlags, point);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

4.鍵盤相應旋轉縮放


BOOL CmyopenglView::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)  // If a keydown message
    {

        if (pMsg->wParam == _T('W'))
        {

            this->rotate_x += 6.0;
            if (this->rotate_x > 360)this->rotate_x = -360;
            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('X'))
        {

            this->rotate_x += 6.0;
            if (this->rotate_x < -360)this->rotate_x = 360;
            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('A'))
        {

            this->rotate_y -= 6.0;
            if (this->rotate_y < -360)this->rotate_y = 360;
            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('D'))
        {

            this->rotate_y += 6.0;
            if (this->rotate_y > 360)this->rotate_y = -360;
            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('Z'))
        {

            this->rotate_z -= 6.0;
            if (this->rotate_z < -360)this->rotate_z = 360;
            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('E'))
        {

            this->rotate_z += 6.0;
            if (this->rotate_z > 360)this->rotate_z = -360;
            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('Q'))
        {
            if ((scale + 2)  <  10)
                scale += 2;

            this->InvalidateRect(NULL, FALSE);
        }
        if (pMsg->wParam == _T('R'))
        {
                scale -= 2;

            this->InvalidateRect(NULL, FALSE);
        }


    }

    return CView::PreTranslateMessage(pMsg);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

這裏寫圖片描述


5.燈光設置: 
單方位燈光/多方位光/多種類型光效果

// 設置材質顏色
GLfloat mat_ambient[] = { 0.6f, 0.6f, 0.6f, 1.0f };                     // 藍色的材質環境光
GLfloat mat_diffuse[] = { 0.6f, 0.6f, 0.9f, 1.0f };                     // 藍色的材質漫反射光
GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };                    // 全白色的材質鏡面反射光
GLfloat mat_emission[] = { 0.5f, 0.5f, 0.5f, 1.0f };                    // 淡白色的材質輻射光

GLfloat no_mat[] = { 0.0f, 0.0f, 0.0f, 1.0f };                          // 無光(黑色光),用於關閉某種屬性光時應用
GLfloat no_shininess[] = { 0.0f };                                      // 無鏡面反射
GLfloat low_shininess[] = { 5.0f };                                     // 低鏡面反射指數
GLfloat high_shininess[] = { 70.0f };                                   // 高鏡面反射指數
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

void CmyopenglView::InitalLigt()
{
    GLfloat light_position1[4] = { -52, -16, -50, 0 };
    GLfloat light_position2[4] = { -26, -48, -50, 0 };
    GLfloat light_position3[4] = { 16, -52, -50, 0 };

    GLfloat direction1[3] = { 52, 16, 50 };
    GLfloat direction2[3] = { 26, 48, 50 };
    GLfloat direction3[3] = { -16, 52, 50 };

    GLfloat light_position4[4] = { 52, 16, 50, 0 };
    GLfloat light_position5[4] = { 26, 48, 50, 0 };
    GLfloat light_position6[4] = { -16, 52, 50, 0 };

    GLfloat direction4[3] = { -52, -16, -50 };
    GLfloat direction5[3] = { -26, -48, -50 };
    GLfloat direction6[3] = { 16, -52, -50 };

    GLfloat color1[4], color2[4], color3[4], color4[4], color5[4], color6[4];

    glClearColor(1, 1, 1, 0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    if (color_type == 0) {  //彩色燈光
        color1[0] = 1; color1[1] = 0; color1[2] = 0; color1[3] = 1;
        color2[0] = 0.5; color2[1] = 1; color2[2] = 0; color2[3] = 1;
        color3[0] = 0; color3[1] = 0; color3[2] = 1; color3[3] = 1;

        color4[0] = 1; color4[1] = 0; color4[2] = 0; color4[3] = 1;
        color5[0] = 0.5; color5[1] = 1; color5[2] = 0; color5[3] = 1;
        color6[0] = 0; color6[1] = 0; color6[2] = 1; color6[3] = 1;

        GLfloat ambient[4] = { 0.3f, 0.3f, 0.3f, 1.0f };

        GLfloat material_color[4] = { 1, 1, 1, 0.5f };
        GLfloat material_specular[4] = { 0.5f, 0.5f, 0.5f, 0.5f };
        GLfloat material_ambient[4] = { 0.0, 0.0, 0.0, 0.0 };

        glLightfv(GL_LIGHT3, GL_POSITION, light_position4);
        glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, direction4);
        glLightfv(GL_LIGHT3, GL_DIFFUSE, color4);
        glLightfv(GL_LIGHT3, GL_SPECULAR, color4);

        glLightfv(GL_LIGHT4, GL_POSITION, light_position5);
        glLightfv(GL_LIGHT4, GL_SPOT_DIRECTION, direction5);
        glLightfv(GL_LIGHT4, GL_DIFFUSE, color5);
        glLightfv(GL_LIGHT4, GL_SPECULAR, color5);

        glLightfv(GL_LIGHT5, GL_POSITION, light_position6);
        glLightfv(GL_LIGHT5, GL_SPOT_DIRECTION, direction6);
        glLightfv(GL_LIGHT5, GL_DIFFUSE, color6);
        glLightfv(GL_LIGHT5, GL_SPECULAR, color6);

        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);

        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_color);
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 128);

        glDisable(GL_LIGHT0);
        glDisable(GL_LIGHTING);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT3);
        glEnable(GL_LIGHT4);
        glEnable(GL_LIGHT5);

        glDisable(GL_COLOR_MATERIAL);
        return;
    }


    if (color_type == 1)
    {
        //白色燈光
        glDisable(GL_LIGHT3);
        glDisable(GL_LIGHT4);
        glDisable(GL_LIGHT5);
        glDisable(GL_LIGHTING);
        GLfloat m_LightPostion[4] = { 0.0f, 10.0f, 10.0f, 1.0f };

        GLfloat ambientLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
        GLfloat diffuseLight[] = { 0.5, 0.5f, 0.5f, 1.0f };
        GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };

        glEnable(GL_LIGHTING);
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
        glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
        glLightfv(GL_LIGHT0, GL_POSITION, m_LightPostion);
        glEnable(GL_LIGHT0);

        glEnable(GL_COLOR_MATERIAL);
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    }
    else {

        glDisable(GL_LIGHT3);
        glDisable(GL_LIGHT4);
        glDisable(GL_LIGHT5);
        glDisable(GL_LIGHTING);
        glDisable(GL_COLOR_MATERIAL);
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        //glDisable(GL_LIGHTING);
        GLfloat no_ambientLight[] = { 0.0f, 0.0f, 0.0f, 1.0f };                 // 用於關掉默認的全局環境光
                                                                                // 設置光源的顏色
        GLfloat ambientLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };                    // 白色環境光
        GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };                    // 白色漫射光
        GLfloat specularLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };                   // 白色鏡面反射光
        GLfloat m_LightPostion[] = { 0.0f, 0.0f, 1.0f, 0.0f };                  // 光源起始位置

        // 1.僅漫射光
        if (color_type == 12) {
            glEnable(GL_LIGHTING);
            //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, no_ambientLight);          // 關掉默認的全局環境光
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
            glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
            glLightfv(GL_LIGHT0, GL_POSITION, m_LightPostion);

            glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);                 // 關閉材質的環境反射光顏色
            glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);            // 設置mat_diffuse的材質漫反射光
            glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);                // 關閉材質的鏡面反射光顏色
            glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);         // 設置材質的鏡面反射指數爲0
            glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);                // 關閉材質的輻射光
            glEnable(GL_LIGHT0);
        }
        // 2.僅鏡面光
        if (color_type == 13) {
            glEnable(GL_LIGHTING);
            //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, no_ambientLight);          // 關掉默認的全局環境光
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
            glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
            glLightfv(GL_LIGHT0, GL_POSITION, m_LightPostion);

            glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, no_mat);
            glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
            glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
            glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
            glEnable(GL_LIGHT0);
        }


        // 3.漫射光與低鏡面光
        if (color_type == 16) {
            glEnable(GL_LIGHTING);
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, no_ambientLight);            // 關掉默認的全局環境光
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
            glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
            glLightfv(GL_LIGHT0, GL_POSITION, m_LightPostion);

            glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
            glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
            glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
            glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
            glEnable(GL_LIGHT0);
        }

        // 4.輻射光與低鏡面光
        if (color_type == 18) {
            glEnable(GL_LIGHTING);
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, no_ambientLight);            // 關掉默認的全局環境光
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
            glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
            glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
            glLightfv(GL_LIGHT0, GL_POSITION, m_LightPostion);

            glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, no_mat);
            glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
            glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
            glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
            glEnable(GL_LIGHT0);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187

這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述



6.紋理載入映射

BOOL CmyopenglView::LoadImageResources()
{
    FILE *File = NULL;
    AUX_RGBImageRec* textrue_Resource[6];
    if (model == 5 && type == 51)
        resource_path[0] = "shuijing.bmp";
    if(model == 5 && type == 52 )
        resource_path[0] = "earth.bmp";
    if (model == 5 && type == 53)
        resource_path[0] = "painting1.bmp"; 
    if (model == 5 && type == 54)
        resource_path[0] = "5.bmp";



    /*resource_path[1] = "image/2.bmp";
    resource_path[2] = "image/3.bmp";
    resource_path[3] = "image/4.bmp";
    resource_path[4] = "image/5.bmp";
    resource_path[5] = "image/6.bmp";*/

    //裝載圖像文件資源  
    for (int i = 0; i < 6; i++)//如果只需要一張貼圖其實resource_path數組只需要一個元素就可以了
    {
        File = fopen(resource_path[0], "r");
        if (!File)
        {
            //MessageBox(NULL, "加載圖像資源文件失敗 !", "Fail", MB_OK);
            return FALSE;
        }
        fclose(File);
        CString str = CString(resource_path[0]);
        USES_CONVERSION;
        LPCWSTR wszClassName = A2CW(W2A(str));
        textrue_Resource[i] = auxDIBImageLoad(wszClassName);
        File = NULL;
    }

    //生成紋理
    glGenTextures(6, texture);
    for (int i = 0; i < 6; i++)
    {
        glBindTexture(GL_TEXTURE_2D, texture[i]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        //Use the mipmap texture
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, \
            textrue_Resource[i]->sizeX, textrue_Resource[i]->sizeY, \
            GL_RGB, GL_UNSIGNED_BYTE, textrue_Resource[i]->data);

        //刪除堆上的臨時圖像
        delete textrue_Resource[i]->data;
        delete textrue_Resource[i];
    }

    return TRUE;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
void CmyopenglView::Draw_textrue() {
    GLUquadricObj* qobj;
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    InitalLigt();       ///初始化光照信息
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    glPushMatrix();
    glTranslatef(0.0f, 0.0f, scale);        //滾輪縮放
    gluLookAt(r*cos(c*du), h, r*sin(c*du), 0, 0, 0, 0, 1, 0); //從視點看遠點,y軸方向(0,1,0)是上方向,鼠標拖動
    glRotatef(this->rotate_x, 1.0, 0.0, 0.0);
    glRotatef(this->rotate_y, 0.0, 1.0, 0.0);
    glRotatef(this->rotate_z, 0.0, 0.0, 1.0);
    if (zuobiao)
        ordination();
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    qobj = gluNewQuadric();
    //畫球體

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glEnable(GL_TEXTURE_2D);
    gluQuadricTexture(qobj, GL_TRUE);//紋理函數
    if (type == 51)
    {
        glBegin(GL_QUADS);
        // Front Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, 3.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -3.0f, 3.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, 3.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 3.0f, 3.0f);

        // Back Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, -3.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, 3.0f, -3.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, -3.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, -3.0f, -3.0f);

        // Top Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, 3.0f, -3.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, 3.0f, 3.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, 3.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, 3.0f, -3.0f);

        // Bottom Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, -3.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, -3.0f, -3.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, -3.0f, 3.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, -3.0f, 3.0f);

        // Right face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(3.0f, -3.0f, -3.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, 3.0f, -3.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 3.0f, 3.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(3.0f, -3.0f, 3.0f);

        // Left Face
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -3.0f, -3.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-3.0f, -3.0f, 3.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(-3.0f, 3.0f, 3.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 3.0f, -3.0f);
        glEnd();
    }
    if( type == 52 )
    gluSphere(qobj, 4, 60, 60);//二次曲面qobj
    if( type == 53 )
    gluCylinder(qobj, 3.5, 3.5, 6, 26, 23);
    if( type == 54 )
    gluCylinder(qobj, 3.5, 0.0, 6, 26, 23);

    glPopMatrix();
    glDisable(GL_TEXTURE_2D);

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述


6.讀取obj模型 
我只是簡單的讀取vt,vn,f等基本參數

void CmyopenglView::ReadObj(char* Filename)
{
    VN.clear();
    V.clear();
    VT.clear();
    F.clear();
    FQ.clear();
    ifstream in(Filename);
    string aline; //逐行讀入
    string erase;
    while (getline(in, aline))
    {
        if (aline[0] == 'v')
        {
            if (aline[1] == 'n') //vn
            {
                istringstream sin(aline);
                Vertex v;
                sin >> erase >> v.x >> v.y >> v.z;
                VN.push_back(v);
            }

            else if (aline[1] == 't')//vt
            {
                istringstream sin(aline);
                Texture v;
                sin >> erase >> v.s >> v.t;
                VT.push_back(v);
            }

            else //v
            {
                istringstream sin(aline);
                Vertex v;
                sin >> erase >> v.x >> v.y >> v.z;
                V.push_back(v);
            }
        }

        else if (aline[0] == 'f')
        {
            istringstream sin(aline);
            sin >> erase;
            vector<string> strvector;
            string temp;


            while (sin >> temp) {
                strvector.push_back(temp);
            }

            if (strvector.size() == 3) {//三角面片
                Face fff;
                for (int count = 0; count < 3; count++) {
                    string kkk = strvector[count];
                    int i = 0;
                    int num = 0;
                    //頂點索引
                    for (; i < kkk.size() && kkk[i] != '/'; i++)
                        num = num * 10 + kkk[i] - '0';
                    fff.v[count] = num;
                    i++;
                    num = 0;
                    //vt
                    num = 0;
                    for (; i < kkk.size() && kkk[i] != '/'; i++)
                        num = num * 10 + kkk[i] - '0';
                    fff.vt[0] = num;
                    i++;
                    num = 0;
                    //法向量索引
                    for (; i < kkk.size() && kkk[i] != '/'; i++)
                        num = num * 10 + kkk[i] - '0';
                    fff.vn[count] = num;

                }
                F.push_back(fff);
            }

            else if (strvector.size() == 4)
            {


                FaceQ fff;
                for (int count = 0; count < strvector.size(); count++) {
                    string kkk = strvector[count];
                    int i = 0;
                    int num = 0;
                    //頂點索引
                    for (; i < kkk.size() && kkk[i] != '/'; i++)
                        num = num * 10 + kkk[i] - '0';
                    fff.v[count] = num;
                    i++;
                    num = 0;
                    //vt
                    num = 0;
                    for (; i < kkk.size() && kkk[i] != '/'; i++)
                        num = num * 10 + kkk[i] - '0';
                    fff.vt[0] = num;
                    i++;
                    num = 0;
                    //法向量索引
                    for (; i < kkk.size() && kkk[i] != '/'; i++)
                        num = num * 10 + kkk[i] - '0';
                    fff.vn[count] = num;

                }

                FQ.push_back(fff);
            }

        }
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116

繪製obj模型:


void CmyopenglView::OnReadobj()
{
    model = 6;


    wchar_t filters[] =
        L"3D模型文件(*.obj)\
|*.obj|所有文件(*.*)|*.*||";
    CFileDialog fileDlg(TRUE, NULL, NULL,
        OFN_HIDEREADONLY, filters);
    if (fileDlg.DoModal() == IDOK)
    {

        CString strBuf = fileDlg.GetPathName();
        USES_CONVERSION;
        char *Filename = T2A(strBuf.GetBuffer(0));
        ReadObj(Filename);
    }
    stringstream ss;
    ss <<"OK!";
    string str;
    ss >> str;
    CString s;
    s = str.c_str();
    MessageBox(s);

    float min_x, min_y, min_z, max_x, max_y, max_z;
    min_x = min_y = min_z = 10000000;
    max_x = max_y = max_z = -1000000;
    for (int i = 0; i < V.size(); i++)
    {
        min_x = min(min_x, V[i].x);
        min_y = min(min_y, V[i].y);
        min_z = min(min_z, V[i].z);
        max_x = max(max_x, V[i].x);
        max_y = max(max_y, V[i].y);
        max_z = max(max_z, V[i].z);
    }
    worldx = (min_x + max_x) / 2;
    worldy = (min_y + max_y) / 2;
    worldz = (min_z + max_z) / 2;
    type = 1;
    Invalidate();
    CDC* ppDC = GetWindowDC();
    OnDrawGL(ppDC);
    // TODO: 在此添加命令處理程序代碼
}

void CmyopenglView::Draw_obj()
{
    if (type == 1) {
        if (!VN.empty()) {
            for (int i = 0; i < F.size(); i++) {
                glBegin(GL_LINE_LOOP);
                for (int j = 0; j < 3; j++) {
                    glVertex3f(V[F[i].v[j] - 1].x, V[F[i].v[j] - 1].y, V[F[i].v[j] - 1].z);
                }
                glEnd();
            }

            for (int i = 0; i < FQ.size(); i++) {
                glBegin(GL_LINE_LOOP);
                for (int j = 0; j < 4; j++) {

                    glVertex3f(V[FQ[i].v[j] - 1].x, V[FQ[i].v[j] - 1].y, V[FQ[i].v[j] - 1].z);
                }
                glEnd();
            }

        }
        else {
            for (int i = 0; i < F.size(); i++) {
                glBegin(GL_LINE_LOOP);
                for (int j = 0; j < 3; j++) {
                    glVertex3f(V[F[i].v[j] - 1].x, V[F[i].v[j] - 1].y, V[F[i].v[j] - 1].z);

                }
                glEnd();
            }

            for (int i = 0; i < FQ.size(); i++) {
                glBegin(GL_LINE_LOOP);
                for (int j = 0; j < 4; j++) {
                    glVertex3f(V[FQ[i].v[j] - 1].x, V[FQ[i].v[j] - 1].y, V[FQ[i].v[j] - 1].z);

                }
                glEnd();
            }
        }

    }

    else if (type == 3) {
        glBegin(GL_POINTS);
        for (int i = 0; i < V.size(); i++)
            glVertex3f(V[i].x, V[i].y, V[i].z);
        glEnd();
    }

    else
    {
        if (!VN.empty()) {
            for (int i = 0; i < F.size(); i++) {
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                glBegin(GL_TRIANGLES);

                for (int j = 0; j < 3; j++) {

                    glNormal3f(VN[F[i].vn[j] - 1].x, VN[F[i].vn[j] - 1].y, VN[F[i].vn[j] - 1].z);
                    glVertex3f(V[F[i].v[j] - 1].x, V[F[i].v[j] - 1].y, V[F[i].v[j] - 1].z);

                }
                glEnd();
            }

            for (int i = 0; i < FQ.size(); i++) {
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                glBegin(GL_QUADS);
                for (int j = 0; j < 4; j++) {

                    glNormal3f(VN[FQ[i].vn[j] - 1].x, VN[FQ[i].vn[j] - 1].y, VN[FQ[i].vn[j] - 1].z);
                    glVertex3f(V[FQ[i].v[j] - 1].x, V[FQ[i].v[j] - 1].y, V[FQ[i].v[j] - 1].z);

                }
                glEnd();
            }
        }

        else
        {
            for (int i = 0; i < F.size(); i++) {
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                glBegin(GL_TRIANGLES);

                for (int j = 0; j < 3; j++) {
                    glVertex3f(V[F[i].v[j] - 1].x, V[F[i].v[j] - 1].y, V[F[i].v[j] - 1].z);
                }
                glEnd();

            }

            for (int i = 0; i < FQ.size(); i++) {
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                glBegin(GL_QUADS);

                for (int j = 0; j < 4; j++) {
                    glVertex3f(V[FQ[i].v[j] - 1].x, V[FQ[i].v[j] - 1].y, V[FQ[i].v[j] - 1].z);
                }
                glEnd();

            }
        }

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158

這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述


源碼下載:鏈接:http://pan.baidu.com/s/1slANShZ 密碼:hbwj

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章