Exemplo n.º 1
0
        private static void Window_Load(object sender, EventArgs e)
        {
            // ### Basic Initialisations
            Console.WriteLine("Loaded Window");
            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.Enable(EnableCap.DepthTest);

            // ### Create Viewport and Perspective
            GL.Viewport(0, 0, WIDTH, HEIGHT);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Matrix4 m4 = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, (float)WIDTH / HEIGHT, 1.0f, 1000.0f); //The (float) is very important!!!

            GL.LoadMatrix(ref m4);
            GL.MatrixMode(MatrixMode.Modelview);

            // ### Lighting
            //GL.Enable(EnableCap.Lighting);
            //GL.Light(LightName.Light0, LightParameter.Position, new Vector4(0.0f, 4.0f, -2f, 1));
            //GL.Light(LightName.Light0, LightParameter.SpotExponent, 0.01f);
            //GL.Enable(EnableCap.Light0);

            // ### Load OBJ
            fileStream = new FileStream("ViolettBerry05.obj", FileMode.Open);
            objResult  = ObjLoaderController.Load(fileStream);
            fileStream.Close();
            allVertices      = objResult.GetVertices();
            allNormals       = objResult.GetNormals();
            allTextureCoords = objResult.GetTextureCoords();
            Console.WriteLine("Count of Faces: " + objResult.faces.Length);
            Console.WriteLine("Count of Face-Vertices: " + allVertices.Length);
            Console.WriteLine("Count of Face-Normals: " + allNormals.Length);
            Console.WriteLine("Count of Face-TextureCoords: " + allNormals.Length);

            GL.EnableClientState(ArrayCap.VertexArray);

            //Set up Vertex-Array-Object (VAO)
            VAO = GL.GenVertexArray();
            GL.BindVertexArray(VAO);

            //Set up Vertex Buffer (Position Buffer)
            VBO = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Marshal.SizeOf <float>() * allVertices.Length), allVertices, BufferUsageHint.StaticDraw);
            //Add to VAO
            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);

            //Set up Normal Buffer
            normalBuffer = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, normalBuffer);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Marshal.SizeOf <float>() * allNormals.Length), allNormals, BufferUsageHint.StaticDraw);
            //Add to VAO
            GL.EnableVertexAttribArray(1);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, 0);

            //Set up Texture Buffer
            normalBuffer = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.ArrayBuffer, normalBuffer);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Marshal.SizeOf <float>() * allNormals.Length), allNormals, BufferUsageHint.StaticDraw);
            //Add to VAO
            GL.EnableVertexAttribArray(2);
            GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 0, 0);

            GL.BindVertexArray(0); //Damit sichern wir, dass sich nachfolgende Aufrufe von glVertexAttribPointer oder glBindBuffer nicht auf unser VAO auswirken.
            //...
        }