
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.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP_TO_EDGE);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP_TO_EDGE);Point3D P1 = ConvertP(new Point(AnT.Width, 0), Z);
Point3D P2 = ConvertP(new Point(AnT.Width, AnT.Height), Z);public static Point3D ConvertP(Point p, double inZ)
{
Point3D P1, P2, Dir;
double x, y, z;
int vx = p.X, vy = AnTH - p.Y - 1;
int[] viewport = new int[4];
double[] projection = new double[16];
double[] modelview = new double[16];
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projection);
Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelview);
int[] zz = new int[1];
Gl.glReadPixels(vx, vy, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, zz);
Glu.gluUnProject(vx, vy, 0, modelview, projection, viewport, out x, out y, out z);
P1 = new Point3D(x, y, z);
Glu.gluUnProject(vx, vy, 1, modelview, projection, viewport, out x, out y, out z);
P2 = new Point3D(x, y, z);
Dir = new Point3D();
Dir = P1 - P2;
Dir.Normalize();
Point3D A = new Point3D(0, 0, 2 * P1.Z + inZ);
Point3D n = new Point3D(0, 0, -1);
double t = n * (A - P1);
Point3D P = P1 + t * Dir;
double R = Math.Sqrt(P.X * P.X + P.Y * P.Y + P.Z * P.Z);
double addz = 0;
while (Math.Abs(R - inZ) > 0.00001)
{
double add= Math.Abs(R - inZ);
if (R > inZ) addz -= add;
else addz += add;
A = new Point3D(0, 0, 2 * P1.Z + inZ + addz);
n = new Point3D(0, 0, -1);
t = n * (A - P1);
P = P1 + t * Dir;
R = Math.Sqrt(P.X * P.X + P.Y * P.Y + P.Z * P.Z);
}
return P;
}class Point3D
{
public double X, Y, Z;
public Point3D()
{
this.X = 0;
this.Y = 0;
this.Z = 0;
}
public Point3D(double X, double Y, double Z)
{
this.X = X;
this.Y = Y;
this.Z = Z;
}
public void Normalize()
{
double Lenght = Math.Sqrt(X * X + Y * Y + Z * Z);
this.X /= Lenght;
this.Y /= Lenght;
this.Z /= Lenght;
}
#region Перегрузка операторов
public static Point3D operator +(Point3D P1, Point3D P2)
{
Point3D tmp = new Point3D();
tmp.X = P1.X + P2.X;
tmp.Y = P1.Y + P2.Y;
tmp.Z = P1.Z + P2.Z;
return tmp;
}
public static Point3D operator -(Point3D P1, Point3D P2)
{
Point3D tmp = new Point3D();
tmp.X = P1.X - P2.X;
tmp.Y = P1.Y - P2.Y;
tmp.Z = P1.Z - P2.Z;
return tmp;
}
public static double operator *(Point3D P1, Point3D P2)
{
return P1.X * P2.X + P1.Y * P2.Y + P1.Z * P2.Z;
}
public static Point3D operator *(double D, Point3D P)
{
Point3D tmp = new Point3D();
tmp.X = P.X * D;
tmp.Y = P.Y * D;
tmp.Z = P.Z * D;
return tmp;
}
#endregion
}

class Cubic
{
public double x, y, z,size,r,g,b;
public Cubic(double x, double y, double z)
{
}
public void Draw()
{
Gl.glPushMatrix();
Gl.glTranslated(x,y,z);
Gl.glRotated(45, 1, 0, 1);
Gl.glColor3d(r, g, b);
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3d(size, size, size);
Gl.glVertex3d(-size, size, size);
Gl.glVertex3d(-size, -size, size);
Gl.glVertex3d(size, -size, size);
Gl.glEnd();
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3d(size, size, -size);
Gl.glVertex3d(size, -size, -size);
Gl.glVertex3d(-size, -size, -size);
Gl.glVertex3d(-size, size, -size);
Gl.glEnd();
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3d(-size, size, size);
Gl.glVertex3d(-size, size, -size);
Gl.glVertex3d(-size, -size, -size);
Gl.glVertex3d(-size, -size, size);
Gl.glEnd();
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3d(size, size, size);
Gl.glVertex3d(size, -size, size);
Gl.glVertex3d(size, -size, -size);
Gl.glVertex3d(size, size, -size);
Gl.glEnd();
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3d(-size, size, -size);
Gl.glVertex3d(-size, size, size);
Gl.glVertex3d(size, size, size);
Gl.glVertex3d(size, size, -size);
Gl.glEnd();
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3d(-size, -size, -size);
Gl.glVertex3d(size, -size, -size);
Gl.glVertex3d(size, -size, size);
Gl.glVertex3d(-size, -size, size);
Gl.glEnd();
Gl.glPopMatrix();
}
}
public Form1()
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();//тут пусто
simpleOpenGlControl2.InitializeContexts();//эту построит
}
CCamera cam = new CCamera();
cam.Position_Camera(0, 5, -10, 0, 0, 0, 0, 1, 0);
Glu.gluLookAt(cam.mPos.x, cam.mPos.y, cam.mPos.z,
cam.mView.x, cam.mView.y, cam.mView.z,
cam.mUp.x, cam.mUp.y, cam.mUp.z);
Gl.glPushMatrix();
//модели
Gl.glPopMatrix();
private void AnT_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
cam.Move_Camera(0.03f);
if (e.KeyCode == Keys.S)
cam.Move_Camera(-0.03f);
if (e.KeyCode == Keys.A)
cam.Rotate_Position(0.03f, 0, 1, 0);
if (e.KeyCode == Keys.D)
cam.Rotate_Position(-0.03f, 0, 1, 0);
label2.Text = e.KeyCode.ToString();
}
public struct Vector3D
{
public float x, y, z;
};
class CCamera
{
public Vector3D mPos;
public Vector3D mView;
public Vector3D mUp;
static float currentRotX;
static float lastRotX;
public void Rotate_Position(float angle, float x, float y, float z)
{
mPos.x = mPos.x - mView.x;
mPos.y = mPos.y - mView.y;
mPos.z = mPos.z - mView.z;
Vector3D vVector = mPos;
Vector3D AVector;
float SinA = (float)Math.Sin(angle);
float CosA = (float)Math.Cos(angle);
// Найдем новую позицию X для вращаемой точки
AVector.x = (CosA + (1 - CosA) * x * x) * vVector.x;
AVector.x += ((1 - CosA) * x * y - z * SinA) * vVector.y;
AVector.x += ((1 - CosA) * x * z + y * SinA) * vVector.z;
// Найдем позицию Y
AVector.y = ((1 - CosA) * x * y + z * SinA) * vVector.x;
AVector.y += (CosA + (1 - CosA) * y * y) * vVector.y;
AVector.y += ((1 - CosA) * y * z - x * SinA) * vVector.z;
// И позицию Z
AVector.z = ((1 - CosA) * x * z - y * SinA) * vVector.x;
AVector.z += ((1 - CosA) * y * z + x * SinA) * vVector.y;
AVector.z += (CosA + (1 - CosA) * z * z) * vVector.z;
mPos.x = mView.x + AVector.x;
mPos.y = mView.y + AVector.y;
mPos.z = mView.z + AVector.z;
}
public void Move_Camera(float speed)
{
Vector3D mmView;
mmView.x = mView.x - mPos.x;
mmView.y = mView.y - mPos.y;
mmView.z = mView.z - mPos.z;
Vector3D vVector = mmView; // Get the view vector
// Двигаем камеру со скоростью speed
mPos.x = mPos.x + vVector.x * speed;
mPos.z = mPos.z + vVector.z * speed;
mView.x = mView.x + vVector.x * speed;
mView.z = mView.z + vVector.z * speed;
}
public void Move()
{
mPos.x = mPos.x + 1;
mView.x = mView.x + 1;
}
public void Rotate_View(float speed)
{
Vector3D mmView;
mmView.x = mView.x - mPos.x;
mmView.y = mView.y - mPos.y;
mmView.z = mView.z - mPos.z;
Vector3D vVector = mmView; // Получаем вектор View
mView.z = (float)(mPos.z + Math.Sin(speed) * vVector.x + Math.Cos(speed) * vVector.z);
mView.x = (float)(mPos.x + Math.Cos(speed) * vVector.x - Math.Sin(speed) * vVector.z);
}
public void Position_Camera(float pos_x, float pos_y, float pos_z,
float view_x, float view_y, float view_z,
float up_x, float up_y, float up_z)
{
mPos.x = pos_x;
mPos.y = pos_y;
mPos.z = pos_z;
mView.x = view_x;
mView.y = view_y;
mView.z = view_z;
mUp.x = up_x;
mUp.y = up_y;
mUp.z = up_z;
}
};
Objects[] mObject = new Objects[999];intcreatedObjects;