Пример #1
0
        protected override void OnLoad()
        {
            base.OnLoad();
            // initialize shader (load sources, create/compile/link shader program, error checking)
            // when using the factory method the shader sources are retrieved from the ShaderSourceAttributes
            _program = ProgramFactory.Create <SimpleColorProgram>();
            // this program will be used all the time so just activate it once and for all
            _program.Use();


            //arbitrary black box to load mesh data
            string   jsonString = File.ReadAllText("./Data/Meshes/OpenTK.fakeformat");
            MeshData meshData   = JsonSerializer.Deserialize <MeshData>(jsonString);


            Buffer <uint> IndexBuffer = new Buffer <uint>();

            IndexBuffer.Init(BufferTarget.ElementArrayBuffer, meshData.Indices.Select(index => index - 1).ToArray());

            Buffer <Vector3> VertBuffer = new Buffer <Vector3>();

            VertBuffer.Init(BufferTarget.ArrayBuffer, Enumerable.Range(0, meshData.Vertices.Count / 3).Select(a => new Vector3(meshData.Vertices[a * 3], meshData.Vertices[a * 3 + 1], meshData.Vertices[a * 3 + 2])).ToArray());

            //a bit of a hack, i wanted the mesh to have some visual depth.
            //the only reason this works is I just happen to know the Z coordinate for the mesh is in a certain range
            //other meshes will either look stupid or just throw exceptions because the color values are out of range
            Buffer <uint> ColorBuffer = new Buffer <uint>();

            ColorBuffer.Init(BufferTarget.ArrayBuffer, VertBuffer.Content.Select(vertex => (uint)Color.FromArgb((int)(vertex.Z * 500) + 100, (int)(vertex.Z * 500) + 100, (int)(vertex.Z * 500) + 100).ToArgb()).ToArray());


            mesh = new DynamicShape()
                   .WithVertexAttrib(_program.InPosition, VertBuffer)
                   .WithVertexAttrib(_program.InColor, ColorBuffer)
                   .WithElementBuffer(IndexBuffer)
                   .WithDisposeFunction(() => {
                VertBuffer?.Dispose();
                IndexBuffer?.Dispose();
                ColorBuffer?.Dispose();
            })
                   .SetDrawFunction((VAO) => {
                VAO.DrawElements(PrimitiveType.Triangles, IndexBuffer.ElementCount);
            });

            // set camera position
            ActiveCamera.Position = new Vector3(0, 0, 3);

            // set a nice clear color
            GL.ClearColor(Color.MidnightBlue);

            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
        }
Пример #2
0
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            // set up render to texture
            _framebuffer.Bind(FramebufferTarget.Framebuffer);
            GL.Viewport(0, 0, FramebufferWidth, FramebufferHeight);
            GL.ClearColor(Color.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // render rotating cube to texture
            _colorProgram.Use();
            _colorProgram.ModelViewProjectionMatrix.Set(
                Matrix4.CreateRotationX((float)FrameTimer.TimeRunning / 1000)
                * Matrix4.CreateRotationY((float)FrameTimer.TimeRunning / 1000)
                * Matrix4.CreateTranslation(0, 0, -5)
                * Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, FramebufferWidth / (float)FramebufferHeight, 0.1f, 100));

            _cube.Draw();

            // reset to default framebuffer
            Framebuffer.Unbind(FramebufferTarget.Framebuffer);

            // set up viewport for the window
            GL.Viewport(0, 0, Size.X, Size.Y);
            GL.ClearColor(Color.MidnightBlue);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // render quad with texture
            _textureProgram.Use();
            _textureProgram.ModelViewProjectionMatrix.Set(ActiveCamera.ViewProjectionMatrix);

            _quad.Draw();

            // swap buffers
            SwapBuffers();
        }
Пример #3
0
        private void OnRenderFrame(object sender, FrameEventArgs e)
        {
            // set up render to texture
            _framebuffer.Bind(FramebufferTarget.Framebuffer);
            GL.Viewport(0, 0, FramebufferWidth, FramebufferHeight);
            GL.ClearColor(Color.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // render rotating cube to texture
            _colorProgram.Use();
            _colorProgram.ModelViewProjectionMatrix.Set(
                Matrix4.CreateRotationX((float)FrameTimer.TimeRunning / 1000)
                * Matrix4.CreateRotationY((float)FrameTimer.TimeRunning / 1000)
                * Matrix4.CreateTranslation(0, 0, -5)
                * Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, FramebufferWidth / (float)FramebufferHeight, 0.1f, 100));
            _cubeVao.Bind();
            _cubeVao.DrawElements(PrimitiveType.Triangles, _cube.IndexBuffer.ElementCount);

            // reset to default framebuffer
            Framebuffer.Unbind(FramebufferTarget.Framebuffer);

            // set up viewport for the window
            GL.Viewport(0, 0, Width, Height);
            GL.ClearColor(Color.MidnightBlue);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            SetupPerspective();

            // render quad with texture
            _textureProgram.Use();
            _textureProgram.ModelViewProjectionMatrix.Set(ModelView * Projection);
            _quadVao.Bind();
            _quadVao.DrawArrays(PrimitiveType.TriangleStrip, 0, _quad.VertexBuffer.ElementCount);

            // swap buffers
            SwapBuffers();
        }