-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.cs
300 lines (252 loc) · 8.73 KB
/
Renderer.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Tao.FreeGlut;
using Tao.OpenGl;
namespace Fractals
{
public abstract class Renderer
{
public class Texture
{
Bitmap bitmap;
uint textureName;
public Bitmap Bitmap {
get {
return bitmap;
}
set {
bitmap = value;
}
}
public uint TextureName {
get {
return textureName;
}
set {
textureName = value;
}
}
}
// Render target
Fractal fractal;
Graphics graphics;
Rectangle renderRectangle;
// Multi-threading
TimeSpan userThreadInterval = TimeSpan.FromMilliseconds(10);
DateTime lastUserThreadActionTime;
public event EventHandler UserThreadAction;
bool aborted = false;
// Caching
DataTree data = new DataTree();
BitmapCache bitmapCache;
RenderSet renderSet;
// Movement
public PointF mousePosition;
public MouseButtons mouseButtons;
// Real-time rendering
double convergance = 0.2d;
double numberOfFragmentsToRender = 16;
int numberOfFragmentsRendered;
TimeSpan targetRenderTime = TimeSpan.FromMilliseconds(1d / 20);
TimeSpan actualRenderTime;
double pixelSize;
public Fractal Fractal {
get {
return fractal;
}
set {
fractal = value;
aborted = false;
}
}
public bool Aborted {
get {
return aborted;
}
}
public int NumberOfFragmentsRendered {
get {
return numberOfFragmentsRendered;
}
}
public TimeSpan TargetRenderTime {
get {
return targetRenderTime;
}
set {
targetRenderTime = value;
}
}
public TimeSpan ActualRenderTime {
get {
return actualRenderTime;
}
}
public double FPS {
get {
return (double)TimeSpan.TicksPerSecond / actualRenderTime.Ticks;
}
}
public Renderer(Fractal fractal, Graphics graphics, Rectangle renderRectangle)
{
this.fractal = fractal;
this.graphics = graphics;
this.renderRectangle = renderRectangle;
this.bitmapCache = new BitmapCache(this);
}
public void Abort()
{
aborted = true;
}
public Texture MakeTexture(int width, int height)
{
Texture tex = new Texture();
tex.Bitmap = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
uint textureName;
Gl.glGenTextures(1, out textureName);
tex.TextureName = textureName;
Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureName);
uint[,] image = new uint[width, height];
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
image[x,y] = 0xFFFF00FF; // Purple
}
}
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.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_NEAREST);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, width, height, 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, image);
return tex;
}
public unsafe void UpdateTexture(Texture tex, int dstX, int dstY, uint[,] image)
{
Bitmap bitmap = new Bitmap(Fragment.BitmapSize, Fragment.BitmapSize, PixelFormat.Format32bppRgb);
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, Fragment.BitmapSize, Fragment.BitmapSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
UInt32* ptr = (UInt32*) bmpData.Scan0.ToPointer();
for(int y = 0; y < Fragment.BitmapSize; y += 1) {
for(int x = 0; x < Fragment.BitmapSize; x += 1) {
if (x == Fragment.FragmentSize) {
*ptr = *(ptr-1); ptr++;
} else if (y == Fragment.FragmentSize) {
*ptr = *(ptr-Fragment.BitmapSize); ptr++;
} else {
*ptr = image[x,y]; ptr++;
}
}
}
// ptr--; *ptr = 0x000000FF; ptr++;
bitmap.UnlockBits(bmpData);
using(Graphics g = Graphics.FromImage(tex.Bitmap)) {
g.DrawImage(bitmap, dstX, dstY);
}
Gl.glBindTexture(Gl.GL_TEXTURE_2D, tex.TextureName);
Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, dstX, dstY, Fragment.BitmapSize, Fragment.BitmapSize, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, image);
}
BitmapCacheItem UpdateBitmap(Fragment f)
{
if (bitmapCache.IsCached(f)) {
return bitmapCache[f];
} else {
BitmapCacheItem c = bitmapCache.AllocateCache(f);
UpdateTexture(c.Texture, c.X, c.Y, f.MakeImage(fractal.ColorMap));
return c;
}
}
void Render(IEnumerable<RenderOperation> operations)
{
foreach (RenderOperation operation in operations) {
Render(operation);
if (aborted) break;
}
}
void Render(RenderOperation operation)
{
if (HighPrecisionTimer.Now - lastUserThreadActionTime > userThreadInterval) {
lastUserThreadActionTime = HighPrecisionTimer.Now;
if (UserThreadAction != null) {
UserThreadAction(this, EventArgs.Empty);
}
}
if (aborted) return;
operation.Fragment.SetColorIndexes(fractal, operation.DataSource.X, operation.DataSource.Y, operation.DataSource.Size / Fragment.FragmentSize);
RenderOperation largeEnoughRenderOperation = operation;
while (largeEnoughRenderOperation.TexelSize < pixelSize && operation.Parent != null) {
largeEnoughRenderOperation = largeEnoughRenderOperation.Parent;
bitmapCache.ReleaseCache(largeEnoughRenderOperation.Fragment);
}
BitmapCacheItem c = UpdateBitmap(largeEnoughRenderOperation.Fragment);
Draw(c.Texture, c.TextureSourceRect, largeEnoughRenderOperation.RenderDestination);
}
protected virtual void Draw(Texture tex, RectangleF src, RectangleD dest)
{
graphics.DrawImage(tex.Bitmap,
new PointF[] {dest.LeftTopCornerF,
dest.RightTopCornerF,
dest.LeftBottomCornerF},
src,
GraphicsUnit.Pixel);
}
public void Render()
{
DateTime startTime = HighPrecisionTimer.Now;
while(data.Size < fractal.View.BoundingBoxSize * 2) {
data.ExtendRoot();
}
Matrix rotation = new Matrix();
rotation.Rotate((float)fractal.View.CurrentAngle);
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.Bilinear;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.ResetTransform();
graphics.MultiplyTransform(rotation, MatrixOrder.Append);
// From [-1,1] to [0,w]
graphics.TranslateTransform(1, 1, MatrixOrder.Append);
graphics.ScaleTransform(renderRectangle.Width / 2, renderRectangle.Height / 2, MatrixOrder.Append);
RectangleD renderArea = new RectangleD((-fractal.View.Xpos + -data.Size / 2) * fractal.View.Xzoom,
(-fractal.View.Ypos + -data.Size / 2) * fractal.View.Xzoom,
fractal.View.Xzoom * data.Size,
fractal.View.Xzoom * data.Size);
pixelSize = Math.Min(2d / renderRectangle.Width, 2d / renderRectangle.Height);
RenderOperation root = new RenderOperation(null, data.Root, data.Area, renderArea, rotation);
renderSet = new RenderSet(root, (int)numberOfFragmentsToRender);
Render(renderSet.RenderOperations);
numberOfFragmentsRendered = renderSet.Count;
actualRenderTime = HighPrecisionTimer.Now - startTime;
double timeFraction = (double)actualRenderTime.Ticks / (double)targetRenderTime.Ticks;
double optimalCount = (double)numberOfFragmentsToRender / timeFraction;
numberOfFragmentsToRender = numberOfFragmentsToRender * (1 - convergance) + optimalCount * convergance;
numberOfFragmentsToRender = Math.Max(16d, numberOfFragmentsToRender);
}
public void HighQualityRender()
{
Render();
Render(renderSet.MakeRefineOperations(pixelSize));
}
public void Rotate(int direction)
{
fractal.View.Angle += direction * 10;
}
public void Move()
{
if (fractal.View.Rotating) {
fractal.View.AnimateRotation();
}
if (mouseButtons != MouseButtons.None) {
double zoomSpeed = 1;
if (mouseButtons == MouseButtons.Left) zoomSpeed = 1+8f/128;
if (mouseButtons == MouseButtons.Middle) zoomSpeed = 1;
if (mouseButtons == MouseButtons.Right) zoomSpeed = 1-8f/128;
PointF logicalPos = new PointF(); // [-1,1] mapping
logicalPos.X = (mousePosition.X - 0.5f) * 2f / 8f;
logicalPos.Y = (mousePosition.Y - 0.5f) * 2f / 8f;
fractal.View.ZoomIn(logicalPos, zoomSpeed);
}
}
}
}