freeglut的安裝步驟


  下載Freeglut源碼: http://freeglut.sourceforge.net/

  vs2015編譯源碼工程,生成.lib和.dll文件(.h文件就在源碼中)

  32位版本,文件目錄

  1、\include\GL中的所有.h文件(包括freeglut.h,glut.h,freeglut_ext.h,freelut_std.h)拷貝到系統相應目錄下,如:C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\include\GL

  2、\lib\x86中的freeglut.lib拷貝到:C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\lib

  3、\lib\x86中的freeglut.dll拷貝到:C:\Windows\SysWOW64\(win7無SysWOW64目錄,C:\Windows\System32\)目錄下 

  64位版本,文件目錄

  1、.h文件同32版本目錄

  2、.lib文件:C:\Program Files(x86)\Microsoft Visual Studio 14.0\VC\lib\amd64

  3、.dll文件:C:\Windows\System32\

  

  VS2015,測試代碼

// OpenGLStarter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

//需要包含的頭文件
#include <windows.h>

//#include <GL/gl.h>
//#include <GL/glu.h>
#include <GL/freeglut.h>

//定義輸出窗口的大小
#define WINDOW_HEIGHT 300
#define WINDOW_WIDTH 500

//攝像機離物體的距離
GLfloat G_fDistance = 3.6f;
//物體的旋轉角度 
GLfloat G_fAngle_horizon = 0.0f;
GLfloat G_fAngle_vertical = 0.0f;

////////////////////////////////////////////////
void myinit(void);
void myReshape(GLsizei w, GLsizei h);
void display(void);

//響應鍵盤輸入, 從而設定物體移近移遠以及旋轉的回調函數
void processSpecialKeys(int key, int x, int y);
void processNormalKeys(unsigned char key,int x,int y);


////////////////////////////////////////////////
//主函數
int main(int argc, char* argv[])
{
    glutInit(&argc, argv);

    //初始化OPENGL顯示方式
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
//    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);

    //設定OPENGL窗口位置和大小
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100);
        
    //打開窗口
    glutCreateWindow ("OpenGL");

    //調用初始化函數
    myinit();

    //設定窗口大小變化的回調函數
    glutReshapeFunc(myReshape);

    //設定鍵盤控制的回調函數
    glutSpecialFunc(processSpecialKeys);
    glutKeyboardFunc(processNormalKeys);
    
    //開始OPENGL的循環
    glutDisplayFunc(display); 

    glutIdleFunc(display);

    glutMainLoop();

    return 0;
}

////////////////////////////////////////////////
//用戶初始化函數
void myinit(void)
{
    //your initialization code
    glEnable(GL_DEPTH_TEST);

//    glEnable(GL_ALPHA_TEST);
//    glAlphaFunc( GL_GREATER, 0.5f );
//    GLfloat a = 0.0f;
}

//窗口大小變化時的回調函數
void myReshape(GLsizei w, GLsizei h)
{
    //設定視區
    glViewport(0, 0, w, h);
    
    //設定透視方式
    glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
    gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
//    gluPerspective(60.0, 1.0, 1.0, 30.0);    //調整窗口比例時物體會變形
//  glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 30.0);
}

//每楨OpenGL都會調用這個函數,用戶應該把顯示代碼放在這個函數中
void display(void)
{
    //設置清除屏幕的顏色,並清除屏幕和深度緩沖
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//    glEnable(GL_ALPHA_TEST);
//    glAlphaFunc(GL_LESS, 0.5f);

    //設置成模型矩陣模式
    glMatrixMode(GL_MODELVIEW);

    //載入單位化矩陣
    glLoadIdentity();

    //坐標中心向Z軸平移-G_fDistance (使坐標中心位於攝像機前方)
    glTranslatef(0.0, 0.0, -G_fDistance);
    glRotatef(G_fAngle_horizon, 0.0f, 1.0f, 0.0f);
    glRotatef(G_fAngle_vertical, 1.0f, 0.0f, 0.0f);

    ////////////////////////////////////////////////
    //繪制物體

    //畫一個正方形面
    glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
//    glColor3ub(255, 0, 255);
    glBegin(GL_QUADS);
        glVertex3f (-1.0, -1.0f, 0.0f);
        glVertex3f (1.0, -1.0f, 0.0f);
        glVertex3f (1.0, 1.0f, 0.0f);
        glVertex3f (-1.0, 1.0f, 0.0f);
    glEnd();

    //畫一個茶壺
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    glutWireTeapot(1.0);
//    glutSolidTeapot(1.0);


    //交換前后緩沖區
    glutSwapBuffers();
//    glFlush();
}


void processSpecialKeys(int key, int x, int y)
{
    switch(key) {
        case GLUT_KEY_LEFT:
            G_fAngle_horizon -= 5.0f;
            break;
        case GLUT_KEY_RIGHT:
            G_fAngle_horizon += 5.0f;
            break;
        case GLUT_KEY_UP:
            G_fAngle_vertical -= 5.0f;
            break;
        case GLUT_KEY_DOWN:
            G_fAngle_vertical += 5.0f;
            break;
    }
//    glutPostRedisplay();
}

void processNormalKeys(unsigned char key,int x,int y)
{
    switch(key) {
        case 97:    //"a"
            G_fDistance -= 0.3f;
            break;
        case 65:        //"A"
            G_fDistance += 0.3f;
            break;
        case 27:    //"esc"
            exit(0);
    }
//    glutPostRedisplay();
}
OpenGLStarter.cpp
// stdafx.cpp : source file that includes just the standard includes
//    OpenGLStarter.pch will be the pre-compiled header
//    stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
stdafx.cpp

 

  VS2013編譯出現錯誤

  錯誤 1 error MSB8020: The build tools for v140 (Platform Toolset = 'v140') cannot be found. To build using the v140 build tools, please install v140 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Upgrade Solution...". C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets 64 5 OpenGLStarter

  解決辦法

  將 "項目——屬性——配置屬性——常規——平台工具集” 選項更改為你所用的VS版本平台集的選項即可,我這里用的是VS2013,便將其更改為v120的,v140應為VS2015/2017版本的平台工具集;

  下載鏈接:

  http://pan.baidu.com/s/1X_3MJUiLHNtsfEab_GMomg  提取碼:k976


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM