类库用的是sharpgl,下边是我用鼠标托动控制旋转的关键代码,在鼠标托动时还是没有实现。在绘图的时主要会用到这个旋转void glRotate{fd}(TYPE angle,TYPE x,TYPE y,TYPE z),比较急请帮忙看一下具体的方法那个地方有问题,或者有人实现了参考一下代码。 double[] lastMatrix = new double[16] // 前一次矩阵,初始化为单位矩阵E {1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0}; private double theta; // 旋转角度 private double[] axis = new double[3]; // 旋转轴 private double[] lastPos = new double[3], curPos = new double[3]; private int hemishere(int x, int y, int d,double[] v) { double z; // 计算x, y坐标 v[0] = (double)x * 2.0 – (double)d; v[1] = (double)d – (double)y * 2.0; // 计算z坐标 z = d * d – v[0] * v[0] – v[1] * v[1]; if (z < 0) { return 0; } v[2] = Math.Sqrt(z); // 单位化 v[0] /= (double)d; v[1] /= (double)d; v[2] /= (double)d; return 1; } private void openGLControl1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { double d, dx, dy, dz; // 计算当前的鼠标单位半球面坐标 if (hemishere(e.X, e.Y, 500, curPos)!=1) { return; } // 计算移动量的三个方向分量 dx = curPos[0] – lastPos[0]; dy = curPos[1] – lastPos[1]; dz = curPos[2] – lastPos[2]; // 如果有移动 if (dx != 0 || dy != 0 || dz != 0) { // 计算移动距离,用来近似移动的球面距离 d = Math.Sqrt(dx * dx + dy * dy + dz * dz); // 通过移动距离计算移动的角度 theta = d * 180.0; // 计算移动平面的法向量,即:lastPos × curPos axis[0] = lastPos[1] * curPos[2] – lastPos[2] * curPos[1]; axis[1] = lastPos[2] * curPos[0] – lastPos[0] * curPos[2]; axis[2] = lastPos[0] * curPos[1] – lastPos[1] * curPos[0]; // 记录当前的鼠标单位半球面坐标 lastPos[0] = curPos[0]; lastPos[1] = curPos[1]; lastPos[2] = curPos[2]; // 重绘窗口 glDraw(); } } } private void openGLControl1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { hemishere(e.X, e.Y, 500, lastPos); } } private void glDraw() { SharpGL.OpenGL gl = this.openGLControl1.OpenGL; gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT); gl.MatrixMode(OpenGL.MODELVIEW); // 设置当前使用模型视图矩阵栈 gl.LoadIdentity(); // Reset The View gl.Translate(-1.5f, 0.0f, -6.0f); // Move Left And Into The Screen // 计算新的旋转矩阵,即:M = E · R = R gl.Rotate(theta, axis[0], axis[1], axis[2]); //// 左乘上前一次的矩阵,即:M = R · L gl.MultMatrix(lastMatrix); //// 保存此次处理结果,即:L = M gl.GetDouble(OpenGL.MODELVIEW_MATRIX, lastMatrix); gl.Begin(OpenGL.TRIANGLES); // Start Drawing The Pyramid gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, 1.0f); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, 1.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0.0f, 1.0f, 0.0f); gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); gl.End(); } |
|
#140分 |
我试了试你给的代码,在拖动的时候,东西越来越小,慢慢消失不见了。
|
#5 |
回复2楼: 我又改了下,现在只能绕X轴和Y轴旋转,Z轴不能旋转,大家帮忙看下,下边是原代码。 float rtri = 0; float rquad = 0; private double zoom = 1.0; private bool isMouseDown = false; private int lastX = 0; private int lastY = 0; private int rotationY = 0; private int rotationX = 0; private void openGLControl1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { lastX = e.X; lastY = e.Y; isMouseDown = true; } } private void openGLControl1_MouseMove(object sender, MouseEventArgs e) { int x, y; if (e.Button == MouseButtons.Left && isMouseDown == true) { x = e.X; y = e.Y; rotationY += (x – lastX) / 10; //鼠标在X方向上移动,相当于绕Y转 rotationX += (y – lastY) / 10; //鼠标在Y方向上移动,相当于绕X转 glDraw(); } } private void glDraw() { SharpGL.OpenGL gl = this.openGLControl1.OpenGL; gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT); gl.MatrixMode(OpenGL.MODELVIEW); // 设置当前使用模型视图矩阵栈 gl.LoadIdentity(); // Reset The View gl.Translate(-1.5f, 0.0f, -6.0f); // Move Left And Into The Screen gl.Rotate((float)rotationX, 1.0f, 0.0f, 0.0f); //绕X轴旋转 gl.Rotate((float)rotationY, 0.0f, 1.0f, 0.0f); //绕Y轴旋转 gl.Begin(OpenGL.TRIANGLES); // Start Drawing The Pyramid gl.Color(1.0f, 0.0f, 0.0f); // Red gl.Vertex(0.0f, 1.0f, 0.0f); // Top Of Triangle (Front) gl.Color(0.0f, 1.0f, 0.0f); // Green gl.Vertex(-1.0f, -1.0f, 1.0f); // Left Of Triangle (Front) gl.Color(0.0f, 0.0f, 1.0f); // Blue gl.Vertex(1.0f, -1.0f, 1.0f); // Right Of Triangle (Front) gl.Color(1.0f, 0.0f, 0.0f); // Red gl.Vertex(0.0f, 1.0f, 0.0f); // Top Of Triangle (Right) gl.Color(0.0f, 0.0f, 1.0f); // Blue gl.Vertex(1.0f, -1.0f, 1.0f); // Left Of Triangle (Right) gl.Color(0.0f, 1.0f, 0.0f); // Green gl.Vertex(1.0f, -1.0f, -1.0f); // Right Of Triangle (Right) gl.Color(1.0f, 0.0f, 0.0f); // Red gl.Vertex(0.0f, 1.0f, 0.0f); // Top Of Triangle (Back) gl.Color(0.0f, 1.0f, 0.0f); // Green gl.Vertex(1.0f, -1.0f, -1.0f); // Left Of Triangle (Back) gl.Color(0.0f, 0.0f, 1.0f); // Blue gl.Vertex(-1.0f, -1.0f, -1.0f); // Right Of Triangle (Back) gl.Color(1.0f, 0.0f, 0.0f); // Red gl.Vertex(0.0f, 1.0f, 0.0f); // Top Of Triangle (Left) gl.Color(0.0f, 0.0f, 1.0f); // Blue gl.Vertex(-1.0f, -1.0f, -1.0f); // Left Of Triangle (Left) gl.Color(0.0f, 1.0f, 0.0f); // Green gl.Vertex(-1.0f, -1.0f, 1.0f); // Right Of Triangle (Left) gl.End(); } |