如何将openGL窗口附加到Windows窗体窗口?

时间:2018-02-16 13:31:06

标签: c# opengl

我一直在四处寻找,看看我能否将我的openGL窗口附加到我的Windows窗体窗口,我已经决定我要么不明白它是如何工作的,要么我没有找到任何东西这可以帮助我。

是否有人可以对此作出任何澄清或解释如何制定流程?

using System;
using System.Windows.Forms;
using Tao.FreeGlut;
using OpenGL;
using System.IO.Ports;
using System.Diagnostics;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing;

namespace Arrduino
{
public partial class Form1 : Form
{
    private static int width = 300, height = 300;
    private static ShaderProgram program;
    private static VBO<Vector3> squareColor;
    private static VBO<Vector3> square;
    private static VBO<int> squareElements;
    private static Stopwatch watch;
    public int delay1, delay2, delay3, delay4, delayStop1, delayStop2, delayStop3, delayStop4;
    private static float angle;
    Vector3[] vec = {new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1)};
    static FilterInfoCollection webCam;
    static VideoCaptureDevice cam;
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Glut.glutInit();
        Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
        Glut.glutInitWindowSize(width, height);
        Glut.glutInitWindowPosition(800, 150);
        Glut.glutCreateWindow("Arduino Window");
        SerialPort myArduino = new SerialPort();
        myArduino.BaudRate = 115200;
        myArduino.PortName = "COM3";
        myArduino.Open();
        myArduino.DataReceived += new SerialDataReceivedEventHandler(SerialData_Changed);

        Glut.glutIdleFunc(OnRenderFrame);
        Glut.glutCloseFunc(OnClose);
        Gl.Enable(EnableCap.DepthTest);

        program = new ShaderProgram(VertexShader, Fragmentshader);
        program.Use();
        program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
        program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.UnitY));

        square = new VBO<Vector3>(new Vector3[] {new Vector3(-1, -1, 1), new Vector3(1, -1, 1), new Vector3(1, 1, 1), new Vector3(-1, 1, 1)});

        squareColor = new VBO<Vector3>(vec);


        squareElements = new VBO<int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);

        watch = Stopwatch.StartNew();
        Glut.glutMainLoop();
    }

    private static void OnRenderFrame()
    {
        watch.Stop();
        float deltaTime = watch.ElapsedMilliseconds / 1000f;
        watch.Restart();

        angle += deltaTime;

        Gl.Viewport(-100, 0, width, height);
        Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        program.Use();

        //Square draw
        program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(1.5f, 0, 0)));
        Gl.BindBufferToShaderAttribute(square, program, "vertexPosition");
        Gl.BindBufferToShaderAttribute(squareColor, program, "vertexColor");
        Gl.BindBuffer(squareElements);
        Gl.DrawElements(BeginMode.Quads, squareElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
        Glut.glutSwapBuffers();
    }
    public static string VertexShader = @"
        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);
    }
  ";

0 个答案:

没有答案