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(); }
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 }
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; }
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); }
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(); }
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"); }
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(); }
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; }
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; }
public void Dispose() { vertices.Dispose(); uvs.Dispose(); triangles.Dispose(); vertices = null; }
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); }
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(); }
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(); }
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); }
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; }
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); }
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(); }
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]; } }
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); }
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; }
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); }
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; }
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; }
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]; } }
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]; } }
public void AddVBO(int attrib, VBO vbo) { vbo.Bind(); GL.EnableVertexAttribArray(attrib); GL.VertexAttribPointer(attrib, vbo.ElementSize, VertexAttribPointerType.Float, false, 0, 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(); }
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; }
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); }