using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CsGL.OpenGL;
using OpenGL;
namespace CsGLTest
{
public partial class MyView : OpenGLControl
{
public bool light = true; // Lighting ON/OFF ( NEW )
public bool lp = false; // L Pressed? ( NEW )
public bool fp = false; // F Pressed? ( NEW )
public float xrot = 0.0f; // X-axis rotation
public float yrot = 0.0f; // Y-axis rotation
public float zrot = 0.0f;
public float xspeed = 0.0f; // X Rotation Speed
public float yspeed = 0.0f; // Y Rotation Speed
public float z = -5.0f; // Depth Into The Screen
// Lighting components for the cube
public float[] LightAmbient = {0.5f, 0.5f, 0.5f, 1.0f};
public float[] LightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
public float[] LightPosition = {0.0f, 0.0f, 2.0f, 1.0f};
public int filter = 0; // Which Filter To Use
public uint[] texture = new uint[3]; // Texture array
public bool finished;
/// <summary>
/// 构造函数
/// </summary>
public MyView()
: base()
{
this.KeyDown += new KeyEventHandler(LessonView_KeyDown);
this.KeyUp += new KeyEventHandler(LessonView_KeyUp);
this.finished = false;
}
uint theToures;
/// <summary>
/// OpenGL初始化。
/// </summary>
protected override void InitGLContext()
{
LoadTextures();
DrawCoords();
GL.glEnable(GL.GL_TEXTURE_2D); // Enable Texture Mapping
GL.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
GL.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
GL.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
GL.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really Nice Perspective Calculations
GL.glClearDepth(1.0f); // Depth Buffer Setup
//antialiasing
GL.glEnable(GL.GL_LINE_SMOOTH);
GL.glBlendFunc(GL.GL_SRC_ALPHA_SATURATE, GL.GL_ONE);
GL.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, this.LightAmbient); // Setup The Ambient Light
GL.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, this.LightDiffuse); // Setup The Diffuse Light
GL.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, this.LightPosition); // Position The Light
GL.glEnable(GL.GL_LIGHT1); // Enable Light One
theToures = GL.glGenLists(1);
//if (this.light) // If lighting, enable it to start
//GL.glEnable(GL.GL_LIGHTING);
}
/// <summary>
/// 窗口大小改变(reshape)
/// </summary>
/// <param name="e"></param>
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
Size s = Size;
if (s.Height == 0)
s.Height = 1;
GL.glViewport(0, 0, s.Width, s.Height);
GL.glMatrixMode(GL.GL_PROJECTION);
GL.glLoadIdentity();
GL.gluPerspective(45.0f, (double)s.Width / (double)s.Height, 0.1f, 100.0f);
GL.glMatrixMode(GL.GL_MODELVIEW);
GL.glLoadIdentity();
}
/// <summary>
/// 载入纹理
/// </summary>
/// <returns></returns>
protected bool LoadTextures()
{
Bitmap p_w_picpath = null;
string file = @"E:\a.gif";
try
{
// If the file doesn't exist or can't be found, an ArgumentException is thrown instead of
// just returning null
p_w_picpath = new Bitmap(file);
}
catch (System.ArgumentException)
{
MessageBox.Show("Could not load " + file + ". Please make sure that Data is a subfolder from where the application is running.", "Error", MessageBoxButtons.OK);
this.finished = true;
}
if (p_w_picpath != null)
{
p_w_picpath.RotateFlip(RotateFlipType.RotateNoneFlipY);
System.Drawing.Imaging.BitmapData bitmapdata;
Rectangle rect = new Rectangle(0, 0, p_w_picpath.Width, p_w_picpath.Height);
bitmapdata = p_w_picpath.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GL.glGenTextures(3, this.texture);
// Create Nearest Filtered Texture
GL.glBindTexture(GL.GL_TEXTURE_2D, this.texture[0]);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGB, p_w_picpath.Width, p_w_picpath.Height, 0, GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
// Create Linear Filtered Texture
GL.glBindTexture(GL.GL_TEXTURE_2D, this.texture[1]);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGB, p_w_picpath.Width, p_w_picpath.Height, 0, GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
// Create MipMapped Texture
GL.glBindTexture(GL.GL_TEXTURE_2D, this.texture[2]);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_NEAREST);
GL.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB, p_w_picpath.Width, p_w_picpath.Height, GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
p_w_picpath.UnlockBits(bitmapdata);
p_w_picpath.Dispose();
return true;
}
return false;
}
/// <summary>
/// 画图(display)
/// </summary>
public override void glDraw()
{
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);//清除屏幕和深度缓存
GL.glLoadIdentity();
//GL.glTranslatef(0.0f, 0.0f, this.z);
GLU.gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
//GL.glRotatef(this.xrot, 1.0f, 0.0f, 0.0f);
//GL.glRotatef(this.yrot, 0.0f, 1.0f, 0.0f);
GL.glBindTexture(GL.GL_TEXTURE_2D, this.texture[filter]);
DrawBox();
DrawCoords();
DrawLine();
GL.glCallList(theToures);
//GL.glTranslated(1, -1, 0);
//DrawBox();
//GL.glBegin(GL.GL_LINES);
//GL.glColor3b(sbyte.MaxValue, 0, 0);
//GL.glVertex2d(1, 1);
//GL.glVertex2d(2, 2);
//GL.glEnd();
this.xrot += this.xspeed;
this.yrot += this.yspeed;
GL.glFlush();
this.SwapBuffer();
}
/// <summary>
/// 画立方体并添加纹理
/// </summary>
private void DrawBox()
{
GL.glBegin(GL.GL_QUADS);
// Front Face
GL.glNormal3f(0.0f, 0.0f, 1.0f);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(-1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
GL.glNormal3f(0.0f, 0.0f, -1.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(-1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(1.0f, -1.0f, -1.0f);
// Top Face
GL.glNormal3f(0.0f, 1.0f, 0.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(-1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(-1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(1.0f, 1.0f, -1.0f);
// Bottom Face
GL.glNormal3f(0.0f, -1.0f, 0.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
GL.glNormal3f(1.0f, 0.0f, 0.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(1.0f, -1.0f, 1.0f);
// Left Face
GL.glNormal3f(-1.0f, 0.0f, 0.0f);
GL.glTexCoord2f(0.0f, 0.0f); GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 0.0f); GL.glVertex3f(-1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f); GL.glVertex3f(-1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 1.0f); GL.glVertex3f(-1.0f, 1.0f, -1.0f);
GL.glEnd();
}
/// <summary>
/// 按下某个键时发生
/// </summary>
/// <param name="Sender"></param>
/// <param name="e"></param>
protected void LessonView_KeyDown(object Sender, KeyEventArgs e)
{
}
/// <summary>
/// 键弹起时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LessonView_KeyUp(object sender, KeyEventArgs e)
{
}
protected override bool ProcessDialogKey(Keys keyData)
{
}
/// <summary>
/// 画坐标系
/// </summary>
private void DrawCoords()
{
GL.glBegin(GL.GL_LINES);
GL.glColor3b(sbyte.MaxValue, 0, 0);
GL.glVertex3f(0, 0, 0);
GL.glVertex3f(100, 0, 0);
GL.glColor3b(0, sbyte.MaxValue, 0);
GL.glVertex3f(0, 0, 0);
GL.glVertex3f(0, 100, 0);
GL.glColor3b(0, 0, sbyte.MaxValue);
GL.glVertex3f(0, 0, 0);
GL.glVertex3f(0, 0, 100);
GL.glEnd();
}
private void DrawLine()
{
GL.glNewList(theToures, GL.GL_COMPILE);
GL.glBegin(GL.GL_TRIANGLES);
GL.glColor3b(0, sbyte.MaxValue, sbyte.MaxValue);
GL.glVertex2d(-1, -1);
GL.glVertex2d(-4, -4);
GL.glVertex2d(1, 5);
GL.glEnd();
GL.glEndList();
}
}
}
C# OpenGL代码
精选 转载上一篇:如何在C#上开发OpenGL

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C# 语法中级
C# 语法中级
c# lambda -
C#调用C++代码
C#调用C++代码
C# C++ 调用