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(); }
public override void OnLoad(GameState state) { base.OnLoad(state); //a vertex buffer is an array of vertices within video memory // //In this test we allocate a VertexBuffer directly. However, for performance in-game we use a Pool<VertexBuffer> //to request/release VertexBuffers. Pooling vertexbuffers allows us to recycle buffers rather than wasting time //destroying and allocating them. VertexBuffer buffer = VertexBuffer.Allocate(); //in order for openGL to know the size and structure of our vertex components, we associate //an array of VertexAttributes to our buffer. each element of the array specifies a component //of a vertex within the buffer. AttributedVertexBuffer aBuffer = new AttributedVertexBuffer(buffer, Util.vector2Attributes); //for vertex types defined within the engine, the VertexAttribute array that describes the struct layout //is specified as a static variable on the struct named vAttributes. Util class //holds vertex attributes for OpenTK types // //in the future it would be nice if this step is automatically handled somewhere in the engine. Unfortunately, //since interfaces cannot have static members we would have to use reflection libraries to auto-generate VertexAttributes. //we use a loader to load vertices into an vertex buffer. VertexLoader <Vector2> loader = new VertexLoader <Vector2>(BufferUsageHint.DynamicDraw, aBuffer); //For VertexLoader, we must manually specify attributes for the buffer. However, higher-level loaders //such as SpriteLoader will automatically associate attributes to the buffer. // //the "dynamic draw" usage hint tells openGL that we only intend to write data to the buffer. //the usage hint only affects the performance of the buffer, not its behavior. //add a triangle. loader.AddVertexSet( PrimitiveType.Triangles, new[] { new Vector2(0, 0), //for simple position vertices, we use Vector2 struct from OpenTK library. new Vector2(0, 1), // new Vector2(1, 1) //for textured vertices we would use the engine's TexturedVertex2 struct }); //add a second triangle loader.AddVertexSet( PrimitiveType.Triangles, new[] { new Vector2(0, 0), new Vector2(0, -1), new Vector2(-1, -1) }); //once all the vertices have been added to the loader, we load the vertex buffer loadedVertexSets = loader.Load(); //The result is an enumeration of VertexSet objects, which represents //a drawable set of vertices within the vertex buffer. //In this test, a VertexSet represents a single 2D triangle. }
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 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 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; }
//see Triangle2DDrawTest for a general tutorial of the resource loading pattern public override void OnLoad(GameState state) { base.OnLoad(state); var tex = Texture.Allocate(); tex.LoadImageFile("assets/sprite-example.png"); var loader = new SpriteLoader(BufferUsageHint.DynamicDraw, VertexBuffer.Allocate()); //TODO: test depth sorting loader.AddSprite( tex, new Vector2(0, 0), //sprite position (top-left corner) new Rectangle(0, 0, 48, 21) //source rectangle in sprite sheet ); sprites = new DrawableSet(loader.Load()); }
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 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(); }
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); }