0

I have been looking around a lot to see if I am able to attach my openGL window to my windows forms window and I have come to the decision that I either don't understand how it works or I haven't found anything that could help me out.

Is there anyone who can give me any clarifications around this or explain how the process is made?

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);
    }
  ";
  • Please show us some code. What have you tried? How are you initializing OpenGL? Normally, you would just pass in the window handle and you're done. – PMF Feb 16 '18 at 13:36
  • I do it like this: [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) find `gl_init(HWND Handle)` function you should provide your C# app Window Handle to it to make it work however the code is in C++ so you need to port it to your C# ... There might be also another problem if your app is repainting the window automaticaly by GDI that cause either flickering or that GL stuff is invisible even if painted. In such case you need to play with repaint and refresh and Update methods of your window .... – Spektre Feb 16 '18 at 16:40

0 Answers0