Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
openTK c#漫游立方体示例_C#_Opengl_Opentk - Fatal编程技术网

openTK c#漫游立方体示例

openTK c#漫游立方体示例,c#,opengl,opentk,C#,Opengl,Opentk,我正在使用C#(visual studio 2008)开发老虎机, 使用OpenTK在我的项目中使用openGL 我们已经完成了基本的功能,但是我们不能得到一个代码来绘制一个可以旋转的立方体 cude需要旋转一段时间,但我不知道怎么做 我们已经试过了 但是我们想把它画在一个窗体上OpenTK提供了一个名为GLControl的Windows窗体控件。它没有内置的主循环,但是如果您遵循,那么让连续渲染在控件上工作并不难 连续渲染工作完成后,可以移动代码以渲染立方体,立方体将在窗体上连续旋转。Open

我正在使用C#(visual studio 2008)开发老虎机, 使用OpenTK在我的项目中使用openGL 我们已经完成了基本的功能,但是我们不能得到一个代码来绘制一个可以旋转的立方体

cude需要旋转一段时间,但我不知道怎么做

我们已经试过了


但是我们想把它画在一个窗体上

OpenTK提供了一个名为
GLControl
的Windows窗体控件。它没有内置的主循环,但是如果您遵循,那么让连续渲染在控件上工作并不难


连续渲染工作完成后,可以移动代码以渲染立方体,立方体将在窗体上连续旋转。

OpenTK提供了一个名为
GLControl
的Windows窗体控件。它没有内置的主循环,但是如果您遵循,那么让连续渲染在控件上工作并不难


连续渲染工作完成后,您可以移动代码以渲染立方体,立方体将在窗体上连续旋转。

以下是使用OpenTK在C#中旋转立方体的示例:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

// adapted from : dreamstatecoding.blogspot.com

namespace RotatingCube
{
    public struct Vertex
    {
        public const int Size = (4 + 4) * 4; // size of struct in bytes

        private readonly Vector4 _position;
        private readonly Color4 _color;

        public Vertex(Vector4 position, Color4 color)
        {
            _position = position;
            _color = color;
        }
    }

    public sealed class MainWindow : GameWindow
    {
        private readonly string _title;
        private int _width;
        private int _height;

        private int _program;
        private double _time;
        private bool _initialized;
        private int _vertexArray;
        private int _buffer;
        private int _verticeCount;

        private Matrix4 _model;
        private Matrix4 _view;
        private Matrix4 _projection;
        private float _FOV = 45.0f;

        private float _lastTimestamp = Stopwatch.GetTimestamp();
        private float _freq = Stopwatch.Frequency;

        private float _angle;

        public MainWindow()
            : base(750, // initial width
                500, // initial height
                GraphicsMode.Default,
                "",  // initial title
                GameWindowFlags.Default,
                DisplayDevice.Default,
                3, // OpenGL major version
                3, // OpenGL minor version
                GraphicsContextFlags.ForwardCompatible)
        {
            _width = 750;
            _height = 500;
            _title += "Spinning Cube, OpenGL Version: " + GL.GetString(StringName.Version);
        }


        protected override void OnLoad(EventArgs e)
        {

            _model =  Matrix4.Identity;
            Vertex[] vertices =
            {
                new Vertex(new Vector4(-0.5f, -0.5f, -0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4( 0.5f, -0.5f, -0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4( 0.5f,  0.5f, -0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4( 0.5f,  0.5f, -0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4(-0.5f,  0.5f, -0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4(-0.5f, -0.5f, -0.5f,  1.0f), Color4.Blue),

                new Vertex(new Vector4(-0.5f, -0.5f,  0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4( 0.5f, -0.5f,  0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4( 0.5f,  0.5f,  0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4( 0.5f,  0.5f,  0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4(-0.5f,  0.5f,  0.5f,  1.0f), Color4.Blue),
                new Vertex(new Vector4(-0.5f, -0.5f,  0.5f,  1.0f), Color4.Blue),

                new Vertex(new Vector4(-0.5f,  0.5f,  0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4(-0.5f,  0.5f, -0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4(-0.5f, -0.5f, -0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4(-0.5f, -0.5f, -0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4(-0.5f, -0.5f,  0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4(-0.5f,  0.5f,  0.5f,  1.0f), Color4.Red),

                new Vertex(new Vector4( 0.5f,  0.5f,  0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4( 0.5f,  0.5f, -0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4( 0.5f, -0.5f, -0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4( 0.5f, -0.5f, -0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4( 0.5f, -0.5f,  0.5f,  1.0f), Color4.Red),
                new Vertex(new Vector4( 0.5f,  0.5f,  0.5f,  1.0f), Color4.Red),

                new Vertex(new Vector4(-0.5f, -0.5f, -0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4( 0.5f, -0.5f, -0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4( 0.5f, -0.5f,  0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4( 0.5f, -0.5f,  0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4(-0.5f, -0.5f,  0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4(-0.5f, -0.5f, -0.5f,  1.0f), Color4.Green),

                new Vertex(new Vector4(-0.5f,  0.5f, -0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4( 0.5f,  0.5f, -0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4( 0.5f,  0.5f,  0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4( 0.5f,  0.5f,  0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4(-0.5f,  0.5f,  0.5f,  1.0f), Color4.Green),
                new Vertex(new Vector4(-0.5f,  0.5f, -0.5f,  1.0f), Color4.Green),
            };

            _verticeCount = vertices.Length;
            _vertexArray = GL.GenVertexArray();
            _buffer = GL.GenBuffer();

            GL.BindVertexArray(_vertexArray);
            GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexArray);

            // create first buffer: vertex
            GL.NamedBufferStorage(
                _buffer,
                Vertex.Size*vertices.Length,        // the size needed by this buffer
                vertices,                           // data to initialize with
                BufferStorageFlags.MapWriteBit);    // at this point we will only write to the buffer


            GL.VertexArrayAttribBinding(_vertexArray, 0, 0);
            GL.EnableVertexArrayAttrib(_vertexArray, 0);
            GL.VertexArrayAttribFormat(
                _vertexArray,
                0,                      // attribute index, from the shader location = 0
                4,                      // size of attribute, vec4
                VertexAttribType.Float, // contains floats
                false,                  // does not need to be normalized as it is already, floats ignore this flag anyway
                0);                     // relative offset, first item


            GL.VertexArrayAttribBinding(_vertexArray, 1, 0);
            GL.EnableVertexArrayAttrib(_vertexArray, 1);
            GL.VertexArrayAttribFormat(
                _vertexArray,
                1,                      // attribute index, from the shader location = 1
                4,                      // size of attribute, vec4
                VertexAttribType.Float, // contains floats
                false,                  // does not need to be normalized as it is already, floats ignore this flag anyway
                16);                     // relative offset after a vec4

            // link the vertex array and buffer and provide the stride as size of Vertex
            GL.VertexArrayVertexBuffer(_vertexArray, 0, _buffer, IntPtr.Zero, Vertex.Size);
            _initialized = true;

            CursorVisible = true;

            try
            {
                _program = GL.CreateProgram();
                var shaders = new List<int>();
                ShaderType type = ShaderType.VertexShader;
                var shader = GL.CreateShader(type);
                string src = @"#version 330 core
                                layout (location = 0) in vec4 position;
                                layout(location = 1) in vec4 color;
                                out vec4 vs_color;

                                out vec3 original_normal;
                                out vec3 transformed_normal;

                                uniform mat4 model;
                                uniform mat4 view;
                                uniform mat4 projection;

                                void main(void)
                                {
                                gl_Position = projection * view * model * position;
                                vs_color = color;
                                original_normal = vec3(color);
                                mat3 normal_matrix = transpose(inverse(mat3(view * model)));
                                transformed_normal = normal_matrix * original_normal;
                            }";
                GL.ShaderSource(shader, src);
                GL.CompileShader(shader);
                var info = GL.GetShaderInfoLog(shader);
                if (!string.IsNullOrWhiteSpace(info))
                    throw new Exception($"CompileShader {type} had errors: {info}");

                shaders.Add(shader);

                type = ShaderType.FragmentShader;
                shader = GL.CreateShader(type);
                src = @"#version 330 core
                        in vec4 vs_color;
                        in vec3 original_normal;
                        in vec3 transformed_normal;
                        out vec4 color;

                        void main(void)
                        {
                            float lighting = abs(dot(transformed_normal, vec3(0,0,-1)));
                            color = vs_color * lighting;
                        }";
                GL.ShaderSource(shader, src);
                GL.CompileShader(shader);
                info = GL.GetShaderInfoLog(shader);
                if (!string.IsNullOrWhiteSpace(info))
                    throw new Exception($"CompileShader {type} had errors: {info}");

                shaders.Add(shader);

                foreach (var shader_ in shaders)
                    GL.AttachShader(_program, shader_);
                GL.LinkProgram(_program);
                var info_ = GL.GetProgramInfoLog(_program);
                if (!string.IsNullOrWhiteSpace(info_))
                    throw new Exception($"CompileShaders ProgramLinking had errors: {info}");

                foreach (var shader_ in shaders)
                {
                    GL.DetachShader(_program, shader_);
                    GL.DeleteShader(shader_);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                throw;
            }

            GL.Enable(EnableCap.DepthTest);
            GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
            GL.PatchParameter(PatchParameterInt.PatchVertices, 3);
            Closed += OnClosed;
        }
        
        private void OnClosed(object sender, EventArgs eventArgs)
        {
            Exit();
        }

        public override void Exit()
        {
            Debug.WriteLine("Exit called");
            GL.DeleteVertexArray(_vertexArray);
            GL.DeleteBuffer(_buffer);
            GL.DeleteProgram(_program);
            base.Exit();
        }

        protected override void OnResize(EventArgs e)
        {
            // Resize the viewport to match the window size.
            GL.Viewport(0, 0, Width, Height);
            base.OnResize(e);
        }

private float[] Matrix4ToArray(Matrix4 matrix)
{
    float[] data = new float[16];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            data[i*4+j] = matrix[i, j];

        }
    }
    return data;
}

        private void PrintMatrix(Matrix4 matrix)
        {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Console.WriteLine(matrix[i,j]);

                }
            }
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {

            var timeStamp = Stopwatch.GetTimestamp();
            _angle += (float)((timeStamp - _lastTimestamp) / (double)_freq);
            _lastTimestamp = timeStamp;

            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            // Clear the color buffer.
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // Bind the VBO
            GL.BindBuffer(BufferTarget.ArrayBuffer, _buffer);
            // Bind the VAO
            GL.BindVertexArray(_vertexArray);
            // Use/Bind the program
            GL.UseProgram(_program);

            _model = Matrix4.CreateFromAxisAngle(new Vector3(1.0f, 0.0f, 1.0f), _angle);
            _view = Matrix4.LookAt(new Vector3(0.0f,0.0f,5.0f), new Vector3(0.0f,0.0f,0.0f), Vector3.UnitY);
            _projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI * (_FOV/180f), _width / (float)_height, 0.2f, 256.0f);
            
            int location = GL.GetUniformLocation(_program, "model");
            GL.UniformMatrix4(location, 1, false, Matrix4ToArray(_model));
            location = GL.GetUniformLocation(_program, "view");
            GL.UniformMatrix4(location, 1, false, Matrix4ToArray(_view));
            location = GL.GetUniformLocation(_program, "projection");
            GL.UniformMatrix4(location, 1, false, Matrix4ToArray(_projection));

            // This draws the triangle.
            GL.DrawArrays(PrimitiveType.Triangles, 0, _verticeCount);

            // Swap the front/back buffers so what we just rendered to the back buffer is displayed in the window.
            Context.SwapBuffers();
            base.OnRenderFrame(e);
        }

        [STAThread]
        static void Main()
        {
            new MainWindow().Run(60);
        }
        
    }

}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用System.Runtime.InteropServices;
使用OpenTK;
使用OpenTK.Graphics;
使用OpenTK.Graphics.OpenGL;
使用OpenTK.Input;
//改编自:dreamstatecoding.blogspot.com
名称空间旋转立方体
{
公共结构顶点
{
public const int Size=(4+4)*4;//结构的大小(字节)
私有只读向量4_位置;
专用只读彩色4_彩色;
公共顶点(矢量4位置,颜色4颜色)
{
_位置=位置;
_颜色=颜色;
}
}
公共密封类主窗口:GameWindow
{
私有只读字符串_title;
私有整数宽度;
私人室内高度;
私人int_计划;
私人双时间;
已初始化私有布尔;
私人int_vertexArray;
专用int_缓冲区;
私人国际垂直计数;
私有矩阵4_模型;
私有矩阵4_视图;
私有矩阵4_投影;
私人浮标FOV=45.0f;
private float_lastTimestamp=Stopwatch.GetTimestamp();
私人浮动频率=秒表频率;
私人浮动角;
公共主窗口()
:底座(750,//初始宽度
500,//初始高度
GraphicsMode.Default,
“”,//初始标题
GameWindowFlags.Default,
DisplayDevice.Default,
3,//OpenGL主要版本
3,//OpenGL次要版本
GraphicsContextFlags.ForwardCompatible)
{
_宽度=750;
_高度=500;
_title+=“旋转立方体,OpenGL版本:”+GL.GetString(StringName.Version);
}
受保护的覆盖无效加载(事件参数e)
{
_模型=矩阵4.标识;
顶点[]顶点=
{
新顶点(新矢量4(-0.5f,-0.5f,-0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(0.5f,-0.5f,-0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(0.5f,0.5f,-0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(0.5f,0.5f,-0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(-0.5f,0.5f,-0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(-0.5f,-0.5f,-0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(-0.5f,-0.5f,0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(0.5f,-0.5f,0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(0.5f,0.5f,0.5f,1.0f),颜色4。蓝色),
新顶点(新矢量4(0.5f,0.5f,0.5f,1.0f),颜色4。蓝色),
新顶点(新矢量4(-0.5f,0.5f,0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(-0.5f,-0.5f,0.5f,1.0f),颜色4.蓝色),
新顶点(新矢量4(-0.5f,0.5f,0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(-0.5f,0.5f,-0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(-0.5f,-0.5f,-0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(-0.5f,-0.5f,-0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(-0.5f,-0.5f,0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(-0.5f,0.5f,0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(0.5f,0.5f,0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(0.5f,0.5f,-0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(0.5f,-0.5f,-0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(0.5f,-0.5f,-0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(0.5f,-0.5f,0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(0.5f,0.5f,0.5f,1.0f),颜色4.红色),
新顶点(新矢量4(-0.5f,-0.5f,-0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(0.5f,-0.5f,-0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(0.5f,-0.5f,0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(0.5f,-0.5f,0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(-0.5f,-0.5f,0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(-0.5f,-0.5f,-0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(-0.5f,0.5f,-0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(0.5f,0.5f,-0.5f,1.0f),颜色4.绿色),
新顶点(新矢量4(0.5f,0.5f,0.5f,1.0f),颜色4。绿色),
新顶点(新矢量4(0.5f,0.5f,0.5f,1.0f),颜色4。绿色),
新顶点(新矢量4(-0.5f,0.5f,0.5f,1.0f),颜色4.绿色),
新维特