Ejemplo n.º 1
0
 private static void OnClose()
 {
     star.Dispose();
     starUV.Dispose();
     starQuads.Dispose();
     starTexture.Dispose();
     program.DisposeChildren = true;
     program.Dispose();
     fontProgram.DisposeChildren = true;
     fontProgram.Dispose();
     font.FontTexture.Dispose();
     information.Dispose();
 }
Ejemplo n.º 2
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // perform rotation of the scene depending on keyboard input
            if (right)
            {
                phi += deltaTime;
            }
            if (left)
            {
                phi -= deltaTime;
            }
            if (up)
            {
                theta += deltaTime;
            }
            if (down)
            {
                theta -= deltaTime;
            }
            if (theta < 0)
            {
                theta += (float)Math.PI * 2;
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // make sure the shader program and texture are being used
            Gl.UseProgram(program.ProgramID);
            Gl.BindTexture(starTexture);

            // calculate the camera position using some fancy polar co-ordinates
            Vector3 position = 20 * new Vector3((float)(Math.Cos(phi) * Math.Sin(theta)), (float)Math.Cos(theta), (float)(Math.Sin(phi) * Math.Sin(theta)));
            Vector3 upVector = ((theta % (Math.PI * 2)) > Math.PI) ? new Vector3(0, 1, 0) : new Vector3(0, -1, 0);

            program["view_matrix"].SetValue(Matrix4.LookAt(position, Vector3.Zero, upVector));

            // loop through the stars, drawing each one
            for (int i = 0; i < stars.Count; i++)
            {
                // set the position and color of this star
                program["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(stars[i].dist, 0, 0)) * Matrix4.CreateRotationZ(stars[i].angle));
                program["color"].SetValue(stars[i].color);

                Gl.BindBufferToShaderAttribute(star, program, "vertexPosition");
                Gl.BindBufferToShaderAttribute(starUV, program, "vertexUV");
                Gl.BindBuffer(starQuads);

                Gl.DrawElements(BeginMode.Triangles, starQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

                // update the position of the star
                stars[i].angle += (float)i / stars.Count * deltaTime * 2;
                stars[i].dist  -= 0.2f * deltaTime;

                // if we've reached the center then move this star outwards and give it a new color
                if (stars[i].dist < 0f)
                {
                    stars[i].dist += 5f;
                    stars[i].color = new Vector3((float)generator.NextDouble(), (float)generator.NextDouble(), (float)generator.NextDouble());
                }
            }

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // build this string every frame, since theta and phi can change
            FontVAO vao = font.CreateString(fontProgram, string.Format("Theta:   {0:0.000},  Phi:   {1:0.000}", theta, phi), BMFont.Justification.Right);

            vao.Position = new Vector2(width / 2 - 10, height / 2 - font.Height - 10);
            vao.Draw();
            vao.Dispose();

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }