C#中使用OpenGL:(五)1.1版本的OpenGL函数

上一篇文章介绍了如何在C#中表示OpenGL常量,这篇文章将介绍如何在C#中调用1.1版本OpenGL函数。

关于OpenGL版本的问题

OpenGL自从1992年7月份发布1.0版本以来,到2017年9月已经发展到了4.6版本。在OpenGL发展的历程中有两个版本需要特别注意,第一个是1.1版本,第二个是2.1版本。1.1版本的OpenGL是微软Windows系统所支持的最高版本的了,而从2.1版本开始,OpenGL摒弃了早期版本的固定管线,升级到现代OpenGL。
这里着重说一下1.1版本的OpenGL。Windows系统对OpenGL的支持只局限于1.1版本以下,在系统盘里,可以找到opengl32.dll这个文件,里边的函数都是OpenGL1.1版本的函数。根据Windows系统的支持程度,可以把OpenGL分为两个部分,一是1.1版本的早期函数,姑且称之为“基本函数”,二是较高版本的函数,姑且称之为“扩展函数”。从程序员的角度来讲,调用OpenGL基本函数是比较方便的,而调用OpenGL扩展函数就略显麻烦,通常要先查询电脑是否支持该扩展函数,然后要获取该函数的函数指针,通过函数指针来调用扩展函数。现在一些第三方机构,把OpenGL基本函数和扩展函数整合到一块,使得扩展函数调用起来也和早期函数一样方便,比如glew这个库。虽说glew库已经将早期函数和扩展函数整到一起,但这两种函数是有区别的,在C#中调用它们,处理方式也略微不同,因此C#调用OpenGL可分为两部分来讲:C#调用OpenGL基本函数和C#调用OpenGL扩展函数。本篇文章将介绍如何在C#中调用1.1版本的OpenGL函数。

C#中声明OpenGL函数

OpenGL1.1版本的函数大概用300多个,我们可以在glew.h这个头文件里看到这些函数C语言版本的声明。如下图:

这里写图片描述

在C#中,需要将OpenGL函数以下面的形式声明:

[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glAccum",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glAccum (uint op, float value);

在C#中新建一个类,命名为GL,用来装OpenGL函数。 需要注意的是:“opengl32.dll”这是必须要有的,这表明函数来自opengl32.dll这个动态链接库,EntryPoint必须是函数入口名称,调用约定CallingConvention = CallingConvention.StdCall,必须是StdCall,不能是其他,函数修饰必须是pubic static。
由于函数较多,手动输入既耗时有易出错,因此我使用代码自动生成。下面是一段C代码,用来生成C#的函数声明。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
    FILE *fpi, *fpo;
    fpi = fopen("glew.h", "rt");
    fpo = fopen("gl.txt", "wt");

    while (!feof(fpi))
    {
        char str1[200] = "";
        char str2[200] = "";
        char str3[200] = "";
        char str4[200] = "";
        char str5[200] = "";
        char str6[600] = "";


        fscanf(fpi, "%s", str1);

        if (strcmp(str1, "GLAPI") == 0)
        {
            fscanf(fpi, "%s%s%s", str2,str3,str4);
            fgets(str5,200 ,fpi);

            char s[400]="[DllImport(\"opengl32.dll\",ExactSpelling=false,EntryPoint=\"";
            strcat(s, str4);
            strcat(s, "\",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]\n");
            char s1[400] = "public static extern ";
            strcat(s1, str2);
            strcat(s1, " ");
            strcat(s1, str4);
            strcat(s1, str5);
            strcat(s1, "\n");
            //sscanf(str6, "%s%s\n", s,s1);
            //puts(str6);
            fputs(s, fpo);
            fputs(s1, fpo);
        }
    }
    fclose(fpi);
    fclose(fpo);
}

代码自动处理的效果如下:
这里写图片描述

C#调用C函数时的参数传递的问题

OpenGL函数的参数,全部是以GL开头的自定义类型,如GLfloat、GLint。在C#中,没有C语言那种自定义类型,如果要实现如C语言自定义类型的效果,则要通过类来实现。如果为每一种自定义数据类型都写一个类的话,我担心会有一些效率上的损害,毕竟C#的效率已经是比C语言低了,我不想更低,因此我用C#基本的数据类型来替代OpenGL的自定义类型。下表是C#基本类型与OpenGL自定义类型的转换关系。

OpenGL自定义类型 C#基本数据类型
GLint/GLint* int/int []
GLuint/GLuint* uint/uint []
GLbyte/GLbyte* sbyte/sbyte []
GLubyte/GLubyte* byte/byte []
GLshort/GLshort* short/short []
GLushort/GLushort* ushort/ushort []
GLfloat/GLfloat* float/float []
GLdouble/Gldouble* double/double []
Glboolean/GLboolean* bool/bool []
GLsizei/GLsizei* int/int []
GLbitfield uint
GLclamf float
GLclamd double

除了以上表格列出的数据类型外,如果遇到二重指针,则在C#中用 ref IntPtr 来替代。如在C语言中函数声明void func(int **p),在C#中声明则是 void func( ref IntPtr p)。对于有GLvoid 类型指针的函数,可以用Intptr类型替代,也可以将函数重载,然后用C#的基本类型来替代GLvoid。
例子:
(1)参数是普通变量

C函数声明
void glColor3f(GLfloat r,GLfloat g,GLfloat b);

C#方法声明
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glColor3f",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glColor3f (float r,float g,float b);

(2)参数是普通指针

C函数声明
void glColor3fv(GLfloat* v);

C#方法声明
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glColor3fv",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glColor3fv (float[] v);
或
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glColor3fv",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glColor3fv (IntPtr v);

(3)参数是二重指针

C函数声明
void glShaderSource( GLuint shader,  GLsizei count,  const GLchar **string, const GLint *length); 

c#方法声明
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glShaderSource",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glShaderSource (uint shader,int count,IntPtr[] string,int[] length);

(4)参数是空类型指针(GLvoid)

C函数声明
void glCallLists (GLint n, GLenum type, GLvoid* lists);

C#方法声明
//用Intptr类型
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glCallLists",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glCallLists (int n, uint type, Intptr lists);
//或者用函数重载
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glCallLists",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glCallLists (int n, uint type, int [] lists);
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glCallLists",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glCallLists (int n, uint type, uint [] lists);
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glCallLists",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glCallLists (int n, uint type, short [] lists);
[DllImport("opengl32.dll",ExactSpelling=false,EntryPoint="glCallLists",CharSet=CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern void glCallLists (int n, uint type, ushort [] lists);

结语

这是第五篇关于C#调用OpenGL的文章,到目前为止,已经解决了两大问题,分别是OpenGL常量在C#中表示(文章四中介绍)和1.1版本OpenGL函数在C#中的声明。下一篇文章将深入地探讨C#中调用OpenGL函数时的参数传递问题。

上一篇:C#中使用OpenGL:(四)C#版的OpenGL常量
下一篇:C#中使用OpenGL:(六)C#中调用C函数时的参数传递问题

猜你喜欢

转载自blog.csdn.net/qq_28249373/article/details/78076406
今日推荐