1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Input;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tao.OpenGl;
using static System.Math;
using Tao.FreeGlut;
using Tao.Platform.Windows;
namespace TaoSchtein
{
public partial class Form1 : Form
{
const float PI = 3.141592F;
double x=0, y=0, z=0; //Положение камеры в пространстве
float angleX, angleY; // Углы поворота камеры
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
public Form1()
{
InitializeComponent();
AnT.InitializeContexts();
}
private void InitGL()
{
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); // отчитка окна
Gl.glClearColor(255, 255, 255, 1); // установка порта вывода в соответствии с размерами элемента anT
Gl.glViewport(0, 0, AnT.Width, AnT.Height); // настройка проекции
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluPerspective(90f, 1f, 1f, 500f);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Glut.glutEnterGameMode();
Gl.glLoadIdentity(); // настройка параметров OpenGL для визуализации Gl.glEnable(Gl.GL_DEPTH_TEST);
}
private void Form1_Load(object sender, EventArgs e)
{
InitGL();
}
private void mousemove()
{
POINT mousexy ;
mousexy.X = Cursor.Position.X;
mousexy.Y = Cursor.Position.Y;
int xt = AnT.Width/2;
int yt = AnT.Height/2;
angleX += (xt - mousexy.X) / 4;
angleY += (yt - mousexy.Y) / 4;
if (angleY < -89.0) { angleY = -89; }
if (angleY > 89.0) { angleY = 89; }
Cursor.Position = new Point(xt, yt);
}
private void camlook()
{
Glu.gluLookAt(x, y, z, x - Sin(angleX / 180 * PI), y + (Tan(angleY / 180 * PI)), z - Cos(angleX / 180 * PI), 0, 1, 0);
}
private void AnT_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
x -= (float)Sin(angleX / 180 * PI);
y = (float)Tan(angleY / 180 * PI);
z -= (float)Cos(angleX / 180 * PI);
}
if (e.KeyCode == Keys.S)
{
x += (float)Sin(angleX / 180 * PI);
y = -(float)Tan(angleY / 180 * PI);
z += (float)Cos(angleX / 180 * PI);
}
if (e.KeyCode == Keys.D)
{
x += (float)Sin((angleX + 90) / 180 * PI);
z += (float)Cos((angleX + 90) / 180 * PI);
}
if (e.KeyCode == Keys.A)
{
x += (float)Sin((angleX - 90) / 180 * PI);
z += (float)Cos((angleX - 90) / 180 * PI);
}
}
private void Draw()
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
camlook();
Gl.glColor3f(1.0f, 0, 0);
for(int i=0; i<10; i++)
for (int j=0; j<10; j++)
{
Gl.glPushMatrix();
Gl.glTranslated(j, 0, i);
// Gl.glRotated(90, 1, 0, 0);
Glut.glutWireCube(1);
Gl.glPopMatrix();
Gl.glFlush();
}
AnT.Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
mousemove();
Draw();
}
}
} |