/
Sun.cs
136 lines (125 loc) · 7.63 KB
/
Sun.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;
using SharpDX.Toolkit;
namespace Project1
{
using SharpDX.Toolkit.Graphics;
class Sun : ColoredGameObject
{
float x;
float yAngle;
float zAngle;
float sideLength;
float diameter;
Matrix projection;
public Sun(Game game, float x, float yAngle, float zAngle, float sideLength, float rotationSpeed, float diameter)
{
this.rotationSpeed = rotationSpeed;
this.x = x;
this.yAngle = yAngle;
this.zAngle = zAngle;
this.sideLength = sideLength;
this.diameter = diameter;
this.projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f,
(float)game.GraphicsDevice.BackBuffer.Width / game.GraphicsDevice.BackBuffer.Height, 0.1f, 1000.0f);
basicEffect = new BasicEffect(game.GraphicsDevice)
{
VertexColorEnabled = true,
LightingEnabled = false,
PreferPerPixelLighting = true,
View = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY),
Projection = projection
};
this.game = game;
BuildVertices();
}
public override void Update(GameTime gameTime, Matrix world, Matrix view)
{
var time = (float)gameTime.TotalGameTime.TotalSeconds;
basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = this.projection;
yAngle += rotationSpeed;
zAngle += rotationSpeed;
BuildVertices();
}
public override void Draw(GameTime gameTime)
{
// Setup the vertices
game.GraphicsDevice.SetVertexBuffer(vertices);
game.GraphicsDevice.SetVertexInputLayout(inputLayout);
// Apply the basic effect technique and draw the rotating cube
basicEffect.CurrentTechnique.Passes[0].Apply();
game.GraphicsDevice.Draw(PrimitiveType.TriangleList, vertices.ElementCount);
}
//this creates the sun (a cube) and assigns the color yellow
//the sun rotates with the same rotation speed as the light direction
private void BuildVertices()
{
float y = diameter * (float)Math.Sin(yAngle);
float z = diameter * (float)Math.Cos(zAngle) + diameter/2f;
Color color = Color.Yellow;
Vector3 frontBottomLeft = new Vector3(x, y, z);
Vector3 frontTopLeft = new Vector3(x, y + sideLength, z);
Vector3 frontTopRight = new Vector3(x + sideLength, y + sideLength, z);
Vector3 frontBottomRight = new Vector3(x + sideLength, y, z);
Vector3 backBottomLeft = new Vector3(x, y, z + sideLength);
Vector3 backBottomRight = new Vector3(x + sideLength, y, z + sideLength);
Vector3 backTopLeft = new Vector3(x, y + sideLength, z + sideLength);
Vector3 backTopRight = new Vector3(x + sideLength, y + sideLength, z + sideLength);
Vector3 frontBottomLeftNormal = new Vector3(-0.333f, -0.333f, -0.333f);
Vector3 frontTopLeftNormal = new Vector3(-0.333f, 0.333f, -0.333f);
Vector3 frontTopRightNormal = new Vector3(0.333f, 0.333f, -0.333f);
Vector3 frontBottomRightNormal = new Vector3(0.333f, -0.333f, -0.333f);
Vector3 backBottomLeftNormal = new Vector3(-0.333f, -0.333f, 0.333f);
Vector3 backBottomRightNormal = new Vector3(0.333f, -0.333f, 0.333f);
Vector3 backTopLeftNormal = new Vector3(-0.333f, 0.333f, 0.333f);
Vector3 backTopRightNormal = new Vector3(0.333f, 0.333f, 0.333f);
vertices = Buffer.Vertex.New(
game.GraphicsDevice,
new[]
{
new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, color), // Front
new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, color),
new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, color),
new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, color),
new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, color),
new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, color),
new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, color), // BACK
new VertexPositionNormalColor(backTopRight, backTopRightNormal, color),
new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, color),
new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, color),
new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, color),
new VertexPositionNormalColor(backTopRight, backTopRightNormal, color),
new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, color), // Top
new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, color),
new VertexPositionNormalColor(backTopRight, backTopRightNormal, color),
new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, color),
new VertexPositionNormalColor(backTopRight, backTopRightNormal, color),
new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, color),
new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, color), // Bottom
new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, color),
new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, color),
new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, color),
new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, color),
new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, color),
new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, color), // Left
new VertexPositionNormalColor(backBottomLeft, backBottomLeftNormal, color),
new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, color),
new VertexPositionNormalColor(frontBottomLeft, frontBottomLeftNormal, color),
new VertexPositionNormalColor(backTopLeft, backTopLeftNormal, color),
new VertexPositionNormalColor(frontTopLeft, frontTopLeftNormal, color),
new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, color), // Right
new VertexPositionNormalColor(backTopRight, backTopRightNormal, color),
new VertexPositionNormalColor(backBottomRight, backBottomRightNormal, color),
new VertexPositionNormalColor(frontBottomRight, frontBottomRightNormal, color),
new VertexPositionNormalColor(frontTopRight, frontTopRightNormal, color),
new VertexPositionNormalColor(backTopRight, backTopRightNormal, color),
});
inputLayout = VertexInputLayout.FromBuffer(0, vertices);
}
}
}