欢迎光临
我们一直在努力

用C#开发opengl-.NET教程,C#语言

建站超值云服务器,限时71元/月

微软提供较少的标准支持,对于和他们竞争的东西—比如corba(com的竞争对手)和opengl(directx的竞争对手)。

不过在c#中实现opengl也并非没有可能,有很多很好的第3方库可以使用,这里列举2个。

(1)csgl

http://csgl.sourceforge.net/index.html

名气较大的opengl库,有稳定版本。sf上的项目地址http://sourceforge.net/project/showfiles.php?group_id=33241。

目前有多个3d引擎项目使用的这个库。

(2)csopengl

http://www.ia.hiof.no/gb/ptools/csharp/p-csharp.html

有较详细的文档和例子。sf上的项目地址http://sourceforge.net/project/showfiles.php?group_id=54966&package_id=49782。

csopengl的一个例子(我在vs2005上编译)

-------form1.cs-------

#region using directives

using system;

using system.collections.generic;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

using csopengl;

#endregion

namespace opentest

{

partial class form1 : system.windows.forms.form

{

//private system.componentmodel.container components = null;

private glview view;

public form1()

{

initializecomponent();

view = new glview();

view.parent = this;

view.createcontrol();

view.dock = dockstyle.fill;

view._onpaint = new painter();

}

/*

protected override void dispose(bool disposing)

{

if (disposing)

{

if (components != null)

{

components.dispose();

}

view.dispose();

}

base.dispose(disposing);

}

*/

private void form1_load(object sender, eventargs e)

{

}

}

public class painter : glviewpainter

{

public painter() { }

public override void paint(glview view)

{

glviewport(0, 0, view.width, view.height);

glmatrixmode(gl_projection);

glloadidentity();

gluperspective(45.0f, 1.0f, 0.1f, 100.0f);

glshademodel(gl_smooth);

glclearcolor(0.0f, 0.0f, 0.0f, 0.0f);

glcleardepth(1.0f);

glenable(gl_depth_test);

gldepthfunc(gl_lequal);

glhint(gl_perspective_correction_hint, gl_nicest);

glclear(gl_color_buffer_bit | gl_depth_buffer_bit);

glmatrixmode(gl_modelview);

//drawpyramid();

//drawsphere();

drawcube();

glflush();

}

private void drawpyramid()

{

glloadidentity();

gltranslatef(-0.2f, 0.3f, -4.0f);

glrotatef(45.0f, 1.0f, 1.0f, 1.0f);

glbegin(gl_triangles);

// front face triangle:

glcolor3f(1.0f, 0.0f, 0.0f);

glvertex3f(0.0f, 1.0f, 0.0f);

glcolor3f(0.0f, 1.0f, 0.0f);

glvertex3f(-1.0f, -1.0f, 1.0f);

glcolor3f(0.0f, 0.0f, 1.0f);

glvertex3f(1.0f, -1.0f, 1.0f);

// right face triangle:

glcolor3f(1.0f, 0.0f, 0.0f);

glvertex3f(0.0f, 1.0f, 0.0f);

glcolor3f(0.0f, 0.0f, 1.0f);

glvertex3f(1.0f, -1.0f, 1.0f);

glcolor3f(0.0f, 1.0f, 0.0f);

glvertex3f(1.0f, -1.0f, -1.0f);

//back face triangle:

glcolor3f(1.0f, 0.0f, 0.0f);

glvertex3f(0.0f, 1.0f, 0.0f);

glcolor3f(0.0f, 1.0f, 0.0f);

glvertex3f(1.0f, -1.0f, -1.0f);

glcolor3f(0.0f, 0.0f, 1.0f);

glvertex3f(-1.0f, -1.0f, -1.0f);

// left face triangle:

glcolor3f(1.0f, 0.0f, 0.0f);

glvertex3f(0.0f, 1.0f, 0.0f);

glcolor3f(0.0f, 0.0f, 1.0f);

glvertex3f(-1.0f, -1.0f, -1.0f);

glcolor3f(0.0f, 1.0f, 0.0f);

glvertex3f(-1.0f, -1.0f, 1.0f);

glend();

}

private void drawcube()

{

// enable color blending:

glenable(gl_blend);

glblendfunc(gl_src_alpha, gl_one_minus_src_alpha);

glloadidentity();

gltranslatef(0.0f, 0.2f, -6.0f);

glrotatef(45.0f, 1.0f, 1.0f, 1.0f);

glbegin(gl_quads);

// tegner først de sidene som ikke skal være gjennomsiktige:

//the back:

glcolor4f(1.0f, 1.0f, 0.0f, 1.0f); // yellow

glvertex3f(1.0f, -1.0f, -1.0f);

glvertex3f(-1.0f, -1.0f, -1.0f);

glvertex3f(-1.0f, 1.0f, -1.0f);

glvertex3f(1.0f, 1.0f, -1.0f);

//the bottom:

glcolor4f(1.0f, 0.5f, 0.0f, 1.0f); // orange

glvertex3f(1.0f, -1.0f, 1.0f);

glvertex3f(-1.0f, -1.0f, 1.0f);

glvertex3f(-1.0f, -1.0f, -1.0f);

glvertex3f(1.0f, -1.0f, -1.0f);

//the right face:

glcolor4f(1.0f, 0.0f, 1.0f, 0.75f); // cyan

glvertex3f(1.0f, 1.0f, -1.0f);

glvertex3f(1.0f, 1.0f, 1.0f);

glvertex3f(1.0f, -1.0f, 1.0f);

glvertex3f(1.0f, -1.0f, -1.0f);

// de sidene som skal være gjennomsiktige:

// setter depthbuffer read-only:

gldepthmask(0);

//the front:

glcolor4f(1.0f, 0.0f, 0.0f, 0.75f); // red

glvertex3f(1.0f, 1.0f, 1.0f);

glvertex3f(-1.0f, 1.0f, 1.0f);

glvertex3f(-1.0f, -1.0f, 1.0f);

glvertex3f(1.0f, -1.0f, 1.0f);

//the top:

glcolor4f(0.0f, 1.0f, 0.0f, 0.75f); // green

glvertex3f(1.0f, 1.0f, -1.0f);

glvertex3f(-1.0f, 1.0f, -1.0f);

glvertex3f(-1.0f, 1.0f, 1.0f);

glvertex3f(1.0f, 1.0f, 1.0f);

//the left face:

glcolor4f(0.0f, 0.0f, 1.0f, 0.75f); // blue

glvertex3f(-1.0f, 1.0f, 1.0f);

glvertex3f(-1.0f, 1.0f, -1.0f);

glvertex3f(-1.0f, -1.0f, -1.0f);

glvertex3f(-1.0f, -1.0f, 1.0f);

glend();

gldepthmask(1);

gldisable(gl_blend);

}

private void drawsphere()

{

float[] ambient = { 1.0f, 1.0f, 0.0f, 1.0f };

float[] diffuse = { 1.0f, 1.0f, 0.0f, 1.0f };

float[] specular = { 1.0f, 1.0f, 1.0f, 1.0f };

float[] position = { 4.0f, 0.0f, 6.0f, 0.0f };

gllightfv(gl_light0, gl_ambient, ambient);

gllightfv(gl_light0, gl_diffuse, diffuse);

gllightfv(gl_light0, gl_specular, specular);

gllightfv(gl_light0, gl_position, position);

glenable(gl_light0);

glenable(gl_lighting);

glulookat(-4.0, -5.0, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

intptr hdc = glunewquadric();

glusphere(hdc, 2.0, 40, 40);

gludeletequadric(hdc);

}

}

}

————-program.cs————–

#region using directives

using system;

using system.collections.generic;

using system.windows.forms;

#endregion

namespace opentest

{

static class program

{

/// <summary>

/// the main entry point for the application.

/// </summary>

[stathread]

static void main()

{

application.enablevisualstyles();

application.enablertlmirroring();

application.run(new form1());

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用C#开发opengl-.NET教程,C#语言
分享到: 更多 (0)