Archived

This topic is now archived and is closed to further replies.

mgs4solidsnake

Help Me To fix my Class Camera

Recommended Posts

Hi every one ; i was so confused from long time how to build a class camera to move arround the world as i need ; and cause i can,t understand vectors well of glulookat while calculation i used a excellent prewritten camera class and converted the code into c# class cause i use csgl and tao under c# ; the question is this class is build to move camera through y vector not z ! so i want to fix that to make to move arround z vector correctly as it doing in y vector ( i mean need to change up vector into glulookat to 1 in z up vector and make other changes ) if some one can help me in that cause i don,t understand how camera works exactly like those complex equation so here is the class in c# : using System; using System.Windows .Forms ; using System.Drawing ; using CsGL .OpenGL ; namespace CsGL.AdvancedCamera { /// /// Summary description for AdvancedCamera. /// public class AdvancedCamera :OpenGLControl { Vector vZero,vView,vUp; public Vector m_vPosition,m_vView,m_vUpVector,m_vStrafe; public int Screen_Width ,Screen_Height; public AdvancedCamera() { vZero = new Vector (); vView = new Vector (); vUp = new Vector (); m_vStrafe = new Vector (); vZero.x =0; vZero.y=0 ; vZero.z =0; vView.x = 0; vView.y =-0; vView.z =-20000; vUp.x =0; vUp.y=1; vUp .z =0; m_vPosition =vZero; m_vView = vView ; m_vUpVector = vUp; } Vector Cross(Vector vVector1,Vector vVector2) { Vector vNormal=new Vector (); vNormal.x =((vVector1 .y*vVector2 .z )-(vVector1 .z *vVector2.y )); vNormal.y =((vVector1.z *vVector2 .x) -(vVector1 .x*vVector2 .z )); vNormal.z = ((vVector1.x*vVector2 .y )-(vVector1.y *vVector2.x )); return vNormal ; } double Magnitude (Vector vNormal) { return Math .Sqrt((vNormal.x*vNormal.x ) +(vNormal.y *vNormal.y)+(vNormal.z*vNormal.z )); } Vector Normalize(Vector vVector) { double magnitude = Magnitude(vVector); vVector.x =vVector.x /magnitude ; vVector.y =vVector.y /magnitude ; vVector.z =vVector.z /magnitude ; return vVector; } public void PositionCamera(double Positionx , double positiony , double Positionz, double Viewx , double Viewy, double Viewz, double UpVectorx, double UpVectory , double UpVectorz) { Vector vPosition = new Vector (); Vector vView = new Vector (); Vector vUpVector = new Vector (); vPosition.x = Positionx ; vPosition.y = positiony ; vPosition.z = Positionz ; vView.x = Viewx; vView.y =Viewy ; vView.z = Viewz; vUpVector.x = UpVectorx ; vUpVector.y = UpVectory ; vUpVector.z = UpVectorz ; m_vPosition = vPosition; m_vView = vView; m_vUpVector = vUpVector; } public void SetViewByMouse() { int x,y; float AngleY , AngleZ; Point MousePos = new Point (0,0); int MiddleScreenX =Screen_Width >>1; int MiddleScreenY =Screen_Height >>1; double CurrentRotX = 0.0 ; CsGL.Util .Mouse .GetPosition (out x ,out y ); MousePos .X = x; MousePos .Y =y; if ((MousePos.X == MiddleScreenX ) && ( MousePos.Y == MiddleScreenY)) return ; CsGL.Util .Mouse .SetPosition (MiddleScreenX ,MiddleScreenY ); AngleY =(float)( MiddleScreenX - MousePos .X)/1000.0f; AngleZ =(float)(MiddleScreenY -MousePos .Y )/1000.0f; CurrentRotX = -AngleZ ; if(CurrentRotX >1.0) { CurrentRotX =1.0; } else if (CurrentRotX <(-1.0)) { CurrentRotX =-1.0; } else { Vector vAxis =new Vector (); Vector vTemp = new Vector (); vTemp.x = m_vView.x - m_vPosition.x ; vTemp.y = m_vView.y - m_vPosition.y ; vTemp.z = m_vView.z - m_vPosition.z ; vAxis= Cross (vTemp,m_vUpVector); vAxis = Normalize(vAxis); // Now rotate around two axis z and y// RotateView(AngleZ ,vAxis .x,vAxis.y ,vAxis.z); RotateView (AngleY ,0,1,0); } } void RotateView(double Angle ,double x,double y, double z) { Vector vNewview = new Vector(); Vector vTempview = new Vector (); vTempview.x = m_vView.x - m_vPosition.x; vTempview.y = m_vView.y - m_vPosition.y; vTempview.z = m_vView.z - m_vPosition.z; double CosAngle = Math.Cos (Angle); double SinAngle = Math.Sin (Angle); // Calculate the new Position for x using the new rotated point // vNewview.x = (CosAngle+((1-CosAngle)*x*x))*vTempview.x ; vNewview.x +=(((1-CosAngle )*x*y) -(z*SinAngle))*vTempview.y; vNewview.x += (((1-CosAngle)*x*z)+(y*SinAngle))*vTempview.z ; //////////////////////////////////////////////////////////////////// // Calculate the new Position for y using the new rotated point // vNewview.y = (((1-CosAngle)*x*y)+(z*SinAngle)) *vTempview.x; vNewview.y += (CosAngle +((1-CosAngle )*y*y))*vTempview.y; vNewview.y += (((1-CosAngle)*y*z)-(x*SinAngle) )*vTempview.z ; //////////////////////////////////////////////////////////////////// // Calculate the new Position for z using the new rotated point // vNewview.z = (((1-CosAngle)*x*z)-(y*SinAngle))*vTempview.x; vNewview.z +=(((1-CosAngle)*y*z)+(x*SinAngle))*vTempview.y ; vNewview.z += (CosAngle +((1-CosAngle)*z*z))*vTempview.z ; //////////////////////////////////////////////////////////////////// m_vView.x = m_vPosition.x +vNewview.x ; m_vView.y = m_vPosition.y +vNewview.y ; m_vView.z = m_vPosition.z +vNewview.z ; } public void StrafeCamera(double speed) { m_vPosition .x +=m_vStrafe.x *speed; m_vPosition.z += m_vStrafe.y*speed; m_vView .x += m_vStrafe .x *speed; m_vView.z +=m_vStrafe.z *speed ; } public void Movecamera(double speed) { Vector vTempvector = new Vector (); vTempvector.x = m_vView.x -m_vPosition .x ; vTempvector.y = m_vView.y -m_vPosition .y ; vTempvector.z = m_vView.z -m_vPosition .z ; vTempvector = Normalize(vTempvector); m_vPosition.x +=vTempvector.x *speed; m_vPosition .z +=vTempvector.z*speed; m_vView.x +=vTempvector.x *speed ; m_vView.z +=vTempvector.z *speed ; } public void VectorUpdate() { Vector vCross = new Vector (); Vector vTempvector = new Vector (); vTempvector.x =m_vView.x - m_vPosition.x ; vTempvector.y =m_vView.y - m_vPosition.y ; vTempvector.z =m_vView.z -m_vPosition.z ; vCross = Cross (vTempvector,m_vUpVector); m_vStrafe =Normalize(vCross); //SetViewByMouse(); } public void Look() { try { GL.glPushMatrix (); GL.gluLookAt (m_vPosition.x , m_vPosition.y ,m_vPosition.z , m_vView.x , m_vView.y,m_vView.z ,m_vUpVector.x , m_vUpVector .y ,m_vUpVector.z ); parent_View.Refresh (); GL.glPopMatrix (); } catch(Exception) {} } MyOpenGL.ViewOptions parent_View=null; public MyOpenGL.ViewOptions UpdateView { get {return parent_View; } set { parent_View=value;} } } public class Vector { public double x,y,z; public Vector() { x=0; y=0; z=0; } } } A New Trend Toward Emulation

Share this post


Link to post
Share on other sites