19,469
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using CsGL.OpenGL;
namespace Vic2D
{
class DIC_3DShow : OpenGLControl
{
const double PI = 3.1415926;
float scaleValue = 1.0f;
const float HEIGHT_RATIO = 1.0f;
const int WIDTH = 680, HEIGHT = 480; //窗口宽度,高度
Double[] p3DShow = new double[CallDLL.width * CallDLL.height * 3];
bool bRender = true;
double sx, sy, sz, lx, ly, lz; //设置视口时的参数
int lastX, lastY; //鼠标中键按下没有移动前的坐标
bool isMouseDown = false;
public DIC_3DShow(double[] p3DShow)
{
this.p3DShow = p3DShow;
sx = 0; sy = 0; sz = 1;
lx = 0; ly = 0; lz = -1;
InitializeComponent();
}
//控件创造的时候
protected override void OnCreateControl()
{
this.Width = WIDTH;
this.Height = HEIGHT;
}
//初始化OpenGl控件
protected override void InitGLContext()
{
GL.glShadeModel(GL.GL_SMOOTH);
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
GL.glClearDepth(1.0f); GL.glEnable(GL.GL_DEPTH_TEST);
GL.glDepthFunc(GL.GL_LEQUAL);
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
}
//当控件大小改变时候
protected override void OnSizeChanged(EventArgs e)
{
Size s = this.Size;
double aspect_ratio = (double)s.Width / (double)s.Height;
//ViewCtrl.VIEWSIZE_WIDTH = s.Width;
//ViewCtrl.VIEWSIZE_HEIGHT = s.Height;
GL.glViewport(0, 0, s.Width, s.Height); //设置视点 // Reset The Current Viewport
GL.glMatrixMode(GL.GL_PROJECTION); // Select The Projection Matrix
GL.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window. Farthest Distance Changed To 500.0f (NEW)
GL.gluPerspective(45.0f, aspect_ratio, 0.0f, 1.0f); //**设置透视投影,创建一个对称透视视景体
GL.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
GL.glLoadIdentity(); // Reset The Modelview Matrix
}
protected override void OnPaint(PaintEventArgs pevent) //用来报异常
{
//try
//{
base.OnPaint(pevent);
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
//重写绘图函数
public override void glDraw()
{
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
GL.glLoadIdentity(); // Position View Up Vector
GL.gluLookAt(sx, sy, sz, lx, ly, lz, 0, 1, 0); //**设置视口
Render3DShow();
GL.glFlush();
DrawReferenceFrame();
//GL.glColor3f(1.0f, 0.0f, 0.0f);
//GL.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
//GL.glFlush();
}
void Render3DShow() //显示三维坐标
{
double x, y, z; // Create Some Variables For Readability
GL.glScalef(scaleValue, scaleValue * HEIGHT_RATIO, scaleValue); //设置X,Y,Z坐标比例变换
//GL.glBegin(GL.GL_QUADS);
if (bRender) GL.glBegin(GL.GL_QUADS); else
GL.glBegin(GL.GL_LINES);
for (int i = 0; i < p3DShow.Length / 3; i++)
{
if (p3DShow[i] != Math.Pow(10, 6) && p3DShow[i + p3DShow.Length / 3] != Math.Pow(10, 6) && p3DShow[i + p3DShow.Length / 3 * 2] != Math.Pow(10, 6))
{
x = p3DShow[i];
y = p3DShow[i + p3DShow.Length / 3];
z = p3DShow[i + p3DShow.Length / 3 * 2];
GL.glColor3d(0, 0, 255);
GL.glVertex3d(x, y, z); //画顶点
}
}
GL.glEnd(); //**glEnd()和glBegin()必须成对使用!
//GL.glColor4d(1.0f, 1.0f, 1.0f, 1.0f);
}
private void DrawReferenceFrame() //画坐标系
{
GL.glColor3d(255, 255, 0);
//x轴
GL.glBegin(GL.GL_LINES); //画直线
GL.glVertex3f(0.7f, -0.8f, 0.0f);
GL.glVertex3f(0.8f, -0.8f, 0.0f);
GL.glEnd();
GL.glPushMatrix(); //画箭头
GL.glTranslatef(0.8f, -0.8f, 0.0f);
GL.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
GL.glutWireCone(0.015, 0.03, 10, 10);
GL.glPopMatrix();
//y轴
GL.glBegin(GL.GL_LINES); //画直线
GL.glVertex3f(0.7f, -0.8f, 0.0f);
GL.glVertex3f(0.7f, -0.6f, 0.0f);
GL.glEnd();
GL.glPushMatrix(); //画箭头
GL.glTranslatef(0.7f, -0.6f, 0.0f);
GL.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
GL.glutWireCone(0.015, 0.03, 100, 100);
GL.glPopMatrix();
//z轴
GL.glPushMatrix();
GL.glTranslatef(0.7f, -0.8f, 0.0f);
GL.glRotatef(135.0f, 1.0f, 0.0f, 0.0f); //**关键是这里旋转不起作用
GL.glBegin(GL.GL_LINES); //画直线
GL.glVertex3f(0.7f, -0.8f, 0.0f);
GL.glVertex3f(0.8f, -0.8f, 0.0f);
GL.glEnd();
GL.glPopMatrix();
//GL.glBegin(GL.GL_LINES); //画直线
//GL.glVertex3f(0.7f, -0.8f, 0.0f);
//GL.glVertex3f(0.6f, -0.9f, 0.0f);
//GL.glEnd();
GL.glPushMatrix(); //画箭头
GL.glTranslatef(0.6f, -0.9f, 0.0f);
GL.glRotatef(90.0f, 1.0f, -1.0f, 0.0f);
GL.glutWireCone(0.015, 0.03, 10, 10);
GL.glPopMatrix();
GL.glColor4d(1.0f, 1.0f, 1.0f, 1.0f);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// DIC_3DShow
//
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DIC_3DShow_MouseMove);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.DIC_3DShow_MouseClick);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DIC_3DShow_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DIC_3DShow_MouseUp);
this.ResumeLayout(false);
}
private void DIC_3DShow_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
bRender = !bRender;
Refresh();
}
}
private void DIC_3DShow_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
lastX = e.X;
lastY = e.Y;
isMouseDown = true;
}
}
private void DIC_3DShow_MouseMove(object sender, MouseEventArgs e)
{
int x, y;
double angleX, angleY;
double rotationX = 0.0, rotationY = 0.0;
if (e.Button == MouseButtons.Middle && isMouseDown == true)
{
x = e.X;
y = e.Y;
/*//angleX = (x-lastX) / 1280 * 2 * PI;
//angleY = (y-lastY) / 1024 * 2 * PI;
angleX = x / 1280 * 2 * PI;
angleY = y / 1024 * PI - PI / 2;
sx += Math.Cos(angleX);
sy += Math.Sin(angleY);
//sz += Math.Cos(angleX);
lx -= Math.Abs(Math.Cos(angleX));
ly -= Math.Abs(Math.Sin(angleY));
//lz -= Math.Abs(Math.Cos(angleX));
Refresh();*/
rotationY += (x - lastX); //鼠标移动的X方向上的位移,相当于绕Y轴旋转
rotationX += (y - lastY); //鼠标移动的Y方向上的位移,相当于绕X轴旋转
GL.glPushMatrix();
GL.glRotatef((float)rotationX, 1.0f, 0.0f, 0.0f); //绕X轴旋转 //**关键是这里旋转不起作用
GL.glRotatef((float)rotationY, 0.0f, 1.0f, 0.0f); //绕Y轴旋转
Refresh();
//DrawReferenceFrame();
GL.glPopMatrix();
}
}
private void DIC_3DShow_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
}
}