public FrameTimeGraphRenderer(int numSamples, int maxSteps)
 {
     vertexVBO     = new VBO("frametime-vertex");
     colourVBO     = new VBO("frametime-colour");
     indexVBO      = new VBO("frametime-index", OpenTK.Graphics.OpenGL.BufferTarget.ElementArrayBuffer);
     shaderProgram = new ShaderProgram("frametime-program");
 }
Example #2
0
        protected override int Submit(Instance[] instances, int count)
        {
            Array.Sort(keys, instances, 0, count);

            var ncalls = 0;

            var i = 0;

            while (i < count)
            {
                // find the range of the group.
                var k = keys[i].key;
                var j = i;
                while (j < count && k.CompareTo(keys[j].key) == 0)
                {
                    j++;
                }

                var range = j - i;

                // copy the range to the gpu.
                VBO.Update(instances, i, 0, range);

                // submit the render call
                Submit(k, range);
                ncalls++;

                i = j;
            }

            return(ncalls);
        }
Example #3
0
        static void Main(string[] args)
        {
            // create an OpenGL window
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

            // provide the Glut callbacks that are necessary for running this tutorial
            Glut.glutIdleFunc(OnRenderFrame);
            Glut.glutDisplayFunc(OnDisplay);
            Glut.glutCloseFunc(OnClose);

            // compile the shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set the view and projection matrix, which are static throughout this tutorial
            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.Up));

            // create a triangle
            triangle = new VBO<Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(-1, -1, 0), new Vector3(1, -1, 0) });
            triangleElements = new VBO<int>(new int[] { 0, 1, 2 }, BufferTarget.ElementArrayBuffer);

            // create a square
            square = new VBO<Vector3>(new Vector3[] { new Vector3(-1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, -1, 0), new Vector3(-1, -1, 0) });
            squareElements = new VBO<int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);

            Glut.glutMainLoop();
        }
Example #4
0
        static void Main(string[] args)
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

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

            Glut.glutKeyboardFunc(OnKeyboardDown);
            Glut.glutKeyboardUpFunc(OnKeyboardUp);

            Glut.glutCloseFunc(OnClose);
            Glut.glutReshapeFunc(OnReshape);

            Gl.Disable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.Blend);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            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.Up));

            program["light_direction"].SetValue(new Vector3(0, 0, 1));
            program["enable_lighting"].SetValue(lighting);

            glassTexture = new Texture("glass.bmp");

            cube = 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),         // top
                new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1),     // bottom
                new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),         // front face
                new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1),     // back face
                new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1),     // left
                new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) });      // right
            cubeNormals = new VBO<Vector3>(new Vector3[] {
                new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), 
                new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), 
                new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), 
                new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0) });
            cubeUV = new VBO<Vector2>(new Vector2[] {
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) });

            cubeQuads = 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();

            Glut.glutMainLoop();
        }
Example #5
0
        public Planet(string dataBaseName, PlanetParameters param, Planet parent, MeshVBOs vbos, Texture t, ShaderProgram shader, ShaderProgram lineShader)
            : base(vbos, t, shader)
        {
            this.parent = parent;
            this.lineShader = lineShader;
            this.RevolutionOrientation = -1;
            this.AxisTilt = Matrix4.Identity;
            this.DrawAxisTilt = true;

            this.ScenicDistance = param.DFSScenic[dataBaseName];
            this.HoursPerRotation = param.RotationPeriod[dataBaseName];
            this.AxisTilt = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, -1), (float)MathHelper.DegreesToRadians(param.AxialTilt[dataBaseName]));
            this.RealisticRadius = param.RadiusRealistic[dataBaseName];
            this.ScenicRadius = param.RadiusScenic[dataBaseName];
            this.Scale = (float)ScenicRadius;
            this.OrbitalPeriod = param.OrbitalPeriod[dataBaseName];
            this.inclinationAngle = param.InclinationAngle[dataBaseName];
            this.longitudeAscendingNode = param.LongitudeAscendingNode[dataBaseName];
            this.longitudePerihelion = param.LongitudePerihelion[dataBaseName];
            this.eccentricity = param.Eccentricity[dataBaseName];
            this.longitudeAscendingNodeRadians = MathHelper.DegreesToRadians(longitudeAscendingNode);
            this.longitudePerihelionRadians = MathHelper.DegreesToRadians(longitudePerihelion);
            this.inclinationAngleRadians = MathHelper.DegreesToRadians(inclinationAngle);
            this.newRadius = 0;
            this.PeriodAngle = longitudePerihelionRadians;
            this.eccentricAnomaly = 0;
            this.createTransform();
            if (parent != null)
                this.Orbit = new Orbit(this, parent.Position, this.ScenicDistance, 360, lineShader, this.inclinationAngle, this.longitudeAscendingNode, this.longitudePerihelion, this.eccentricity);
            float[] axisVerts = { 0, -2, 0, 0, 2, 0 };
            this.axisLine = new VBO<float>(axisVerts, BufferTarget.ArrayBuffer, VBO<float>.PointerType.vertex);
        }
        public VAO CreateString(ShaderProgram Program, string Text)
        {
            Vector3[] vertices = new Vector3[Text.Length * 4];
            Vector2[] uvs = new Vector2[Text.Length * 4];
            int[] indices = new int[Text.Length * 6];

            for (int i = 0; i < Text.Length; i++)
            {
                // Note: These are fixed width fonts so just use 2x2 quads (-1..1)
                vertices[i * 4 + 0] = new Vector3(-1 + i * 2, 1, 0);
                vertices[i * 4 + 1] = new Vector3(-1 + i * 2, -1, 0);
                vertices[i * 4 + 2] = new Vector3(1 + i * 2, 1, 0);
                vertices[i * 4 + 3] = new Vector3(1 + i * 2, -1, 0);

                UVPair ch = Character[Text[i] > 256 ? ' ' : Text[i]];
                uvs[i * 4 + 0] = new Vector2(ch.Topleft.x, ch.BottomRight.y);
                uvs[i * 4 + 1] = ch.Topleft;
                uvs[i * 4 + 2] = ch.BottomRight;
                uvs[i * 4 + 3] = new Vector2(ch.BottomRight.x, ch.Topleft.y);

                indices[i * 6 + 0] = i * 4 + 1;
                indices[i * 6 + 1] = i * 4 + 0;
                indices[i * 6 + 2] = i * 4 + 2;
                indices[i * 6 + 3] = i * 4 + 1;
                indices[i * 6 + 4] = i * 4 + 2;
                indices[i * 6 + 5] = i * 4 + 3;
            }

            // Create the vertex buffer objects and then create the array object
            VBO<Vector3> vertexArray = new VBO<Vector3>(vertices, BufferTarget.ArrayBuffer, BufferUsageHint.StaticRead);
            VBO<Vector2> uvArray = new VBO<Vector2>(uvs, BufferTarget.ArrayBuffer, BufferUsageHint.StaticRead);
            VBO<int> elementArray = new VBO<int>(indices, BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticRead);

            return new VAO(Program, vertexArray, uvArray, elementArray);
        }
Example #7
0
        public virtual void Load(int width, int height)
        {
            Width = width;
            Height = height;
            _page = new Texture();
            GUIShader = Assets.Fetch<ShaderProgram>("GUI");
            GUIShader.AddUniform("scale");
            VBO = new VBO<VertexPositionNormalTexture>();
            VAO = new VAO<VertexPositionNormalTexture>();
            VBO.Buffer(new[]{
                new VertexPositionNormalTexture{Position = new Vector3(0, Height, 0f),Normal = Vector3.UnitZ,TexCoord = new Vector2(0, Height)},
                new VertexPositionNormalTexture{Position = new Vector3(Width, Height, 0f),Normal = Vector3.UnitZ,TexCoord = new Vector2(Width, Height)},
                new VertexPositionNormalTexture{Position = new Vector3(Width, 0, 0f),Normal = Vector3.UnitZ,TexCoord = new Vector2(Width, 0)},
                new VertexPositionNormalTexture{Position = new Vector3(0, 0, 0),Normal = Vector3.UnitZ,TexCoord = new Vector2(0,0)}
            });
            VAO.Setup(GUIShader, VBO);
            _webView = WebCore.CreateWebView(Width, Height);
            WebCore.BaseDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GUI");
            _webView.FlushAlpha = false;
            _webView.IsTransparent = true;
            _webView.CreateObject("console");
            _webView.SetObjectCallback("console", "log", (sender, e) => Bus.Add(new DebugMessage(Timer.LastTickTime, e.Arguments[0].ToString())));

            _webView.CreateObject("GUI");
            _webView.CreateObject("Bus");

            _webView.LoadFile("index.htm");
        }
Example #8
0
 public FontVAO(ShaderProgram program, VBO<Vector3> vertices, VBO<Vector2> uvs, VBO<int> triangles)
 {
     this.program = program;
     this.vertices = vertices;
     this.uvs = uvs;
     this.triangles = triangles;
 }
Example #9
0
        /// <summary>
        /// Generates the vertices, normals and indices and creates them for the OpenGL.
        /// This method has to be called once before drawing.
        /// </summary>
        /// <param name="gl"></param>
        public void GenerateGeometry(OpenGL gl, OGLModelUsage usage)
        {
            GL     = gl;
            _usage = usage;

            // Create the data buffers.
            var buffers = OGLBufferId.CreateBufferIds(gl, 3);

            IndexBuffer  = new IBO(buffers[0]);
            NormalBuffer = new VBO(buffers[1]);
            VertexBuffer = new VBO(buffers[2]);

            if (AutoCalculateNormals)
            {
                CalculateNormals();
            }

            var vertData = Vertices.SelectMany(v => v.to_array()).ToArray();

            VertexBuffer.BindBuffer(gl);                                                         // GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer.Buffer.Value);
            VertexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, vertData, usage, 3); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, vertData, (uint)usage);

            var normData = Normals.SelectMany(v => v.to_array()).ToArray();

            NormalBuffer.BindBuffer(gl);                                                         //GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, NormalBuffer.Buffer.Value);
            NormalBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, normData, usage, 3); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, normData, (uint)usage);

            IndexBuffer.BindBuffer(gl);                                                          // GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, IndexBuffer.Buffer.Value);
            IndexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, Indices, usage, 1);   // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, Indices, (uint)usage);

            if (new OGLModelUsage[] { OGLModelUsage.StaticCopy, OGLModelUsage.StaticDraw, OGLModelUsage.StaticRead }.Contains(usage))
            {
                ClearStaticData();
            }
        }
Example #10
0
        public Sun(string dataBaseName, PlanetParameters param, Planet parent, MeshVBOs vbos, Texture t, ShaderProgram shader, ShaderProgram lineShader)
            : base(dataBaseName, param, parent, vbos, t, shader, lineShader)
        {
            bufferVerts = new VBO<float>(new float[] { -1, 1, 0, 1, 1, 0, 1, -1, 0, -1, -1, 0 }, BufferTarget.ArrayBuffer, VBO<float>.PointerType.vertex);
            bufferElems = new VBO<ushort>(new ushort[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer, VBO<ushort>.PointerType.element);

            squareUvs = new VBO<float>(new float[] { 0, 1, 1, 1, 1, 0, 0, 0 }, BufferTarget.ArrayBuffer, VBO<float>.PointerType.texCoord);
            squareVerts = new VBO<float>(new float[] { -2, 2, 0, 2f, 2f, 0, 2f, -2f, 0, -2f, -2f, 0 }, BufferTarget.ArrayBuffer, VBO<float>.PointerType.vertex);
            squareElems = new VBO<ushort>(new ushort[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer, VBO<ushort>.PointerType.element);

            this.coronaShader = new ShaderProgram(File.ReadAllText("content/shaders/coronaShader.vert"), File.ReadAllText("content/shaders/coronaShader.frag"), "coronaShader");
            Console.WriteLine(coronaShader.ProgramInfoLog);
            coronaShader.setUniform("proj", Form1.projectionMatrix);
            coronaShader.setUniform("model", Matrix4.Identity);

            this.billboardShader = new ShaderProgram(File.ReadAllText("content/shaders/billboardShader.vert"), File.ReadAllText("content/shaders/billboardShader.frag"), "billboardShader");
            Console.WriteLine(billboardShader.ProgramInfoLog);
            billboardShader.setUniform("proj", Form1.projectionMatrix);
            billboardShader.setUniform("model", Matrix4.Identity);

            frameBufferTime = 0;
            bufferResolution = new Vector2(1024, 1024);

            bufferTexture = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, bufferTexture);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)bufferResolution.X, (int)bufferResolution.Y, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);

            frameBuffer = GL.GenFramebuffer();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, bufferTexture, 0);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); // make sure to set the framebuffer back to the default, or else bugs,
            //bugs that take two hours to debug
        }
Example #11
0
        public static VBO<Vector3> BufferData(VBO<Vector3> vbo, Vector3[] data, GCHandle handle)
        {
            if (vbo == null) return new VBO<Vector3>(data, BufferTarget.ArrayBuffer, BufferUsageHint.StaticDraw);

            vbo.BufferSubDataPinned(BufferTarget.ArrayBuffer, 12 * data.Length, handle.AddrOfPinnedObject());
            return vbo;
        }
Example #12
0
 public FontVAO(ShaderProgram program, VBO <Vector3> vertices, VBO <Vector2> uvs, VBO <uint> triangles)
 {
     this.program   = program;
     this.vertices  = vertices;
     this.uvs       = uvs;
     this.triangles = triangles;
 }
Example #13
0
        public static void Draw3DPlotLeft(float[] data, float depth, Vector3 color, Matrix4 viewMatrix, bool log = false)
        {
            if (data.Length < 441) throw new ArgumentException("The argument data was not the correct length.");

            for (int i = 0; i < fftData.Length; i++)
                fftData[i] = new Vector3((log ? Math.Log10(i) * 166 : i) - 441 / 2f, Math.Max(-200, Math.Min(200, 200 * data[i])), depth);
                //fftData[i] = new Vector3(i - 441 / 2f, Math.Max(-200, Math.Min(200, 200 * data[i])), depth);

            if (fftVAO == null)
            {
                int[] array = new int[441];
                for (int i = 0; i < array.Length; i++) array[i] = i;

                fftHandle = GCHandle.Alloc(fftData, GCHandleType.Pinned);
                fftVBO = BufferData(fftVBO, fftData, fftHandle);
                fftVAO = new VAO<Vector3>(Shaders.SimpleColoredShader, fftVBO, "in_position", new VBO<int>(array, BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticDraw));
                fftVAO.DrawMode = BeginMode.LineStrip;
            }
            else fftVBO = BufferData(fftVBO, fftData, fftHandle);

            Shaders.SimpleColoredShader.Use();
            Shaders.SimpleColoredShader["projectionMatrix"].SetValue(Program.uiProjectionMatrix);
            Shaders.SimpleColoredShader["viewMatrix"].SetValue(viewMatrix);
            Shaders.SimpleColoredShader["modelMatrix"].SetValue(Matrix4.CreateTranslation(new Vector3(72 + 441 / 2f, 288, 0)));
            Shaders.SimpleColoredShader["color"].SetValue(color);
            fftVAO.Draw();
        }
Example #14
0
        static void Main(string[] args)
        {
            // create an OpenGL window
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE
                                     | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

            // provide the Glut callbacks that are necessary for running this tutorial
            Glut.glutIdleFunc(OnRenderFrame);
            Glut.glutDisplayFunc(OnDisplay);
            Gl.Enable(EnableCap.DepthTest);

            program = new ShaderProgram(VertexShader, FragmentShader);

            // set the view and projection matrix, which are static throughout this tutorial
            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, new Vector3(0, 1, 0)));

            // create a triangle
            squareVertexes = new VBO <Vector3>(new Vector3[] { new Vector3(-1, -1, 0),
                                                               new Vector3(-1, 1, 0),
                                                               new Vector3(1, 1, 0),
                                                               new Vector3(1, -1, 0) });
            squareElements = new VBO <int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);

            squares = Reader.readFromFile("input.txt");

            watch = System.Diagnostics.Stopwatch.StartNew();

            Glut.glutMainLoop();
        }
Example #15
0
        static void Main(string[] args)
        {
            // create an OpenGL window
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

            // provide the Glut callbacks that are necessary for running this tutorial
            Glut.glutIdleFunc(OnRenderFrame);
            Glut.glutDisplayFunc(OnDisplay);
            Glut.glutCloseFunc(OnClose);

            // compile the shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set the view and projection matrix, which are static throughout this tutorial
            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, new Vector3(0, 1, 0)));

            // create a triangle
            triangle         = new VBO <Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(-1, -1, 0), new Vector3(1, -1, 0) });
            triangleElements = new VBO <uint>(new uint[] { 0, 1, 2 }, BufferTarget.ElementArrayBuffer);

            // create a square
            square         = new VBO <Vector3>(new Vector3[] { new Vector3(-1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, -1, 0), new Vector3(-1, -1, 0) });
            squareElements = new VBO <uint>(new uint[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);

            Glut.glutMainLoop();
        }
Example #16
0
 public Square(Vector3[] v, Vector3[] c)
 {
     this.v   = v;
     this.c   = c;
     square   = new VBO <Vector3>(v);
     elements = new VBO <uint>(new uint[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);
     color    = new VBO <Vector3>(c);
 }
Example #17
0
 public MeshVBOs(VBO<float> verts, VBO<float> uvs, VBO<float> normals, VBO<ushort> elements)
 {
     Verts = verts;
     Uvs = uvs;
     Normals = normals;
     Elements = elements;
     this.ElementsSize = Elements.Size;
 }
Example #18
0
        public static void BindBufferToShaderAttribute <T>(VBO <T> buffer, ShaderProgram program, string attributeName) where T : struct
        {
            uint attribLocation = (uint)GL.GetAttribLocation(program.ProgramId, attributeName);

            GL.EnableVertexAttribArray(attribLocation);
            BindBuffer <T>(buffer);
            GL.VertexAttribPointer(attribLocation, buffer.Size, buffer.PointerType, true, Marshal.SizeOf(typeof(T)), IntPtr.Zero);
        }
Example #19
0
        public void Dispose()
        {
            vertices.Dispose();
            uvs.Dispose();
            triangles.Dispose();

            vertices = null;
        }
Example #20
0
        public void Dispose()
        {
            vertices.Dispose();
            uvs.Dispose();
            triangles.Dispose();

            vertices = null;
        }
Example #21
0
        static void Main(string[] args)
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

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

            Glut.glutKeyboardFunc(OnKeyboardDown);
            Glut.glutKeyboardUpFunc(OnKeyboardUp);

            Glut.glutCloseFunc(OnClose);
            Glut.glutReshapeFunc(OnReshape);

            // enable blending and set to accumulate the star colors
            Gl.Disable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.Blend);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);

            // create our shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set up the projection and view matrix
            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, 20), Vector3.Zero, new Vector3(0, 1, 0)));

            // load the star texture
            starTexture = new Texture("star.bmp");

            // each star is simply a quad
            star      = new VBO <Vector3>(new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(1, 1, 0), new Vector3(-1, 1, 0) });
            starUV    = new VBO <Vector2>(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) });
            starQuads = new VBO <int>(new int[] { 0, 1, 2, 0, 2, 3 }, BufferTarget.ElementArrayBuffer);

            // create 50 stars for this tutorial
            int numStars = 50;

            for (int i = 0; i < numStars; i++)
            {
                stars.Add(new Star(0, (float)i / numStars * 4f, new Vector3((float)generator.NextDouble(), (float)generator.NextDouble(), (float)generator.NextDouble())));
            }

            font        = new BMFont("font24.fnt", "font24.png");
            fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource);

            fontProgram.Use();
            fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000));
            fontProgram["color"].SetValue(new Vector3(1, 1, 1));

            information = font.CreateString(fontProgram, "OpenGL  C#  Tutorial  10");

            watch = System.Diagnostics.Stopwatch.StartNew();

            Glut.glutMainLoop();
        }
Example #22
0
        static void Main(string[] args)
        {
            // create an OpenGL window
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

            // provide the Glut callbacks that are necessary for running this tutorial
            Glut.glutIdleFunc(OnRenderFrame);
            Glut.glutDisplayFunc(OnDisplay);
            Glut.glutCloseFunc(OnClose);

            // enable depth testing to ensure correct z-ordering of our fragments
            Gl.Enable(EnableCap.DepthTest);

            // compile the shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set the view and projection matrix, which are static throughout this tutorial
            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.Up));

            // create a pyramid with vertices and colors
            pyramid = new VBO<Vector3>(new Vector3[] {
                new Vector3(0, 1, 0), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),        // front face
                new Vector3(0, 1, 0), new Vector3(1, -1, 1), new Vector3(1, -1, -1),        // right face
                new Vector3(0, 1, 0), new Vector3(1, -1, -1), new Vector3(-1, -1, -1),      // back face
                new Vector3(0, 1, 0), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1) });   // left face
            pyramidColor = new VBO<Vector3>(new Vector3[] {
                new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1),
                new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 1, 0),
                new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1),
                new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 1, 0) });
            pyramidTriangles = new VBO<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, BufferTarget.ElementArrayBuffer);

            // create a cube with vertices and colors
            cube = 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) });
            cubeColor = new VBO<Vector3>(new Vector3[] {
                new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0),
                new Vector3(1, 0.5, 0), new Vector3(1, 0.5, 0), new Vector3(1, 0.5, 0), new Vector3(1, 0.5, 0),
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0),
                new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0),
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1),
                new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1) });
            cubeQuads = 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();

            Glut.glutMainLoop();
        }
Example #23
0
        static void Main(string[] args)
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

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

            Glut.glutKeyboardFunc(OnKeyboardDown);
            Glut.glutKeyboardUpFunc(OnKeyboardUp);

            Glut.glutCloseFunc(OnClose);
            Glut.glutReshapeFunc(OnReshape);

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

            program["light_direction"].SetValue(new Vector3(0, 0, 1));
            program["enable_lighting"].SetValue(lighting);

            crateTexture = new Texture("crate.jpg");

            cube = 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),         // top
                new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1),     // bottom
                new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),         // front face
                new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1),     // back face
                new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1),     // left
                new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) });      // right
            cubeNormals = new VBO<Vector3>(new Vector3[] {
                new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), 
                new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), 
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), 
                new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), 
                new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), 
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0) });
            cubeUV = new VBO<Vector2>(new Vector2[] {
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) });

            cubeQuads = 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();

            Glut.glutMainLoop();
        }
Example #24
0
        public Orbit(Planet parent, Vector3 center, double radius, int sides, ShaderProgram shader, double ia, double lan, double lp, double ecc)
        {
            this.eccentricAnomaly = 0;
            this.shader = shader;
            this.parent = parent;
            this.Positon = center;
            this.Radius = (float)radius;
            this.Sides = sides;

            this.inclinationAngleRadians = MathHelper.DegreesToRadians(ia);
            this.longitudeAscendingNodeRadians = MathHelper.DegreesToRadians(lan);
            this.longitudePerihelionRadians = MathHelper.DegreesToRadians(lp);
            this.eccentricity = ecc;

            float step = MathHelper.DegreesToRadians(360f / sides);

            List<float> verts = new List<float>();
            List<float> angles = new List<float>();
            List<ushort> elements = new List<ushort>();

            float alpha = (float)longitudePerihelionRadians;
            ushort count = 0;
            while (alpha < Math.PI * 2f + longitudePerihelionRadians)
            {
                if (alpha < Math.PI + longitudePerihelionRadians)
                    this.eccentricAnomaly += step;
                if (alpha > Math.PI + longitudePerihelionRadians)
                    this.eccentricAnomaly -= step;
                this.newRadius = (float)((radius / (1 - this.eccentricity)) * (1 - (this.eccentricity * Math.Cos(this.eccentricAnomaly))));
                //verts.Add((float)Math.Cos(alpha) * Radius + center.X);
                //verts.Add(0f + center.Y);
                //verts.Add((float)Math.Sin(alpha) * Radius + center.Z);
                //verts.Add(Radius * ((float)Math.Cos(alpha + longitudeAscendingNodeRadians) - (float)Math.Sin(alpha + longitudeAscendingNodeRadians) * (float)Math.Cos(inclinationAngleRadians)) + center.Z);
                //verts.Add(Radius * ((float)Math.Sin(alpha + longitudeAscendingNodeRadians) * (float)Math.Sin(inclinationAngleRadians)) + center.Y);
                //verts.Add(Radius * ((float)Math.Sin(alpha + longitudeAscendingNodeRadians) + (float)Math.Cos(alpha + longitudeAscendingNodeRadians) * (float)Math.Cos(inclinationAngleRadians)) + center.X);

                //verts.Add(newRadius * ((float)Math.Cos(alpha) * (float)Math.Cos(longitudeAscendingNodeRadians) - (float)Math.Sin(longitudeAscendingNodeRadians) * (float)Math.Sin(alpha) * (float)Math.Sin(inclinationAngleRadians) ) + center.X);
                //verts.Add(newRadius * ((float)Math.Sin(alpha) * (float)Math.Sin(inclinationAngleRadians)) + center.Y);
                //verts.Add(newRadius * ((float)Math.Sin(alpha) * (float)Math.Cos(longitudeAscendingNodeRadians) + (float)Math.Cos(longitudeAscendingNodeRadians) * (float)Math.Sin(alpha) * (float)Math.Cos(inclinationAngleRadians)) + center.Z);

                verts.Add(newRadius * (float)Math.Cos(alpha) + center.X);
                verts.Add(newRadius * (float)inclinationAngleRadians * (float)Math.Sin(alpha - longitudeAscendingNodeRadians) + center.Y);
                verts.Add(newRadius * (float)Math.Sin(alpha) + center.Z);

                elements.Add(count);
                angles.Add(alpha);
                alpha += step;
                count++;
            }

            float[] vertsf = verts.ToArray();
            float[] anglesf = angles.ToArray();
            ushort[] elementsf = elements.ToArray();

            this.vertsBuffer = new VBO<float>(vertsf, BufferTarget.ArrayBuffer, VBO<float>.PointerType.vertex);
            this.anglesBuffer = new VBO<float>(anglesf, BufferTarget.ArrayBuffer, VBO<float>.PointerType.color);
            this.elementsBuffer = new VBO<ushort>(elementsf, BufferTarget.ElementArrayBuffer, VBO<ushort>.PointerType.element);
        }
Example #25
0
        /// <summary>
        /// (Internal) Draws the control on the provided VBO.
        /// </summary>
        /// <param name="vbo">UI VBO on which to draw the control.</param>
        internal override void UpdateVBOTiles(VBO vbo)
        {
            if (string.IsNullOrEmpty(Text_))
            {
                return;
            }

            DrawTextOnVBO(vbo, Text_, Position.X, Position.Y, FontTile_, Color, TileEffect);
        }
Example #26
0
        static void Main(string[] args)
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

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

            Glut.glutKeyboardFunc(OnKeyboardDown);
            Glut.glutKeyboardUpFunc(OnKeyboardUp);

            Glut.glutCloseFunc(OnClose);
            Glut.glutReshapeFunc(OnReshape);

            // enable blending and set to accumulate the star colors
            Gl.Disable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.Blend);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);

            // create our shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set up the projection and view matrix
            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, 20), Vector3.Zero, Vector3.Up));

            // load the star texture
            starTexture = new Texture("star.bmp");

            // each star is simply a quad
            star = new VBO<Vector3>(new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(1, 1, 0), new Vector3(-1, 1, 0) });
            starUV = new VBO<Vector2>(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) });
            starQuads = new VBO<int>(new int[] { 0, 1, 2, 0, 2, 3 }, BufferTarget.ElementArrayBuffer);

            // create 50 stars for this tutorial
            int numStars = 50;
            for (int i = 0; i < numStars; i++)
            {
                stars.Add(new Star(0, (float)i / numStars * 4f, new Vector3(generator.NextDouble(), generator.NextDouble(), generator.NextDouble())));
            }

            font = new BMFont("font24.fnt", "font24.png");
            fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource);

            fontProgram.Use();
            fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000));
            fontProgram["color"].SetValue(new Vector3(1, 1, 1));

            information = font.CreateString(fontProgram, "OpenGL  C#  Tutorial  10");

            watch = System.Diagnostics.Stopwatch.StartNew();

            Glut.glutMainLoop();
        }
Example #27
0
        internal override void UpdateVBOTiles(VBO vbo)
        {
            base.UpdateVBOTiles(vbo);

            if (ReadInputs_)
            {
                vbo.UpdateTileData(Position.X + Text.Length, Position.Y, FontTile + 63, Color, Tilemap, TileVFX.BlinkFast);
            }
        }
Example #28
0
        public Gizmo()
        {
            shader = new ShaderProgram(VertexShaderSource, FragmentShaderSource);
            shader["modelMatrix"].SetValue(Matrix4.Identity);
            shader["projectionMatrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.6f, 1280f / 720f, 0.1f, 1000f));
            shader["viewMatrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 4), Vector3.Zero, Vector3.Up));

            double size = 0.05;

            Vector3[] verts = new Vector3[] {
                // cube in the middle
                new Vector3(-size, -size, -size), new Vector3(size, -size, -size), new Vector3(size, -size, size), new Vector3(-size, -size, size),
                new Vector3(-size, size, -size), new Vector3(size, size, -size), new Vector3(size, size, size), new Vector3(-size, size, size),
                // unit x
                new Vector3(1 + size, size, size), new Vector3(1 + size, size, -size), new Vector3(1 + size, -size, -size),new Vector3(1 + size, -size, size),
                // unit z
                new Vector3(-size, size, 1+ size), new Vector3(size, size, 1+ size), new Vector3(size, -size, 1+size), new Vector3(-size, -size, 1+ size),
                // unit y
                new Vector3(-size, 1 + size, -size), new Vector3(size, 1 + size, -size),new Vector3(size, 1 + size, size),new Vector3(-size, 1 + size, size)
            };

            vertices = new VBO<Vector3>(verts);

            Vector3[] cols = 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, 0, 0),new Vector3(1, 0, 0),new Vector3(1, 0, 0),new Vector3(1, 0, 0),
                new Vector3(0, 0, 1),new Vector3(0, 0, 1),new Vector3(0, 0, 1),new Vector3(0, 0, 1),
                new Vector3(0, 1, 0),new Vector3(0, 1, 0),new Vector3(0, 1, 0),new Vector3(0, 1, 0)
            };

            colors = new VBO<Vector3>(cols);
            indices = new VBO<int>(new int[] {
                // center cube
                0, 2, 1, 0, 3, 2,
                4, 7, 3, 4, 3, 0,
                5, 4, 0, 5, 0, 1,
                // unit x
                6, 5, 9, 6, 9, 8,
                6, 8, 11, 6, 11, 2,
                9, 5, 1, 9, 1, 10,
                2, 11, 10, 2, 10, 1,
                8, 9, 10, 8, 10, 11,
                // unit z
                7, 6, 13, 7, 13, 12,
                7, 12, 15, 7, 15, 3,
                13, 6, 2, 13, 2, 14,
                3, 15, 14, 3, 14, 2,
                12, 13, 14, 12, 14, 15,
                // unit y
                19, 18, 6, 19, 6, 7,
                18, 17, 5, 18, 5, 6,
                17, 16, 4, 17, 4, 5,
                16, 19, 7, 16, 7, 4,
                16, 17, 18, 16, 18, 19
            }, BufferTarget.ElementArrayBuffer);
        }
Example #29
0
        public Mesh(Vector2[] vertices, PrimitiveType pt)
        {
            primitiveType = pt;

            vertexBuffer = new VBO <Vector2>();
            uvBuffer     = new VBO <Vector2>();

            Vertices = vertices;
        }
Example #30
0
        public SpriteVAO(ShaderProgram program, VBO <Vector2> vertex, VBO <uint> element)
            : base(program)
        {
            List <IGenericVBO> vbos = new List <IGenericVBO>();

            vbos.Add(new GenericVBO <Vector2>(vertex, "in_position"));
            vbos.Add(new GenericVBO <uint>(element));
            Init(vbos.ToArray());
        }
Example #31
0
 public void Dispose()
 {
     if (!disposed)
     {
         return;
     }
     VBO.Dispose();
     IBO.Dispose();
     disposed = true;
 }
Example #32
0
        public static void Main()
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("Lyubo's Coursework");

            Glut.glutIdleFunc(RenderFrame);

            Glut.glutKeyboardFunc(KeyboardPress);

            Gl.Enable(EnableCap.DepthTest);

            ShaderProgram = new ShaderProgram(VertexShader, FragmentShader);

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

            pyramid = new VBO <Vector3>(new Vector3[] {
                new Vector3(0, 1, 0), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),
                new Vector3(0, 1, 0), new Vector3(1, -1, 1), new Vector3(1, -1, -1),
                new Vector3(0, 1, 0), new Vector3(1, -1, -1), new Vector3(-1, -1, -1),
                new Vector3(0, 1, 0), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1)
            });
            pyramidColor = new VBO <Vector3>(new Vector3[] {
                new Vector3(0, 1, 1), new Vector3(0, 1, 1), new Vector3(0, 1, 1),
                new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0),
                new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1),
                new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0)
            });
            pyramidTriangles = new VBO <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, BufferTarget.ElementArrayBuffer);

            cube = 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)
            });
            cubeColor = new VBO <Vector3>(new Vector3[] {
                new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0),
                new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1),
                new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0),
                new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1),
                new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0),
                new Vector3(0, 1, 1), new Vector3(0, 1, 1), new Vector3(0, 1, 1), new Vector3(0, 1, 1)
            });
            cubeQuads = 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);

            stopWatch = Stopwatch.StartNew();

            Glut.glutMainLoop();
        }
Example #33
0
 public VBO<VertexPositionNormalColour> Load(string name, GetVBO action)
 {
     if (_buffers.ContainsKey(name))
     {
         return _buffers[name];
     }
     var vbo = new VBO<VertexPositionNormalColour>();
     action(vbo);
     _buffers.Add(name,vbo);
     return vbo;
 }
        static void Main(string[] args)
        {
            // create an OpenGL window
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

            // provide the Glut callbacks that are necessary for running this tutorial
            Glut.glutIdleFunc(OnRenderFrame);
            Glut.glutDisplayFunc(OnDisplay);
            Glut.glutCloseFunc(OnClose);

            // enable depth testing to ensure correct z-ordering of our fragments
            Gl.Enable(OpenGL.EnableCap.DepthTest);

            // compile the shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // set the view and projection matrix, which are static throughout this tutorial
            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, new Vector3(0, 1, 0)));

            // load a crate texture
            crateTexture = new Texture("res/textures/willem_side.png");

            // create a crate with vertices and UV coordinates

            cube[0] = 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)
            });
            cubeUV[0] = new VBO <Vector2>(new Vector2[] {
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1)
            });

            //crateTexture = new Texture("res/textures/crate.jpg");

            cubeQuads = new VBO <uint>(new uint[] { 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();

            Glut.glutMainLoop();
        }
Example #35
0
        static void Main()
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Toutorial");

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

            Gl.Enable(EnableCap.DepthTest);

            ShaderProgram = new ShaderProgram(VertexShader, FragmentShader);

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

            CrateTexture = new Texture("Grass1.jpg");

            cube = 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)
            });
            int x1 = 500; int y1 = -5; int z1 = 10;
            int x2 = -500; int y2 = -50; int z2 = -1000;

            cube = new VBO <Vector3>(new Vector3[] {
                new Vector3(x1, y1, z2), new Vector3(x2, y1, z2), new Vector3(x2, y1, z1), new Vector3(x1, y1, z1), //top
                new Vector3(x1, y2, z1), new Vector3(x2, y2, z1), new Vector3(x2, y2, z2), new Vector3(x1, y2, z2), //bottom
                new Vector3(x1, y1, z1), new Vector3(x2, y1, z1), new Vector3(x2, y2, z1), new Vector3(x1, y2, z1), //front face
                new Vector3(x1, y2, z2), new Vector3(x2, y2, z2), new Vector3(x2, y1, z2), new Vector3(x1, y1, z2), //back face
                new Vector3(x2, y1, z1), new Vector3(x2, y1, z2), new Vector3(x2, y2, z2), new Vector3(x2, y2, z1), //left face
                new Vector3(x1, y1, z2), new Vector3(x1, y1, z1), new Vector3(x1, y2, z1), new Vector3(x1, y2, z2)
            });                                                                                                     //right face
            cubeUV = new VBO <Vector2>(new Vector2[] {
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1)
            });
            cubeQuads = 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();

            Glut.glutMainLoop();
        }
Example #36
0
 public void CreateCustomShader(Vector3[] posData, Vector3[] colorData)
 {
     int[] dataQueue = new int[posData.Length];
     for (int i = 0; i < posData.Length; i++)
     {
         dataQueue[i] = i;
     }
     MinAndMaxCoords(posData);
     _shaderVertices  = new VBO <Vector3>(posData);
     _shaderDrawQueue = new VBO <int>(dataQueue, BufferTarget.ElementArrayBuffer);
     _shaderColors    = new VBO <Vector3>(colorData);
 }
Example #37
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // make sure the shader program and texture are being used
            Gl.UseProgram(program.ProgramID);
            Gl.BindTexture(particleTexture);

            // update our particle list
            for (int i = 0; i < particles.Count; i++)
            {
                particles[i].Update(deltaTime);
                //if (particles[i].Life < 0) particles[i] = new Particle(Vector3.Zero);
                if (particles[i].Life < 0)
                {
                    particles[i] = new Particle(new Vector3((float)generator.NextDouble() * 200 - 100, 6, 0));
                }
                particlePositions[i] = particles[i].Position;
            }

            // delete our previous particle positions (if applicable) and then create a new VBO
            if (particleVertices != null)
            {
                particleVertices.Dispose();
            }
            particleVertices = new VBO <Vector3>(particlePositions);

            // bind the VBOs to their shader attributes
            Gl.BindBufferToShaderAttribute(particleVertices, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(particleColors, program, "vertexColor");
            Gl.BindBuffer(particlePoints);

            // enable point sprite mode (which enables the gl_PointCoord value)
            Gl.Enable(EnableCap.PointSprite);
            Gl.DrawElements(BeginMode.Points, particlePoints.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.Disable(EnableCap.PointSprite);

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }
Example #38
0
        /// <summary>
        /// (Internal) Draws the control on the provided VBO.
        /// </summary>
        /// <param name="vbo">UI VBO on which to draw the control.</param>
        internal override void UpdateVBOTiles(VBO vbo)
        {
            int x, y;

            for (x = 0; x < Tiles.GetLength(0); x++)
            {
                for (y = 0; y < Tiles.GetLength(1); y++)
                {
                    vbo.UpdateTileData(x + Position.X, y + Position.Y, Tiles[x, y].TileIndex, Tiles[x, y].Color, Tiles[x, y].Tilemap, Tiles[x, y].VFX);
                }
            }
        }
Example #39
0
        public static void SquareDrawing()
        {
            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, new Vector3(0, 1, 0)));

            square         = new VBO <Vector3>(new Vector3[] { new Vector3(-1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, -1, 0), new Vector3(-1, -1, 0) });
            squareColor    = new VBO <Vector3>(new Vector3[] { new Vector3(0.5f, 1.5f, 1), new Vector3(0.5f, 0.5f, 1), new Vector3(1.5f, 0.5f, 1), new Vector3(0.5f, 0.5f, 0.8f) });
            SquareElements = new VBO <int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer);
        }
Example #40
0
        private void BuildFrameData()
        {
            meshVbo = new VBO();
            meshVbo.SetData(new[] {
                new Vertex(0.0f, 0.5f, Color4.Red),
                new Vertex(-0.5f, -0.5f, Color4.Green),
                new Vertex(0.5f, -0.5f, Color4.Blue)
            });
            meshVao = new VAO(3);             // 3 вершины
            // Нулевой атрибут вершины – позиция, у неё 2 компонента типа float
            meshVao.AttachVBO(0, meshVbo, 2, VertexAttribPointerType.Float, 5 * sizeof(float), 0);
            // Первый атрибут вершины – цвет, у него 3 компонента типа float
            meshVao.AttachVBO(1, meshVbo, 3, VertexAttribPointerType.Float, 5 * sizeof(float), 2 * sizeof(float));

            shaderProgram = new ShaderProgram();

            using (var vertexShader = new Shader(ShaderType.VertexShader))
                using (var fragmentShader = new Shader(ShaderType.FragmentShader))
                {
                    System.Console.WriteLine("vertexShader.Compile");
                    vertexShader.Compile(@"
					#version 400

					layout(location = 0) in vec2 Position;
					layout(location = 1) in vec3 Color;

					out vec3 fragColor;

					void main()
					{
                        gl_Position = vec4(Position, 0.0, 1.0);
                        fragColor = Color;
					}
					"                    );
                    fragmentShader.Compile(@"
					#version 400

					in vec3 fragColor;

					layout(location = 0) out vec4 outColor;

					void main()
					{
                        outColor = vec4(fragColor, 1.0);
					}
					"                    );
                    System.Console.WriteLine("shaderProgram.AttachShader(vertexShader);");
                    shaderProgram.AttachShader(vertexShader);
                    shaderProgram.AttachShader(fragmentShader);
                    shaderProgram.Link();
                }
        }
Example #41
0
        private TextureFilter(GLProgram program)
        {
            _program = program;
            _uniforms = new TextureUniforms (_program, new Sampler2D (0).LinearFiltering ()
                .ClampToEdges (Axes.X | Axes.Y));

            _framebuffer = new Framebuffer (FramebufferTarget.Framebuffer);

            var rectangle = Quadrilateral<TexturedVertex>.Rectangle (2f, 2f);
            rectangle.ApplyTextureFront (1f, new Vec2 (0f), new Vec2 (1f));
            _vertexBuffer = new VBO<TexturedVertex> (rectangle.Vertices, BufferTarget.ArrayBuffer);
            _indexBuffer = new VBO<int> (rectangle.Indices, BufferTarget.ElementArrayBuffer);
        }
Example #42
0
        public void InitTexcoordVBO()
        {
            this.texcoord = new Vector2[NUMVERTICES];

            for (int i = 0; i < NUMVERTICES; i++)
            {
                this.texcoord[i] = Vector2.Zero;
            }

            this.texcoordVBO = new VBO(this.Name + "_texcoord", BufferTarget.ArrayBuffer, BufferUsageHint.DynamicDraw);
            this.texcoordVBO.SetData(this.texcoord);
            this.IsTexcoordVBOLoaded = true;
        }
Example #43
0
        public void InitColourVBO()
        {
            this.colour = new Vector4[NUMVERTICES];

            for (int i = 0; i < NUMVERTICES; i++)
            {
                this.colour[i] = Vector4.One;
            }

            this.colourVBO = new VBO(this.Name + "_colour", BufferTarget.ArrayBuffer, BufferUsageHint.DynamicDraw);
            this.colourVBO.SetData(this.colour);
            this.IsColourVBOLoaded = true;
        }
Example #44
0
        public void InitVertexVBO()
        {
            this.vertex = new Vector3[NUMVERTICES];

            for (int i = 0; i < NUMVERTICES; i++)
            {
                this.vertex[i] = Vector3.Zero;
            }

            this.vertexVBO = new VBO(this.Name + "_vertex", BufferTarget.ArrayBuffer, BufferUsageHint.DynamicDraw);
            this.vertexVBO.SetData(this.vertex);
            this.IsVertexVBOLoaded = true;
        }
Example #45
0
        private static void DrawShapeCustom(VBO <Vector3> shape, VBO <Vector2> uvs, VBO <int> elements, Texture tex, OpenGL.BeginMode mode, float x, float y, float z)
        {
            Gl.BindTexture(tex);
            program["model_matrix"].SetValue(
                Matrix4.CreateTranslation(new Vector3(x, y, z)));
            program["enable_lighting"].SetValue(lighting);

            Gl.BindBufferToShaderAttribute(shape, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(uvs, program, "vertexUV");
            Gl.BindBuffer(elements);

            Gl.DrawElements(mode, cubeElements.Count,
                            DrawElementsType.UnsignedInt, IntPtr.Zero);
        }
Example #46
0
        public void Dispose()
        {
            if (vertexBuffer != null)
            {
                vertexBuffer.Dispose();
            }
            if (uvBuffer != null)
            {
                uvBuffer.Dispose();
            }

            vertexBuffer = null;
            uvBuffer     = null;
        }
Example #47
0
        static void drawSquare(Square square)
        {
            Matrix4 scaling     = Matrix4.CreateScaling(square.size);
            Matrix4 translation = Matrix4.CreateTranslation(new Vector3(square.x, square.y, 0));

            program["model_matrix"].SetValue(scaling * translation);
            if (squareColor != null)
            {
                squareColor.Dispose();
            }
            squareColor = new VBO <Vector3>(new Vector3[] { square.color, square.color, square.color, square.color });
            Gl.BindBufferToShaderAttribute(squareColor, program, "vertexColor");
            Gl.DrawElements(BeginMode.Quads, squareElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
        }
Example #48
0
        public void Create(Vector3[] positionVboData,Vector3[] normalVboData, int[] indicesVboData)
        {
            length = indicesVboData.Length;
            VBO pos, nor;

            pos = new VBO();
            pos.Create<Vector3>(BufferTarget.ArrayBuffer, positionVboData);

            nor = new VBO();
            nor.Create<Vector3>(BufferTarget.ArrayBuffer, normalVboData);

            ind = new VBO();
            ind.Create<int>(BufferTarget.ElementArrayBuffer, indicesVboData);

            vao = new VAO();
            vao.Create();
            vao.SetVertexAttribPointer(0, 3, pos);
            vao.SetVertexAttribPointer(1, 3, nor);
            VAO.Unbind();
        }
Example #49
0
        public Point(float x, float y, float z, Vector3 c)
        {
            //Initialise elements for the point
            elements = new VBO<int>(new int[] { 0 }, BufferTarget.ElementArrayBuffer);

            point = new VBO<Vector3>(new Vector3[] { new Vector3(x, y, z) });
            color = new VBO<Vector3>(new Vector3[] { c });

            //Store raw data about the point
            rawColor = c;
            rawPoint = new Vector3(x, y, z);

            firstPoint = x.ToString() + "," + y.ToString() + "," + z.ToString();

            //Find the appropriate name of the points colour
            int index = colors.IndexOf(rawColor);
            if (index > -1)
            {
                colorString = colorStrings[index];
            }
        }
Example #50
0
        public void Load()
        {
            _program = _assets.Fetch<ShaderProgram>("Main");
            _vbo = new VBO();
            data = new List<Vertex>();
            _tex = _assets.Fetch<Texture>("blank.png");

            const float size = 1f;
            data.Add(new Vertex { Position = new Vector3(0, 0, 0), Colour = Color.Blue.ToVector4() });
            data.Add(new Vertex { Position = new Vector3(size, 0, 0), Colour = Color.Blue.ToVector4() });

            data.Add(new Vertex { Position = new Vector3(0, 0, 0), Colour = Color.Red.ToVector4() });
            data.Add(new Vertex { Position = new Vector3(0, size, 0), Colour = Color.Red.ToVector4() });

            data.Add(new Vertex { Position = new Vector3(0, 0, 0), Colour = Color.Green.ToVector4() });
            data.Add(new Vertex { Position = new Vector3(0, 0, size), Colour = Color.Green.ToVector4() });

            _vbo.Buffer(data.ToArray());
            _vao = new VAO(_program, _vbo);
            Loaded = true;
        }
Example #51
0
        public static Reaction<Camera> Renderer(SceneGraph sceneGraph, Vec3 skyColor)
        {
            _skyboxShader = new GLProgram (VertexShader (), FragmentShader ());
            _skybox = new Skybox (_skyboxShader);
            _skyColor = skyColor;
            var cube = Extrusion.Cube<PositionalVertex> (_cubeSize, _cubeSize, _cubeSize).Center ();
            _vertices = new VBO<PositionalVertex> (cube.Vertices, BufferTarget.ArrayBuffer);
            _indices = new VBO<int> (cube.Indices, BufferTarget.ElementArrayBuffer);
            var environmentMap = Texture.CubeMapFromFiles (
                _paths.Map (s => string.Format (@"Textures/{0}.bmp", s)), 0)
                .LinearFiltering ().ClampToEdges (Axes.All);
            sceneGraph.GlobalLighting.DiffuseMap = environmentMap;

            return React.By<Camera> (_skybox.Render)
                .BindSamplers (new Dictionary<Sampler, Texture> ()
                {
                    { !_skybox.cubeMap, environmentMap }
                })
                .Culling (CullFaceMode.Front)
                .Program (_skyboxShader);
        }
Example #52
0
        public void Load()
        {
            _program = _assets.Fetch<ShaderProgram>("Main");
            _vbo = new VBO();
            _tex = _assets.Fetch<Texture>("star.png");
            data = new List<Vertex>();
            var rnd = new Random();
            for (var i = 0; i < 150; i++)
            {
                //rnd.NextDouble()

                var x = rnd.NextFloat(-80f, 80f);
                var y = rnd.NextFloat(-80f, 80f);

                var z = rnd.NextFloat(0f, 10f) - 60.0f;
                AddStar((float)rnd.NextDouble() + 0.1f, new Vector3(x, y,z));
            }

            _vbo.Buffer(data.ToArray());
            _vao = new VAO(_program, _vbo);
            Loaded = true;
        }
Example #53
0
        public MeshVBOs loadVBOS(string path)
        {
            this.faces = new List<Polygon>();
            this.verts.Clear();
            this.normals.Clear();
            this.txtCoords.Clear();
            loadFromObj(path);

            List<float> verts = new List<float>();
            List<float> uvs = new List<float>();
            List<float> normals = new List<float>();
            List<ushort> elements = new List<ushort>();

            ushort e = 0;
            foreach (Polygon p in faces)
            {
                for (int i = 0; i < 4 * 8; i += 8)
                {
                    verts.Add(p.Verts[i]);
                    verts.Add(p.Verts[i + 1]);
                    verts.Add(p.Verts[i + 2]);

                    uvs.Add(p.Verts[i + 3]);
                    uvs.Add(p.Verts[i + 4]);

                    normals.Add(p.Verts[i + 5]);
                    normals.Add(p.Verts[i + 6]);
                    normals.Add(p.Verts[i + 7]);
                    elements.Add(e);
                    e++;
                }
            }

            VBO<float> vertsVBO = new VBO<float>(verts.ToArray(), OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, VBO<float>.PointerType.vertex);
            VBO<float> uvsVBO = new VBO<float>(uvs.ToArray(), OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, VBO<float>.PointerType.texCoord);
            VBO<float> normalsVBO = new VBO<float>(normals.ToArray(), OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, VBO<float>.PointerType.normal);
            VBO<ushort> elemtsVBO = new VBO<ushort>(elements.ToArray(), OpenTK.Graphics.OpenGL.BufferTarget.ElementArrayBuffer, VBO<ushort>.PointerType.element);
            return new MeshVBOs(vertsVBO, uvsVBO, normalsVBO, elemtsVBO);
        }
Example #54
0
        public void Load()
        {
            _program = _assets.Fetch<ShaderProgram>("Main");
            _vbo = new VBO();
            _data = new List<Vertex>();

            _tex = _assets.Fetch<Texture>("blank.png");
            stl = new STLLoader("Media/Models/testship.stl");
            var col = Color.FromArgb(255, 94, 169, 198);

            foreach (var element in stl.Elements)
            {
                _data.Add(new Vertex { Position = element.P1.ToVector3(), Normal = element.Normal.ToVector3(), Colour = new Vector4(col.R / 255f, col.G / 255f, col.B / 255f, col.A / 255f) });
                _data.Add(new Vertex { Position = element.P2.ToVector3(), Normal = element.Normal.ToVector3(), Colour = new Vector4(col.R / 255f, col.G / 255f, col.B / 255f, col.A / 255f) });
                _data.Add(new Vertex { Position = element.P3.ToVector3(), Normal = element.Normal.ToVector3(), Colour = new Vector4(col.R / 255f, col.G / 255f, col.B / 255f, col.A / 255f) });
            }

            _vbo.Buffer(_data.ToArray());
            _vao = new VAO(_program, _vbo);

            Loaded = true;
        }
Example #55
0
        public Line(float ox, float oy, float oz, float fx, float fy, float fz, Vector3 c)
        {
            //Initialises elements for the line
            elements = new VBO<int>(new int[] { 0, 1 }, BufferTarget.ElementArrayBuffer);

            line = new VBO<Vector3>(new Vector3[] { new Vector3(ox, oy, oz), new Vector3(fx, fy, fz) });
            color = new VBO<Vector3>(new Vector3[] { c, c });

            //Stores raw data about the line
            rawColor = c;
            rawFirstPoint = new Vector3(ox, oy, oz);
            rawSecondPoint = new Vector3(fx, fy, fz);

            firstPoint = ox.ToString() + "," + oy.ToString() + "," + oz.ToString();
            secondPoint = fx.ToString() + "," + fy.ToString() + "," + fz.ToString();

            //Find the appropriate name of the lines colour
            int index = colors.IndexOf(rawColor);
            if (index > -1)
            {
                colorString = colorStrings[index];
            }
        }
Example #56
0
        public Angle(Line l1, Line l2, Vector3 c)
        {
            //Initialises elements for the angle
            elements = new VBO<int>(new int[] { 0, 1 }, BufferTarget.ElementArrayBuffer);

            Line1 = l1; //Line1 is bottom line
            Line2 = l2; //Line2 is top line

            rawColor = c;

            color = new VBO<Vector3>(new Vector3[] { c, c });

            //Calculate normalised direction
            Vector3 direction = NormalizedDirection(Line1);

            //Calculate the amount the line needs to be travelled down to find the first point of the line
            Vector3 step = new Vector3(direction.x * 0.2, direction.y * 0.2, direction.z * 0.2);

            //Calculate the point 'step' distance down the line
            point1 = Line1.rawFirstPoint + step;

            //Repeat the above process for the second line
            direction = NormalizedDirection(Line2);
            step = new Vector3(direction.x * 0.2, direction.y * 0.2, direction.z * 0.2);
            point2 = Line2.rawFirstPoint + step;

            line = new VBO<Vector3>(new Vector3[] { point1, point2 });

            firstLine = "( " + Line1.firstPoint + " ) , ( " + Line1.secondPoint + " )";
            secondLine = "( " + Line2.firstPoint + " ) , ( " + Line2.secondPoint + " )";

            int index = colors.IndexOf(rawColor);
            if (index > -1)
            {
                colorString = colorStrings[index];
            }
        }
Example #57
0
        static void Main(string[] args)
        {
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH | Glut.GLUT_MULTISAMPLE);   // multisampling makes things beautiful!
            Glut.glutInitWindowSize(width, height);
            Glut.glutCreateWindow("OpenGL Tutorial");

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

            Glut.glutKeyboardFunc(OnKeyboardDown);
            Glut.glutKeyboardUpFunc(OnKeyboardUp);

            Glut.glutCloseFunc(OnClose);
            Glut.glutReshapeFunc(OnReshape);

            // add our mouse callbacks for this tutorial
            Glut.glutMouseFunc(OnMouse);
            Glut.glutMotionFunc(OnMove);

            Gl.Enable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.Blend);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            // create our shader program
            program = new ShaderProgram(VertexShader, FragmentShader);

            // create our camera
            camera = new Camera(new Vector3(0, 0, 10), Quaternion.Identity);
            camera.SetDirection(new Vector3(0, 0, -1));

            // set up the projection and view matrix
            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.Up));

            program["light_direction"].SetValue(new Vector3(0, 0, 1));
            program["enable_lighting"].SetValue(lighting);
            program["normalTexture"].SetValue(1);
            program["enable_mapping"].SetValue(normalMapping);

            brickDiffuse = new Texture("AlternatingBrick-ColorMap.png");
            brickNormals = new Texture("AlternatingBrick-NormalMap.png");

            Vector3[] vertices = new Vector3[] {
                new Vector3(1, 1, -1), new Vector3(-1, 1, -1), new Vector3(-1, 1, 1), new Vector3(1, 1, 1),         // top
                new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1),     // bottom
                new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),         // front face
                new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1),     // back face
                new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1),     // left
                new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) };
            cube = new VBO<Vector3>(vertices);

            Vector2[] uvs = new Vector2[] {
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1),
                new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) };
            cubeUV = new VBO<Vector2>(uvs);

            List<int> triangles = new List<int>();
            for (int i = 0; i < 6; i++)
            {
                triangles.Add(i * 4);
                triangles.Add(i * 4 + 1);
                triangles.Add(i * 4 + 2);
                triangles.Add(i * 4);
                triangles.Add(i * 4 + 2);
                triangles.Add(i * 4 + 3);
            }
            cubeTriangles = new VBO<int>(triangles.ToArray(), BufferTarget.ElementArrayBuffer);

            Vector3[] normals = Geometry.CalculateNormals(vertices, triangles.ToArray());
            cubeNormals = new VBO<Vector3>(normals);

            Vector3[] tangents = CalculateTangents(vertices, normals, triangles.ToArray(), uvs);
            cubeTangents = new VBO<Vector3>(tangents);

            // load the bitmap font for this tutorial
            font = new BMFont("font24.fnt", "font24.png");
            fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource);

            fontProgram.Use();
            fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000));
            fontProgram["color"].SetValue(new Vector3(1, 1, 1));

            information = font.CreateString(fontProgram, "OpenGL  C#  Tutorial  15");

            watch = System.Diagnostics.Stopwatch.StartNew();

            Glut.glutMainLoop();
        }
Example #58
0
 public void AddVBO(int attrib, VBO vbo)
 {
     vbo.Bind();
     GL.EnableVertexAttribArray(attrib);
     GL.VertexAttribPointer(attrib, vbo.ElementSize, VertexAttribPointerType.Float, false, 0, 0);
 }
Example #59
0
        public ObjObject(List<string> lines, Dictionary<string, ObjMaterial> materials, int vertexOffset, int uvOffset)
        {
            // we need at least 1 line to be a valid file
            if (lines.Count == 0) return;

            List<Vector3> vertexList = new List<Vector3>();
            List<Vector2> uvList = new List<Vector2>();
            List<int> triangleList = new List<int>();
            List<Vector2> unpackedUvs = new List<Vector2>();
            List<int> normalsList = new List<int>();

            // now we read the lines
            for (int i = 0; i < lines.Count; i++)
            {
                string[] split = lines[i].Split(' ');

                switch (split[0])
                {
                    case "o":
                    case "g":
                        this.Name = lines[i].Substring(2);
                        break;
                    case "v":
                        vertexList.Add(new Vector3(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3])));
                        break;
                    case "vt":
                        uvList.Add(new Vector2(double.Parse(split[1]), double.Parse(split[2])));
                        break;
                    case "f":
                        if (split.Length == 5)  // this is a quad, so split it up
                        {
                            string[] split1 = new string[] { split[0], split[1], split[2], split[3] };
                            UnpackFace(split1, vertexOffset, uvOffset, vertexList, uvList, triangleList, unpackedUvs, normalsList);

                            string[] split2 = new string[] { split[0], split[1], split[3], split[4] };
                            UnpackFace(split2, vertexOffset, uvOffset, vertexList, uvList, triangleList, unpackedUvs, normalsList);
                        }
                        else UnpackFace(split, vertexOffset, uvOffset, vertexList, uvList, triangleList, unpackedUvs, normalsList);
                        break;
                    case "usemtl":
                        if (materials.ContainsKey(split[1])) Material = materials[split[1]];
                        break;
                }
            }

            // calculate the normals (if they didn't exist)
            Vector3[] vertexData = vertexList.ToArray();
            int[] elementData = triangleList.ToArray();
            Vector3[] normalData = CalculateNormals(vertexData, elementData);

            // now convert the lists over to vertex buffer objects to be rendered by OpenGL
            this.vertices = new VBO<Vector3>(vertexData);
            this.normals = new VBO<Vector3>(normalData);
            if (unpackedUvs.Count != 0) this.uvs = new VBO<Vector2>(unpackedUvs.ToArray());
            this.triangles = new VBO<int>(elementData, BufferTarget.ElementArrayBuffer);
        }
Example #60
0
 public MeshRenderer(Mesh m)
 {
     for (int i = 0; i < m.Verticies.Length; i++)
       {
     m.Verticies[i].Color = ToRgba((byte)(m.Material.Diffuse[0] * byte.MaxValue), (byte)(m.Material.Diffuse[1] * byte.MaxValue), (byte)(m.Material.Diffuse[2] * byte.MaxValue), (byte)(m.Material.Diffuse[3] * byte.MaxValue));
       }
       this.verticies = m.Verticies;
       this.material = m.Material;
       handle = new VBO();
       int size;
       GL.GenBuffers(1, out handle.VboID);
       GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID);
       GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(verticies.Length * BlittableValueType.StrideOf(m.Verticies)), verticies, BufferUsageHint.StaticDraw);
       GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);
       if (m.Verticies.Length * BlittableValueType.StrideOf(m.Verticies) != size)
     throw new ApplicationException("Vertex data not uploaded correctly");
       GL.GenBuffers(1, out handle.EboID);
       GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID);
       GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(m.Indicies.Length * sizeof(short)), m.Indicies, BufferUsageHint.StaticDraw);
       GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);
       if (m.Indicies.Length * sizeof(short) != size)
     throw new ApplicationException("Element data not uploaded correctly");
       handle.NumElements = m.Indicies.Length;
       mode = m.Mode;
 }