如何在C#OpenGL中更改顶点的颜色?

时间:2018-02-14 07:45:34

标签: c# opengl

我想在事件下更改几个顶点的颜色,但我似乎无法使它工作。

当您想要更改顶点颜色时,常规程序是如何完成的?

我有一个初始化,我给出了有关如何制作多维数据集和onRenderFrame()的说明;在哪里我绘制立方体所以形状和颜色已经设置但我似乎无法在事件(如if语句)下进行颜色更改。

我将发送整个代码。它还涉及Arduino之间的连接,它将参与颜色变化

using System;
using System.IO.Ports;
using Tao.FreeGlut;
using OpenGL;
namespace Arduino
{
class Program
{
    private static int width = 1200, height = 720;
    private static ShaderProgram program;
    private static VBO<Vector3> squareColor;
    private static VBO<Vector3> square;
    private static VBO<int> squareElements;
    private static System.Diagnostics.Stopwatch watch;
    private static float angle;
    private static bool exitWhile = false;
    private static bool initOK = true;

    static void Main(string[] args)
    {

        while(exitWhile == false)
        {
            if(initOK == true)
            {
                Init();
            }

        }

    }
    private static void Init()
    {

        Glut.glutInit();
        Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
        Glut.glutInitWindowSize(width, height);
        Glut.glutCreateWindow("OpenGL Arduino");
        SerialPort myArduino = new SerialPort();
        myArduino.BaudRate = 9600;
        myArduino.PortName = "COM3";
        myArduino.Open();
        myArduino.DataReceived += new SerialDataReceivedEventHandler(Comm);

        Glut.glutIdleFunc(OnRenderFrame);
        Glut.glutDisplayFunc(OnDisplay);

        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),
            new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1),
            new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),
            new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1),
            new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1),
            new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) });

        squareColor = 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),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1) });

        squareElements = 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);

        watch = System.Diagnostics.Stopwatch.StartNew();
        initOK = false;
        Glut.glutMainLoop();
    }

     static void Comm(Object Sender, SerialDataReceivedEventArgs args)
    {
        SerialPort sp = (SerialPort)Sender;
        string indata = sp.ReadExisting();
        Console.Write(indata);


        if (indata.Contains("1.10"))
        {
            Vector3[] vertexBuffer = (new Vector3[] {
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 0, 0), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 0, 0), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 1),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
            new Vector3(1, 0, 0), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 0, 0),
            new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1) });

            squareColor.BufferSubData(vertexBuffer);
        }

    }

    private static void OnClose()
    {
        square.Dispose();
        squareColor.Dispose();
        squareColorChange.Dispose();
        squareElements.Dispose();
        program.DisposeChildren = true;
        program.Dispose();
        exitWhile = true;
    }

    private static void OnDisplay()
    {

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

        angle += deltaTime;

        Gl.Viewport(0, 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.BindBufferToShaderAttribute(squareColorChange, 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);
    }
  ";
    public static string Fragmentshader = @"
    in vec3 color;
    out vec4 fragment;
    void main(void)
    {
        fragment = vec4(color, 1);
    }
  ";
}

这可能看起来有点粗糙,因为我在C#中都很新,并在Stackoverflow上提问。

我通过在事件中添加subBuffer来编辑代码,尽管它无论如何都不会呈现新颜色。我忘了做某事吗?

提前致谢!

0 个答案:

没有答案