关于OpenGl编程指南 第6版 第142页 示例程序6-1 的运行问题

        这个程序就编译问题就花了我一个晚上的时间,很是郁闷,由于用的是 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.01.00.00.0);

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


void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);

   glColor3f(
0.00.01.0);
   glRectf(
-0.5,-0.5,0.5,0.5);

   glFlush();
}


void reshape(int w, int h)
{
   GLdouble aspect 
= (GLdouble) w / h;

   glViewport(
00, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   
if (aspect < 1.0{
     aspect 
= 1.0 / aspect;
     glOrtho(
-aspect, aspect, -1.01.0-1.01.0);
   }
 else
     glOrtho(
-1.01.0-aspect, aspect, -1.01.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(
100100);
   glutCreateWindow(argv[
0]);

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



 

posted on 2010-02-14 21:37 蜗牛也Coding 阅读(1099) 评论(3)  编辑 收藏 引用

评论

# re: 关于OpenGl编程指南 第6版 第142页 示例程序6-1 的运行问题 2010-02-27 15:53 philip

太好了
正为这个问题奇怪呢
感谢感谢  回复  更多评论   

# re: 关于OpenGl编程指南 第6版 第142页 示例程序6-1 的运行问题 2011-04-28 10:18 susan

Linking...
blendeqn.obj : error LNK2001: unresolved external symbol __imp__glewInit
blendeqn.obj : error LNK2001: unresolved external symbol __imp____glewBlendEquation
Debug/blendeqn.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
Creating browse info file...

我按你说的改了,还是不行啊,为什么?  回复  更多评论   

# re: 关于OpenGl编程指南 第6版 第142页 示例程序6-1 的运行问题 2011-04-28 10:38 susan

噢,想起来了,glew32.lib 在project setting 的links模块下没加上。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年2月>
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

导航

统计

常用链接

留言簿(8)

随笔档案(78)

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜