@@ -18,11 +18,7 @@ public class Game : OpenGLXRGame
1818 private static BufferObject < float > Vbo ;
1919 private static BufferObject < uint > Ebo ;
2020 private static VertexArrayObject < float , uint > VaoCube ;
21- private static Shader LightingShader ;
22- private static Shader LampShader ;
23- private static Vector3 CubePosition = new ( 0.2f , 0.0f , 1.0f ) ;
24- private static Vector3 LampPosition = new ( 1.2f , 1.0f , 2.0f ) ;
25-
21+ private static Shader CubesShader ;
2622 private static Camera Camera ;
2723 private static Vector2 LastOrientation ;
2824
@@ -95,13 +91,10 @@ protected override void Load()
9591 VaoCube . VertexAttributePointer ( 0 , 3 , VertexAttribPointerType . Float , 6 , 0 ) ;
9692 VaoCube . VertexAttributePointer ( 1 , 3 , VertexAttribPointerType . Float , 6 , 3 ) ;
9793
98- //The lighting shader will give our main cube it's colour multiplied by the light's intensity
99- LightingShader = new ( Gl , "shader.vert" , "lighting.frag" ) ;
100- //The Lamp shader uses a fragment shader that just colours it solid white so that we know it is the light source
101- LampShader = new ( Gl , "shader.vert" , "shader.frag" ) ;
94+ CubesShader = new ( Gl , "cubes.vert" , "cubes.frag" ) ;
10295
10396 //Start a camera at position 3 on the Z axis, looking at position -1 on the Z axis
104- Camera = new ( Vector3 . UnitZ * 6 * 6 , Renderer ) ;
97+ Camera = new ( Vector3 . One + Vector3 . One , Renderer ) ;
10598 }
10699
107100 protected override void Update ( double delta )
@@ -116,47 +109,39 @@ protected override void Render(int eye, double delta)
116109 Gl . Clear ( ( uint ) ( ClearBufferMask . ColorBufferBit | ClearBufferMask . DepthBufferBit ) ) ;
117110
118111 VaoCube . Bind ( ) ;
119- LightingShader . Use ( ) ;
112+ CubesShader . Use ( ) ;
120113
121- // Define the matrix for our cube
122- var cubeMatrix = Matrix4x4 . Identity ;
123- cubeMatrix *= Matrix4x4 . CreateTranslation ( CubePosition ) ;
114+ CubesShader . SetUniform ( "uniformColor" , Vector3 . Zero ) ;
115+ CubesShader . SetUniform ( "view" , Matrix4x4 . Transpose ( Camera . GetViewMatrix ( eye ) ) ) ;
116+ CubesShader . SetUniform ( "proj" , Matrix4x4 . Transpose ( Camera . GetProjectionMatrix ( eye ) ) ) ;
124117
125- // Slightly rotate the cube to give it an angled face to look at
126- cubeMatrix *= Matrix4x4 . CreateRotationY ( Scalar . DegreesToRadians ( 25f ) ) ;
127-
128- //LightingShader.SetUniform("uModel", Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(25f)));
129- LightingShader . SetUniform ( "uModel" , cubeMatrix ) ;
130- LightingShader . SetUniform ( "uView" , Camera . GetViewMatrix ( eye ) ) ;
131- LightingShader . SetUniform ( "uProjection" , Camera . GetProjectionMatrix ( eye ) ) ;
132- LightingShader . SetUniform ( "objectColor" , new Vector3 ( 1.0f , 0.5f , 0.31f ) ) ;
133- LightingShader . SetUniform ( "lightColor" , Vector3 . One ) ;
134- LightingShader . SetUniform ( "lightPos" , LampPosition ) ;
135- LightingShader . SetUniform ( "viewPos" , Camera . Position ) ;
136-
137- // We're drawing with just vertices and no indicies, and it takes 36 verticies to have a six-sided textured cube
138- Gl . DrawArrays ( PrimitiveType . Triangles , 0 , 36 ) ;
139-
140- LampShader . Use ( ) ;
141-
142- //The Lamp cube is going to be a scaled down version of the normal cubes verticies moved to a different screen location
143- var lampMatrix = Matrix4x4 . Identity ;
144- lampMatrix *= Matrix4x4 . CreateScale ( 0.2f ) ;
145- lampMatrix *= Matrix4x4 . CreateTranslation ( LampPosition ) ;
146-
147- LampShader . SetUniform ( "uModel" , lampMatrix ) ;
148- LampShader . SetUniform ( "uView" , Camera . GetViewMatrix ( eye ) ) ;
149- LampShader . SetUniform ( "uProjection" , Camera . GetProjectionMatrix ( eye ) ) ;
150-
151- Gl . DrawArrays ( PrimitiveType . Triangles , 0 , 36 ) ;
118+ const float rotations_per_sec = .25f ;
119+
120+ float angle = ( ( long ) ( delta * 360f * rotations_per_sec ) ) % 360f ;
121+
122+ float dist = 1.5f ;
123+ float height = 0.5f ;
124+ DrawRotatedCube ( new ( 0 , height , - dist ) , .33f , angle ) ;
125+ DrawRotatedCube ( new ( 0 , height , dist ) , .33f , angle ) ;
126+ DrawRotatedCube ( new ( dist , height , 0 ) , .33f , angle ) ;
127+ DrawRotatedCube ( new ( - dist , height , 0 ) , .33f , angle ) ;
128+ }
129+
130+ private void DrawRotatedCube ( Vector3 position , float cube_size , float rotation )
131+ {
132+ var modelmatrix = Matrix4x4 . CreateTranslation ( position )
133+ * Matrix4x4 . CreateScale ( new Vector3 ( cube_size / 2f , cube_size / 2f , cube_size / 2f ) ) ;
134+ modelmatrix *= Matrix4x4 . CreateRotationY ( Scalar . DegreesToRadians ( rotation ) ) ;
135+ CubesShader . SetUniform ( "model" , Matrix4x4 . Transpose ( modelmatrix ) ) ;
136+ Gl . DrawArrays ( GLEnum . Triangles , 0 , 36 ) ;
152137 }
153138
154139 protected override void Unload ( )
155140 {
156141 Vbo . Dispose ( ) ;
157142 Ebo . Dispose ( ) ;
158143 VaoCube . Dispose ( ) ;
159- LightingShader . Dispose ( ) ;
144+ CubesShader . Dispose ( ) ;
160145 }
161146 }
162147}
0 commit comments