Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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
C# 按键使图像停止移动_C#_Opengl_Tao Framework - Fatal编程技术网

C# 按键使图像停止移动

C# 按键使图像停止移动,c#,opengl,tao-framework,C#,Opengl,Tao Framework,我有一个基于Giawa教程1-5的OpenGl程序。基本上我创建了一个图像,在屏幕上显示,并使其旋转。我想要完成的就是添加键盘事件来更改图像的某些参数。例如,在我的代码中,我想按F6使图像旋转得更快: using System; using Tao.FreeGlut; using OpenGL; using System; using System.Windows; using System.Windows.Forms; namespace OpenGLTutorial5 { clas

我有一个基于Giawa教程1-5的OpenGl程序。基本上我创建了一个图像,在屏幕上显示,并使其旋转。我想要完成的就是添加键盘事件来更改图像的某些参数。例如,在我的代码中,我想按F6使图像旋转得更快:

using System;
using Tao.FreeGlut;
using OpenGL;
using System;
using System.Windows;
using System.Windows.Forms;

namespace OpenGLTutorial5
{
    class Program
    {
        private static int width = 640, height = 480;
        private static ShaderProgram program;

        private static VBO<Vector3> top_pyramid, cube, bottom_pyramid;
        private static VBO<Vector3> top_pyramidColor, cubeColor, bottom_pyramidColor;
        private static VBO<int> top_pyramidElements, cubeElements, bottom_pyramidElements;

        private static System.Diagnostics.Stopwatch watch;
        private static float angle;
        private static int rotate = 1;

        static void Main(string[] args)
        {
            // create an OpenGL window
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

            // provide the Glut callbacks that are necessary for running this tutorial
            Glut.glutIdleFunc(OnRenderFrame);
            Glut.glutDisplayFunc(OnDisplay);
           // Glut.glutKeyboardFunc(new Glut.KeyboardCallback(OnKeyPress));
            Glut.glutSpecialFunc(new Glut.SpecialCallback(OnKeyPress));
            Glut.glutCloseFunc(OnClose);

            // enable depth testing to ensure correct z-ordering of our fragments
            Gl.Enable(EnableCap.DepthTest);

            // compile the shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set the view and projection matrix, which are static throughout this tutorial
            program.Use();
            program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
            program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(10, 0, 10), Vector3.Zero, Vector3.Up));


            top_pyramid = new VBO<Vector3>(new Vector3[] 
            {
                new Vector3(-1.5, 0, -0.5), new Vector3(-0.5, 1, -0.5), new Vector3(0, 0, -1.5),
                new Vector3(-0.5, 1, -0.5), new Vector3(0.5, 1, -0.5), new Vector3(0, 0, -1.5),
                new Vector3(0.5, 1, -0.5), new Vector3(1.5, 0, -0.5), new Vector3(0, 0, -1.5),
                new Vector3(1.5, 0, -0.5), new Vector3(0.5, -1, -0.5), new Vector3(0, 0, -1.5),
                new Vector3(0.5, -1, -0.5), new Vector3(-0.5, -1, -0.5), new Vector3(0, 0, -1.5),
                new Vector3(-0.5, -1, -0.5), new Vector3(-1.5, 0, -0.5), new Vector3(0, 0, -1.5)
            });

            cube = new VBO<Vector3>(new Vector3[]
            {
                new Vector3(-1.5, 0, -0.5), new Vector3(-0.5, 1, -0.5), new Vector3(-0.5, 1, 0.5), new Vector3(-1.5, 0, 0.5),
                new Vector3(-0.5, 1, -0.5), new Vector3(0.5, 1, -0.5), new Vector3(0.5, 1, 0.5), new Vector3(-0.5, 1, 0.5),
                new Vector3(0.5, 1, -0.5), new Vector3(1.5, 0, -0.5), new Vector3(1.5, 0, 0.5), new Vector3(0.5, 1, 0.5),
                new Vector3(1.5, 0, -0.5), new Vector3(1.5, 0, 0.5), new Vector3(0.5, -1, 0.5), new Vector3(0.5, -1, -0.5),
                new Vector3(0.5, -1, 0.5), new Vector3(0.5, -1, -0.5), new Vector3(-0.5, -1, -0.5), new Vector3(-0.5, -1, 0.5),
                new Vector3(-0.5, -1, -0.5), new Vector3(-0.5, -1, 0.5), new Vector3(-1.5, 0, 0.5), new Vector3(-1.5, 0, -0.5)
             });


            bottom_pyramid = new VBO<Vector3>(new Vector3[]
            {
                new Vector3(-1.5, 0, 0.5), new Vector3(-0.5, 1, 0.5), new Vector3(0, 0, 1.5),
                new Vector3(-0.5, 1, 0.5), new Vector3(0.5, 1, 0.5), new Vector3(0, 0, 1.5),
                new Vector3(0.5, 1, 0.5), new Vector3(1.5, 0, 0.5), new Vector3(0, 0, 1.5),
                new Vector3(1.5, 0, 0.5), new Vector3(0.5, -1, 0.5), new Vector3(0, 0, 1.5),
                new Vector3(0.5, -1, 0.5), new Vector3(-0.5, -1, 0.5), new Vector3(0, 0, 1.5),
                new Vector3(-0.5, -1, 0.5), new Vector3(-1.5, 0, 0.5), new Vector3(0, 0, 1.5)
            });

            top_pyramidColor = new VBO<Vector3>(new Vector3[] 
            {
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0)
            });

            cubeColor = new VBO<Vector3>(new Vector3[]
            {
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0,0,1),
            });

            bottom_pyramidColor = new VBO<Vector3>(new Vector3[]
            {
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 1, 0)
            });

            top_pyramidElements = new VBO<int>(new int[] { 
                0,1,2, 
                3,4,5,
                6,7,8,
                9,10,11,
                12,13,14, 
                15,16,17}, BufferTarget.ElementArrayBuffer);

            cubeElements = new VBO<int>(new int[]{
                0,1,2,3,
                4,5,6,7,
                8,9,10,11,
                12,13,14,15,
                16,17,18,19,
                20,21,22,23}, BufferTarget.ElementArrayBuffer);

            bottom_pyramidElements = new VBO<int>(new int[] { 
                0,1,2, 
                3,4,5,
                6,7,8,
                9,10,11,
                12,13,14, 
                15,16,17}, BufferTarget.ElementArrayBuffer);

            watch = System.Diagnostics.Stopwatch.StartNew();

            Glut.glutMainLoop();
        }



        public static void OnKeyPress(int theKey, int x, int y)
        {
            switch (theKey)
            {
                case Glut.GLUT_KEY_F6:
                    {
                        rotate += 3;
                        Console.WriteLine("Hallo!");
                        Console.ReadLine();
                    }
                   break;
            }   
            Glut.glutPostRedisplay();
        }

        private static void OnClose()
        {
            top_pyramid.Dispose();
            top_pyramidColor.Dispose();
            top_pyramidElements.Dispose();

           program.DisposeChildren = true;
           program.Dispose();
        }

        private static void OnDisplay()
        {

        }

        private static void OnRenderFrame()
        {
           // calculate how much time has elapsed since the last frame
           watch.Stop();
           float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;
           watch.Restart();

           // use the deltaTime to adjust the angle of the cube and pyramid
           angle += deltaTime;



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

            // use our shader program
            Gl.UseProgram(program);

           //top pyramid
            program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate) * Matrix4.CreateTranslation(new Vector3(0.0f, 0, 1)));
           Gl.BindBufferToShaderAttribute(top_pyramid, program, "vertexPosition");
           Gl.BindBufferToShaderAttribute(top_pyramidColor, program, "vertexColor");
           Gl.BindBuffer(top_pyramidElements);
           //top pyramid
           Gl.DrawElements(BeginMode.Triangles, top_pyramidElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

           //cubes
           program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate) * Matrix4.CreateTranslation(new Vector3(0.0f, 0, 1)));
           Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
           Gl.BindBufferToShaderAttribute(cubeColor, program, "vertexColor");
           Gl.BindBuffer(cubeElements);
           //cubes
           Gl.DrawElements(BeginMode.Quads, cubeElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

           //bottom pyramid
           program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle * rotate) * Matrix4.CreateTranslation(new Vector3(0.0f, 0, 1)));
           Gl.BindBufferToShaderAttribute(bottom_pyramid, program, "vertexPosition");
           Gl.BindBufferToShaderAttribute(bottom_pyramidColor, program, "vertexColor");
           Gl.BindBuffer(bottom_pyramidElements);
           //top pyramid
           Gl.DrawElements(BeginMode.Triangles, bottom_pyramidElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            Glut.glutSwapBuffers();
        }

        public static string VertexShader = @"
#version 130

in vec3 vertexPosition;
in vec3 vertexColor;

out vec3 color;

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;

void main(void)
{
    color = vertexColor;
    gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";

        public static string FragmentShader = @"
#version 130

in vec3 color;

out vec4 fragment;

void main(void)
{
    fragment = vec4(color, 1);
}
";
    }
}
使用系统;
使用道。自由GLUT;
使用OpenGL;
使用制度;
使用System.Windows;
使用System.Windows.Forms;
命名空间OpenGLTutorial5
{
班级计划
{
专用静态整数宽度=640,高度=480;
私有静态着色器程序;
私有静态VBO顶部金字塔、立方体、底部金字塔;
私有静态VBO顶部金字塔颜色、立方体颜色、底部金字塔颜色;
私有静态VBO顶部金字塔元素、立方体元素、底部金字塔元素;
专用静态系统。诊断。秒表;
专用静态浮动角;
私有静态int旋转=1;
静态void Main(字符串[]参数)
{
//创建OpenGL窗口
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.Glut_双精度| Glut.Glut_深度);
Glut.glutInitWindowSize(宽度、高度);
glutCreateWindow(“OpenGL教程”);
//提供运行本教程所需的Glut回调
Glut.glutIdleFunc(OnRenderFrame);
Glut.glutDisplayFunc(OnDisplay);
//Glut.glutKeyboardFunc(新的Glut.KeyboardCallback(OnKeyPress));
Glut.glutSpecialFunc(新Glut.SpecialCallback(OnKeyPress));
Glut.glutCloseFunc(OnClose);
//启用深度测试以确保碎片的z顺序正确
总账启用(启用CAP.深度测试);
//编译着色器程序
程序=新着色器程序(VertexShader、FragmentShader);
//设置视图和投影矩阵,它们在本教程中是静态的
program.Use();
程序[“投影矩阵”]。设置值(Matrix4.CreatePerspectiveFieldOfView(0.45f,(浮动)宽度/高度,0.1f,1000f));
程序[“查看矩阵”].SetValue(矩阵4.LookAt(新向量3(10,0,10),向量3.Zero,向量3.Up));
顶部金字塔=新VBO(新矢量3[]
{
新向量3(-1.5,0,-0.5),新向量3(-0.5,1,-0.5),新向量3(0,0,-1.5),
新向量3(-0.5,1,-0.5),新向量3(0.5,1,-0.5),新向量3(0,0,-1.5),
新向量3(0.5,1,-0.5),新向量3(1.5,0,-0.5),新向量3(0,0,-1.5),
新向量3(1.5,0,-0.5),新向量3(0.5,-1,-0.5),新向量3(0,0,-1.5),
新向量3(0.5,-1,-0.5),新向量3(-0.5,-1,-0.5),新向量3(0,0,-1.5),
新向量3(-0.5,-1,-0.5),新向量3(-1.5,0,-0.5),新向量3(0,0,-1.5)
});
多维数据集=新VBO(新矢量3[]
{
新向量3(-1.5,0,-0.5),新向量3(-0.5,1,-0.5),新向量3(-0.5,1,0.5),新向量3(-1.5,0,0.5),
新向量3(-0.5,1,-0.5),新向量3(0.5,1,-0.5),新向量3(0.5,1,0.5),新向量3(-0.5,1,0.5),
新矢量3(0.5,1,-0.5),新矢量3(1.5,0,-0.5),新矢量3(1.5,0,0.5),新矢量3(0.5,1,0.5),
新向量3(1.5,0,-0.5),新向量3(1.5,0,0.5),新向量3(0.5,-1,0.5),新向量3(0.5,-1,-0.5),
新向量3(0.5,-1,0.5),新向量3(0.5,-1,-0.5),新向量3(-0.5,-1,-0.5),新向量3(-0.5,-1,0.5),
新向量3(-0.5,-1,-0.5),新向量3(-0.5,-1,0.5),新向量3(-1.5,0,0.5),新向量3(-1.5,0,-0.5)
});
底部金字塔=新VBO(新矢量3[]
{
新矢量3(-1.5,0,0.5),新矢量3(-0.5,1,0.5),新矢量3(0,0,1.5),
新矢量3(-0.5,1,0.5),新矢量3(0.5,1,0.5),新矢量3(0,0,1.5),
新矢量3(0.5,1,0.5),新矢量3(1.5,0,0.5),新矢量3(0,0,1.5),
新矢量3(1.5,0,0.5),新矢量3(0.5,-1,0.5),新矢量3(0,0,1.5),
新矢量3(0.5,-1,0.5),新矢量3(-0.5,-1,0.5),新矢量3(0,0,1.5),
新矢量3(-0.5,-1,0.5),新矢量3(-1.5,0,0.5),新矢量3(0,0,1.5)
});
顶部颜色=新VBO(新矢量3[]
{
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,1,0),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,1,0),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,1,0),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,1,0),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,1,0),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,1,0)
});
立方体颜色=新VBO(新矢量3[]
{
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,0,1),新向量3(0,0,1),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,0,1),新向量3(0,0,1),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,0,1),新向量3(0,0,1),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,0,1),新向量3(0,0,1),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,0,1),新向量3(0,0,1),
新向量3(1,0,0),新向量3(1,0,0),新向量3(0,0,1),新向量3(0,0,1),
});
底部颜色=新VBO(新矢量3[]
{
新向量3(0,0,1),新向量3(0,0,1),新向量3(1,1,0),
新向量3(0,0,1),新向量3(0,0,1),新向量3(1,1,0),
新向量3(0,0,1),新向量3(0,0,1),新向量3(1,1,0),
新矢量3(0,0,
rotate += 3;
Console.WriteLine("Hallo!");
Console.ReadLine();