示例#1
0
        private void Window_Load(object sender, EventArgs e)
        {
            DoubleBuffered = false;
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            SetStyle(ControlStyles.Opaque, true);

            AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            Size          = new Size(800, 600);

            _WGLContext = Ratchet.Drawing.OpenGL.WGL.CreateContext(this);
            _Context    = Ratchet.Drawing.OpenGL.glContext.CreateContextFromWGLContext(_WGLContext);
            _Context.MakeCurrent();
            _Context.Enable(Ratchet.Drawing.OpenGL.glContext.Capability.GL_DEBUG_OUTPUT);
            _Context.DebugMessageCallback((int source, int id, int type, int severity, string message) =>
            {
                Console.WriteLine(message);
            });
            _VertexBuffer = _Context.GenBuffer();
            _VertexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER);
            float[] triangle = new float[] { -0.5f, -0.5f, 0.0f, 1.0f, // Vertex 1 Position
                                             1.0f, 0.0f, 0.0f, 1.0f,   // Vertex 1 Color (red)

                                             0.5f, -0.5f, 0.0f, 1.0f,  // Vertex 2 Position
                                             0.0f, 1.0f, 0.0f, 1.0f,   // Vertex 2 Color (green)

                                             0.0f, 0.5f, 0.0f, 1.0f,   // Vertex 3 Position
                                             0.0f, 0.0f, 1.0f, 1.0f,   // Vertex 3 Color (blue)
            };
            _VertexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER, sizeof(float) * triangle.Length, triangle, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);

            _IndexBuffer = _Context.GenBuffer();
            _IndexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER);
            _IndexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 3, new int[] { 0, 1, 2 }, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);

            var vertexShader   = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_VERTEX_SHADER, vertexShaderSource);
            var fragmentShader = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_FRAGMENT_SHADER, fragmentShaderSource);

            _Shader = _Context.CreateProgram();
            _Shader.AttachShader(vertexShader);
            _Shader.AttachShader(fragmentShader);
            _Shader.LinkProgram();

            _Context.Viewport(0, 0, Width, Height);
        }
示例#2
0
        private void SimpleCube_Load(object sender, EventArgs e)
        {
            DoubleBuffered = false;
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            SetStyle(ControlStyles.Opaque, true);


            AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            Size          = new Size(800, 600);

            _WGLContext = Ratchet.Drawing.OpenGL.WGL.CreateContext(this);
            _Context    = Ratchet.Drawing.OpenGL.glContext.CreateContextFromWGLContext(_WGLContext);
            _Context.MakeCurrent();
            _Context.Enable(Ratchet.Drawing.OpenGL.glContext.Capability.GL_DEBUG_OUTPUT);
            _Context.DebugMessageCallback((int source, int id, int type, int severity, string message) =>
            {
                Console.WriteLine(message);
            });
            _VertexBuffer = _Context.GenBuffer();
            _VertexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER);
            float[] triangles = new float[] { -0.5f, -0.5f, -0.5f, 1.0f, // Vertex 0 Position
                                              1.0f, 1.0f,

                                              0.5f, -0.5f, -0.5f, 1.0f, // Vertex 1 Position
                                              0.0f, 1.0f,

                                              0.5f, 0.5f, -0.5f, 1.0f,  // Vertex 2 Position
                                              0.0f, 0.0f,

                                              -0.5f, 0.5f, -0.5f, 1.0f,  // Vertex 3 Position
                                              1.0f, 0.0f,

                                              -0.5f, -0.5f, 0.5f, 1.0f, // Vertex 4 Position
                                              1.0f, 1.0f,

                                              0.5f, -0.5f, 0.5f, 1.0f, // Vertex 5 Position
                                              0.0f, 1.0f,

                                              0.5f, 0.5f, 0.5f, 1.0f,  // Vertex 6 Position
                                              0.0f, 0.0f,

                                              -0.5f, 0.5f, 0.5f, 1.0f,  // Vertex 7 Position
                                              1.0f, 0.0f, };
            _VertexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER, sizeof(float) * triangles.Length, triangles, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);

            _IndexBuffer = _Context.GenBuffer();
            _IndexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER);
            ///        7 --- 6
            ///       /     /|
            ///      3 --- 2 5
            ///      |     |/
            ///      0 --- 1
            _IndexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 36, new int[] { 0, 1, 2, 0, 2, 3,
                                                                                                                                      4, 5, 6, 4, 6, 7,
                                                                                                                                      0, 1, 4, 1, 4, 5,
                                                                                                                                      2, 7, 3, 2, 7, 6,
                                                                                                                                      1, 6, 2, 1, 6, 5,
                                                                                                                                      0, 7, 3, 0, 7, 4, }, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);
            var vertexShader   = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_VERTEX_SHADER, vertexShaderSource);
            var fragmentShader = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_FRAGMENT_SHADER, fragmentShaderSource);

            _Shader = _Context.CreateProgram();
            _Shader.AttachShader(vertexShader);
            _Shader.AttachShader(fragmentShader);
            _Shader.LinkProgram();

            _Context.Viewport(0, 0, Width, Height);

            _Projection = mat4.Perspective(2.8f, (float)Width / (float)Height, 0.01f, 10.0f);
            _Lookat     = mat4.LookAt(new vec3(0.0f, 0.2f, -2.0f), new vec3(0.1f, 0.1f, 0.1f), new vec3(0.0f, 1.0f, 0.0f));

            _Context.Enable(Ratchet.Drawing.OpenGL.glContext.Capability.GL_DEPTH_TEST);
            _Context.DepthFunc(Ratchet.Drawing.OpenGL.glContext.DepthFuncion.GL_LEQUAL);
        }
示例#3
0
        private void Windows_Load(object sender, EventArgs e)
        {
            DoubleBuffered = false;
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            SetStyle(ControlStyles.Opaque, true);

            AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            Size          = new Size(800, 600);

            _WGLContext = Ratchet.Drawing.OpenGL.WGL.CreateContext(this);
            _Context    = Ratchet.Drawing.OpenGL.glContext.CreateContextFromWGLContext(_WGLContext);
            _Context.MakeCurrent();
            _Context.Enable(Ratchet.Drawing.OpenGL.glContext.Capability.GL_DEBUG_OUTPUT);
            _Context.DebugMessageCallback((int source, int id, int type, int severity, string message) =>
            {
                Console.WriteLine(message);
            });
            _VertexBuffer = _Context.GenBuffer();
            _VertexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER);
            float[] triangle = new float[] { -0.5f, -0.5f, 0.0f, 1.0f, // Vertex 1 Position
                                             1.0f, 1.0f,

                                             0.5f, -0.5f, 0.0f, 1.0f,  // Vertex 2 Position
                                             0.0f, 1.0f,

                                             0.5f, 0.5f, 0.0f, 1.0f,   // Vertex 3 Position
                                             0.0f, 0.0f,

                                             -0.5f, 0.5f, 0.0f, 1.0f,   // Vertex 4 Position
                                             1.0f, 0.0f, };
            _VertexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER, sizeof(float) * triangle.Length, triangle, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);

            _IndexBuffer = _Context.GenBuffer();
            _IndexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER);
            _IndexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 6, new int[] { 0, 1, 2, 0, 2, 3 }, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);

            System.Drawing.Bitmap image = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(@"./ratchet.png");

            // Create a new texture
            _Texture = _Context.GenTexture();
            // Bind the texture we have created to Texture 2D in sampler 0 in order to configure it
            // At that point configuring the sampler to use sampler 0 is not needed but we do it for consistency
            _Context.ActiveTexture(0);
            _Texture.BindTexture(Ratchet.Drawing.OpenGL.glTexture.BindTarget.GL_TEXTURE_2D);
            var lockRegion = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            Swap_BGRA_RGBA(lockRegion.Scan0, image.Width * image.Height);
            _Texture.TexImage2D(Ratchet.Drawing.OpenGL.glTexture.BindTarget.GL_TEXTURE_2D, 0, Ratchet.Drawing.OpenGL.glTexture.InternalFormat.GL_RGBA, image.Width, image.Height, 0, Ratchet.Drawing.OpenGL.glTexture.Format.GL_RGBA, Ratchet.Drawing.OpenGL.glTexture.Type.GL_UNSIGNED_BYTE, lockRegion.Scan0);
            image.UnlockBits(lockRegion);
            // Filters needs to be set so the sampler can sample the texture correctly.
            _Texture.TexParameter(Ratchet.Drawing.OpenGL.glTexture.BindTarget.GL_TEXTURE_2D, Ratchet.Drawing.OpenGL.glTexture.Parameter.GL_TEXTURE_MIN_FILTER, Ratchet.Drawing.OpenGL.glTexture.ParameterValue.GL_NEAREST);
            _Texture.TexParameter(Ratchet.Drawing.OpenGL.glTexture.BindTarget.GL_TEXTURE_2D, Ratchet.Drawing.OpenGL.glTexture.Parameter.GL_TEXTURE_MAG_FILTER, Ratchet.Drawing.OpenGL.glTexture.ParameterValue.GL_NEAREST);

            var vertexShader   = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_VERTEX_SHADER, vertexShaderSource);
            var fragmentShader = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_FRAGMENT_SHADER, fragmentShaderSource);

            _Shader = _Context.CreateProgram();
            _Shader.AttachShader(vertexShader);
            _Shader.AttachShader(fragmentShader);

            // If you wish to specify the bind location manually you can do so
            // before linking
            // _Shader.BindAttribLocation(0, "in_Position");
            // _Shader.BindAttribLocation(1, "in_textureCoord");
            _Shader.LinkProgram();

            _Context.Viewport(0, 0, Width, Height);
        }