-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamera.cs
250 lines (227 loc) · 10.3 KB
/
Camera.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenGl;
using System.Drawing;
namespace GL3DLab
{
/// <summary>
/// Das Kameraobjekt verwaltet den Blickwinkel und die Position des "Betrachters"
/// </summary>
public class Camera
{
// Position
/// <summary>
/// Die X-Komponente
/// </summary>
public float X = 0;
/// <summary>
/// Die Y-Komponente
/// </summary>
public float Y = 0;
/// <summary>
/// Die Z-Komponente
/// </summary>
public float Z = 0;
// Blickwinkel
/// <summary>
/// Der Blickwinkel auf der X-Y-Ebene
/// </summary>
public float angleZ = 0;
/// <summary>
/// Der Blickwinkel in die Höhe
/// </summary>
public float angleX = 0;
/// <summary>
/// Der Blickwinkel wird um mx und mz Grad verändert.
/// </summary>
/// <param name="mz">Blickwinkel auf X-Y-Ebene wird um mz Grad verändert.</param>
/// <param name="mx">Blickwinkel nach "Oben" wird um mx Grad verändert.</param>
public void SetNewCameraPosition(double mz, double mx)
{
angleZ += (float)mz / 2;
if (angleZ > 360)
{
angleZ -= 360;
}
if (angleZ < 0)
{
angleZ += 360;
}
angleX -= (float)mx / 2;
if (angleX > 180)
{
angleX = 180;
}
if (angleX < 0)
{
angleX = 0;
}
}
/// <summary>
/// Cameraposition wird im OpenGl positioniert
/// </summary>
public void SetCamera()
{
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
// Blickrichtung
Gl.glRotatef(angleX, 1, 0, 0);
Gl.glRotatef(angleZ, 0, 0, 1);
//Position
Gl.glTranslatef(X, Y, Z);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
}
}
/// <summary>
/// Die Skybox umgibt die Kamera mit einer nicht Z-Gepufferten Textur.
/// </summary>
class Skybox
{
/// <summary>
/// Soll Skybox gezeichnet werden?
/// </summary>
public bool DoDrawSkybox { get; set; }
/// <summary>
/// Enthält Indizes für die Texturen
/// </summary>
private uint[] _skybox = new uint[6];
/// <summary>
/// Die einzelnen Textruren werden hier geladen.
/// </summary>
/// <param name="filename">Dateiname der Textur</param>
/// <param name="nr">Index der Textur</param>
private void InternalInitTextur(string filename, int nr)
{
Bitmap image = new Bitmap(filename);
image.RotateFlip(RotateFlipType.RotateNoneFlipY);
System.Drawing.Imaging.BitmapData bitmapdata;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[nr]);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_NEAREST);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
image.UnlockBits(bitmapdata);
image.Dispose();
}
/// <summary>
/// Texturen werden den Indizes zugeordnet und geladen
/// </summary>
public void InitGLTexture()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // black background
Gl.glClearDepth(1.0f); // depth buffer setup
Gl.glDepthFunc(Gl.GL_LEQUAL); // type of depth testing
Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); // nice perspective calculations
Gl.glGenTextures(6, _skybox);
InternalInitTextur("RT.jpg", 0);
InternalInitTextur("DN.jpg", 1);
InternalInitTextur("UP.jpg", 2);
InternalInitTextur("FR.jpg", 3);
InternalInitTextur("LF.jpg", 4);
InternalInitTextur("BK.jpg", 5);
DoDrawSkybox = true;
}
/// <summary>
/// Zeichenroutine für die Skybox
/// </summary>
/// <param name="c">Das Kameraobjekt um Ausrichtung im Raum zu ermitteln.</param>
public void DrawSky(Camera c)
{
if (DoDrawSkybox)
{
float dist = 0.5f;
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
Gl.glPushAttrib(Gl.GL_ENABLE_BIT);
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glDisable(Gl.GL_DEPTH_TEST);
Gl.glDisable(Gl.GL_LIGHTING);
Gl.glDisable(Gl.GL_BLEND);
Gl.glRotatef(c.angleX, 1.0f, 0.0f, 0.0f); // rotate on the X-axis
Gl.glRotatef(c.angleZ, 0.0f, 0.0f, 1.0f); // rotate on the Z-axis
Gl.glColor4f(1f, 1f, 1f, 1f);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[1]);
Gl.glBegin(Gl.GL_QUADS);
// Unten
Gl.glTexCoord2f(1.0f, 1.0f); // top right of texture
Gl.glVertex3f(dist, dist, dist); // top right of quad
Gl.glTexCoord2f(0.0f, 1.0f); // top left of texture
Gl.glVertex3f(-dist, dist, dist); // top left of quad
Gl.glTexCoord2f(0.0f, 0.0f); // bottom left of texture
Gl.glVertex3f(-dist, -dist, dist); // bottom left of quad
Gl.glTexCoord2f(1.0f, 0.0f); // bottom right of texture
Gl.glVertex3f(dist, -dist, dist); // bottom right of quad
Gl.glEnd();
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[2]);
Gl.glBegin(Gl.GL_QUADS);
// Ground
Gl.glTexCoord2f(1.0f, 1.0f); // top right of texture
Gl.glVertex3f(-dist, dist, -dist); // top right of quad
Gl.glTexCoord2f(0.0f, 1.0f); // top left of texture
Gl.glVertex3f(dist, dist, -dist); // top left of quad
Gl.glTexCoord2f(0.0f, 0.0f); // bottom left of texture
Gl.glVertex3f(dist, -dist, -dist); // bottom left of quad
Gl.glTexCoord2f(1.0f, 0.0f); // bottom right of texture
Gl.glVertex3f(-dist, -dist, -dist); // bottom right of quad
Gl.glEnd();
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[3]);
Gl.glBegin(Gl.GL_QUADS);
// Up
Gl.glTexCoord2f(1.0f, 1.0f); // top right of texture
Gl.glVertex3f(dist, dist, -dist); // top right of quad
Gl.glTexCoord2f(0.0f, 1.0f); // top left of texture
Gl.glVertex3f(-dist, dist, -dist); // top left of quad
Gl.glTexCoord2f(0.0f, 0.0f); // bottom left of texture
Gl.glVertex3f(-dist, dist, dist); // bottom left of quad
Gl.glTexCoord2f(1.0f, 0.0f); // bottom right of texture
Gl.glVertex3f(dist, dist, dist); // bottom right of quad
Gl.glEnd();
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[4]);
Gl.glBegin(Gl.GL_QUADS);
// Right Face
Gl.glTexCoord2f(1.0f, 1.0f); // top right of texture
Gl.glVertex3f(dist, dist, -dist); // top right of quad
Gl.glTexCoord2f(0.0f, 1.0f); // top left of texture
Gl.glVertex3f(dist, dist, dist); // top left of quad
Gl.glTexCoord2f(0.0f, 0.0f); // bottom left of texture
Gl.glVertex3f(dist, -dist, dist); // bottom left of quad
Gl.glTexCoord2f(1.0f, 0.0f); // bottom right of texture
Gl.glVertex3f(dist, -dist, -dist); // bottom right of quad
Gl.glEnd();
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[5]);
Gl.glBegin(Gl.GL_QUADS);
// Left Face
Gl.glTexCoord2f(0.0f, 1.0f); // top right of texture
Gl.glVertex3f(dist, -dist, dist); // top right of quad
Gl.glTexCoord2f(0.0f, 0.0f); // top left of texture
Gl.glVertex3f(-dist, -dist, dist); // top left of quad
Gl.glTexCoord2f(1.0f, 0.0f); // bottom left of texture
Gl.glVertex3f(-dist, -dist, -dist); // bottom left of quad
Gl.glTexCoord2f(1.0f, 1.0f); // bottom right of texture
Gl.glVertex3f(dist, -dist, -dist); // bottom right of quad
Gl.glEnd();
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _skybox[0]);
Gl.glBegin(Gl.GL_QUADS);
// Left Face
Gl.glTexCoord2f(1.0f, 1.0f); // top right of texture
Gl.glVertex3f(-dist, dist, dist); // top right of quad
Gl.glTexCoord2f(0.0f, 1.0f); // top left of texture
Gl.glVertex3f(-dist, dist, -dist); // top left of quad
Gl.glTexCoord2f(0.0f, 0.0f); // bottom left of texture
Gl.glVertex3f(-dist, -dist, -dist); // bottom left of quad
Gl.glTexCoord2f(1.0f, 0.0f); // bottom right of texture
Gl.glVertex3f(-dist, -dist, dist); // bottom right of quad
Gl.glEnd();
Gl.glPopAttrib();
Gl.glPopMatrix();
}
}
}
}