opengl对称函数:Win32 OpenGL编程(2) 寻找缺失的OpenGL函数

  、   提要

  以前在 Win32 OpenGL 编程(1)Win32下OpenGL编程必须步骤文中提供了个较为完整Win32下编写OpenGL框架但是仅仅提到是此Win32框架所需要些东西事实上由于MS为了维护自家DirectX所以很早就放弃了对OpenGL支持在Windows XP平台上实现OpenGL还是1.1版(新版Windows看资料好像会好但是事实上OpenGL最新版已经是3.2了)本文讲述就是使用本机显卡支持最新OpenGL所需要东西顺便还讲了下GLUT这样基本形成个较为完整Win32下OpenGL变成学习环境由于工作很简单所以本文会很短最长可能就是提要^^

  另外事实上参考资料4中文章已经较为详细介绍了本文所要讲大部分内容大家可以直接去看写作此文主要还是希望此系列完整在后面文章中已经有个完整可用编程环境顺面补上glew和glut简单安装使用介绍说明

   2、   寻找缺失OpenGL

  OpenGL是由显卡直接支持并实现当时在设计时候考虑到显卡厂商可能会比操作系统/开发软件Software制造商先行步提供新扩展所以有提供获取新扩展思路方法在Windows下就是利用wglGetProcAddress:

PROC wglGetProcAddress(
  LPCSTR  lpszProc   // name of the extension function
);


  此和GetProcAddress功能类似实现也类似无非就是从动态库中通过名字直接获取到地址

FARPROC WINAPI GetProcAddress(
  __in          HMODULE hModule,
  __in          LPCSTR lpProcName
);


  wglGetProcAddress很明确知道该从哪个Dll获取所以省略了第参数模块名仅仅通过名就能获取到对应地址使用思路方法也是类似通过名获取到地址后通过强制转换方式使用此思路方法虽然可行但是用到大量时候比较麻烦事实上此思路方法提供时候是作为种补充手段是在显卡生厂商走在操作系统和开发工具提供商前面时种暂时应付手段但是很不幸Windows是在落后太多了

   3、   Windows下使用OpenGL救星

  MS当年作为OpenGL发起人的仅仅对OpenGL支持到1.1然后就退出了OpenGL组织自己发展DirectX系统去了我们要在Windows上使用OpenGL光靠MS那么OpenGL 1.1后所有都得通过上述手段来完成那简直是让人吐血事情-_-!

  以前似乎有关官方(SGI)OpenGL SDK下载但是现在没有那么好了OpenGL官方网站WebSite已经明确介绍说明不再提供SDK下载叫咱们自己到其他网站WebSite上找去-_-!在OpenGL.org上OpenGL SDK中Library为我们提供了几个开源项目其中有两个就是和扩展有关

  GLee is a free cross-platform extension loading library that takes the burden off your application. GLee makes it easy to check for OpenGL extension and core version availability, automatically ting up the entry pos with no effort _disibledevent=>       (err != GLEW_OK)
       {
              exit(-2);
       }

  这样就能使用缺失就像它们原来就被操作系统支持那样而不用自己通过wglGetProcAddress去获取了说是能像原来就被操作系统支持那样使用其实不完成正确起码在VS中由于都是通过强转后指针使用VS和VA都不能正确解析其原型使得代码编写时智能提醒原型功能不能正常使用这也算是个小瑕疵吧

   4、   为了红宝书OpenGL Programming Guide安装glut

  事实上 Win32 OpenGL 编程(1)Win32下OpenGL编程必须步骤中我就提到了不想使用glut窗口管理系统但是想完整学习OpenGL Programming Guide要完全避过glut好像比较难就算不想使用其窗口管理系统但是些简单易用快捷要是不用那就太麻烦了我想画个立方体都得先去想办法才行-_-!茶壶那就更无语了这里将glut安装使用顺面也讲下吧

  glut在http://www.opengl.org/resources/libraries/glut/ 上有介绍下载页是:http://www.opengl.org/resources/libraries/glut/glut_downloads.php

  也有Windows 2进制版本下载安装思路方法同glew,拷贝头文件动态库到相应位置使用思路方法嘛见红宝书-_-!呵呵其实glut包含了平台相关头文件和gl.h,glu.h所以在使用时候包含glut.h个文件就行了

  包含了glut后基本上Win32下OpenGL工程可以以下面代码开始了:

# <GL/glew.h>
# <GL/wglew.h>
# <GL/glut.h>
 
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" ) 
#pragma comment( lib, "glew32.lib" ) 
#pragma comment( lib, "glew32s.lib" ) 
#pragma comment( lib, "glut32.lib" ) 


  这些都是必须(事实上由于glut32.lib会自动包含OpenGL核心所需包含glut32.lib时可以省略opengl32.lib和glu32.lib)

   5、   完整举例

  这里提供个使用了glewglut完整举例这个举例在上述两个库都安装完成工程配置完成时候才能正确运行也可以用此举例来检验安装完整举例从OpenGL 编程指南(OpenGL Programming Guide)举例代码mv.gif' />.c改过来其中宏GL_VERSION_1_3是由glew定义用来表示支持OpenGL1.3版本其中glMultiDrawElementsEXT就是Windows本身不支持用了glew后才能使用OpenGL扩展接口(首先要显卡支持啊)glut使用那就更不用说了完整举例在博客举例代码2009-9-29\TestOpenGL目录下可以找到下载方式见本文最后介绍说明

/*
 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
 * All Rights Reserved
 *
 * Permission to use, copy, mody, and distribute this software for any
 * purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright
 * notice and this permission notice appear in supporting documentation,
 * and that the name of Silicon Graphics, Inc. not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specic, written prior permission.
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF
 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
 * OR PERFORMANCE OF THIS SOFTWARE.
 *
 * US Government Users Restricted Rights
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions  forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
 * in the FAR or the DOD or NASA FAR Supplement.  Unpublished - rights
 * reserved under the copyright laws of the United States.
 *
 * Contractor/manufacturer is:
 *    Silicon Graphics, Inc.
 *    1500 Crittenden Lane
 *    Mountain View, CA  94043
 *    United State of America
 *
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
 */
 
/*
 *  mv.gif' />.c
 *  This program demonstrates multiple vertex .gif' />s,
 *  specically the OpenGL routine glMultiDrawElements.
 */
# <GL/glew.h>
# <GL/wglew.h>
# <GL/glut.h>
# <stdio.h>
 
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" ) 
#pragma comment( lib, "glew32.lib" ) 
#pragma comment( lib, "glew32s.lib" ) 
#pragma comment( lib, "glut32.lib" ) 
 
 
#def GL_VERSION_1_3
 
void upPoer(void)
{
    GL vertices = {25, 25,
                       75, 75,
                       100, 125,
                       150, 75,
                       200, 175,
                       250, 150,
                       300, 125,
                       100, 200,
                       150, 250,
                       200, 225,
                       250, 300,
                       300, 250};
 
   glEnableClientState (GL_VERTEX_ARRAY);
   glVertexPoer (2, GL_INT, 0, vertices);
}
 
void init(void)
{
       GLenum err = glewInit;
       (err != GLEW_OK)
       {
              exit(-2);
       }
 
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_SMOOTH);
   upPoer ;
}
 
void display(void)
{
    GLu oneIndices = {0, 1, 2, 3, 4, 5, 6};
    const GLu twoIndices = {1, 7, 8, 9, 10, 11};
    GLsizei count = {7, 6};
    const GLvoid * indices[2] = {oneIndices, twoIndices};
  
   glClear (GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);
 
   glMultiDrawElementsEXT (GL_LINE_STRIP, count, GL_UNSIGNED_BYTE,
                           indices, 2);
 
 
   // 上面句相当于下面两句
   //glDrawElements(GL_LINE_STRIP, count[0], GL_UNSIGNED_BYTE, oneIndices);
   //glDrawElements(GL_LINE_STRIP, count[1], GL_UNSIGNED_BYTE, twoIndices);
 
   glFlush ;
}
 
void reshape ( w,  h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ;
   gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
 
void keyboard(unsigned char key,  x,  y)
{
   switch (key) {
       27:
         exit(0);
         ;
   }
}
 
 ( argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (350, 350);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ;
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc (keyboard);
   glutMainLoop;
    0;
}
#
 ( argc, char** argv)
{
    fprf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
    fprf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
    fprf (stderr, "you may be able to mody this program to make it run.\n");
     0;
}
#end




   6、   参考资料

  1.       OpenGL Reference ManualOpenGL参考手册

  2.       OpenGL 编程指南(OpenGL Programming Guide)Dave ShreinerMason WooJackie NeiderTom Davis 著徐波译机械工业出版社

  3.       MSDN事实上我收回MSDN上有完整OpenGL参考应该是说MSDN上有OpenGL1.1前版本较完整资料

  4.       如何使用OpenGL扩展曹添著已经包含了本文大部分内容

   7、   最后介绍说明

  本文中所有代码(如果有话)都能用Mercurial在Google Code中下载

  文章以博文发表日期分目录存放下载地址为:

  http://code.google.com/p/jtianling/source/checkout?repo=blog-sample-code



Tags:  opengl对称函数

延伸阅读

最新评论

发表评论