关于OpenGL旋转和缩放的问题??? 用glScalef来缩放。旋转还是用glRotatef,不过要先用glTranslatef将物体移到原点,再用glRotatef,完了再用glTranslatef移回原来的位置。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/xlzxlich/archive/2007/06/05/1639780.aspx to xlzxlich(阳光) 你给的网页中没有 缩放 旋转 的例子 假设 glulookat(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z); 你想绕相应的 Z 转 angle 度,则 glRotated(angle, n.m_x, n.m_y, n.m_z),其中 n = eye - center 绕其他轴类似。 缩放: glTranslatef( -eye.x, -eye.y, -eye.z); glScalef( sx, sy, sz); glTranslatef( eye.x, eye.y, eye.z);其中,sx, sy, sz是放大倍数,eye同上 要想效率高些的话,用 glMultMatrix(m) m自己算 谢谢 htzyhtzy() 我试试 本人写的C#代码片断,希望对你有帮助~ class _dView : OpenGLControl { protected override void OnMouseDown(MouseEventArgs e) { if (e.Button== MouseButtons.Left) { xTransInit = e.X; yTransInit = e.Y; } if (e.Button==MouseButtons.Right) { xRotAngInit = e.X; yRotAngInit = e.Y; } } protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { xTrans += (e.X - xTransInit) / scale; yTrans += (e.Y - yTransInit) / scale; xTransInit = e.X; yTransInit = e.Y; this.Invalidate(); //调用glDraw()重绘window } if (e.Button == MouseButtons.Right) { xRotAng += (e.X - xRotAngInit); yRotAng += (e.Y - yRotAngInit); xRotAngInit = e.X; yRotAngInit = e.Y; this.Invalidate(); //调用glDraw()重绘window } } protected override void OnMouseWheel(MouseEventArgs e) { zZoom +=e.Delta/scale; this.Invalidate(); } private double xTrans = 0; // X方向上的平移量 private double yTrans = 0; // Y方向上的平移量 private double xTransInit = 0; // X方向上的平移量 private double yTransInit = 0; // Y方向上的平移量 private double xRotAng = 0; // 角度 private double yRotAng = 0; // 角度 private double xRotAngInit = 0; // 角度 private double yRotAngInit = 0; // 角度 private double zZoom = -6.0d; // 相对屏幕视口(Z轴)移进移出 public override void glDraw() { GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // 清理窗口和缓存 GL.glLoadIdentity(); // 重新设置当前视口矩阵 double x = m_dXrange / (2 * scale); double y = m_dYrange / (2 * scale); double z = m_dZrange / (2 * scale); GL.glTranslated(xTrans, 0.0d, 0.0d); GL.glTranslated(0.0d, -yTrans, 0.0d); GL.glTranslated(0.0d, 0.0d, zZoom); //GL.glTranslated(xTrans, -yTrans, zZoom); // 作相应的平移 GL.glRotated(-xRotAng, 0.0d, yTrans, 0.0d); // 旋转x轴 GL.glRotated(-yRotAng, xTrans, 0.0d, 0.0d); // 旋转y轴 BuildBoundary(x,y,z); //绘制长方体的8条线 BuildAxisList(x,y,z); //绘制x,y,z轴 并且显示x,y,z提示 BuildDataList(); //绘制立方体内部 BuildWallList(); //绘制立方体表面 GL.glFlush(); //强制刷新 }} 由于openGL只有一个坐标系,所以平常画物体,应该注意保存当前坐标系坐标,以便画完后返回。glPushMatrix()坐标变换画好物体AglPopMatrix();继续新的代码这样画A就不会影响其他部分了。gluLookat()的内部实现就是translatef和glRotatef()另外上面有个人的说法有问题的。缩放: glTranslatef( -eye.x, -eye.y, -eye.z); glScalef( sx, sy, sz); glTranslatef( eye.x, eye.y, eye.z);其中,sx, sy, sz是放大倍数,eye同上 要想效率高些的话,用 glMultMatrix(m) m自己算我记得是宝典好象,已经明确的说了glMultMatrix(m)是用来实现你自己特殊的变换,其他时候最好不用它,它的效率比那3个标准转换函数要差, 所以效率高的说法应该是不妥的。但是他的代码是对的。应该可以结帐了吧,我不知道你是否的确需要缩放,如果你只需要象CS那样的控制(glulookat()实现,VC6.0,C++,文件.h .cpp),可以留个邮件。 glMultMatrix(m)关于m的计算,有个类CArcBall,可以通过鼠标拖动来实现旋转缩放,其中一个例子见:http://blog.csdn.net/ciweiyu/archive/2007/07/13/1689806.aspx其中的代码包含了旋转缩放功能 另外上面有个人的说法有问题的。缩放: glTranslatef( -eye.x, -eye.y, -eye.z); glScalef( sx, sy, sz); glTranslatef( eye.x, eye.y, eye.z);其中,sx, sy, sz是放大倍数,eye同上 要想效率高些的话,用 glMultMatrix(m) m自己算我记得是宝典好象,已经明确的说了glMultMatrix(m)是用来实现你自己特殊的变换,其他时候最好不用它,它的效率比那3个标准转换函数要差, 所以效率高的说法应该是不妥的。但是他的代码是对的。 /*********************************************************************/To emptyness: 我想你误会我的意思了 我是说一次glMultMatrix(m)的效率会比 glTranslatef( -eye.x, -eye.y, -eye.z); glScalef( sx, sy, sz); glTranslatef( eye.x, eye.y, eye.z); 这样三次函数调用的效率要高。 刻了两个月光盘终于转正了 矢量图转位图(巨大)无法转换的问题(在线等) 问个有关法律的问题 COM真的过时了么? 急,如何在web browser中直接读写mht内容? 为什么GetModuleFileNameEx一使用就会出错? #include 上一层目录底下的文件怎么表示? ?请教大虾一个硬件问题?SOS! Unix下有个random()函数,它和VC下的哪个函数一样呀? 怎么得到capGrabFrameNoStop这个函数所截的帧 在VC++中包含头文件<unistd.h> 编译报错 怎么办?(急) 画y=Asin(ωx+φ)函数曲线,并可以改变,A,ω,φ的值
你想绕相应的 Z 转 angle 度,则 glRotated(angle, n.m_x, n.m_y, n.m_z),
其中 n = eye - center
绕其他轴类似。
glScalef( sx, sy, sz);
glTranslatef( eye.x, eye.y, eye.z);
其中,sx, sy, sz是放大倍数,eye同上
要想效率高些的话,用 glMultMatrix(m)
m自己算
class _dView : OpenGLControl
{
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button== MouseButtons.Left)
{
xTransInit = e.X;
yTransInit = e.Y;
}
if (e.Button==MouseButtons.Right)
{
xRotAngInit = e.X;
yRotAngInit = e.Y;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xTrans += (e.X - xTransInit) / scale;
yTrans += (e.Y - yTransInit) / scale;
xTransInit = e.X;
yTransInit = e.Y;
this.Invalidate(); //调用glDraw()重绘window
}
if (e.Button == MouseButtons.Right)
{
xRotAng += (e.X - xRotAngInit);
yRotAng += (e.Y - yRotAngInit);
xRotAngInit = e.X;
yRotAngInit = e.Y;
this.Invalidate(); //调用glDraw()重绘window
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
zZoom +=e.Delta/scale;
this.Invalidate();
}
private double xTrans = 0; // X方向上的平移量
private double yTrans = 0; // Y方向上的平移量
private double xTransInit = 0; // X方向上的平移量
private double yTransInit = 0; // Y方向上的平移量 private double xRotAng = 0; // 角度
private double yRotAng = 0; // 角度
private double xRotAngInit = 0; // 角度
private double yRotAngInit = 0; // 角度 private double zZoom = -6.0d; // 相对屏幕视口(Z轴)移进移出 public override void glDraw()
{ GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // 清理窗口和缓存
GL.glLoadIdentity(); // 重新设置当前视口矩阵
double x = m_dXrange / (2 * scale);
double y = m_dYrange / (2 * scale);
double z = m_dZrange / (2 * scale); GL.glTranslated(xTrans, 0.0d, 0.0d);
GL.glTranslated(0.0d, -yTrans, 0.0d);
GL.glTranslated(0.0d, 0.0d, zZoom); //GL.glTranslated(xTrans, -yTrans, zZoom); // 作相应的平移
GL.glRotated(-xRotAng, 0.0d, yTrans, 0.0d); // 旋转x轴
GL.glRotated(-yRotAng, xTrans, 0.0d, 0.0d); // 旋转y轴 BuildBoundary(x,y,z); //绘制长方体的8条线
BuildAxisList(x,y,z); //绘制x,y,z轴 并且显示x,y,z提示
BuildDataList(); //绘制立方体内部
BuildWallList(); //绘制立方体表面 GL.glFlush(); //强制刷新
}
}
应该注意保存当前坐标系坐标,以便画完后返回。
glPushMatrix()
坐标变换
画好物体A
glPopMatrix();
继续新的代码
这样画A就不会影响其他部分了。
gluLookat()的内部实现就是translatef和glRotatef()
另外上面有个人的说法有问题的。缩放: glTranslatef( -eye.x, -eye.y, -eye.z);
glScalef( sx, sy, sz);
glTranslatef( eye.x, eye.y, eye.z);
其中,sx, sy, sz是放大倍数,eye同上
要想效率高些的话,用 glMultMatrix(m)
m自己算我记得是宝典好象,已经明确的说了glMultMatrix(m)是用来实现你自己特殊的变换,其他时候最好不用它,它的效率比那3个标准转换函数要差, 所以效率高的说法应该是不妥的。但是他的代码是对的。应该可以结帐了吧,
我不知道你是否的确需要缩放,如果你只需要象CS那样的控制
(glulookat()实现,VC6.0,C++,文件.h .cpp),可以留个邮件。
关于m的计算,有个类CArcBall,可以通过鼠标拖动来实现旋转缩放,其中一个例子见:
http://blog.csdn.net/ciweiyu/archive/2007/07/13/1689806.aspx
其中的代码包含了旋转缩放功能
glScalef( sx, sy, sz);
glTranslatef( eye.x, eye.y, eye.z);
其中,sx, sy, sz是放大倍数,eye同上
要想效率高些的话,用 glMultMatrix(m)
m自己算我记得是宝典好象,已经明确的说了glMultMatrix(m)是用来实现你自己特殊的变换,其他时候最好不用它,它的效率比那3个标准转换函数要差, 所以效率高的说法应该是不妥的。但是他的代码是对的。 /*********************************************************************/
To emptyness:
我想你误会我的意思了
我是说一次glMultMatrix(m)的效率会比 glTranslatef( -eye.x, -eye.y, -eye.z);
glScalef( sx, sy, sz);
glTranslatef( eye.x, eye.y, eye.z);
这样三次函数调用的效率要高。