天天看點

vs2013環境下搭建openGL開發環境

環境搭建 參考:  http://jingyan.baidu.com/article/d5c4b52bca5005da560dc5d6.html    

程式設計方法,對opengl的了解,參考 :     http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html

首先要下載下傳glut庫,可從OpenGL的官網上下:

http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

下載下傳後解開壓縮包,裡面有五個檔案:glut.h,glut.lib,glut32.lib,glut.dll,glut32.dll。

vs2013環境下搭建openGL開發環境

前提是你安裝好vs2013 ,并且你記得安裝目錄

1.解壓後将得到的glut.lib和glut32.lib這兩個靜态函數庫複制到檔案目錄的lib檔案夾下

X:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib

2.将glut.dll,glut32.dll這兩個動态庫檔案放到作業系統目錄下面的C:\Windows\system32檔案夾内(32位系統)或‪C:\Windows\SysWOW64(64位系統)。

為了相容性考慮,最好在這兩個目錄下都複制相應的檔案。

3.将解壓得到的頭檔案glut.h複制到目錄如下目錄下:

X:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL

提示:如果在incluce目錄下沒有GL檔案夾,則需要手動建立

建立一個空白的Win32控制台應用程式

在代碼最前面添加包含目錄

#include <GL/glut.h>

然後就可以編輯自己的OpenGL程式了

示例代碼:

// ConsoleApplication1.cpp : 定義控制台應用程式的入口點。
//

#include "stdafx.h"
#include <GL/glut.h>

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
static int year = 0, spin = 0, day = 0;
static GLint fogMode;
const int n = 100;
const GLfloat R = 1.0f;
const GLfloat Pi = 3.1415926536f;
void DrawCircle()
{
	int  i;
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_LINE_LOOP);
	for (i = 0; i < n; ++i)
	{
		glColor3f(1.0, 0.0, 0.0);
		glVertex2f(R*cos(2 * Pi / n*i), R*sin(2 * Pi / n*i));
	}
	glEnd();
	glFlush();
}
void init(void)
{
	GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 };
	glEnable(GL_DEPTH_TEST);                          //防止遮擋
	glLightfv(GL_LIGHT0, GL_POSITION, position);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	{
		GLfloat mat[3] = { 0.1745, 0.01175, 0.01175 };
		glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
		mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
		glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
		mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
		glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
		glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
	}
	glEnable(GL_FOG);
	{
		GLfloat fogColor[4] = { 0.5, 0.5, 0.5, 1.0 };
		fogMode = GL_EXP;
		glFogi(GL_FOG_MODE, fogMode);
		glFogfv(GL_FOG_COLOR, fogColor);
		glFogf(GL_FOG_DENSITY, 0.35);
		glHint(GL_FOG_HINT, GL_DONT_CARE);
		glFogf(GL_FOG_START, 1.0);
		glFogf(GL_FOG_END, 5.0);
	}
	glClearColor(0.5, 0.9, 0.9, 1.0);  /* fog color */
}
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(0.0, 1.0, 1.0);
	glPushMatrix(); //記住自己的位置
	glutSolidSphere(1.0, 20, 16);   /* 畫太陽半徑、 20經度、16緯度*/
	glRotatef(spin, 0.0, 1.0, 0.0);  //自轉,繞着一個向量以給定角度旋轉(正的為逆時針)
	glTranslatef(2.0, 1.0, 0.0);
	glRotatef(spin, 1.0, 0.0, 0.0); //公轉
	glRectf(0.1, 0.1, 0.5, 0.5);
	glColor3f(0.0, 0.0, 1.0);
	glutWireSphere(0.2, 8, 8);    /* 畫第一顆小行星 */
	glColor3f(1.0, 0.0, 0.0);
	glTranslatef(2.0, 1.0, 0.0);
	glRotatef(2 * spin, 0.0, 1.0, 0.0);
	glutSolidSphere(0.5, 16, 8);
	glPopMatrix();//回到原來的位置
	glutSwapBuffers();
}
void spinDisplay(void)
{
	spin = spin + 2;
	if (spin > 360)
		spin = spin - 360;
	glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
	switch (button)
	{
	case GLUT_LEFT_BUTTON:
		if (state == GLUT_DOWN)
			glutIdleFunc(spinDisplay);
		break;
	case GLUT_MIDDLE_BUTTON:
		if (state == GLUT_DOWN)
			glutIdleFunc(NULL);
		break;
	default:
		break;
	}
}
void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.5, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
	case 'd':
		day = (day + 10) % 360;
		glutPostRedisplay();
		break;
	case 'D':
		day = (day - 10) % 360;
		glutPostRedisplay();
		break;
	case 'y':
		year = (year + 5) % 360;
		glutPostRedisplay();
		break;
	case 'Y':
		year = (year - 5) % 360;
		glutPostRedisplay();
		break;
	case 27:
		exit(0);
		break;
	default:
		break;
	}
}
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(400, 400);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("OpengGL");
	init();
	//glutDisplayFunc(DrawCircle);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	//glutKeyboardFunc(keyboard);
	glutMouseFunc(mouse);
	glutMainLoop();
	return 0;
}
           

一個簡單的OpenGL程式如下:(注意,如果需要編譯并運作,需要正确安裝GLUT,安裝方法如上所述)

#include <GL/glut.h>
void myDisplay(void)
{
     glClear(GL_COLOR_BUFFER_BIT);
     glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
     glFlush();
}
int main(int argc, char *argv[])
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
     glutInitWindowPosition(100, 100);
     glutInitWindowSize(400, 400);
     glutCreateWindow("第一個OpenGL程式");
     glutDisplayFunc(&myDisplay);
     glutMainLoop();
     return 0;
}
           

首先,需要包含頭檔案#include <GL/glut.h>,這是GLUT的頭檔案。

本來OpenGL程式一般還要包含<GL/gl.h>和<GL/glu.h>,但GLUT的頭檔案中已經自動将這兩個檔案包含了,不必再次包含。

然後看main函數。

int main(int argc, char *argv[]),這個是帶指令行參數的main函數,各位應該見過吧?沒見過的同志們請多翻翻書,等弄明白了再往下看。

注意main函數中的各語句,除了最後的return之外,其餘全部以glut開頭。這種以glut開頭的函數都是GLUT工具包所提供的函數,下面對用到的幾個函數進行介紹。

1、glutInit,對GLUT進行初始化,這個函數必須在其它的GLUT使用之前調用一次。其格式比較死闆,一般照抄這句glutInit(&argc, argv)就可以了。

2、 glutInitDisplayMode,設定顯示方式,其中GLUT_RGB表示使用RGB顔色,與之對應的還有GLUT_INDEX(表示使用索引顔色)。GLUT_SINGLE表示使用單緩沖,與之對應的還有GLUT_DOUBLE(使用雙緩沖)。更多資訊,請自己Google。當然以後的教程也會有一些講解。

3、glutInitWindowPosition,這個簡單,設定視窗在螢幕中的位置。

4、glutInitWindowSize,這個也簡單,設定視窗的大小。

5、glutCreateWindow,根據前面設定的資訊建立視窗。參數将被作為視窗的标題。注意:視窗被建立後,并不立即顯示到螢幕上。需要調用glutMainLoop才能看到視窗。

6、glutDisplayFunc,設定一個函數,當需要進行畫圖時,這個函數就會被調用。(這個說法不夠準确,但準确的說法可能初學者不太好了解,暫時這樣說吧)。

7、glutMainLoop,進行一個消息循環。(這個可能初學者也不太明白,現在隻需要知道這個函數可以顯示視窗,并且等待視窗關閉後才會傳回,這就足夠了。)

在glutDisplayFunc函數中,我們設定了“當需要畫圖時,請調用myDisplay函數”。于是myDisplay函數就用來畫圖。觀察myDisplay中的三個函數調用,發現它們都以gl開頭。這種以gl開頭的函數都是OpenGL的标準函數,下面對用到的函數進行介紹。

1、glClear,清除。GL_COLOR_BUFFER_BIT表示清除顔色,glClear函數還可以清除其它的東西,但這裡不作介紹。

2、glRectf,畫一個矩形。四個參數分别表示了位于對角線上的兩個點的橫、縱坐标。

3、glFlush,保證前面的OpenGL指令立即執行(而不是讓它們在緩沖區中等待)。其作用跟fflush(stdout)類似。