這個程序就編譯問題就花了我一個晚上的時間,很是郁悶,由于用的是 Visual Studio 2008,并且因為微軟對OpenGL的支持只到1.1,1.1以后微軟就不再支持了,為什么,因為微軟更想發展自家的DirectX。所以如果想使用OpenGL1.1以上的功能或者函數,只能使用OpenGL擴展,這些擴展是一些OpenGL團體或個人開發出來的能Windows上使用的OpenGL1.1以后的一些功能及函數。所以,在Windows上根本就沒有什么OpenGL2.0的頭文件或庫文件了,OpenGL1.1以后的東西都已經以擴展的形式存在了,而且,并沒有一個統一的標準。
網上關于這個解決的方法已經有很多,其中一個是用glew庫,glew也是一個擴展庫,包含了OpenGL中許多核心及擴展函數,可以到這里下載:
http://glew.sourceforge.net/ 先安裝,再按下面順序包含頭文件(glew.h在glut.h前)
#include <GL/glew.h>
#include <GL/glut.h>
其中安裝了glew庫方法如下:
bin/glew32.dll |
to |
%SystemRoot%/system32 |
lib/glew32.lib |
to |
{VC Root}/Lib |
include/GL/glew.h |
to |
{VC Root}/Include/GL |
include/GL/wglew.h |
to |
{VC Root}/Include/GL |
GLenum err = glewInit() ;
if (GLEW_OK != err)

{
exit(-2);
}
上述都是簡單的問題,一直執行到這里的時候程序編譯是通過了,但是發現出現另外一個問題,那就是,當按下,a,s,r,m,x等鍵的時候完全沒有反應,于是我有查了一下,最后發現是由于沒有初始化glew的緣故,網上提供的通用方法是在init()函數調用之前先寫上如上幾行代碼:
然后偶很郁悶的發現,編譯出現錯誤,這個地方也就是最神奇的地方,我試了很多次,都是提示錯誤,提示GLenum 標示符聲明不合法什么的,嘗試了一個晚上后終于放棄,過了幾天,重新回來看的時候,又試了幾次,做的都是換湯不換藥的事情,并將上述代碼的 exit(-2) 換成 return 0 ,還有就是把初始化的代碼換了個位置,放到init()函數的里面的開始部分,運行就OK了,不知道為什么出現這個問題,又換到以前的位置或是將 return 0 重新換回 exit(-2) 運行又是錯誤,鑒于這個問題的詭異性,相信大家或許沒遇到過,特此記錄下來,以供對后來可能會遇到的朋友們一個提示。也期待有哪位高人指點一下緣故?
以下為最終運行成功的代碼:



/**//**//**//*
此程序有時成功有時失敗,只發現原因在于初始化glew的那幾句代碼的具體位置,具體緣由有待解決
*/


/**//**//**//*
* blendeqn.c
* Demonstrate the different blending functions available with the
* OpenGL imaging subset. This program demonstrates use of the
* glBlendEquation() call.
*
* The following keys change the selected blend equation function:
*
* 'a' -> GL_FUNC_ADD
* 's' -> GL_FUNC_SUBTRACT
* 'r' -> GL_FUNC_REVERSE_SUBTRACT
* 'm' -> GL_MIN
* 'x' -> GL_MAX
*/


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

void init(void)


{

GLenum err = glewInit() ;
if (GLEW_OK != err)return 0;

/**//**//**//*上面兩行代碼很奇怪,加到主函數的init()函數前就不行,有時候又可以,此問題留待解決*/

glClearColor(1.0, 1.0, 0.0, 0.0);

glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
}

void display(void)


{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0, 0.0, 1.0);
glRectf(-0.5,-0.5,0.5,0.5);

glFlush();
}

void reshape(int w, int h)


{
GLdouble aspect = (GLdouble) w / h;

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

if (aspect < 1.0)
{
aspect = 1.0 / aspect;
glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
} else
glOrtho(-1.0, 1.0, -aspect, aspect, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)


{

switch (key)
{
case 'a': case 'A':

/**//**//**//* Colors are added as: (1, 1, 0) + (0, 0, 1) = (1, 1, 1)
* which will produce a white square on a yellow background.
*/
glBlendEquation(GL_FUNC_ADD);
break;

case 's': case 'S':

/**//**//**//* Colors are subtracted as: (0, 0, 1) - (1, 1, 0) = (-1, -1, 1)
* which is clamped to (0, 0, 1), producing a blue square on a
* yellow background
*/ glBlendEquation(GL_FUNC_SUBTRACT);
break;

case 'r': case 'R':

/**//**//**//* Colors are subtracted as: (1, 1, 0) - (0, 0, 1) = (1, 1, -1)
* which is clamed to (1, 1, 0). This produces yellow for both
* the square and the background.
*/
glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
break;

case 'm': case 'M':

/**//**//**//* The minimum of each component is computed, as
* [min(1, 0), min(1, 0), min(0, 1)] which equates to (0, 0, 0).
* This will produce a black square on the yellow background.
*/
glBlendEquation(GL_MIN);
break;

case 'x': case 'X':

/**//**//**//* The minimum of each component is computed, as
* [max(1, 0), max(1, 0), max(0, 1)] which equates to (1, 1, 1)
* This will produce a white square on the yellow background.
*/
glBlendEquation(GL_MAX);
break;

case 27:
exit(0);
}
glutPostRedisplay();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512,512);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);

init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
