protected override void OnLoad(EventArgs e) { prog = new ShaderProgram(Shader.FromFile(vertPath), Shader.FromFile(fragPath)); textureWall = new Texture(System.Drawing.Image.FromFile("Data/wall.jpg"), 0, "wall"); textureContainer = new Texture(System.Drawing.Image.FromFile("Data/container2.png"), 1, "container"); vbo = new VertexBuffer <float>(); vbo.Bind(); vbo.BufferData(vertices); vao = new VertexArray(); vao.Bind(); ibo = new IndexBuffer(); ibo.Bind(); ibo.BufferData(indices); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * 4, 0); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * 4, 3 * 4); GL.EnableVertexAttribArray(1); vao.Unbind(); ibo.Unbind(); vbo.Unbind(); prog.Use(); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); }
/// <summary> /// Updates the vertex buffer object from the <see cref="Vertices"/> array. /// </summary> /// <typeparam name="TVertex">The type of vertex buffer to use.</typeparam> /// <param name="transform">Function that transforms the vertex array data to a interleaved struct format.</param> private void UpdateVertices <TVertex>( Func <Vector3, Vector3, Color4, TVertex> transform ) where TVertex : struct, IVertexData { // if the type of the vertex data has changed we need to create a new buffer if (m_vertexBuffer != null && m_vertexBuffer.GetType() != typeof(VertexBuffer <TVertex>)) { m_vertexBuffer.Dispose(); m_vertexBuffer = null; } // create vertex buffer if needed if (m_vertexBuffer == null) { m_vertexBuffer = new VertexBuffer <TVertex>(m_vertices.Length); } // copy the vertices into the buffer VertexBuffer <TVertex> buffer = m_vertexBuffer as VertexBuffer <TVertex>; buffer.Clear(); int offset; TVertex[] vertexArray = buffer.WriteDirectly(m_vertices.Length, out offset); for (int i = 0; i < m_vertices.Length; i++) { vertexArray[i] = transform(m_vertices[i], m_normals[i], m_colors[i]); } // upload to the GPU buffer.BufferData(); m_vertexBufferDirty = false; }
public void InitializeGraphics() { if (mat == null) { owner.Resources.Load("shaders\\Grid", out mat); } if (vb == null) { vb = new VertexBuffer <VertexPositionColor>(VertexPositionColor.Descriptor); vb.Allocate(8); } if (ib == null) { ib = new IndexBuffer <uint>(); ib.Allocate(24); ib[0] = 0; ib[1] = 1; ib[2] = 1; ib[3] = 2; ib[4] = 2; ib[5] = 3; ib[6] = 3; ib[7] = 0; ib[8] = 4; ib[9] = 5; ib[10] = 5; ib[11] = 6; ib[12] = 6; ib[13] = 7; ib[14] = 4; ib[15] = 0; ib[16] = 4; ib[17] = 1; ib[18] = 5; ib[19] = 2; ib[20] = 6; ib[21] = 3; ib[22] = 7; ib.BufferData(VboUsage.GL_STATIC_DRAW); } if (points != null) { for (int i = 0; i < 8; i++) { vb[i] = new VertexPositionColor() { Position = points[i], Color = Colors.Yellow }; } } vb.BufferData(VboUsage.GL_DYNAMIC_DRAW); }
protected override void Build() { vb = new VertexBuffer <Vector3>(Vector3.Descriptor); vb.Allocate(8); ib = new IndexBuffer <ushort>(); ib.Allocate(4 * 6); vb[0] = new Vector3(-0.5f, -0.5f, -0.5f); vb[1] = new Vector3(0.5f, -0.5f, -0.5f); vb[2] = new Vector3(0.5f, 0.5f, -0.5f); vb[3] = new Vector3(-0.5f, 0.5f, -0.5f); vb[4] = new Vector3(-0.5f, -0.5f, 0.5f); vb[5] = new Vector3(0.5f, -0.5f, 0.5f); vb[6] = new Vector3(0.5f, 0.5f, 0.5f); vb[7] = new Vector3(-0.5f, 0.5f, 0.5f); ib[0] = 0; ib[1] = 1; ib[2] = 2; ib[3] = 3; ib[4] = 4; ib[5] = 0; ib[6] = 3; ib[7] = 7; ib[8] = 5; ib[9] = 4; ib[10] = 7; ib[11] = 6; ib[12] = 1; ib[13] = 5; ib[14] = 6; ib[15] = 2; ib[16] = 6; ib[17] = 7; ib[18] = 3; ib[19] = 2; ib[20] = 0; ib[21] = 4; ib[22] = 5; ib[23] = 1; vb.BufferData(VboUsage.GL_STATIC_DRAW); ib.BufferData(VboUsage.GL_STATIC_DRAW); // Client data is no longer needed. Free it! vb.FreeClientData(); ib.FreeClientData(); }
/// <summary> /// Called just prior to rendering the component. /// </summary> protected override void OnRender() { if (m_vertexBuffer == null) { m_vertexBuffer = new VertexBuffer <VertexPNC>(24); m_surface.SetVertexBuffer(m_vertexBuffer, PrimitiveType.Lines); } if (m_dirty) { ReadOnlyCollection <Vector3> corners = m_mesh.Bounds.Corners; // clear any lines m_vertexBuffer.Clear(); // get the underlying buffer int offset; VertexPNC[] verts = m_vertexBuffer.WriteDirectly(24, out offset); // each consecutive pair of elements forms a line segment drawn to form the box wireframe verts[0] = new VertexPNC(corners[0], Vector3.Zero, Color4.White); verts[1] = new VertexPNC(corners[4], Vector3.Zero, Color4.White); verts[2] = new VertexPNC(corners[1], Vector3.Zero, Color4.White); verts[3] = new VertexPNC(corners[5], Vector3.Zero, Color4.White); verts[4] = new VertexPNC(corners[2], Vector3.Zero, Color4.White); verts[5] = new VertexPNC(corners[6], Vector3.Zero, Color4.White); verts[6] = new VertexPNC(corners[3], Vector3.Zero, Color4.White); verts[7] = new VertexPNC(corners[7], Vector3.Zero, Color4.White); verts[8] = new VertexPNC(corners[0], Vector3.Zero, Color4.White); verts[9] = new VertexPNC(corners[1], Vector3.Zero, Color4.White); verts[10] = new VertexPNC(corners[1], Vector3.Zero, Color4.White); verts[11] = new VertexPNC(corners[3], Vector3.Zero, Color4.White); verts[12] = new VertexPNC(corners[3], Vector3.Zero, Color4.White); verts[13] = new VertexPNC(corners[2], Vector3.Zero, Color4.White); verts[14] = new VertexPNC(corners[2], Vector3.Zero, Color4.White); verts[15] = new VertexPNC(corners[0], Vector3.Zero, Color4.White); verts[16] = new VertexPNC(corners[4], Vector3.Zero, Color4.White); verts[17] = new VertexPNC(corners[5], Vector3.Zero, Color4.White); verts[18] = new VertexPNC(corners[5], Vector3.Zero, Color4.White); verts[19] = new VertexPNC(corners[7], Vector3.Zero, Color4.White); verts[20] = new VertexPNC(corners[7], Vector3.Zero, Color4.White); verts[21] = new VertexPNC(corners[6], Vector3.Zero, Color4.White); verts[22] = new VertexPNC(corners[6], Vector3.Zero, Color4.White); verts[23] = new VertexPNC(corners[4], Vector3.Zero, Color4.White); // upload the lines to the GPU if changed m_vertexBuffer.BufferData(); } }
public override Model Build() { Model ret = new Model(); VertexBuffer <VertexPositionTexCoordNormal> vb = new VertexBuffer <VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor); //IndexBuffer<uint> ib = new IndexBuffer<uint>(); float ang_inc = (float)(2 * Math.PI) / sides; int vsides = sides / 2 - 2; float ang = 0; vb.Allocate(sides * (sides / 2 - 2) + 2); int index = 0; for (int i = 0; i < sides; i++, ang += ang_inc) { float cs = (float)Math.Cos(ang); float ss = (float)Math.Sin(ang); float sang_inc = (float)Math.PI / vsides; float sang = -((float)Math.PI / 2) + sang_inc; for (int j = 0; j < vsides; j++, sang += sang_inc, ++index) { float vcs = (float)Math.Cos(sang); float vss = (float)Math.Sin(sang); Vector3 pos = new Vector3( cs * radius * vcs, vss * radius, ss * radius * vss); vb[index] = new VertexPositionTexCoordNormal() { Position = pos, TexCoord = new Vector2(), Normal = new Vector3(cs * vcs, vss, ss * vss) }; } vb[index] = new VertexPositionTexCoordNormal() { Position = new Vector3(0, -radius, 0), Normal = new Vector3(0, -1, 0) }; vb[index + 1] = new VertexPositionTexCoordNormal() { Position = new Vector3(0, radius, 0), Normal = new Vector3(0, radius, 0) }; } ret.VertexBuffer = vb; vb.BufferData(Glorg2.Graphics.OpenGL.VboUsage.GL_STATIC_DRAW); return(ret); }
public BoneDebugger([NotNull, ItemNotNull] IReadOnlyList <BoneNode> boneList, [NotNull] SimpleColor simpleColor) { _simpleColor = simpleColor; _boneList = boneList; var vertexBuffer = new VertexBuffer(); var indexBuffer = new IndexBuffer(); var boneCount = boneList.Count; var vertices = new Vector3[boneCount]; for (var i = 0; i < boneCount; ++i) { vertices[i] = boneList[i].CurrentPosition; } _vertices = vertices; var indices = new uint[(boneCount - 1) * 2]; var c = 0; foreach (var bone in boneList) { var parent = bone.Parent; if (parent == null) { continue; } indices[c * 2] = (uint)parent.Index; indices[c * 2 + 1] = (uint)bone.Index; ++c; if (c >= boneCount - 1) { break; } } vertexBuffer.BufferData(vertices, BufferUsageHint.StreamDraw); indexBuffer.BufferData(indices, BufferUsageHint.StaticDraw); _vertexBuffer = vertexBuffer; _indexBuffer = indexBuffer; }
protected override void OnLoad(EventArgs e) { cam = new Kamera(this, new Vector3(0.0f, 0.0f, 3.0f), new Vector3(0, 0, 0), 0.1f, 0.05f); VertexBuffer <float> vbo = new VertexBuffer <float>(); vbo.Bind(); vbo.BufferData(vertices); boxVao = new VertexArray(); boxVao.Bind(); boxVao.SetVertexAttrib <float>(0, 3, VertexAttribPointerType.Float, false, 8, 0); //pos boxVao.SetVertexAttrib <float>(1, 3, VertexAttribPointerType.Float, false, 8, 3); //norm boxVao.SetVertexAttrib <float>(2, 2, VertexAttribPointerType.Float, false, 8, 6); //texCoord boxVao.Unbind(); lampVao = new VertexArray(); lampVao.Bind(); vbo.Bind(); //müssen wir nochmal lampVao.SetVertexAttrib <float>(0, 3, VertexAttribPointerType.Float, false, 8, 0); //pos lampVao.SetVertexAttrib <float>(1, 3, VertexAttribPointerType.Float, false, 8, 3); //norm lampVao.SetVertexAttrib <float>(2, 2, VertexAttribPointerType.Float, false, 8, 6); //texCoord lampVao.Unbind(); vbo.Unbind(); lightingShader = new ShaderProgram(Shader.FromFile("Shader/vertexBase.c"), Shader.FromFile("Shader/fragmentBase.c")); lampShader = new ShaderProgram(Shader.FromFile("Shader/vertexBase.c"), Shader.FromFile("Shader/fragmentLamp.c")); diffuseMapTexture = new Texture("Data/container2.png", 0, "material.diffuse"); specularMapTexture = new Texture("Data/container2_specular.png", 1, "material.specular"); containerMat = new Material(); containerMat.AddTexture("diffuse", diffuseMapTexture); containerMat.AddTexture("specular", specularMapTexture); containerMat.AddFloat("shininess", 32.0f); GL.Enable(EnableCap.DepthTest); }
internal AxesDebugger([NotNull] SimpleColor simpleColor) { _simpleColor = simpleColor; var vertices = new[] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1) }; var indices = new uint[] { 0, 1, 0, 2, 0, 3 }; var vertexBuffer = new VertexBuffer(); var indexBuffer = new IndexBuffer(); vertexBuffer.BufferData(vertices, BufferUsageHint.StaticDraw); indexBuffer.BufferData(indices, BufferUsageHint.StaticDraw); _vertexBuffer = vertexBuffer; _indexBuffer = indexBuffer; }
protected override void OnLoad(EventArgs e) { cam = new Kamera(this, new Vector3(0, 0, 2), new Vector3(0), 0.2f, 0.1f); lightingShader = new ShaderProgram(Shader.FromFile(basicVertexSrc), Shader.FromFile(lightingFragSrc)); lampShader = new ShaderProgram(Shader.FromFile(basicVertexSrc), Shader.FromFile(lampFragSrc)); diffuseMap = new Texture("Data/container2.png", 0, "diffuse"); specularMap = new Texture("Data/container2_specular.png", 1, "specular"); VertexBuffer <float> vbo = new VertexBuffer <float>(); vbo.Bind(); vbo.BufferData(vertices); //container vao containerVao = new VertexArray(); containerVao.Bind(); int vertexStride = (3 + 3 + 2) * 4; GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, vertexStride, 0); //0 = pos in basic_vertex.c GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, vertexStride, 12); GL.EnableVertexAttribArray(1); GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, vertexStride, 24); GL.EnableVertexAttribArray(2); containerVao.Unbind(); lampVao = new VertexArray(); lampVao.Bind(); vbo.Bind(); //so vao knows to use this GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, vertexStride, 0); //0 = pos in basic_vertex.c GL.EnableVertexAttribArray(0); lampVao.Unbind(); GL.Enable(EnableCap.DepthTest); }
public override Model Build() { Model ret = new Model(); VertexBuffer<VertexPositionTexCoordNormal> vb = new VertexBuffer<VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor); //IndexBuffer<uint> ib = new IndexBuffer<uint>(); float ang_inc = (float)(2 * Math.PI) / sides; int vsides = sides / 2 - 2; float ang = 0; vb.Allocate(sides * (sides / 2 - 2) + 2); int index = 0; for (int i = 0; i < sides; i++, ang += ang_inc) { float cs = (float)Math.Cos(ang); float ss = (float)Math.Sin(ang); float sang_inc = (float)Math.PI / vsides; float sang = -((float)Math.PI / 2) + sang_inc; for (int j = 0; j < vsides; j++, sang += sang_inc, ++index) { float vcs = (float)Math.Cos(sang); float vss = (float)Math.Sin(sang); Vector3 pos = new Vector3( cs * radius * vcs, vss * radius, ss * radius * vss); vb[index] = new VertexPositionTexCoordNormal() { Position = pos, TexCoord = new Vector2(), Normal = new Vector3(cs * vcs, vss, ss * vss) }; } vb[index] = new VertexPositionTexCoordNormal() { Position = new Vector3(0, -radius, 0), Normal = new Vector3(0, -1, 0) }; vb[index + 1] = new VertexPositionTexCoordNormal() { Position = new Vector3(0, radius, 0), Normal = new Vector3(0, radius, 0) }; } ret.VertexBuffer = vb; vb.BufferData(Glorg2.Graphics.OpenGL.VboUsage.GL_STATIC_DRAW); return ret; }
public static Terrain Build(IOpenGL33 gl, int columns, int rows, float width, float depth) { var noise = new PerlinNoise(); var noise2 = new PerlinNoise(); var vertexBuffer = new VertexBuffer<PositionNormalTexCoord>((columns + 1) * (rows + 1), gl); var indexBuffer = new ElementBuffer<uint>(columns * rows * 6 , gl); float fx = - width / 2; float fy = -depth / 2; float dx = width / columns; float dy = depth / rows; int i = 0; for (int y = 0; y < (rows + 1); y++) { for (int x = 0; x < (columns + 1); x++, i++) { float ix = fx + x * dx; float iy = fy + y * dy; const float detail = 0.25f; const float detailScale = 0.2f; const float macro = 0.008f; const float macroScale = 4f; var fUp = (float)((noise.Noise(x * detail, 0, (y + 1) * detail) - noise2.Noise(x * detail, 0, (y + 1) * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, (y + 1) * macro) * macroScale) ; var fDown = (float)((noise.Noise(x * detail, 0, (y - 1) * detail) - noise2.Noise(x * detail, 0, (y - 1) * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, (y - 1) * macro) * macroScale) ; var fLeft = (float)((noise.Noise((x - 1) * detail, 0, y * detail) - noise2.Noise((x - 1) * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise((x - 1) * macro, 0, y * macro) * macroScale); var fRight = (float)((noise.Noise((x + 1) * detail, 0, y * detail) - noise2.Noise((x + 1) * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise((x + 1) * macro, 0, y * macro) * macroScale); var fThis = (float)((noise.Noise(x * detail, 0, y * detail) - noise2.Noise(x * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, y * macro) * macroScale); var vUp = new Vector3f(ix, fUp, iy - dy); var vDown = new Vector3f(ix, fDown, iy + dy); var vLeft = new Vector3f(ix - 1, fLeft, iy); var vRight = new Vector3f(ix + 1, fRight, iy); var vThis = new Vector3f(ix, fThis, iy); var upperLeft = CalculateNormal(vLeft, vUp, vThis); var upperRight = CalculateNormal(vUp, vRight, vThis); var lowerRight = CalculateNormal(vRight, vDown, vThis); var lowerLeft = CalculateNormal(vDown, vLeft, vThis); var normal = upperLeft + upperRight + lowerLeft + lowerRight; normal = new Vector3f(normal.X * 0.25f, normal.Y * 0.25f, normal.Z * 0.25f); vertexBuffer[i] = new PositionNormalTexCoord { Normal = normal.Normalize(), Position = vThis, TexCoord = new Vector2f(vThis.X, vThis.Z)}; } } int offset = 0; foreach(var index in Enumerable.Range(0, columns * rows + columns).Where(index => (index % (columns + 1)) != columns)) { indexBuffer[offset] = (uint)index; indexBuffer[offset + 1] = (uint)(index + (columns + 1) + 1); indexBuffer[offset + 2] = (uint)index + 1; indexBuffer[offset + 3] = (uint)index; indexBuffer[offset + 4] = (uint)(index + (columns + 1)); indexBuffer[offset + 5] = (uint)(index + (columns + 1) + 1); offset += 6; } using(vertexBuffer.Bind()) vertexBuffer.BufferData(BufferUsage.StaticDraw); using(indexBuffer.Bind()) indexBuffer.BufferData(BufferUsage.StaticDraw); var vertexArray = new VertexArray(gl, new[] {vertexBuffer}, new[] {PositionNormalTexCoord.Descriptor}); var shader = new TerrainShader(gl); return new Terrain(gl, vertexArray, vertexBuffer, indexBuffer, shader); }
public void InitializeGraphics() { if (vb != null) vb.Dispose(); if (mat == null) { Glorg2.Resource.MaterialImporter imp = new Glorg2.Resource.MaterialImporter(); using(var stream = System.IO.File.OpenRead(".\\shaders\\Grid.mxl")) { mat = imp.Import<StdMaterial>(stream, "grid", null); } } vb = new VertexBuffer<WireframeVertex>(WireframeVertex.Description); vb.Allocate(Columns * 2 + Rows * 2 + 4); float tot_w = Size * Columns; float tot_h = Size * Rows; float fi = -tot_w / 2; int i; for (i = 0; i <= Columns; i++, fi += Size) { Vector4 color; if(Major == 0 || (i % Major) == 0) color = MajorColor; else color = MinorColor; vb[i * 2] = new WireframeVertex() { Position = new Vector3(fi, 0, -tot_h / 2), Color = color }; vb[i * 2 + 1] = new WireframeVertex() { Position = new Vector3(fi, 0, tot_h / 2), Color = color }; } int start = i * 2; fi = -tot_h / 2; for (int j = 0; j <= Rows; j++, fi += Size) { Vector4 color; if (Major == 0 || (j % Major) == 0) color = MajorColor; else color = MinorColor; vb[start + j * 2] = new WireframeVertex() { Position = new Vector3(-tot_h / 2, 0, fi), Color = color }; vb[start + j * 2 + 1] = new WireframeVertex() { Position = new Vector3( tot_h / 2, 0, fi), Color = color }; } vb.BufferData(VboUsage.GL_STATIC_DRAW); vb.FreeClientData(); }
public void InitializeGraphics() { if (vb != null) { vb.Dispose(); } if (mat == null) { Glorg2.Resource.MaterialImporter imp = new Glorg2.Resource.MaterialImporter(); using (var stream = System.IO.File.OpenRead(".\\shaders\\Grid.mxl")) { mat = imp.Import <StdMaterial>(stream, "grid", null); } } vb = new VertexBuffer <WireframeVertex>(WireframeVertex.Description); vb.Allocate(Columns * 2 + Rows * 2 + 4); float tot_w = Size * Columns; float tot_h = Size * Rows; float fi = -tot_w / 2; int i; for (i = 0; i <= Columns; i++, fi += Size) { Vector4 color; if (Major == 0 || (i % Major) == 0) { color = MajorColor; } else { color = MinorColor; } vb[i * 2] = new WireframeVertex() { Position = new Vector3(fi, 0, -tot_h / 2), Color = color }; vb[i * 2 + 1] = new WireframeVertex() { Position = new Vector3(fi, 0, tot_h / 2), Color = color }; } int start = i * 2; fi = -tot_h / 2; for (int j = 0; j <= Rows; j++, fi += Size) { Vector4 color; if (Major == 0 || (j % Major) == 0) { color = MajorColor; } else { color = MinorColor; } vb[start + j * 2] = new WireframeVertex() { Position = new Vector3(-tot_h / 2, 0, fi), Color = color }; vb[start + j * 2 + 1] = new WireframeVertex() { Position = new Vector3(tot_h / 2, 0, fi), Color = color }; } vb.BufferData(VboUsage.GL_STATIC_DRAW); vb.FreeClientData(); }
protected override void Build() { vb = new VertexBuffer<Vector3>(Vector3.Descriptor); vb.Allocate(8); ib = new IndexBuffer<ushort>(); ib.Allocate(4 * 6); vb[0] = new Vector3(-0.5f, -0.5f, -0.5f); vb[1] = new Vector3(0.5f, -0.5f, -0.5f); vb[2] = new Vector3(0.5f, 0.5f, -0.5f); vb[3] = new Vector3(-0.5f, 0.5f, -0.5f); vb[4] = new Vector3(-0.5f, -0.5f, 0.5f); vb[5] = new Vector3(0.5f, -0.5f, 0.5f); vb[6] = new Vector3(0.5f, 0.5f, 0.5f); vb[7] = new Vector3(-0.5f, 0.5f, 0.5f); ib[0] = 0; ib[1] = 1; ib[2] = 2; ib[3] = 3; ib[4] = 4; ib[5] = 0; ib[6] = 3; ib[7] = 7; ib[8] = 5; ib[9] = 4; ib[10] = 7; ib[11] = 6; ib[12] = 1; ib[13] = 5; ib[14] = 6; ib[15] = 2; ib[16] = 6; ib[17] = 7; ib[18] = 3; ib[19] = 2; ib[20] = 0; ib[21] = 4; ib[22] = 5; ib[23] = 1; vb.BufferData(VboUsage.GL_STATIC_DRAW); ib.BufferData(VboUsage.GL_STATIC_DRAW); // Client data is no longer needed. Free it! vb.FreeClientData(); ib.FreeClientData(); }
public virtual void InitializeGraphics() { if (heightmap == null && string.IsNullOrEmpty(Heightmap)) { return; } if (heightmap == null) { owner.Resources.Load(Heightmap, out heightmap); } //if (heightmap.Width != heightmap.Height) //throw new NotSupportedException("Must be rectangular heightmap"); int num_vertices = (heightmap.Width) * (heightmap.Height); if (vb != null) { DoDispose(); } vb = new VertexBuffer <VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor); vb.Allocate(num_vertices); if (mat_name != null) { owner.Resources.Load(mat_name, out mat); } size = CellSize * heightmap.Width; float s2 = size / 2; int vi = 0; float xx = 0; float yy = -s2; Vector3[,] norms = new Vector3[heightmap.Height, heightmap.Width]; for (int i = 0; i < heightmap.Height; i++) { for (int j = 0; j < heightmap.Width; j++) { Vector3 n = new Vector3(); n.x = (heightmap[i - 1, j] - heightmap[i + 1, j]) * Height; n.y = (heightmap[i, j - 1] - heightmap[i, j + 1]) * Height; n.z = 2.0f / (heightmap.Width + 1) / CellSize + 2.0f / (heightmap.Height + 1) / CellSize; norms[i, j] = n.Normalize(); } } for (int i = 0; i < heightmap.Height; i++) { xx = -s2; for (int j = 0; j < heightmap.Width; j++, vi++) { vb[vi] = new VertexPositionTexCoordNormal() { Position = new Vector3(xx, heightmap[i, j] * Height, yy), Normal = norms[i, j] }; xx += CellSize; } yy += CellSize; } float offset = s2 / 2; int x_count = heightmap.Width / 2; int y_count = heightmap.Height / 2; var new_size = new Vector3(s2, size / 8, s2); blocks = new TerrainBlock[4]; blocks[0] = new TerrainBlock(0, 0, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(-offset, 0, offset) }, 0, this); blocks[1] = new TerrainBlock(x_count - 1, 0, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(offset, 0, offset) }, 0, this); blocks[2] = new TerrainBlock(x_count - 1, y_count - 1, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(offset, 0, -offset) }, 0, this); blocks[3] = new TerrainBlock(0, y_count - 1, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(-offset, 0, -offset) }, 0, this); vb.BufferData(VboUsage.GL_STATIC_DRAW); vb.FreeClientData(); init_finished = true; }
public virtual void InitializeGraphics() { if (heightmap == null && string.IsNullOrEmpty(Heightmap)) return; if (heightmap == null) owner.Resources.Load(Heightmap, out heightmap); //if (heightmap.Width != heightmap.Height) //throw new NotSupportedException("Must be rectangular heightmap"); int num_vertices = (heightmap.Width) * (heightmap.Height); if (vb != null) DoDispose(); vb = new VertexBuffer<VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor); vb.Allocate(num_vertices); if (mat_name != null) owner.Resources.Load(mat_name, out mat); size = CellSize * heightmap.Width; float s2 = size / 2; int vi = 0; float xx = 0; float yy = -s2; Vector3[,] norms = new Vector3[heightmap.Height, heightmap.Width]; for (int i = 0; i < heightmap.Height; i++) { for (int j = 0; j < heightmap.Width; j++) { Vector3 n = new Vector3(); n.x = (heightmap[i - 1, j] - heightmap[i + 1, j]) * Height; n.y = (heightmap[i, j - 1] - heightmap[i, j + 1]) * Height; n.z = 2.0f / (heightmap.Width + 1) / CellSize + 2.0f / (heightmap.Height + 1) / CellSize; norms[i, j] = n.Normalize(); } } for (int i = 0; i < heightmap.Height; i++) { xx = -s2; for (int j = 0; j < heightmap.Width; j++, vi++) { vb[vi] = new VertexPositionTexCoordNormal() { Position = new Vector3(xx, heightmap[i, j] * Height, yy), Normal = norms[i, j] }; xx += CellSize; } yy += CellSize; } float offset = s2 / 2; int x_count = heightmap.Width / 2; int y_count = heightmap.Height / 2; var new_size = new Vector3(s2, size / 8, s2); blocks = new TerrainBlock[4]; blocks[0] = new TerrainBlock(0, 0, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(-offset, 0, offset) }, 0, this); blocks[1] = new TerrainBlock(x_count-1, 0, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(offset, 0, offset) }, 0, this); blocks[2] = new TerrainBlock(x_count-1, y_count-1, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(offset, 0, -offset) }, 0, this); blocks[3] = new TerrainBlock(0, y_count-1, x_count, y_count, new BoundingBox() { Size = new_size, Position = new Vector3(-offset, 0, -offset) }, 0, this); vb.BufferData(VboUsage.GL_STATIC_DRAW); vb.FreeClientData(); init_finished = true; }
private void GenerateDome() { int vertices = Segments * Segments * 2 + 1; int indices = Segments * 6 + (Segments - 1) * 2 * Segments * 6; vb = new VertexBuffer <SkydomeVertex>(SkydomeVertex.Descriptor); ib = new IndexBuffer <uint>(); vb.Allocate(vertices); ib.Allocate(indices); int index = 0; float vang_inc = (float)(Math.PI / 2) / Segments; float hang_inc = (float)(2 * Math.PI) / Segments; float hang = hang_inc; float vang = vang_inc; for (int i = 0; i < Segments; i++, vang += vang_inc) { float v_c = (float)Math.Cos(vang); float v_s = (float)Math.Sin(vang); for (int j = 0; j < Segments * 2; j++, hang += hang_inc) { float h_c = (float)Math.Cos(hang); float h_s = (float)Math.Sin(hang); Vector3 pos = new Vector3(h_c * v_s, v_c, h_s * v_s); vb[index++] = new SkydomeVertex() { Position = pos, Normal = -pos.Normalize() }; } hang = 0; } vb[index] = new SkydomeVertex() { Normal = Vector3.Down, Position = new Vector3(0, 1f, 0) }; vb.BufferData(VboUsage.GL_STATIC_DRAW); vb.FreeClientData(); index = 0; for (int i = 0; i < Segments * 2; i++) { ib[index++] = (uint)vertices - 1; ib[index++] = (uint)i; ib[index++] = (uint)(i + 1) % (Segments * 2); // Wraparound } for (int i = 0; i < Segments - 1; i++) { for (int j = 0; j < Segments * 2; j++) { int offset_1 = i * Segments * 2; int offset_2 = i * Segments * 2 + Segments * 2; int j2 = (j + 1) % (Segments * 2); ib[index++] = (uint)(offset_1 + j); ib[index++] = (uint)(offset_2 + j); ib[index++] = (uint)(offset_1 + j2); ib[index++] = (uint)(offset_2 + j); ib[index++] = (uint)(offset_2 + j2); ib[index++] = (uint)(offset_1 + j2); } } ib.BufferData(VboUsage.GL_STATIC_DRAW); ib.FreeClientData(); }
public AnimationRenderer([NotNull] Game game, [NotNull] PrettyMesh bodyMesh, [NotNull] PrettyAvatar bodyAvatar, [NotNull, ItemNotNull] IReadOnlyList <BoneNode> bodyBoneList, [NotNull] PrettyMesh headMesh, [NotNull] PrettyAvatar headAvatar, [NotNull, ItemNotNull] IReadOnlyList <BoneNode> headBoneList, [NotNull] BodyAnimation animation) { _game = game; _bodyMesh = bodyMesh; _bodyAvatar = bodyAvatar; _bodyBoneList = bodyBoneList; _headMesh = headMesh; _headAvatar = headAvatar; _headBoneList = headBoneList; _animation = animation; #region Body var bodyVertexBuffer = new VertexBuffer(); var bodyIndexBuffer = new IndexBuffer(); var bodyVertices = new PosNorm[bodyMesh.Vertices.Length]; var bodyIndices = new uint[bodyMesh.Indices.Length]; for (var k = 0; k < bodyVertices.Length; ++k) { bodyVertices[k] = new PosNorm { Position = bodyMesh.Vertices[k].ToOpenTK().FixCoordSystem(), Normal = bodyMesh.Normals[k].ToOpenTK().FixCoordSystem() }; } _originalBodyVertices = bodyVertices; _bodyVertices = (PosNorm[])bodyVertices.Clone(); for (var k = 0; k < bodyIndices.Length; ++k) { bodyIndices[k] = bodyMesh.Indices[k]; } bodyVertexBuffer.BufferData(bodyVertices, BufferUsageHint.StreamDraw); bodyIndexBuffer.BufferData(bodyIndices, BufferUsageHint.StaticDraw); _bodyVertexBuffer = bodyVertexBuffer; _bodyIndexBuffer = bodyIndexBuffer; #endregion #region Head var headVertexBuffer = new VertexBuffer(); var headIndexBuffer = new IndexBuffer(); var headVertices = new PosNorm[headMesh.Vertices.Length]; var headIndices = new uint[headMesh.Indices.Length]; for (var k = 0; k < headVertices.Length; ++k) { headVertices[k] = new PosNorm { Position = headMesh.Vertices[k].ToOpenTK().FixCoordSystem(), Normal = headMesh.Normals[k].ToOpenTK().FixCoordSystem() }; } _originalHeadVertices = headVertices; _headVertices = (PosNorm[])headVertices.Clone(); for (var k = 0; k < headIndices.Length; ++k) { headIndices[k] = headMesh.Indices[k]; } headVertexBuffer.BufferData(headVertices, BufferUsageHint.StreamDraw); headIndexBuffer.BufferData(headIndices, BufferUsageHint.StaticDraw); _headVertexBuffer = headVertexBuffer; _headIndexBuffer = headIndexBuffer; #endregion }
private IVertexBuffer CreateBuffer() { var result = new VertexBuffer<Vertex>(36, gl); // Set up every face omn the cube // Top result[0] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(0, 1, 0)}; result[1] = new Vertex { Position = new Vector3f( 1, 1, 1), Normal = new Vector3f(0, 1, 0) }; result[2] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 1, 0) }; result[3] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 1, 0) }; result[4] = new Vertex { Position = new Vector3f( 1, 1, 1), Normal = new Vector3f(0, 1, 0) }; result[5] = new Vertex { Position = new Vector3f( 1, 1, -1), Normal = new Vector3f(0, 1, 0) }; // Bottom result[6] = new Vertex { Position = new Vector3f(-1, -1, 1), Normal = new Vector3f(0, -1, 0) }; result[7] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(0, -1, 0) }; result[8] = new Vertex { Position = new Vector3f( 1, -1, 1), Normal = new Vector3f(0, -1, 0) }; result[9] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(0, -1, 0) }; result[10] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(0, -1, 0) }; result[11] = new Vertex { Position = new Vector3f( 1, -1, 1), Normal = new Vector3f(0, -1, 0) }; // Left result[12] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(-1, 0, 0)}; result[13] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(-1, 0, 0) }; result[14] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(-1, 0, 0) }; result[15] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(-1, 0, 0) }; result[16] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(-1, 0, 0) }; result[17] = new Vertex { Position = new Vector3f(-1, -1, 1), Normal = new Vector3f(-1, 0, 0) }; // Right result[18] = new Vertex { Position = new Vector3f(1, 1, 1), Normal = new Vector3f(1, 0, 0) }; result[19] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(1, 0, 0) }; result[20] = new Vertex { Position = new Vector3f(1, 1, -1), Normal = new Vector3f(1, 0, 0) }; result[21] = new Vertex { Position = new Vector3f(1, 1, 1), Normal = new Vector3f(1, 0, 0) }; result[22] = new Vertex { Position = new Vector3f(1, -1, 1), Normal = new Vector3f(1, 0, 0) }; result[23] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(1, 0, 0) }; // Front result[24] = new Vertex {Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(0, 0, 1)}; result[25] = new Vertex { Position = new Vector3f(1, -1, 1), Normal = new Vector3f(0, 0, 1) }; result[26] = new Vertex { Position = new Vector3f(1, 1, 1), Normal = new Vector3f(0, 0, 1) }; result[27] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(0, 0, 1) }; result[29] = new Vertex { Position = new Vector3f(1, -1, 1), Normal = new Vector3f(0, 0, 1) }; result[28] = new Vertex { Position = new Vector3f(-1, -1, 1), Normal = new Vector3f(0, 0, 1) }; // Back result[30] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 0, -1) }; result[32] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(0, 0, -1) }; result[31] = new Vertex { Position = new Vector3f(1, 1, -1), Normal = new Vector3f(0, 0, -1) }; result[33] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 0, -1) }; result[34] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(0, 0, -1) }; result[35] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(0, 0, -1) }; using (result.Bind()) { result.BufferData(BufferUsage.StaticDraw); } return result; }