Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
180 lines (170 loc) · 7 KB
/
Copy pathProgram.cs
File metadata and controls
180 lines (170 loc) · 7 KB
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
using PixelEngine;
using PixelEngine.Utilities;
using System;
namespace PX_Vox
{
public class PXVox : Game
{
private Pixel fog;
private Sprite terrainColor;
private Vector PlayerPos;
private Vector mouseBuffer;
private Vector moveDelta;
private float phi, theta;
private float terrainScale = 128;
private bool enableFog = true;
private float CurrentFramesPerSecond;
private const float movSpeed = 64.0f;
private const int resolutionDemult = 4;
public override void OnCreate()
{
terrainColor = Sprite.Load("level.png");
PixelMode = Pixel.Mode.Normal;
fog = new Pixel(135, 181, 255,255);
PlayerPos = new Vector(256,256,128);
moveDelta = new Vector(0, 0, 0);
terrainScale /= resolutionDemult;
}
//Program entrypoint
static void Main()
{
PXVox rp = new PXVox();
rp.Construct(1024/resolutionDemult, 720/resolutionDemult, resolutionDemult, resolutionDemult);
rp.Start();
}
//Input keys
public override void OnKeyDown(Key k)
{
switch (k)
{
case Key.A:
moveDelta += -Vector.Right;
break;
case Key.D:
moveDelta += Vector.Right;
break;
case Key.W:
moveDelta += -Vector.Up;
break;
case Key.S:
moveDelta += Vector.Up;
break;
case Key.Shift:
moveDelta += -Vector.Forward;
break;
case Key.Space:
moveDelta += Vector.Forward;
break;
case Key.Z:
enableFog = !enableFog;
Delay(0.25f);
break;
case Key.Escape:
Finish();
break;
}
}
public override void OnUpdate(float elapsed)
{
//Let's move the player
Vector movVec = SteerPlayer(phi);
Vector mouseVec = MouseDelta(new Vector(MouseX, MouseY));
PlayerPos += (movVec * (float)Clock.Elapsed.TotalSeconds);
phi += (mouseVec.X) * 0.05f;
theta += (mouseVec.Y) * 4.0f;
phi = phi % 6.28318f;
//Clear the screen with the fog color and start drawing the terrain
Clear(fog);
RenderTerrain(PlayerPos,-phi,PlayerPos.Z,-theta,terrainScale,512);
//Draw GUI
DrawLine(new Point(ScreenWidth/2, ScreenHeight/2), new Point((ScreenWidth / 2) + ((int)movVec.X/2), (ScreenHeight / 2) + ((int)movVec.Y / 2)), Pixel.Presets.Red);
DrawText(new Point(0,0), CurrentFramesPerSecond.ToString("0.0") + " FPS", Pixel.Presets.Black, 1);
DrawText(new Point(0,8), "FOG:" + enableFog.ToString(), Pixel.Presets.Black, 1);
DrawText(new Point(0,16), "X:" + PlayerPos.X.ToString("0.0") + " Y:" + PlayerPos.Y.ToString("0.0") + " Z:" + PlayerPos.Z.ToString("0.0"), Pixel.Presets.Black, 1);
DrawText(new Point(0,24),"PHI:" + phi.ToString("0.0") + " THETA:" + theta.ToString("0.0"), Pixel.Presets.Black, 1);
DrawText(new Point(0,32), "dX:" + mouseVec.X.ToString() + " dY:" + mouseVec.Y.ToString(), Pixel.Presets.Black, 1);
UpdateFrames(elapsed);
moveDelta = Vector.Zero;
}
public void RenderTerrain(Vector p, float phi, float height, float horizon, float scale_height, int distance)
{
//Precalculate angles
float sinphi = Sin(phi);
float cosphi = Cos(phi);
//Buffer for previously drawn column heights
float[] ybuffer = new float[ScreenWidth];
ArrayTools.ArrayFill(ybuffer, ScreenHeight);
float dz = 1;
float z = 0;
//Draw from front to back
while (z<distance)
{
Vector pleft = new Vector((-cosphi * z - sinphi * z) + p.X,(sinphi * z - cosphi * z) + p.Y);
Vector pright = new Vector((cosphi * z - sinphi * z) + p.X,(-sinphi * z - cosphi * z) + p.Y);
float dx = (pright.X - pleft.X) / ScreenWidth;
float dy = (pright.Y - pleft.Y) / ScreenWidth;
//Draw columns from left to right, discard overdrawn columns
for (int i = 0; i < ScreenWidth; ++i)
{
float heightOnScreen = (height - terrainColor[Math.Abs((int)pleft.X%terrainColor.Width),Math.Abs((int)pleft.Y%terrainColor.Height)].A) / z * scale_height + horizon;
//Calculate fog
Pixel px = (enableFog) ? Pixel.Mix(terrainColor[Math.Abs((int)pleft.X%terrainColor.Width), Math.Abs((int)pleft.Y%terrainColor.Height)],fog, (float)z/distance): terrainColor[Math.Abs((int)pleft.X % terrainColor.Width), Math.Abs((int)pleft.Y % terrainColor.Height)];
DrawTerrainChunk(new Point(i, (int)heightOnScreen), new Point(i, (int)ybuffer[i]), px);
if (heightOnScreen < ybuffer[i])
{
ybuffer[i] = heightOnScreen;
}
pleft = new Vector(pleft.X + dx, pleft.Y + dy);
}
z += dz;
dz += 0.005f;
}
}
public NumberField PopulateHeightmap(Sprite spr)
{
NumberField hmap = new NumberField(spr.Width+1, spr.Height+1,255.0f, 0.0f);
for (int y = 0; y < spr.Width; y++)
{
for (int x = 0; x < spr.Width; x++)
{
hmap.Field[x, y] = spr[x,y].A;
}
}
return hmap;
}
public void DrawTerrainChunk(Point top, Point bottom, Pixel px)
{
if (top.Y > bottom.Y)
return;
else
DrawLine(top, bottom, px);
}
public void UpdateFrames(float deltaTime)
{
CurrentFramesPerSecond = 1.0f / deltaTime;
}
public Vector MouseDelta(Vector mousePos)
{
Vector mpos = mousePos;
Vector output = mpos - mouseBuffer;
mouseBuffer = mpos;
return output;
}
public Vector SteerPlayer(float phi)
{
Vector output = Rotate(moveDelta, phi, this);
if (output.Equals(new Vector(0, 0, 0)))
return output;
else
return output.Normalize() * movSpeed;
}
public static Vector Rotate(Vector v, float radians, PXVox rp)
{
float sin = rp.Sin(radians);
float cos = rp.Cos(radians);
float tx = v.X;
float ty = v.Y;
return new Vector(cos * tx - sin * ty, sin * tx + cos * ty, v.Z);
}
}
}