之前用VC6.0 開發opengl程序,閑着沒事,就想試試visual studio 2010,因為VS2010編寫程序有比較好的“糾錯”功能,所以就像用vs2010試試。具體步驟如下
一. 首先你要有一個glut包,(如果沒有請到網上下載一個),里面會有這么一些文件:(如圖)
二. 接下來的事情就是你要把對應的文件放到對應的位置:
(1) glut.h
這個文件放目目錄C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\gl
這個目錄不用你考慮你的軟件安裝在哪個文件夾
(2) glut.dll,glut32.dll
這兩個dll放在目錄C:\Windows\SysWOW64(64位)
C:\Windows\System32(32位)
這個目錄也不用你考慮你的軟件安裝在哪個文件夾
(3) glut.lib,glut32.lib
這兩個文件放在目錄 (XXXXXXXXXXX)\VC\lib
請注意,這兩個文件放的位置跟你vs2010安裝在哪里有關,比如我的安裝在G:\編程工具\VS 2010 旗艦版\vs2010安裝
這個目錄下那么就放在目錄 G:\編程工具\VS 2010 旗艦版\vs2010安裝\VC\lib目錄下面
三.在vs2010中的設置:
- 打開VS2010 ,按照下面的步驟新建一個win32控制台程序我們來測試一下我們剛剛配置的環境
新建項目àwin32控制台應用程序-->輸入名稱為:openGL(如圖)
點擊“確定”--->“下一步”--->“空項目”--->“完成”
這樣一個空的控制台項目就建好了
2.按ctrl+Shift+A,添加一個cpp文件到該解決方案,名稱為openGL(如下圖)
3.設置目錄
①包含目錄:
點項目—>openGL屬性(根據工程名不同)--->配置屬性(點開)--->VC++目錄-->包含目錄(用鼠標選中它,然后最右邊會有一個向下的三角形,點它)--->編輯--->點
圖標,輸入 你剛剛放入 glut.h文件的目錄 即C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\gl(如下圖)
②庫目錄:
就在包含目錄下面,點開“庫目錄”,輸入你剛剛放入glut.lib,glut32.lib的目錄即(XXXXXXXXXXX)\VC\lib
向我的就是G:\編程工具\VS 2010 旗艦版\vs2010安裝\VC\lib
四.運行測試程序:
把下面這個簡單的程序輸入到剛剛新建的openGL.cpp中,然后運行,成功的話就會一個正方形移動的圖:
// Bounce.cpp
#include<windows.h>
#include<glut.h>
#include<GLU.h>
#include<GL.h>
// Initial square position and size
GLfloat x = 0.0f;
GLfloat y = 0.0f;
GLfloat rsize = 25;
// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
// Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f);
// Draw a filled rectangle with current color
glRectf(x, y, x + rsize, y - rsize);
// Flush drawing commands and swap
glutSwapBuffers();
}
///////////////////////////////////////////////////////////
// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{
// Reverse direction when you reach left or right edge
if(x > windowWidth-rsize || x < -windowWidth)
xstep = -xstep;
// Reverse direction when you reach top or bottom edge
if(y > windowHeight || y < -windowHeight + rsize)
ystep = -ystep;
// Actually move the square
x += xstep;
y += ystep;
// Check bounds. This is in case the window is made
// smaller while the rectangle is bouncing and the
// rectangle suddenly finds itself outside the new
// clipping volume
if(x > (windowWidth-rsize + xstep))
x = windowWidth-rsize-1;
else if(x < -(windowWidth + xstep))
x = -windowWidth -1;
if(y > (windowHeight + ystep))
y = windowHeight-1;
else if(y < -(windowHeight - rsize + ystep))
y = -windowHeight + rsize - 1;
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
///////////////////////////////////////////////////////////
// Setup the rendering state
void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w, int h)
{
GLfloat aspectRatio;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
{
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
}
else
{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800,600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);
SetupRC();
glutMainLoop();
return 0;
}